-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.y
325 lines (269 loc) · 6.39 KB
/
parser.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
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
%{
package lql
import (
"fmt"
"math"
bsq "github.com/blugelabs/bluge"
)
//go:generate go run golang.org/x/tools/cmd/goyacc -l -o parser.go parser.y
var debugGrammer bool
func logDebugGrammar(msg interface{}) {
if debugGrammer {
fmt.Printf("%+v\n", msg)
}
}
%}
%union{
num float64
str string
query bsq.Query
}
// multi byte tokens
%token tOR tAND tRETURNS tGREATERTHAN tLESSTHAN tNUMBER tSTRING
// single byte tokens
%token tEQUAL tEXCLAMATION tLBRACKET tRBRACKET tASTERISK
%token tTILDE tCOMMA tLSQUAREBRACKET tRSQUAREBRACKET
// Tokens only used internally and not in the grammar def
%token tWHITESPACE tBACKSLASH tDOUBLEQUOTE
// https://stackoverflow.com/questions/12876543/left-and-right-in-yacc
// https://www.gnu.org/software/bison/manual/html_node/Precedence-Decl.html
%left tCOMMA
%left tOR
%left tAND
%left tLBRACKET tRBRACKET
%type <num> tNUMBER
%type <str> tSTRING
%type <str> tASTERISK
%type <query> lql
%type <query> searchParts
%start main
// Read this for some info although figuring out exactly how yacc works
//
// http://dinosaur.compilertools.net/yacc/
// https://arcb.csc.ncsu.edu/~mueller/codeopt/codeopt00/y_man.pdf
%%
main:
searchParts {
logDebugGrammar("searchParts")
// TODO: add finish function here that can do any cleanup
// such as closing out any remaining logical operators that
// were in the queue.
query := $1
yylex.(*lex).setSearchQuery(query)
}
|
searchParts tRETURNS returnPart {
logDebugGrammar("searchParts tRETURNS returnPart")
// TODO: add finish function here that can do any cleanup
// such as closing out any remaining logical operators that
// were in the queue.
query := $1
yylex.(*lex).setSearchQuery(query)
}
;
returnPart:
tSTRING {
field := $1
yylex.(*lex).addReturnField(field)
}
|
returnPart tCOMMA returnPart {
logDebugGrammar("returnPart tCOMMA returnPart")
}
|
tLBRACKET returnPart tRBRACKET {
logDebugGrammar("tLBRACKET returnPart tRBRACKET")
}
;
searchParts:
lql {
logDebugGrammar("lql")
$$ = $1
}
|
searchParts tOR searchParts {
logDebugGrammar("searchParts tOR searchParts")
queryOne := $1
queryTwo := $3
query := bsq.NewBooleanQuery()
query.AddShould(queryOne)
query.AddShould(queryTwo)
$$ = query
}
|
searchParts tAND searchParts {
logDebugGrammar("searchParts tAND searchParts")
queryOne := $1
queryTwo := $3
query := bsq.NewBooleanQuery()
query.AddMust(queryOne)
query.AddMust(queryTwo)
$$ = query
}
|
tLBRACKET searchParts tRBRACKET {
logDebugGrammar("tLBRACKET searchParts tRBRACKET")
queryPar := $2
query := bsq.NewBooleanQuery()
query.AddMust(queryPar)
$$ = query
};
lql:
tSTRING tEQUAL tSTRING {
// k=v
logDebugGrammar("tSTRING tEQUAL tSTRING")
key := $1
value := $3
query := bsq.NewMatchQuery(value)
query.SetField(key)
$$ = query
}
|
tSTRING tEQUAL tNUMBER {
// k=1
logDebugGrammar("tSTRING tEQUAL tNUMBER")
key := $1
value := $3
query := bsq.NewNumericRangeInclusiveQuery(value, value, true, true)
query.SetField(key)
$$ = query
}
|
tSTRING tEXCLAMATION tEQUAL tSTRING {
// k!=v
logDebugGrammar("tSTRING tEXCLAMATION tEQUAL tSTRING")
key := $1
value := $4
match := bsq.NewMatchQuery(value)
match.SetField(key)
query := bsq.NewBooleanQuery()
query.AddMustNot(match)
$$ = query
}
|
tSTRING tEXCLAMATION tEQUAL tNUMBER {
// k!=1
logDebugGrammar("tSTRING tEXCLAMATION tEQUAL tNUMBER")
key := $1
value := $4
match := bsq.NewNumericRangeInclusiveQuery(value, value, true, true)
match.SetField(key)
query := bsq.NewBooleanQuery()
query.AddMustNot(match)
$$ = query
}
|
tSTRING tEQUAL tLSQUAREBRACKET tNUMBER tCOMMA tNUMBER tRSQUAREBRACKET {
// k=[v1,v2]
logDebugGrammar("tSTRING tEQUAL tLSQUAREBRACKET tNUMBER tCOMMA tNUMBER tRSQUAREBRACKET")
key := $1
min := $4
max := $6
// Read comment about func for more info
query := bsq.NewNumericRangeQuery(min, max)
query.SetField(key)
$$ = query
}
|
tSTRING tEQUAL tLSQUAREBRACKET tASTERISK tCOMMA tNUMBER tRSQUAREBRACKET {
// k=[*,v2]
logDebugGrammar("tSTRING tEQUAL tLSQUAREBRACKET tASTERISK tCOMMA tNUMBER tRSQUAREBRACKET")
key := $1
max := $6
// Read comment about func for more info
query := bsq.NewNumericRangeQuery(0.0, max)
query.SetField(key)
$$ = query
}
|
tSTRING tEQUAL tLSQUAREBRACKET tNUMBER tCOMMA tASTERISK tRSQUAREBRACKET {
// k=[v1,*]
logDebugGrammar("tSTRING tEQUAL tLSQUAREBRACKET tNUMBER tCOMMA tASTERISK tRSQUAREBRACKET")
key := $1
min := $4
// Read comment about func for more info
query := bsq.NewNumericRangeQuery(min, math.MaxFloat64)
query.SetField(key)
$$ = query
}
|
tSTRING tEQUAL tLSQUAREBRACKET tASTERISK tCOMMA tASTERISK tRSQUAREBRACKET {
// k=[*,*]
logDebugGrammar("tSTRING tEQUAL tLSQUAREBRACKET tASTERISK tCOMMA tASTERISK tRSQUAREBRACKET")
key := $1
// Read comment about func for more info
query := bsq.NewNumericRangeQuery(0.0, math.MaxFloat64)
query.SetField(key)
$$ = query
}
|
tSTRING tGREATERTHAN tNUMBER {
// k>v
logDebugGrammar("tSTRING tGREATERTHAN tNUMBER")
key := $1
value := $3
setInclusiveRangeQuery := false
query := bsq.NewNumericRangeInclusiveQuery(value, math.MaxFloat64,
setInclusiveRangeQuery, setInclusiveRangeQuery)
query.SetField(key)
$$ = query
}
|
tSTRING tLESSTHAN tNUMBER {
// k<v
logDebugGrammar("tSTRING tLESSTHAN tNUMBER")
key := $1
value := $3
setInclusiveRangeQuery := false
query := bsq.NewNumericRangeInclusiveQuery(0.0, value,
setInclusiveRangeQuery, setInclusiveRangeQuery)
query.SetField(key)
$$ = query
}
|
tSTRING tGREATERTHAN tEQUAL tNUMBER {
// k>=v
logDebugGrammar("tSTRING tGREATERTHAN tEQUAL tNUMBER")
key := $1
value := $4
setInclusiveRangeQuery := true
query := bsq.NewNumericRangeInclusiveQuery(value, math.MaxFloat64,
setInclusiveRangeQuery, setInclusiveRangeQuery)
query.SetField(key)
$$ = query
}
|
tSTRING tLESSTHAN tEQUAL tNUMBER {
// k<=v
logDebugGrammar("tSTRING tLESSTHAN tEQUAL tNUMBER")
key := $1
value := $4
setInclusiveRangeQuery := true
query := bsq.NewNumericRangeInclusiveQuery(0.0, value,
setInclusiveRangeQuery, setInclusiveRangeQuery)
query.SetField(key)
$$ = query
}
|
tSTRING tEQUAL tTILDE tSTRING {
// k=~v
logDebugGrammar("tSTRING tEQUAL tTILDE tSTRING")
key := $1
value := $4
query := bsq.NewRegexpQuery(value)
query.SetField(key)
$$ = query
}
|
tSTRING tEXCLAMATION tTILDE tSTRING {
// k!~v
logDebugGrammar("tSTRING tEXCLAMATION tTILDE tSTRING")
key := $1
value := $4
match := bsq.NewRegexpQuery(value)
match.SetField(key)
query := bsq.NewBooleanQuery()
query.AddMustNot(match)
$$ = query
}
;