-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.pegjs
141 lines (110 loc) · 2.23 KB
/
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
start
= Search
/*
* A search is defined as a single predicate
*/
Search
= predicate:Predicate
{ return predicate; }
Predicate
= match:Match
{ return match; }
/ comparator:Comparator
{ return comparator; }
Constraints
= constraints:(
head:Predicate
tail:(value_separator p:Predicate { return p; })*
{ return [head].concat(tail); }
)?
{ return constraints !== null ? constraints: []; }
Match
= _ fn:LogicalFunction name_separator begin_object constraints:Constraints end_object
{ return { type: 'logical', fn: fn, constraints: constraints }; }
Args
= begin_array identifier:string _ "," _ value:SerializableValue end_array
{ return { identifier: identifier, value: value }; }
Comparator
= _ fn:ComparisonFunction name_separator args:Args _
{ return { type: 'comparison', fn: fn, args: args }; }
ComparisonFunction
= "eq"
/ "leq"
/ "geq"
/ "neq"
/ "lt"
/ "gt"
/ "like"
/ "in"
{ return text(); }
LogicalFunction
= "match_all"
/ "match_any"
{ return text(); }
begin_object = _ "{" _
end_object = _ "}" _
begin_array = _ "[" _
end_array = _ "]" _
name_separator = _ ":" _
value_separator = _ "," _
_ "whitespace"
= [ \t\n\r]*
// Values
SerializableValue
= false
/ true
/ null
/ string
/ number
false = "false" { return false; }
null = "null" { return null; }
true = "true" { return true; }
// Numbers
number "number"
= minus? int frac? exp? { return parseFloat(text()); }
decimal_point
= "."
digit1_9
= [1-9]
e
= [eE]
exp
= e (minus / plus)? DIGIT+
frac
= decimal_point DIGIT+
int
= zero / (digit1_9 DIGIT*)
minus
= "-"
plus
= "+"
zero
= "0"
// Strings
string "string"
= quotation_mark chars:char* quotation_mark { return chars.join(""); }
char
= unescaped
/ escape
sequence:(
'"'
/ "\\"
/ "/"
/ "b" { return "\b"; }
/ "f" { return "\f"; }
/ "n" { return "\n"; }
/ "r" { return "\r"; }
/ "t" { return "\t"; }
/ "u" digits:$(HEXDIG HEXDIG HEXDIG HEXDIG) {
return String.fromCharCode(parseInt(digits, 16));
}
)
{ return sequence; }
escape
= "\\"
quotation_mark
= '"'
unescaped
= [^\0-\x1F\x22\x5C]
DIGIT = [0-9]
HEXDIG = [0-9a-f]i