-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FilterExpressionSyntax.g4
371 lines (326 loc) · 7.31 KB
/
FilterExpressionSyntax.g4
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//******************************************************************************************************
// FilterExpressionSyntax.g4 - Gbtc
//
// Copyright © 2019, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 10/27/2018 - J. Ritchie Carroll
// Generated original version of source code.
//
//******************************************************************************************************
grammar FilterExpressionSyntax;
parse
: ( filterExpressionStatementList | err ) EOF
;
err
: UNEXPECTED_CHAR
{
panic("Unexpected character: " + $UNEXPECTED_CHAR.text);
}
;
filterExpressionStatementList
: ';'* filterExpressionStatement ( ';'+ filterExpressionStatement )* ';'*
;
filterExpressionStatement
: identifierStatement
| filterStatement
| expression
;
identifierStatement
: GUID_LITERAL
| MEASUREMENT_KEY_LITERAL
| POINT_TAG_LITERAL
;
filterStatement
: K_FILTER ( K_TOP topLimit )? tableName K_WHERE expression ( K_ORDER K_BY orderingTerm ( ',' orderingTerm )* )?
;
topLimit
: ( '-' | '+' )? INTEGER_LITERAL
;
orderingTerm
: exactMatchModifier? orderByColumnName ( K_ASC | K_DESC )?
;
expressionList
: expression ( ',' expression )*
;
/*
Expressions understand the following binary operators,
in order from highest to lowest precedence:
* / %
+ -
<< >> & |
< <= > >=
= == === != !== <>
IS NULL IN LIKE
AND &&
OR ||
*/
expression
: notOperator expression
| expression logicalOperator expression
| predicateExpression
;
predicateExpression
: predicateExpression notOperator? K_IN exactMatchModifier? '(' expressionList ')'
| predicateExpression K_IS notOperator? K_NULL
| predicateExpression comparisonOperator predicateExpression
| predicateExpression notOperator? K_LIKE exactMatchModifier? predicateExpression
| valueExpression
;
valueExpression
: literalValue
| columnName
| functionExpression
| unaryOperator valueExpression
| '(' expression ')'
| valueExpression mathOperator valueExpression
| valueExpression bitwiseOperator valueExpression
;
notOperator
: K_NOT
| '!'
;
unaryOperator
: '-' | '+'
| '~' | '!' | K_NOT
;
exactMatchModifier
: K_BINARY
| '==='
;
// === and !== are for case sensitive string match
comparisonOperator
: '<' | '<=' | '>' | '>='
| '=' | '==' | '==='
| '!='| '!==' | '<>'
;
logicalOperator
: K_AND | '&&'
| K_OR | '||'
;
bitwiseOperator
: '<<' | '>>'
| '&' | '|'
| K_XOR | '^'
;
mathOperator
: '*' | '/' | '%'
| '+' | '-'
;
functionName
: K_ABS
| K_CEILING
| K_COALESCE
| K_CONVERT
| K_CONTAINS
| K_DATEADD
| K_DATEDIFF
| K_DATEPART
| K_ENDSWITH
| K_FLOOR
| K_IIF
| K_INDEXOF
| K_ISDATE
| K_ISINTEGER
| K_ISGUID
| K_ISNULL
| K_ISNUMERIC
| K_LASTINDEXOF
| K_LEN
| K_LOWER
| K_MAXOF
| K_MINOF
| K_NOW
| K_NTHINDEXOF
| K_POWER
| K_REGEXMATCH
| K_REGEXVAL
| K_REPLACE
| K_REVERSE
| K_ROUND
| K_SPLIT
| K_SQRT
| K_STARTSWITH
| K_STRCOUNT
| K_STRCMP
| K_SUBSTR
| K_TRIM
| K_TRIMLEFT
| K_TRIMRIGHT
| K_UPPER
| K_UTCNOW
;
functionExpression
: functionName '(' expressionList? ')'
;
literalValue
: INTEGER_LITERAL
| NUMERIC_LITERAL
| STRING_LITERAL
| DATETIME_LITERAL
| GUID_LITERAL
| BOOLEAN_LITERAL
| K_NULL
;
tableName
: IDENTIFIER
;
columnName
: IDENTIFIER
;
orderByColumnName
: IDENTIFIER
;
// Terminals for keywords should come before terminals with pattern expressions
// Keywords
K_ABS: A B S;
K_AND : A N D;
K_ASC : A S C;
K_BINARY : B I N A R Y;
K_BY : B Y;
K_CEILING : C E I L I N G;
K_COALESCE : C O A L E S C E;
K_CONVERT : C O N V E R T;
K_CONTAINS : C O N T A I N S;
K_DATEADD : D A T E A D D;
K_DATEDIFF : D A T E D I F F;
K_DATEPART : D A T E P A R T;
K_DESC : D E S C;
K_ENDSWITH : E N D S W I T H;
K_FILTER : F I L T E R;
K_FLOOR : F L O O R;
K_IIF : I I F;
K_IN : I N;
K_INDEXOF : I N D E X O F;
K_IS : I S;
K_ISDATE : I S D A T E;
K_ISINTEGER : I S I N T E G E R;
K_ISGUID : I S G U I D;
K_ISNULL : I S N U L L;
K_ISNUMERIC : I S N U M E R I C;
K_LASTINDEXOF : L A S T I N D E X O F;
K_LEN : L E N;
K_LIKE : L I K E;
K_LOWER : L O W E R;
K_MAXOF : M A X O F;
K_MINOF : M I N O F;
K_NOT : N O T;
K_NOW : N O W;
K_NTHINDEXOF : N T H I N D E X O F;
K_NULL : N U L L;
K_OR : O R;
K_ORDER : O R D E R;
K_POWER : P O W E R;
K_REGEXMATCH : R E G E X M A T C H;
K_REGEXVAL : R E G E X V A L;
K_REPLACE : R E P L A C E;
K_REVERSE : R E V E R S E;
K_ROUND : R O U N D;
K_SQRT : S Q R T;
K_SPLIT : S P L I T;
K_STARTSWITH : S T A R T S W I T H;
K_STRCOUNT : S T R C O U N T;
K_STRCMP: S T R C M P;
K_SUBSTR: S U B S T R;
K_TOP : T O P;
K_TRIM : T R I M;
K_TRIMLEFT : T R I M L E F T;
K_TRIMRIGHT : T R I M R I G H T;
K_UPPER : U P P E R;
K_UTCNOW : U T C N O W;
K_WHERE : W H E R E;
K_XOR : X O R;
BOOLEAN_LITERAL
: T R U E
| F A L S E
;
IDENTIFIER
: '`' ( ~'`' )+ '`'
| '[' ( ~']' )+ ']'
| [a-zA-Z_] [a-zA-Z_0-9]* // TODO check: needs more chars in set
;
INTEGER_LITERAL
: DIGIT+
| '0' X HEX_DIGIT+
;
NUMERIC_LITERAL
: DIGIT+ ( '.' DIGIT* )? ( E [-+]? DIGIT+ )?
| '.' DIGIT+ ( E [-+]? DIGIT+ )?
;
GUID_LITERAL
: '\'' GUID_VALUE '\''
| '{' GUID_VALUE '}'
| GUID_VALUE
;
MEASUREMENT_KEY_LITERAL
: ACRONYM_DIGIT+ ':' DIGIT+
;
POINT_TAG_LITERAL
: '"' ACRONYM_DIGIT+ '"'
;
STRING_LITERAL
: '\'' ( ~'\'' | '\'\'' )* '\''
;
DATETIME_LITERAL
: '#' ( ~'#' )+ '#'
;
SINGLE_LINE_COMMENT
: '--' ~[\r\n]* -> channel(HIDDEN)
;
MULTILINE_COMMENT
: '/*' .*? ( '*/' | EOF ) -> channel(HIDDEN)
;
SPACES
: [ \u000B\t\r\n] -> channel(HIDDEN)
;
UNEXPECTED_CHAR
: .
;
fragment DIGIT : [0-9];
fragment HEX_DIGIT : [0-9a-fA-F];
fragment ACRONYM_DIGIT : ( [a-zA-Z0-9] | '-' | '!' | '_' | '.' | '@' | '#' | '$' );
fragment GUID_VALUE
: HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT '-'?
HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT '-'?
HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT '-'?
HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT '-'?
HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
fragment A : [aA];
fragment B : [bB];
fragment C : [cC];
fragment D : [dD];
fragment E : [eE];
fragment F : [fF];
fragment G : [gG];
fragment H : [hH];
fragment I : [iI];
fragment J : [jJ];
fragment K : [kK];
fragment L : [lL];
fragment M : [mM];
fragment N : [nN];
fragment O : [oO];
fragment P : [pP];
fragment Q : [qQ];
fragment R : [rR];
fragment S : [sS];
fragment T : [tT];
fragment U : [uU];
fragment V : [vV];
fragment W : [wW];
fragment X : [xX];
fragment Y : [yY];
fragment Z : [zZ];