-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgenTest.pijnu
49 lines (43 loc) · 1.06 KB
/
genTest.pijnu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# a mini test grammar for the generator
genTest
<toolset>
def doMult(node):
(a,b) = node
node.value = a.value * b.value
def doAdd(node):
(a,b) = node
node.value = a.value + b.value
def formatResult(node):
node.value = "%.3f" % node.value
<definition>
# constants
SPACE : ' ' : drop
SPACING : SPACE* : drop
DOT : "."
MINUS : "-"
PLUS : "+" : drop
ADD : PLUS
_ADD_ : SPACING ADD SPACING : drop
MULT : "*"
_MULT_ : SPACING MULT SPACING : drop
DIGIT : [0..9]
SIGN : PLUS / MINUS
SIGN_ : SIGN SPACING
LPAREN : "(" : drop
RPAREN : ")" : drop
# operand
digits : DIGIT+
integer : SIGN_? digits
real : integer (DOT digits)?
number : real / integer : join toFloat
group : LPAREN operation RPAREN : liftNode
operand : group / number
# operation
mult : operand _MULT_ (mult/operand) : @ doMult
addOp : mult / operand
add : addOp _ADD_ (add/addOp) : @ doAdd
operation : add / mult : @
foo : ("a"/"b"){3}
bar : [1..9]{3}
baz : '1'{3}
result : operation / operand : formatResult