-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryLang.g4
175 lines (159 loc) · 4.33 KB
/
QueryLang.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
grammar QueryLang;
options {
language = JavaScript;
}
/*
* Parser Rules
*/
wholeQuery: query (WS+ wholeQuery)* EOF;
query:
numberOfQuery
| nameQuery
| similarityQuery
| typeQuery
| setNameQuery
| setQuery
| cmcQuery
| colorQuery
| commanderIdentityQuery
| powerQuery
| toughnessQuery
| textQuery;
numberOfQuery: '|' (COMMANDER_IDENTITY_KEYWORD | COLOR_KEYWORD | TYPE_KEYWORD) '|' operatorComparison numberValue;
typeQuery: TYPE_KEYWORD operatorEquality value;
similarityQuery: SIMILARITY_KEYWORD WS value | SIMILARITY_KEYWORD operatorEq value;
cmcQuery: CMC_KEYWORD operatorComparison numberValue;
nameQuery: NAME_KEYWORD operatorEquality value;
powerQuery: POWER_KEYWORD operatorComparison numberValue;
toughnessQuery:
TOUGHNESS_KEYWORD operatorComparison numberValue;
commanderIdentityQuery:
COMMANDER_IDENTITY_KEYWORD operatorComparison colorValue;
colorQuery: COLOR_KEYWORD operatorComparison colorValue;
setNameQuery: SET_NAME_KEYWORD operatorEquality value;
setQuery: SET_KEYWORD operatorEquality value;
textQuery: (TEXT_KEYWORD operatorEquality value) | value;
operatorComparison: operatorEquality | operatorOrder;
operatorEquality: operatorEq | operatorNeq;
operatorOrder:
operatorGt
| operatorGte
| operatorLt
| operatorLte;
operatorEq: OPERATOR_EQ;
operatorLt: OPERATOR_LT;
operatorLte: OPERATOR_LTE;
operatorGt: OPERATOR_GT;
operatorGte: OPERATOR_GTE;
operatorNeq: OPERATOR_NEQ;
numberValue: NUMBER_VALUE;
/*
There are some issues with lexer when color value is token COLOR_VALUE or (BLACK | RED | GREEN |
BLUE | WHITE)+;
Having 'value' here works for now, but let's fix it later.
*/
colorValue: value;
/*
Imagine query 'color color < rg' where 'color < rg' should be matched as color query 'color' should
be matched as text query
because it does not match color query rule (there is no operator afterwards)
ANTL4's Lexer converts everything to tokens, so it saves string 'color' as COLOR_KEYWORD and not as
VALUE.
So without having the COLOR_KEYWORD here, it would never match string 'color' as value but only as
a "keyword" in the color query.
Note: the order of the parser rules is extremly important for this trick.
*/
value:
VALUE
| QUOTED_VALUE
| TYPE_KEYWORD
| COLOR_KEYWORD
| SET_NAME_KEYWORD
| SET_KEYWORD
| NAME_KEYWORD
| CMC_KEYWORD
| TEXT_KEYWORD
| POWER_KEYWORD
| TOUGHNESS_KEYWORD
| SIMILARITY_KEYWORD
| COMMANDER_IDENTITY_KEYWORD;
/*
* Lexer Rules
*/
TYPE_KEYWORD: T Y P E | T;
POWER_KEYWORD: P O W E R | P O W;
TOUGHNESS_KEYWORD: T O U G H N E S S | T O U;
COLOR_KEYWORD: C O L O R | C O L O U R | C;
SET_NAME_KEYWORD: S E T '_' N A M E | S E T N A M E;
SET_KEYWORD: S E T | S;
NAME_KEYWORD: N A M E | N;
SIMILARITY_KEYWORD: S I M | S I M I L A R WS? T O | S I M I L A R;
CMC_KEYWORD:
C M C
| (C O N V E R T E D M A N A C O S T)
| (C O N V E R T E D WS M A N A C O S T);
TEXT_KEYWORD: T E X T | O | T;
COMMANDER_IDENTITY_KEYWORD:
C M D
| C O M M A N D E R
| C O M M A N D E R WS? I D E N T I T Y;
NUMBER_VALUE: NUMBER+;
/*
// RED: R E D | R; RED: R; // GREEN: G R E E N | G; GREEN: G; // BLUE: B L U E | U; BLUE: U; //
WHITE: W H I T E | W; WHITE: W; // BLACK: B L A C K | B; BLACK: B; COLOR_VALUE: (BLACK | RED |
GREEN | BLUE | WHITE)+;
*/
OPERATOR_EQ:
WS* '=' WS*
| WS* ':' WS*
| (WS+ E Q WS+)
| (WS+ E Q U A L WS+)
| (WS+ E Q U A L S WS+)
| (WS+ T O WS E Q U A L WS+)
| (WS+ T O WS E Q U A L S WS+);
OPERATOR_LT: WS* '<' WS* | (WS+ L T WS+);
OPERATOR_LTE: WS* '<=' WS* | (WS+ L T E WS+);
OPERATOR_GT: WS* '>' WS* | (WS+ G T WS+);
OPERATOR_GTE: WS* '>=' WS* | (WS+ G T E WS+);
OPERATOR_NEQ:
WS* '!=' WS*
| WS* '!:' WS*
| (WS+ N E Q WS+)
| (WS+ N O T WS E Q U A L WS+)
| (WS+ N O T WS E Q U A L S WS+)
| (WS+ T O WS N O T WS E Q U A L WS+);
WS: (' ' | '\t')+;
NEWLINE: ('\r'? '\n' | '\r')+;
VALUE: VAL;
QUOTED_VALUE:
// double quotes:
'"' ~('"')+ '"';
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];
fragment NUMBER: [0-9];
fragment VAL: [a-zA-Z0-9',]+;
fragment VAL_SPACE: [a-zA-Z0-9/,' _\-]+;