-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcondition-parser.pegjs
88 lines (68 loc) · 2.04 KB
/
condition-parser.pegjs
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
start
= _* additive:additive _* { return additive; }
additive
= left:multiplicative _+ operator:or_operator _+ right:additive _* { return { left: left, operator: operator, right: right }; }
/ multiplicative: multiplicative { return multiplicative; }
multiplicative
=
left:primary _+ operator:and_operator _+ right:multiplicative { return { left: left, operator: operator, right: right }; }
/ primary
primary
= not_operator basic:basic_primary { return { not: basic }; }
/ basic_primary
basic_primary
= statement
/ "(" _* additive:additive _* ")" { return additive; }
statement
= property:property _* operator:comparison_operators _* value:value { return { property: property, operator:operator, value: value }; }
/ property:property { return { property: property}; }
property
= head:property_name tail:subProperty { return [head].concat(tail); }
/ property:property_name { return [property]; }
subProperty
= '.' property:property { return property; }
/ '[' value:value ']' tail:(subProperty)? { return [value].concat(tail || []); }
/ '[]' tail:(subProperty)? { var result = { type: 'array' }; if (tail) { result.property = tail; } return [result]; }
property_name
= term:[a-zA-Z] tail:[a-zA-Z0-9-_]* { return term + tail.join(''); }
value
= quoted_value
/ bool
/ alpha
/ integer
quoted_value
= '"' value:char* '"' { return value.join(''); }
bool
= 'true' { return true; }
/ 'false' { return false; }
alpha
= term:[a-zA-Z]+ { return term.join(''); }
integer
= digits:[0-9]+ { return parseInt(digits.join(""), 10); }
and_operator
= ('AND' / 'and' / '&&') { return 'AND'; }
or_operator
= ('OR' / 'or' / '||') { return 'OR'; }
not_operator
= ('NOT' _+ / 'not' _+ / '!' _*) { return 'NOT'; }
comparison_operators
='<='
/ '>='
/ '==='
/ '!=='
/ ('==' / '=') { return '=='; }
/ ('!=' / '<>') { return '!='; }
/ '<'
/ '>'
char
= unescaped
/ escape
sequence:(
'"'
/ "\\"
)
{ return sequence; }
escape = "\\"
unescaped = [^\0-\x1F\x22\x5C]
_ "whitespace"
= [ \t\r\n\f]+