Skip to content

Commit 40562c6

Browse files
committed
Updated grammar and examples.
1 parent cd95a42 commit 40562c6

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

GRAMMAR.ABNF

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,3 +650,59 @@ int = zero / ( digit1-9 *digit )
650650
minus = %x2D ; -
651651
plus = %x2B ; +
652652
zero = %x30 ; 0
653+
654+
expression =/ arithmetic-expression ;; ## Arithmetic Expressions
655+
656+
arithmetic-expression =/ "+" expression ; + %x43
657+
arithmetic-expression =/ ( "-" / "" ) expression ; - %x45 – %x2212
658+
659+
arithmetic-expression = expression "%" expression ; % %x37
660+
arithmetic-expression =/ expression ( "*" / "×" ) expression ; * %x42 ×  %xD7
661+
arithmetic-expression =/ expression "+" expression ; + %x43
662+
arithmetic-expression =/ expression ( "-" / "" ) expression ; - %x45 – %x2212
663+
arithmetic-expression =/ expression ( "/" / "÷" ) expression ; / %x47 ÷
664+
665+
;; An `arithmetic-expression` enables simple computations using the four basic operations,
666+
;; as well as the modulo and integer-division operations.
667+
;;
668+
;; To support arithmetic operations, the following operators are available:
669+
;;
670+
;; - `+` addition operator
671+
;; - `-` subtraction operator
672+
;; - `*` multiplication operator
673+
;; - `/` division operator
674+
;; - `%` modulo operator
675+
;; - `//` integer division operator
676+
;;
677+
;; Proper mathematical operators are also supported using the following UNICODE characters:
678+
;;
679+
;; - `–` (U+2212 MINUS SIGN)
680+
;; - `÷` (U+00F7 DIVISION SIGN)
681+
;; - `×` (U+00D7 MULTIPLY SIGN)
682+
;;
683+
;; Arithmetic operations adhere to the usual precedence rules, from lowest to highest:
684+
;;
685+
;; - `-` subtraction operator and `+` addition operator
686+
;; - `/` division, `*` multiplication, `%` modulo and `//` integer division operators
687+
;;
688+
;; In the absence of parentheses, operators of the same level of precedence are evaluated from left to right.
689+
;; Arithmetic operators have higher precedence than comparison operators and lower precedence than the `.` "dot" `sub-expression` token separator.
690+
;;
691+
;; ## Examples
692+
;;
693+
;; ```
694+
;; search(a + b, {"a": 1, "b": 2}) -> 3
695+
;; search(a - b, {"a": 1, "b": 2}) -> -1
696+
;; search(a * b, {"a": 2, "b": 4}) -> 8
697+
;; search(a / b, {"a": 2, "b": 3}) -> 0.666666666666667
698+
;; search(a % b, {"a": 10, "b": 3} -> 1
699+
;; search(a // b, {"a": 10, "b": 3} -> -3
700+
;; search(a.b + cd, {"a": {"b": 1}, "c": {"d": 2}}) -> 3
701+
;; ```
702+
;;
703+
;; Since `arithmetic-expression` is not valid on the right-hand-side of a `sub-expression`, a `pipe-expression` can be used instead:
704+
;;
705+
;; ```
706+
;; search({ab: a.b, cd: c.d} | ab + cd, {"a": {"b": 1}, "c": {"d": 2}}) -> 3
707+
;; ```
708+
;;

grammar/arithmetic.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
test0:
2+
context:
3+
query: "`1` + `2`"
4+
returns: 3.0
5+
test1:
6+
context:
7+
query: "`1` − `2`"
8+
returns: -1.0
9+
test2:
10+
context:
11+
query: "`2` × `4`"
12+
returns: 8.0
13+
test3:
14+
context:
15+
query: "`2` ÷ `3`"
16+
returns: 0.666666666666667
17+
test4:
18+
context:
19+
query: "`10` % `3`"
20+
returns: 1.0
21+
test5:
22+
context:
23+
query: "`10` // `3`"
24+
returns: 3.0
25+
test6:
26+
context:
27+
query: "-`1` − +`2`"
28+
returns: -3.0
29+
test7:
30+
context:
31+
query: "`1` ÷ `0`"
32+
error: not-a-number

0 commit comments

Comments
 (0)