-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgrammar.pegjs
283 lines (220 loc) · 6.64 KB
/
grammar.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//<?php
// Root definitions
Root
= _? settings:(Settings _)? first:QueryCommand rest:(_? '|' _? TailCommand)* _? {
return [
isset($settings) ? $settings[0]:[],
Util::combine($first, $rest, 3)
];
}
TailCommand
= JoinCommand
/ AggCommand
/ TransactionCommand
// Settings
Settings
= first:Setting rest:(_ Setting)* {
return Util::assoc($first, $rest, 1);
}
Setting
= '$' ret:(
key:'date_field' SEP val:Field { return [$key, $val]; } /
key:'to' SEP val:Date { return [$key, $val]; } /
key:'from' SEP val:Date { return [$key, $val]; } /
key:'size' SEP val:Integer { return [$key, $val]; } /
key:'flatten' SEP val:Boolean { return [$key, $val]; } /
key:'sort' SEP '[' val:SortsSetting ']' { return [$key, $val]; } /
key:'fields' SEP '[' first:Field rest:(_ Field)* ']' { return [$key, Util::combine($first, $rest, 1)]; } /
key:'map' SEP '[' val:MapSetting ']' { return [$key, $val]; } /
) { return $ret; }
MapSetting
= _? first:FieldMap rest:(_? ',' _? FieldMap)* _? { return Util::assoc($first, $rest, 3); }
FieldMap
= field:Field new_field:(SEP Field) { return [$field, $new_field[1]]; }
SortsSetting
= _? first:FieldSort rest:(_? ',' _? FieldSort)* _? { return Util::combine($first, $rest, 3); }
FieldSort
= field:Field order:(SEP Order)? { return [$field, isset($order) ? $order[1]:0]; }
Date
= Integer
/ $[0-9a-zA-Z|/+-]+ // FIXME: Should be more strict.
Order
= 'ASC' { return 0; }
/ 'DESC' { return 1; }
// Glue
SEP
= ':'
_ "WhitespaceOrComment"
= Whitespace? Comment Whitespace?
/ Whitespace
Comment
= '#' [^\r\n]*
Whitespace
= WhitespaceChar+
WhitespaceChar
= [ \t\n\r]
// Field names and values
Field
= $[a-zA-Z0-9\._\-]+
Value
= $[a-zA-Z0-9\._\-]+
Boolean
= 'true' { return true; }
/ 'false' { return false; }
Integer
= num:[0-9]+ { return intval(implode('', $num)); }
// Special values
WildCardValue
= '"' chrs:DoubleQuotedChar* '"' { return [[implode('', $chrs)], false]; }
/ "'" chrs:SingleQuotedChar* "'" { return [[implode('', $chrs)], false]; }
/ !"NOT" !"AND" !"OR" chunks:WildCardChunk+ { return [$chunks, true]; }
WildCardChunk
= '*' { return Token::W_STAR; }
/ '?' { return Token::W_QMARK; }
/ chrs:EscapedChar+ { return implode('', $chrs); }
EscapedChar
= '\\' chr:MetaChar { return $chr; }
/ SpecialChar
/ !MetaChar chr:. { return $chr; }
LiteralValue
= '"' chrs:DoubleQuotedChar* '"' { return implode('', $chrs); }
/ "'" chrs:SingleQuotedChar* "'" { return implode('', $chrs); }
/ chrs:LiteralChar+ { return implode('', $chrs); }
LiteralChar
= EscapedChar
DoubleQuotedChar
= '\\"' { return '"'; }
/ SpecialChar
/ '\\'? chr:[^"] { return $chr; }
SingleQuotedChar
= "\\'" { return "'"; }
/ chr:[^'] { return $chr; }
MetaChar
= chr:WhitespaceChar { return $chr[0]; }
/ '(' / ')'
/ '{' / '}'
/ '[' / ']'
/ '+'
/ '-'
/ ':'
/ '\\'
/ '/'
/ '@'
/ '^'
/ '|'
/ '"'
/ "'"
/ '*'
/ '?'
SpecialChar
= '\\n' { return "\n"; }
/ '\\r' { return "\r"; }
/ '\\t' { return "\t"; }
// Query command
QueryCommand
= query:QueryExpression { return [Token::C_SEARCH, $query]; }
/ '*' { return [Token::C_SEARCH, []]; }
QueryExpression
= QueryOR
QueryOR
= a:QueryAND b:(_ 'OR' _ QueryAND)* {
if(isset($b) && count($b)) {
return [Token::F_OR, Util::combine($a, $b, 3)];
}
return $a;
}
QueryAND
= a:QueryNOT b:((_ 'AND')? _ QueryNOT)* {
if(isset($b) && count($b)) {
return [Token::F_AND, Util::combine($a, $b, 2)];
}
return $a;
}
QueryNOT
= a:QueryUnaryNOT b:((_ 'AND')? _ 'NOT' _ QueryUnaryNOT)? {
if(isset($b) && count($b)) {
return [Token::F_AND, [$a, [Token::F_NOT, $b[4]]]];
}
return $a;
}
QueryUnaryNOT
= neg:'-'? '(' _? expr:QueryExpression _? ')' {
if($neg) {
return [Token::F_NOT, $expr];
}
return $expr;
}
/ neg:'-'? clause:QueryClause {
if($neg) {
$clause = [Token::F_NOT, $clause];
}
return $clause;
}
QueryClause
= '_exists_' SEP ret:(
field:Field { return [Token::F_EXISTS, $field]; } /
{ $this->error('Invalid field'); }) { return $ret; }
/ '_missing_' SEP ret:(
field:Field { return [Token::F_MISSING, $field]; } /
{ $this->error('Invalid field'); }) { return $ret; }
/ field:Field SEP a:QueryRangeLow _? ret:(
lo:QueryRangeValue _ 'TO' _ hi:QueryRangeValue _? b:QueryRangeHigh
{ return [$b == ']', $lo, $hi]; } /
{ $this->error('Invalid range'); }) { return array_merge([Token::F_RANGE, $field, $a == '['], $ret); }
/ field:Field SEP '^' ret:(
val:LiteralValue { return [$val]; } /
{ $this->error('Invalid prefix'); }) { return array_merge([Token::F_PREFIX, $field], $ret); }
/ field:Field SEP '/' ret:(
regex:RegexValue '/' { return [$regex]; } /
{ $this->error('Invalid regex'); }) { return array_merge([Token::F_REGEX, $field], $ret); }
/ field:Field SEP '(' ret:(
_? first:WildCardValue rest:((_ 'OR')? _ WildCardValue)* _? ')'
{ return [Util::combine($first, $rest, 2)]; } /
{ $this->error('Invalid list'); }) { return array_merge([Token::X_LIST, $field], $ret, [true]); }
/ field:Field SEP esc:('@' '@'?) ret:(
list:Field
{ return [$list]; } /
{ $this->error('Invalid list name'); }) { return array_merge([Token::X_LIST, $field], $ret, [$esc[1] == '@']); }
/ field:Field SEP val:WildCardValue { return [Token::Q_QUERYSTRING, $field, $val]; }
/ val:WildCardValue { return [Token::Q_QUERYSTRING, null, $val]; }
RegexValue
= chrs:RegexChar* { return implode('', $chrs); }
RegexChar
= '\\/' { return '/'; }
/ [^/]
QueryRangeLow
= '['
/ '{'
QueryRangeHigh
= ']'
/ '}'
QueryRangeValue
= '*' { return null; }
/ LiteralValue
// Join command
JoinCommand
= 'join' _ 'source_field' SEP source:Field _ 'target_field' SEP target:Field query:(_ QueryExpression)? {
return [Token::C_JOIN, $source, $target, isset($query) ? $query[1]:[]];
}
// Agg command
AggCommand
= 'agg' SEP type:AggType _ 'field' SEP field:Field settings:(_ AggSettings)? {
return [Token::C_AGG, $type, $field, isset($settings) ? $settings[1]:[]];
}
AggType
= 'terms' { return Token::A_TERMS; }
/ 'sigterms' { return Token::A_SIGTERMS; }
/ 'card' { return Token::A_CARD; }
/ 'min' { return Token::A_MIN; }
/ 'max' { return Token::A_MAX; }
/ 'avg' { return Token::A_AVG; }
/ 'sum' { return Token::A_SUM; }
AggSettings
= first:AggSetting rest:(_ AggSetting)* {
return Util::assoc($first, $rest, 1);
}
AggSetting
= field:Field SEP val:Value { return [$field, is_numeric($val) ? (float)$val:$val]; }
// Transaction command
TransactionCommand
= 'transaction' _ 'field' SEP field:Field { return [Token::C_TRANS, $field]; }