-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphinxexpr.y
252 lines (220 loc) · 8.51 KB
/
sphinxexpr.y
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
%lex-param { ExprParser_t * pParser }
%parse-param { ExprParser_t * pParser }
%pure-parser
%error-verbose
%union {
int64_t iConst; // constant value
float fConst; // constant value
uint64_t iAttrLocator; // attribute locator (rowitem for int/float; offset+size for bits)
int iFunc; // function id
int iNode; // node, or uservar, or udf index
const char * sIdent; // generic identifier (token does NOT own ident storage; ie values are managed by parser)
};
%token <iConst> TOK_CONST_INT
%token <fConst> TOK_CONST_FLOAT
%token <iConst> TOK_CONST_STRING
%token <iConst> TOK_SUBKEY
%token <iConst> TOK_DOT_NUMBER
%token <iAttrLocator> TOK_ATTR_INT
%token <iAttrLocator> TOK_ATTR_BITS
%token <iAttrLocator> TOK_ATTR_FLOAT
%token <iAttrLocator> TOK_ATTR_MVA32
%token <iAttrLocator> TOK_ATTR_MVA64
%token <iAttrLocator> TOK_ATTR_STRING
%token <iAttrLocator> TOK_ATTR_FACTORS
%token <iFunc> TOK_FUNC
%token <iFunc> TOK_FUNC_IN
%token <iFunc> TOK_FUNC_REMAP
%token <iNode> TOK_FUNC_PF
%token <iNode> TOK_USERVAR
%token <iNode> TOK_UDF
%token <iNode> TOK_HOOK_IDENT
%token <iNode> TOK_HOOK_FUNC
%token <sIdent> TOK_IDENT
%token <iAttrLocator> TOK_ATTR_JSON
%token TOK_ATID
%token TOK_ATWEIGHT
%token TOK_ID
%token TOK_GROUPBY
%token TOK_WEIGHT
%token TOK_COUNT
%token TOK_DISTINCT
%token TOK_CONST_LIST
%token TOK_ATTR_SINT
%token TOK_MAP_ARG
%token TOK_FOR
%token TOK_ITERATOR
%token TOK_IS
%token TOK_NULL
%token TOK_IS_NULL
%token TOK_IS_NOT_NULL
%type <iNode> attr
%type <iNode> expr
%type <iNode> arg
%type <iNode> arglist
%type <iNode> constlist
%type <iNode> constlist_or_uservar
%type <iNode> maparg
%type <sIdent> map_key
%type <iNode> function
%type <sIdent> ident
%type <iNode> stringlist
%type <iNode> json_field
%type <iNode> json_expr
%type <iNode> subkey
%type <iNode> subscript
%type <iNode> for_loop
%type <iNode> iterator
%type <iNode> streq
%type <iNode> strval
%left TOK_OR
%left TOK_AND
%left '|'
%left '&'
%left TOK_EQ TOK_NE
%left '<' '>' TOK_LTE TOK_GTE
%left '+' '-'
%left '*' '/' '%' TOK_DIV TOK_MOD
%nonassoc TOK_NOT
%nonassoc TOK_NEG
%%
exprline:
expr { pParser->m_iParsed = $1; }
;
attr:
TOK_ATTR_INT { $$ = pParser->AddNodeAttr ( TOK_ATTR_INT, $1 ); }
| TOK_ATTR_BITS { $$ = pParser->AddNodeAttr ( TOK_ATTR_BITS, $1 ); }
| TOK_ATTR_FLOAT { $$ = pParser->AddNodeAttr ( TOK_ATTR_FLOAT, $1 ); }
| TOK_ATTR_JSON { $$ = pParser->AddNodeAttr ( TOK_ATTR_JSON, $1 ); }
| '`' attr '`' { $$ = $2; }
;
expr:
attr
| function
| TOK_CONST_INT { $$ = pParser->AddNodeInt ( $1 ); }
| TOK_CONST_FLOAT { $$ = pParser->AddNodeFloat ( $1 ); }
| TOK_DOT_NUMBER { $$ = pParser->AddNodeDotNumber ( $1 ); }
| TOK_ATID { $$ = pParser->AddNodeID(); }
| TOK_ATWEIGHT { $$ = pParser->AddNodeWeight(); }
| TOK_ID { $$ = pParser->AddNodeID(); }
| TOK_WEIGHT '(' ')' { $$ = pParser->AddNodeWeight(); }
| TOK_HOOK_IDENT { $$ = pParser->AddNodeHookIdent ( $1 ); }
| '-' expr %prec TOK_NEG { $$ = pParser->AddNodeOp ( TOK_NEG, $2, -1 ); }
| TOK_NOT expr { $$ = pParser->AddNodeOp ( TOK_NOT, $2, -1 ); if ( $$<0 ) YYERROR; }
| expr '+' expr { $$ = pParser->AddNodeOp ( '+', $1, $3 ); }
| expr '-' expr { $$ = pParser->AddNodeOp ( '-', $1, $3 ); }
| expr '*' expr { $$ = pParser->AddNodeOp ( '*', $1, $3 ); }
| expr '/' expr { $$ = pParser->AddNodeOp ( '/', $1, $3 ); }
| expr '<' expr { $$ = pParser->AddNodeOp ( '<', $1, $3 ); }
| expr '>' expr { $$ = pParser->AddNodeOp ( '>', $1, $3 ); }
| expr '&' expr { $$ = pParser->AddNodeOp ( '&', $1, $3 ); }
| expr '|' expr { $$ = pParser->AddNodeOp ( '|', $1, $3 ); }
| expr '%' expr { $$ = pParser->AddNodeOp ( '%', $1, $3 ); }
| expr TOK_DIV expr { $$ = pParser->AddNodeFunc ( FUNC_IDIV, pParser->AddNodeOp ( ',', $1, $3 ) ); }
| expr TOK_MOD expr { $$ = pParser->AddNodeOp ( '%', $1, $3 ); }
| expr TOK_LTE expr { $$ = pParser->AddNodeOp ( TOK_LTE, $1, $3 ); }
| expr TOK_GTE expr { $$ = pParser->AddNodeOp ( TOK_GTE, $1, $3 ); }
| expr TOK_EQ expr { $$ = pParser->AddNodeOp ( TOK_EQ, $1, $3 ); }
| expr TOK_NE expr { $$ = pParser->AddNodeOp ( TOK_NE, $1, $3 ); }
| expr TOK_AND expr { $$ = pParser->AddNodeOp ( TOK_AND, $1, $3 ); if ( $$<0 ) YYERROR; }
| expr TOK_OR expr { $$ = pParser->AddNodeOp ( TOK_OR, $1, $3 ); if ( $$<0 ) YYERROR; }
| '(' expr ')' { $$ = $2; }
| json_expr
| iterator
| streq
| json_field TOK_IS TOK_NULL { $$ = pParser->AddNodeOp ( TOK_IS_NULL, $1, -1); }
| json_field TOK_IS TOK_NOT TOK_NULL { $$ = pParser->AddNodeOp ( TOK_IS_NOT_NULL, $1, -1); }
;
maparg:
map_key TOK_EQ TOK_CONST_INT { $$ = pParser->AddNodeMapArg ( $1, NULL, $3 ); }
| map_key TOK_EQ TOK_IDENT { $$ = pParser->AddNodeMapArg ( $1, $3, 0 ); }
| maparg ',' map_key TOK_EQ TOK_CONST_INT { pParser->AppendToMapArg ( $$, $3, NULL, $5 ); }
| maparg ',' map_key TOK_EQ TOK_IDENT { pParser->AppendToMapArg ( $$, $3, $5, 0 ); }
;
map_key:
ident { $$ = $1; }
| TOK_ATTR_STRING { $$ = pParser->Attr2Ident($1); }
| TOK_FUNC_IN { $$ = strdup("in"); }
;
arg:
expr
| '{' maparg '}' { $$ = $2; }
| TOK_ATTR_STRING { $$ = pParser->AddNodeAttr ( TOK_ATTR_STRING, $1 ); }
| TOK_ATTR_MVA32 { $$ = pParser->AddNodeAttr ( TOK_ATTR_MVA32, $1 ); }
| TOK_ATTR_MVA64 { $$ = pParser->AddNodeAttr ( TOK_ATTR_MVA64, $1 ); }
| TOK_ATTR_FACTORS { $$ = pParser->AddNodeAttr ( TOK_ATTR_FACTORS, $1 ); }
| TOK_CONST_STRING { $$ = pParser->AddNodeString ( $1 ); }
;
arglist:
arg { $$ = $1; }
| arglist ',' arg { $$ = pParser->AddNodeOp ( ',', $1, $3 ); }
;
constlist:
TOK_CONST_INT { $$ = pParser->AddNodeConstlist ( $1 ); }
| '-' TOK_CONST_INT { $$ = pParser->AddNodeConstlist ( -$2 );}
| TOK_CONST_FLOAT { $$ = pParser->AddNodeConstlist ( $1 ); }
| '-' TOK_CONST_FLOAT { $$ = pParser->AddNodeConstlist ( -$2 );}
| constlist ',' TOK_CONST_INT { pParser->AppendToConstlist ( $$, $3 ); }
| constlist ',' '-' TOK_CONST_INT { pParser->AppendToConstlist ( $$, -$4 );}
| constlist ',' TOK_CONST_FLOAT { pParser->AppendToConstlist ( $$, $3 ); }
| constlist ',' '-' TOK_CONST_FLOAT { pParser->AppendToConstlist ( $$, -$4 );}
;
stringlist:
TOK_CONST_STRING { $$ = pParser->AddNodeConstlist ( $1 ); }
| stringlist ',' TOK_CONST_STRING { pParser->AppendToConstlist ( $$, $3 ); }
;
constlist_or_uservar:
constlist
| stringlist
| TOK_USERVAR { $$ = pParser->AddNodeUservar ( $1 ); }
;
ident:
TOK_ATTR_INT { $$ = pParser->Attr2Ident ( $1 ); }
| TOK_IDENT
;
function:
TOK_FUNC '(' arglist ')' { $$ = pParser->AddNodeFunc ( $1, $3 ); if ( $$<0 ) YYERROR; }
| TOK_FUNC '(' ')' { $$ = pParser->AddNodeFunc ( $1, -1 ); if ( $$<0 ) YYERROR; }
| TOK_UDF '(' arglist ')' { $$ = pParser->AddNodeUdf ( $1, $3 ); if ( $$<0 ) YYERROR; }
| TOK_UDF '(' ')' { $$ = pParser->AddNodeUdf ( $1, -1 ); if ( $$<0 ) YYERROR; }
| TOK_FUNC_IN '(' arg ',' constlist_or_uservar ')'{ $$ = pParser->AddNodeFunc ( $1, $3, $5 ); }
| TOK_HOOK_FUNC '(' arglist ')' { $$ = pParser->AddNodeHookFunc ( $1, $3 ); if ( $$<0 ) YYERROR; }
| TOK_FUNC '(' expr for_loop ')' { $$ = pParser->AddNodeFunc ( $1, $3, $4 ); }
| TOK_FUNC_REMAP '(' expr ',' expr ',' '(' constlist ')' ',' '(' constlist ')' ')' { $$ = pParser->AddNodeFunc ( $1, $3, $5, $8, $12 ); }
| TOK_FUNC_PF '(' ')' { $$ = pParser->AddNodePF ( $1, -1 ); }
| TOK_FUNC_PF '(' arg ')' { $$ = pParser->AddNodePF ( $1, $3 ); }
;
json_field:
json_expr
| attr
;
json_expr:
TOK_ATTR_JSON subscript { $$ = pParser->AddNodeJsonField ( $1, $2 ); }
subscript:
subkey
| subscript subkey { $$ = pParser->AddNodeOp ( ',', $1, $2 ); }
;
subkey:
'[' expr ']' { $$ = $2; }
| TOK_SUBKEY { $$ = pParser->AddNodeJsonSubkey ( $1 ); }
| TOK_DOT_NUMBER { $$ = pParser->AddNodeJsonSubkey ( $1 ); }
| '[' TOK_CONST_STRING ']' { $$ = pParser->AddNodeString ( $2 ); }
| '[' TOK_ATTR_STRING ']' { $$ = pParser->AddNodeAttr ( TOK_ATTR_STRING, $2 ); }
;
for_loop:
TOK_FOR TOK_IDENT TOK_FUNC_IN json_field { $$ = pParser->AddNodeIdent ( $2, $4 ); }
;
iterator:
TOK_IDENT { $$ = pParser->AddNodeIdent ( $1, -1 ); }
| TOK_IDENT subscript { $$ = pParser->AddNodeIdent ( $1, $2 ); }
;
streq:
expr TOK_EQ strval { $$ = pParser->AddNodeOp ( TOK_EQ, $1, $3 ); }
| strval TOK_EQ expr { $$ = pParser->AddNodeOp ( TOK_EQ, $3, $1 ); }
| strval TOK_EQ strval { $$ = pParser->AddNodeOp ( TOK_EQ, $1, $3 ); }
;
strval:
TOK_CONST_STRING { $$ = pParser->AddNodeString ( $1 ); }
| TOK_ATTR_STRING { $$ = pParser->AddNodeAttr ( TOK_ATTR_STRING, $1 ); }
;
%%