-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparse.go
621 lines (542 loc) · 13.1 KB
/
parse.go
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package qs
import (
"fmt"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/search/query"
"strconv"
"strings"
"time"
)
// ParseError is the error type returned by Parse()
type ParseError struct {
// Pos is the character position where the error occured
Pos int
// Msg is a description of the error
Msg string
}
func (pe ParseError) Error() string { return fmt.Sprintf("%d: %s", pe.Pos, pe.Msg) }
type OpType int
const (
OR OpType = 0
AND = 1
)
type Parser struct {
tokens []token
pos int
// DefaultOp is used when no explict OR or AND is present
// ie: foo bar => foo OR bar | foo AND bar
// TODO: not sure AND/OR is the right terminology (but it's what others use)
// Doesn't actually use OR or AND. AND is treated as an implied '+' prefix
DefaultOp OpType
// Loc is the location to use for parsing dates in range queries.
// If nil, UTC is assumed.
Loc *time.Location
}
// context is used to hold settings active within a given scope during parsing
// (TODO: maybe this should just be absorbed back into the Parser struct instead?)
type context struct {
// field is the name of the field currently in scope (or "")
field string
fieldPos int
}
// Parse takes a query string and turns it into a bleve Query.
//
// Returned errors are type ParseError, which includes the position
// of the offending part of the input string.
//
// BNF(ish) query syntax:
// exprList = expr1*
// expr1 = expr2 {"OR" expr2}
// expr2 = expr3 {"AND" expr3}
// expr3 = {"NOT"} expr4
// expr4 = {("+"|"-")} expr5
// expr5 = {field} part {boost}
// part = lit {"~" number} | range | "(" exprList ")"
// field = lit ":"
// range = ("["|"}") {lit} "TO" {lit} ("]"|"}")
// relational = ("<"|">"|"<="|">=") lit
// boost = "^" number
//
// (where lit is a string, quoted string or number)
func (p *Parser) Parse(q string) (query.Query, error) {
p.tokens = lex(q)
ctx := context{field: ""}
return p.parseExprList(ctx)
}
// Parse takes a query string and turns it into a bleve Query using
// the default Parser.
// Returned errors are type ParseError, which includes the position
// of the offending part of the input string.
func Parse(q string) (query.Query, error) {
p := Parser{DefaultOp: OR}
return p.Parse(q)
}
// peek looks at the next token without consuming it.
// peeks beyond the end of the token stream will return EOF
func (p *Parser) peek() token {
if p.pos < len(p.tokens) {
tok := p.tokens[p.pos]
return tok
}
return token{typ: tEOF}
}
// backup steps back one position in the token stream
func (p *Parser) backup() {
p.pos -= 1
}
// next fetches the next token in the stream
func (p *Parser) next() token {
if p.pos < len(p.tokens) {
tok := p.tokens[p.pos]
p.pos += 1
return tok
}
p.pos += 1 // to make sure backup() works
return token{typ: tEOF}
}
// starting point
// exprList = expr1*
func (p *Parser) parseExprList(ctx context) (query.Query, error) {
// <empty>
if p.peek().typ == tEOF {
return bleve.NewMatchNoneQuery(), nil
}
must := []query.Query{}
mustNot := []query.Query{}
should := []query.Query{}
for {
tok := p.peek()
if tok.typ == tEOF {
break
}
// slightly kludgy...
if tok.typ == tRPAREN {
break
}
prefix, q, err := p.parseExpr1(ctx)
if err != nil {
return nil, err
}
switch prefix {
case tPLUS:
must = append(must, q)
case tMINUS:
mustNot = append(mustNot, q)
default:
if p.DefaultOp == AND {
must = append(must, q)
} else { // OR
should = append(should, q)
}
}
}
// some obvious shortcuts
total := len(must) + len(mustNot) + len(should)
if total == 0 {
return bleve.NewMatchNoneQuery(), nil
}
if total == 1 && len(must) == 1 {
return must[0], nil
}
if total == 1 && len(should) == 1 {
return should[0], nil
}
// no shortcuts - go with the full-fat version
q := bleve.NewBooleanQuery()
if len(must) > 0 {
q.AddMust(must...)
}
if len(should) > 0 {
q.AddShould(should...)
}
if len(mustNot) > 0 {
q.AddMustNot(mustNot...)
}
return q, nil
}
// parseExpr1 handles OR expressions
//
// expr1 = expr2 {"OR" expr2}
func (p *Parser) parseExpr1(ctx context) (tokType, query.Query, error) {
queries := []query.Query{}
prefixes := []tokType{}
for {
prefix, q, err := p.parseExpr2(ctx)
if err != nil {
return tEOF, nil, err
}
prefixes = append(prefixes, prefix)
queries = append(queries, q)
tok := p.next()
if tok.typ != tOR {
p.backup()
break
}
}
// let single, non-OR expressions bubble upward, prefix intact
if len(queries) == 1 {
return prefixes[0], queries[0], nil
}
// KLUDGINESS - prefixes on terms in OR expressions
// we'll ignore "+" and treat "-" as NOT
// eg:
// `+alice OR -bob OR chuck` => `alice OR (NOT bob) OR chuck`
for i, _ := range queries {
if prefixes[i] == tMINUS {
q := bleve.NewBooleanQuery()
q.AddMustNot(queries[i])
queries[i] = q
}
}
return tEOF, bleve.NewDisjunctionQuery(queries...), nil
}
// parseExpr2 handles AND expressions
//
// expr2 = expr3 {"AND" expr3}
func (p *Parser) parseExpr2(ctx context) (tokType, query.Query, error) {
queries := []query.Query{}
prefixes := []tokType{}
for {
prefix, q, err := p.parseExpr3(ctx)
if err != nil {
return tEOF, nil, err
}
prefixes = append(prefixes, prefix)
queries = append(queries, q)
tok := p.next()
if tok.typ != tAND {
p.backup()
break
}
}
// let single, non-AND expressions bubble upward, prefix intact
if len(queries) == 1 {
return prefixes[0], queries[0], nil
}
// KLUDGINESS - prefixes on terms in AND expressions
// we'll ignore "+" and treat "-" as NOT
// eg:
// `+alice AND -bob AND chuck` => `alice AND (NOT bob) AND chuck`
for i, _ := range queries {
if prefixes[i] == tMINUS {
q := bleve.NewBooleanQuery()
q.AddMustNot(queries[i])
queries[i] = q
}
}
return tEOF, bleve.NewConjunctionQuery(queries...), nil
}
// parseExpr3 handles NOT expressions
//
// expr3 = {"NOT"} expr4
func (p *Parser) parseExpr3(ctx context) (tokType, query.Query, error) {
tok := p.next()
if tok.typ != tNOT {
p.backup()
// just let the lower, non-NOT expression bubble up with its prefix
return p.parseExpr4(ctx)
}
prefix, q, err := p.parseExpr4(ctx)
if err != nil {
return tEOF, nil, err
}
// KLUDGINESS - prefixes on terms in NOT expressions:
// `NOT -bob` => `bob`
// `NOT +bob` => `NOT bob`
if prefix != tMINUS {
q2 := bleve.NewBooleanQuery()
q2.AddMustNot(q)
q = q2
}
return tEOF, q, nil
}
// Here's where all the prefix-bubbling-up begins...
// expr4 = {("+"|"-")} expr5
func (p *Parser) parseExpr4(ctx context) (tokType, query.Query, error) {
var prefix tokType
tok := p.next()
switch tok.typ {
case tMINUS, tPLUS:
prefix = tok.typ
default:
p.backup()
prefix = tEOF
}
q, err := p.parseExpr5(ctx)
return prefix, q, err
}
// expr5 = {field} part {boost}
func (p *Parser) parseExpr5(ctx context) (query.Query, error) {
fldpos := p.peek().pos
fld, err := p.parseField()
if err != nil {
return nil, err
}
if fld != "" {
if ctx.field != "" {
return nil, ParseError{fldpos, fmt.Sprintf("'%s:' clashes with '%s:'", fld, ctx.field)}
}
ctx.field = fld
ctx.fieldPos = fldpos
}
q, err := p.parsePart(ctx)
if err != nil {
return nil, err
}
// parse (optional) suffix
boostpos := p.peek().pos
boost, err := p.parseBoostSuffix()
if err != nil {
return nil, err
}
if boost > 0 {
if boostable, ok := q.(query.BoostableQuery); ok {
boostable.SetBoost(boost)
} else {
return nil, ParseError{boostpos, "can't specify a boost value here"}
}
}
return q, nil
}
// part = lit {"~" number} | range | "(" exprList ")"
func (p *Parser) parsePart(ctx context) (query.Query, error) {
tok := p.next()
// lit
if tok.typ == tLITERAL {
var q query.Query
if strings.ContainsAny(tok.val, "*?") {
q = bleve.NewWildcardQuery(tok.val)
} else {
if p.peek().typ == tFUZZY {
fuzziness, err := p.parseFuzzySuffix()
if err != nil {
return nil, err
}
fuzz := bleve.NewFuzzyQuery(tok.val)
fuzz.SetFuzziness(fuzziness)
q = fuzz
} else {
q = bleve.NewMatchPhraseQuery(tok.val)
}
}
if ctx.field != "" {
if fieldable, ok := q.(query.FieldableQuery); ok {
fieldable.SetField(ctx.field)
} else {
return nil, ParseError{ctx.fieldPos, "unexpected field"}
}
}
return q, nil
}
if tok.typ == tQUOTED {
// strip quotes (ugh)
txt := string(tok.val[1 : len(tok.val)-1])
/*
if strings.ContainsAny(txt, "*?") {
return nil, ParseError{tok.pos, "wildcards not supported in phrases"}
}
*/
q := bleve.NewMatchPhraseQuery(txt)
if ctx.field != "" {
q.SetField(ctx.field)
}
return q, nil
}
// | "(" exprList ")"
if tok.typ == tLPAREN {
q, err := p.parseExprList(ctx)
if err != nil {
return nil, err
}
tok = p.next()
if tok.typ != tRPAREN {
return nil, ParseError{tok.pos, "missing )"}
}
return q, nil
}
// | range
if tok.typ == tLSQUARE || tok.typ == tLBRACE {
p.backup()
q, err := p.parseRange(ctx)
if err != nil {
return nil, err
}
return q, nil
}
// | relational
if tok.typ == tGREATER || tok.typ == tLESS {
p.backup()
q, err := p.parseRelational(ctx)
if err != nil {
return nil, err
}
return q, nil
}
if tok.typ == tERROR {
return nil, ParseError{tok.pos, tok.val}
}
return nil, ParseError{tok.pos, fmt.Sprintf("unexpected %s", tok.val)}
}
// returns >0 if there is a value given
func (p *Parser) parseBoostSuffix() (float64, error) {
tok := p.next()
if tok.typ != tBOOST {
p.backup()
return 0, nil
}
v := tok.val[1:]
if v == "" {
return 1.0, nil
}
boost, err := strconv.ParseFloat(v, 64)
if err != nil {
return 0, ParseError{tok.pos, "bad boost value"}
}
return boost, nil
}
//
func (p *Parser) parseFuzzySuffix() (int, error) {
tok := p.next()
if tok.typ != tFUZZY {
return 0, ParseError{tok.pos, "expected ~"}
}
v := tok.val[1:]
if v == "" {
return 1, nil // Default fuzziness is 1
}
fuzz, err := strconv.Atoi(v)
if err != nil {
return 0, ParseError{tok.pos, "bad fuzziness value"}
}
return fuzz, nil
}
// parse (optional) field specifier
// [ lit ":" ]
// returns field name or "" if not a field
func (p *Parser) parseField() (string, error) {
tok := p.next()
if tok.typ != tLITERAL {
// not a field
p.backup()
return "", nil
}
field := tok.val
tok = p.next()
if tok.typ != tCOLON {
// oop, not a field after all!
p.backup()
p.backup()
return "", nil
}
return field, nil // it's OK
}
// range = ("["|"}") {lit} "TO" {lit} ("]"|"}")
func (p *Parser) parseRange(ctx context) (query.Query, error) {
var minVal, maxVal string
var minInclusive, maxInclusive bool
openTok := p.next()
switch openTok.typ {
case tLSQUARE:
minInclusive = true
case tLBRACE:
minInclusive = false
default:
return nil, ParseError{openTok.pos, "expected range"}
}
tok := p.next()
switch tok.typ {
case tLITERAL:
minVal = tok.val
case tQUOTED:
minVal = tok.val[1 : len(tok.val)-1]
case tTO:
p.backup()
// empty start
default:
return nil, ParseError{tok.pos, fmt.Sprintf("unexpected %s", tok.val)}
}
tok = p.next()
if tok.typ != tTO {
return nil, ParseError{tok.pos, "expected TO"}
}
tok = p.next()
switch tok.typ {
case tLITERAL:
maxVal = tok.val
case tQUOTED:
maxVal = tok.val[1 : len(tok.val)-1]
case tRSQUARE:
p.backup() // empty end value
case tRBRACE:
p.backup() // empty end value
default:
return nil, ParseError{tok.pos, fmt.Sprintf("unexpected %s", tok.val)}
}
closeTok := p.next()
switch closeTok.typ {
case tRSQUARE:
maxInclusive = true
case tRBRACE:
maxInclusive = false
default:
return nil, ParseError{closeTok.pos, "expected ] or }"}
}
rp := newRangeParams(minVal, maxVal, minInclusive, maxInclusive, p.Loc)
q, err := rp.generate()
if err != nil {
return nil, ParseError{openTok.pos, err.Error()}
}
if ctx.field != "" {
if fieldable, ok := q.(query.FieldableQuery); ok {
fieldable.SetField(ctx.field)
} else {
return nil, ParseError{ctx.fieldPos, "unexpected field"}
}
}
return q, nil
}
// parseRelational handles greaterthan/lessthan etc...
// Implemented as a range.
// relational = ("<"|">"|"<="|">=") lit
func (p *Parser) parseRelational(ctx context) (query.Query, error) {
var minVal, maxVal string
var minInclusive, maxInclusive bool
rel := p.next()
if rel.typ != tGREATER && rel.typ != tLESS {
return nil, ParseError{rel.pos, "expected > or <"}
}
eq := p.next()
if eq.typ != tEQUAL {
p.backup()
}
var val string
tok := p.next()
switch tok.typ {
case tLITERAL:
val = tok.val
case tQUOTED:
val = tok.val[1 : len(tok.val)-1]
default:
return nil, ParseError{tok.pos, fmt.Sprintf("unexpected %s", tok.val)}
}
if rel.typ == tGREATER {
minVal = val
minInclusive = (eq.typ == tEQUAL)
} else { // if rel.typ == tLESS
maxVal = val
maxInclusive = (eq.typ == tEQUAL)
}
rp := newRangeParams(minVal, maxVal, minInclusive, maxInclusive, p.Loc)
q, err := rp.generate()
if err != nil {
return nil, ParseError{rel.pos, err.Error()}
}
if ctx.field != "" {
if fieldable, ok := q.(query.FieldableQuery); ok {
fieldable.SetField(ctx.field)
} else {
return nil, ParseError{ctx.fieldPos, "unexpected field"}
}
}
return q, nil
}