-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
970 lines (895 loc) · 23.5 KB
/
Copy pathparser.go
File metadata and controls
970 lines (895 loc) · 23.5 KB
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
package eql
import (
"errors"
"fmt"
"strconv"
"strings"
)
// MaxInputSize is the largest query Parse and ExtractConditions accept.
const MaxInputSize = 1 << 20 // 1 MiB
// maxExprDepth bounds expression nesting to keep hostile inputs from
// exhausting the stack.
const maxExprDepth = 200
// ErrEmptyQuery is returned when the input contains no tokens.
var ErrEmptyQuery = errors.New("empty query")
// ErrInputTooLarge is returned when the input exceeds MaxInputSize.
var ErrInputTooLarge = errors.New("input exceeds maximum size")
// Parse parses a single EQL statement and returns its AST. The parser is
// tolerant: it recovers from many errors and still produces a partial AST,
// but any problem encountered is reported in the returned error (the AST is
// still valid to inspect). Input is parsed as-is; use NormalizeQuery first
// for content pasted from documents.
func Parse(query string) (*Query, error) {
q, errs := parseTolerant(query)
if len(errs) > 0 {
return q, errors.New(strings.Join(errs, "; "))
}
return q, nil
}
// ParseExpression parses a standalone boolean expression (no event category,
// sequences, or pipes).
func ParseExpression(input string) (Expr, error) {
if len(input) > MaxInputSize {
return nil, ErrInputTooLarge
}
lex := newLexer(input)
p := &parser{toks: lex.tokens, errs: lex.errs}
if p.cur().Type == TokenEOF {
return nil, ErrEmptyQuery
}
e := p.parseExpression()
if p.cur().Type != TokenEOF {
p.errorAt(p.cur(), "unexpected trailing input")
}
if len(p.errs) > 0 {
return e, errors.New(strings.Join(p.errs, "; "))
}
return e, nil
}
// parseTolerant runs the parser collecting every error instead of failing.
func parseTolerant(query string) (*Query, []string) {
if len(query) > MaxInputSize {
return &Query{}, []string{ErrInputTooLarge.Error()}
}
lex := newLexer(query)
p := &parser{toks: lex.tokens, errs: lex.errs}
if p.cur().Type == TokenEOF {
return &Query{}, []string{ErrEmptyQuery.Error()}
}
q := p.parseStatement()
return q, p.errs
}
type parser struct {
toks []Token
pos int
errs []string
depth int
tooDeep bool
}
func (p *parser) cur() Token {
// pos is maintained in [0, len(toks)-1]: next() never advances past the
// final EOF token, so this index is always valid.
return p.toks[p.pos]
}
func (p *parser) peekType(offset int) TokenType {
i := p.pos + offset
if i < len(p.toks) {
return p.toks[i].Type
}
return TokenEOF
}
func (p *parser) next() Token {
t := p.cur()
if p.pos < len(p.toks)-1 {
p.pos++
}
return t
}
func (p *parser) accept(typ TokenType) bool {
if p.cur().Type == typ {
p.next()
return true
}
return false
}
func (p *parser) expect(typ TokenType) (Token, bool) {
if p.cur().Type == typ {
return p.next(), true
}
p.errorAt(p.cur(), "expected %s, found %s", typ, describeToken(p.cur()))
return p.cur(), false
}
func (p *parser) errorAt(t Token, format string, args ...any) {
p.errs = append(p.errs, fmt.Sprintf("line %d:%d: %s", t.Line, t.Col, fmt.Sprintf(format, args...)))
}
func describeToken(t Token) string {
switch t.Type {
case TokenEOF:
return "end of query"
case TokenIdent, TokenNumber:
return fmt.Sprintf("%q", t.Text)
case TokenString:
return "string"
default:
return fmt.Sprintf("%q", t.Type.String())
}
}
// ---------------------------------------------------------------------------
// Statements
// ---------------------------------------------------------------------------
func (p *parser) parseStatement() *Query {
q := &Query{}
switch p.cur().Type {
case TokenSequence:
q.Body = p.parseSequence(KindSequence)
case TokenJoin:
q.Body = p.parseSequence(KindJoin)
case TokenSample:
q.Body = p.parseSequence(KindSample)
default:
q.Body = p.parseEventQuery(false)
}
q.Pipes = p.parsePipes()
if p.tooDeep {
// Deep-nesting bailout: discard the remainder.
p.pos = len(p.toks) - 1
}
if p.cur().Type != TokenEOF {
p.errorAt(p.cur(), "unexpected trailing input starting at %s", describeToken(p.cur()))
}
return q
}
// parseEventQuery parses `category where expr`, `any where expr`, or (when
// tolerated) a bare boolean expression. inBrackets marks sequence-term and
// lineage contexts, where a category is normally mandatory.
func (p *parser) parseEventQuery(inBrackets bool) *EventQuery {
e := &EventQuery{}
// Category form requires `where` as the second token.
switch p.cur().Type {
case TokenAny:
if p.peekType(1) == TokenWhere {
p.next()
p.next()
e.CategoryAny = true
e.Where = p.parseExpression()
return e
}
case TokenIdent, TokenString, TokenBacktickIdent:
if p.peekType(1) == TokenWhere {
t := p.next()
p.next() // where
if t.Type == TokenIdent {
e.Category = t.Text
} else {
e.Category = t.Value
}
e.Where = p.parseExpression()
return e
}
case TokenWhere:
// `where expr` with a missing category: tolerate.
p.errorAt(p.cur(), "missing event category before 'where'")
p.next()
e.CategoryAny = true
e.Where = p.parseExpression()
return e
}
// Bare expression form (extension for stored rule fragments).
if inBrackets {
p.errorAt(p.cur(), "expected 'category where condition' inside brackets")
}
e.Bare = true
e.Where = p.parseExpression()
return e
}
// parseSequence parses sequence/join/sample constructs, which share a shape.
func (p *parser) parseSequence(kind SequenceKind) *Sequence {
s := &Sequence{Kind: kind}
p.next() // sequence/join/sample keyword
// Header: `by ... [with maxspan=...]` or `with maxspan=... [by ...]`.
if p.cur().Type == TokenBy {
s.By = p.parseByKeys()
if p.cur().Type == TokenWith {
s.MaxSpan = p.parseWithMaxspan(kind)
}
} else if p.cur().Type == TokenWith {
s.MaxSpan = p.parseWithMaxspan(kind)
if p.cur().Type == TokenBy {
s.WithByOrder = true
s.By = p.parseByKeys()
}
}
// Terms.
for {
if p.tooDeep {
return s
}
t := p.cur().Type
if t != TokenLBrack && t != TokenMissing {
break
}
// cur is '[' or '![', so parseSequenceStep always returns a step here.
s.Steps = append(s.Steps, p.parseSequenceStep(kind))
}
if p.accept(TokenUntil) {
if kind == KindSample {
p.errorAt(p.cur(), "sample does not support until")
}
s.Until = p.parseSequenceStep(kind)
}
p.validateSequence(s)
return s
}
func (p *parser) validateSequence(s *Sequence) {
positive := 0
missing := 0
for _, st := range s.Steps {
if st.Missing {
missing++
} else {
positive++
}
}
switch s.Kind {
case KindSequence:
// A single step repeated with `with runs=N` (N>=2) is a valid
// multi-event sequence — the term matches N consecutive times.
singleWithRuns := len(s.Steps) == 1 && s.Steps[0].Runs >= 2
if len(s.Steps) < 2 && !singleWithRuns {
p.errs = append(p.errs, "sequence requires at least two events")
}
if missing > 0 {
if positive == 0 {
p.errs = append(p.errs, "sequence with missing events requires at least one positive event")
}
if s.MaxSpan == "" {
p.errs = append(p.errs, "sequence with missing events requires 'with maxspan'")
}
}
case KindJoin:
if len(s.Steps) < 2 {
p.errs = append(p.errs, "join requires at least two events")
}
case KindSample:
if len(s.Steps) < 2 {
p.errs = append(p.errs, "sample requires at least two events")
}
if len(s.By) == 0 {
hasStepBy := false
for _, st := range s.Steps {
if len(st.By) > 0 {
hasStepBy = true
break
}
}
if !hasStepBy {
p.errs = append(p.errs, "sample requires join keys ('by')")
}
}
}
// Join-key arity must be consistent across steps.
if len(s.Steps) > 1 {
first := len(s.By) + len(s.Steps[0].By)
for _, st := range s.Steps[1:] {
if len(s.By)+len(st.By) != first {
p.errs = append(p.errs, fmt.Sprintf("inconsistent join key count across %s events", s.Kind))
break
}
}
}
}
func (p *parser) parseSequenceStep(kind SequenceKind) *SequenceStep {
st := &SequenceStep{}
switch p.cur().Type {
case TokenMissing:
st.Missing = true
if kind != KindSequence {
p.errorAt(p.cur(), "missing events (![...]) are only supported in sequence")
}
p.next()
case TokenLBrack:
p.next()
default:
p.errorAt(p.cur(), "expected '[' to open a %s event", kind)
return nil
}
st.Query = p.parseEventQuery(true)
if _, ok := p.expect(TokenRBrack); !ok {
// Recovery: skip to the next plausible boundary.
p.skipTo(TokenRBrack, TokenLBrack, TokenMissing, TokenUntil, TokenPipe)
p.accept(TokenRBrack)
}
if p.cur().Type == TokenBy {
st.By = p.parseByKeys()
}
// `with runs=N` (sequence only; the grammar allows any key, ES accepts
// only "runs").
if p.cur().Type == TokenWith && p.peekType(1) != TokenMaxspan {
p.next()
keyTok := p.cur()
if keyTok.Type != TokenIdent {
p.errorAt(keyTok, "expected modifier name after 'with'")
return st
}
p.next()
st.WithKey = keyTok.Value
if !strings.EqualFold(keyTok.Value, "runs") {
p.errorAt(keyTok, "unknown event modifier %q (expected 'runs')", keyTok.Value)
}
if _, ok := p.expect(TokenAssign); !ok {
return st
}
numTok, ok := p.expect(TokenNumber)
if !ok {
return st
}
n, err := strconv.Atoi(numTok.Text)
if err != nil {
p.errorAt(numTok, "runs value must be an integer")
return st
}
st.Runs = n
if strings.EqualFold(keyTok.Value, "runs") && (n < 1 || n > 100) {
p.errorAt(numTok, "runs value must be between 1 and 100")
}
if st.Missing {
p.errorAt(keyTok, "missing events do not support 'with runs'")
}
}
return st
}
func (p *parser) parseByKeys() []Expr {
p.next() // by
var keys []Expr
for {
if p.tooDeep {
return keys
}
keys = append(keys, p.parseExpression())
if !p.accept(TokenComma) {
break
}
}
return keys
}
func (p *parser) parseWithMaxspan(kind SequenceKind) string {
p.next() // with
if kind != KindSequence {
p.errorAt(p.cur(), "%s does not support maxspan", kind)
}
if _, ok := p.expect(TokenMaxspan); !ok {
p.skipTo(TokenLBrack, TokenMissing, TokenBy, TokenPipe)
return ""
}
if _, ok := p.expect(TokenAssign); !ok {
return ""
}
numTok, ok := p.expect(TokenNumber)
if !ok {
return ""
}
span := numTok.Text
// Optional time unit written as a trailing identifier (30s lexes as
// number 30 + identifier s).
if p.cur().Type == TokenIdent && isTimeUnit(p.cur().Value) {
span += p.next().Value
} else if p.cur().Type == TokenIdent {
p.errorAt(p.cur(), "unknown time unit %q", p.cur().Value)
p.next()
} else {
p.errorAt(numTok, "maxspan duration requires a time unit (e.g. 30s)")
}
return span
}
func isTimeUnit(s string) bool {
switch strings.ToLower(s) {
case "ms", "s", "m", "h", "d", "micros", "nanos",
"sec", "secs", "second", "seconds",
"min", "mins", "minute", "minutes",
"hour", "hours", "day", "days":
return true
}
return false
}
// MaxSpanDuration converts a maxspan value like "30s" or "2h" to
// milliseconds. Returns false when the text is not a recognized duration.
func MaxSpanDuration(span string) (int64, bool) {
if span == "" {
return 0, false
}
i := 0
for i < len(span) && (span[i] >= '0' && span[i] <= '9' || span[i] == '.') {
i++
}
num, err := strconv.ParseFloat(span[:i], 64)
if err != nil {
return 0, false
}
var mult float64
switch strings.ToLower(span[i:]) {
case "ms":
mult = 1
case "micros":
mult = 0.001
case "nanos":
mult = 0.000001
case "s", "sec", "secs", "second", "seconds", "":
mult = 1000
case "m", "min", "mins", "minute", "minutes":
mult = 60 * 1000
case "h", "hour", "hours":
mult = 60 * 60 * 1000
case "d", "day", "days":
mult = 24 * 60 * 60 * 1000
default:
return 0, false
}
return int64(num * mult), true
}
func (p *parser) parsePipes() []*Pipe {
var pipes []*Pipe
for p.accept(TokenPipe) {
if p.tooDeep {
return pipes
}
nameTok := p.cur()
if nameTok.Type != TokenIdent {
p.errorAt(nameTok, "expected pipe name after '|'")
p.skipTo(TokenPipe)
continue
}
p.next()
pipe := &Pipe{Name: strings.ToLower(nameTok.Value)}
for p.cur().Type != TokenPipe && p.cur().Type != TokenEOF && !p.tooDeep {
pipe.Args = append(pipe.Args, p.parseExpression())
if p.accept(TokenComma) {
continue
}
// Legacy Endgame field-list pipes accept space-separated fields
// (e.g. `unique pid destination_port`), not just comma-separated.
if fieldListPipes[pipe.Name] && isFieldStart(p.cur().Type) {
continue
}
break
}
p.validatePipe(nameTok, pipe)
pipes = append(pipes, pipe)
}
return pipes
}
// fieldListPipes are pipes whose arguments are a list of fields, which legacy
// EQL content may separate with spaces rather than commas.
var fieldListPipes = map[string]bool{
"unique": true, "unique_count": true, "sort": true, "count": true,
}
// isFieldStart reports whether a token can begin a field reference.
func isFieldStart(t TokenType) bool {
return t == TokenIdent || t == TokenBacktickIdent || t == TokenOptional
}
func (p *parser) validatePipe(nameTok Token, pipe *Pipe) {
switch pipe.Name {
case "head", "tail":
if len(pipe.Args) != 1 {
p.errorAt(nameTok, "pipe %q expects exactly one argument", pipe.Name)
return
}
lit, ok := pipe.Args[0].(*Literal)
if !ok || lit.Kind != LitNumber || strings.ContainsAny(lit.Text, ".eE") {
p.errorAt(nameTok, "pipe %q expects an integer argument", pipe.Name)
}
case "count":
// Endgame allowed `count field...`; zero or more args.
case "unique", "unique_count", "sort":
if len(pipe.Args) == 0 {
p.errorAt(nameTok, "pipe %q expects at least one field", pipe.Name)
}
case "filter":
if len(pipe.Args) != 1 {
p.errorAt(nameTok, "pipe %q expects exactly one expression", pipe.Name)
}
default:
p.errorAt(nameTok, "unknown pipe %q", pipe.Name)
}
}
// skipTo advances until one of the given token types (or EOF) is current.
func (p *parser) skipTo(types ...TokenType) {
for p.cur().Type != TokenEOF {
for _, t := range types {
if p.cur().Type == t {
return
}
}
p.next()
}
}
// ---------------------------------------------------------------------------
// Expressions
// ---------------------------------------------------------------------------
func (p *parser) enter() bool {
p.depth++
if p.depth > maxExprDepth {
if !p.tooDeep {
p.tooDeep = true
p.errs = append(p.errs, "expression nesting too deep")
}
return false
}
return true
}
func (p *parser) leave() { p.depth-- }
func (p *parser) parseExpression() Expr {
if !p.enter() {
p.depth--
return &BadExpr{}
}
defer p.leave()
return p.parseOr()
}
func (p *parser) parseOr() Expr {
left := p.parseAnd()
for p.cur().Type == TokenOr && !p.tooDeep {
p.next()
right := p.parseAnd()
left = &Binary{Op: "or", L: left, R: right}
}
return left
}
func (p *parser) parseAnd() Expr {
left := p.parseNot()
for p.cur().Type == TokenAnd && !p.tooDeep {
p.next()
right := p.parseNot()
left = &Binary{Op: "and", L: left, R: right}
}
return left
}
func (p *parser) parseNot() Expr {
// `not in` at this position belongs to a predicate, not a logical not;
// that case is handled inside parsePredicated, so only a `not` NOT
// followed by `in`/`in~` is a logical negation here.
if p.cur().Type == TokenNot && p.peekType(1) != TokenIn && p.peekType(1) != TokenInTilde {
if !p.enter() {
p.depth--
return &BadExpr{}
}
defer p.leave()
p.next()
return &Not{X: p.parseNot()}
}
return p.parsePredicated()
}
func (p *parser) parsePredicated() Expr {
v := p.parseComparison()
switch p.cur().Type {
case TokenNot:
if p.peekType(1) == TokenIn || p.peekType(1) == TokenInTilde {
p.next()
insensitive := p.cur().Type == TokenInTilde
p.next()
list := p.parseParenList()
return &InExpr{X: v, List: list, Negated: true, Insensitive: insensitive}
}
case TokenIn, TokenInTilde:
insensitive := p.cur().Type == TokenInTilde
p.next()
list := p.parseParenList()
return &InExpr{X: v, List: list, Insensitive: insensitive}
case TokenColon:
p.next()
return p.parsePatternRHS(PatternSeq, false, v)
case TokenLike, TokenLikeTilde:
insensitive := p.cur().Type == TokenLikeTilde
p.next()
return p.parsePatternRHS(PatternLike, insensitive, v)
case TokenRegex, TokenRegexTilde:
insensitive := p.cur().Type == TokenRegexTilde
p.next()
return p.parsePatternRHS(PatternRegex, insensitive, v)
}
return v
}
func (p *parser) parsePatternRHS(kind PatternKind, insensitive bool, x Expr) Expr {
pe := &PatternExpr{Kind: kind, X: x, Insensitive: insensitive}
if p.cur().Type == TokenLParen {
p.next()
pe.Parenthesized = true
for p.cur().Type != TokenRParen && p.cur().Type != TokenEOF && !p.tooDeep {
pe.Patterns = append(pe.Patterns, p.parsePatternConstant(kind))
if !p.accept(TokenComma) {
break
}
}
if _, ok := p.expect(TokenRParen); !ok {
p.skipTo(TokenRParen, TokenPipe, TokenAnd, TokenOr, TokenRBrack)
p.accept(TokenRParen)
}
if len(pe.Patterns) == 0 {
p.errs = append(p.errs, fmt.Sprintf("empty pattern list for %q", string(kind)))
}
return pe
}
pe.Patterns = []Expr{p.parsePatternConstant(kind)}
return pe
}
// parsePatternConstant parses the RHS of :, like, regex — a constant per the
// grammar. Non-constants are parsed anyway (tolerance) with an error note.
func (p *parser) parsePatternConstant(kind PatternKind) Expr {
t := p.cur()
switch t.Type {
case TokenString, TokenNumber, TokenTrue, TokenFalse, TokenNull:
return p.parsePrimary()
default:
e := p.parseComparison()
p.errorAt(t, "%q expects a literal value", string(kind))
return e
}
}
func (p *parser) parseParenList() []Expr {
var list []Expr
if _, ok := p.expect(TokenLParen); !ok {
return list
}
for p.cur().Type != TokenRParen && p.cur().Type != TokenEOF && !p.tooDeep {
list = append(list, p.parseExpression())
if !p.accept(TokenComma) {
break
}
}
if _, ok := p.expect(TokenRParen); !ok {
p.skipTo(TokenRParen, TokenPipe, TokenRBrack)
p.accept(TokenRParen)
}
if len(list) == 0 {
p.errs = append(p.errs, "empty list in 'in' expression")
}
return list
}
func (p *parser) parseComparison() Expr {
left := p.parseAdditive()
op := ""
switch p.cur().Type {
case TokenEQ:
op = "=="
case TokenNEQ:
op = "!="
case TokenLT:
op = "<"
case TokenLTE:
op = "<="
case TokenGT:
op = ">"
case TokenGTE:
op = ">="
case TokenAssign:
// Legacy Endgame equality; modern EQL rejects bare `=`.
op = "=="
default:
return left
}
p.next()
right := p.parseAdditive()
cmp := &Binary{Op: op, L: left, R: right}
// Comparison chaining (a < b <= c) is invalid EQL; note it but keep
// parsing so extraction sees both comparisons.
switch p.cur().Type {
case TokenEQ, TokenNEQ, TokenLT, TokenLTE, TokenGT, TokenGTE:
p.errorAt(p.cur(), "comparison chaining is not supported; use 'and'")
opTok := p.next()
right2 := p.parseAdditive()
return &Binary{Op: "and", L: cmp, R: &Binary{Op: opTok.Value, L: right, R: right2}}
}
return cmp
}
func (p *parser) parseAdditive() Expr {
left := p.parseMultiplicative()
for !p.tooDeep {
var op string
switch p.cur().Type {
case TokenPlus:
op = "+"
case TokenMinus:
op = "-"
default:
return left
}
p.next()
right := p.parseMultiplicative()
left = &Binary{Op: op, L: left, R: right}
}
return left
}
func (p *parser) parseMultiplicative() Expr {
left := p.parseUnary()
for !p.tooDeep {
var op string
switch p.cur().Type {
case TokenStar:
op = "*"
case TokenSlash:
op = "/"
case TokenPercent:
op = "%"
default:
return left
}
p.next()
right := p.parseUnary()
left = &Binary{Op: op, L: left, R: right}
}
return left
}
func (p *parser) parseUnary() Expr {
switch p.cur().Type {
case TokenMinus:
if !p.enter() {
p.depth--
return &BadExpr{}
}
defer p.leave()
p.next()
return &Unary{Op: "-", X: p.parseUnary()}
case TokenPlus:
if !p.enter() {
p.depth--
return &BadExpr{}
}
defer p.leave()
p.next()
return &Unary{Op: "+", X: p.parseUnary()}
}
return p.parsePrimary()
}
func (p *parser) parsePrimary() Expr {
if p.tooDeep {
return &BadExpr{}
}
t := p.cur()
switch t.Type {
case TokenLParen:
if !p.enter() {
p.depth--
p.next()
return &BadExpr{}
}
defer p.leave()
p.next()
inner := p.parseExpression()
if _, ok := p.expect(TokenRParen); !ok {
p.skipTo(TokenRParen, TokenPipe, TokenRBrack)
p.accept(TokenRParen)
}
return &Paren{X: inner}
case TokenString:
p.next()
return &Literal{Kind: LitString, Value: t.Value, Raw: t.Raw}
case TokenNumber:
p.next()
return &Literal{Kind: LitNumber, Text: t.Text}
case TokenTrue, TokenFalse:
p.next()
return &Literal{Kind: LitBool, Bool: t.Type == TokenTrue}
case TokenNull:
p.next()
return &Literal{Kind: LitNull}
case TokenOptional:
p.next()
if p.cur().Type != TokenIdent && p.cur().Type != TokenBacktickIdent {
p.errorAt(p.cur(), "expected field name after '?'")
return &BadExpr{Near: "?"}
}
return p.parseField(true)
case TokenBacktickIdent:
return p.parseField(false)
case TokenIdent:
// Legacy lineage predicates: child of [...], descendant of [...],
// event of [...].
lower := strings.ToLower(t.Value)
if (lower == "child" || lower == "descendant" || lower == "event") && p.peekType(1) == TokenOf {
return p.parseLineage(lower)
}
// Function call only when '(' directly follows the identifier.
if p.peekType(1) == TokenLParen {
return p.parseCall()
}
if t.Tilde {
p.errorAt(t, "'~' is only valid on function names")
}
return p.parseField(false)
case TokenAny:
// `any` as a value only appears in malformed queries; recover as a
// field named any.
p.next()
p.errorAt(t, "'any' is a reserved keyword")
return &Field{Path: []PathSeg{{Name: "any"}}}
default:
p.errorAt(t, "unexpected %s in expression", describeToken(t))
p.next()
return &BadExpr{Near: t.Text}
}
}
func (p *parser) parseLineage(kind string) Expr {
if !p.enter() {
p.depth--
return &BadExpr{}
}
defer p.leave()
p.next() // child/descendant/event
p.next() // of
if _, ok := p.expect(TokenLBrack); !ok {
return &BadExpr{Near: kind + " of"}
}
sub := p.parseEventQuery(true)
if _, ok := p.expect(TokenRBrack); !ok {
p.skipTo(TokenRBrack, TokenPipe)
p.accept(TokenRBrack)
}
return &Lineage{Kind: kind, Sub: sub}
}
func (p *parser) parseCall() Expr {
if !p.enter() {
p.depth--
return &BadExpr{}
}
defer p.leave()
nameTok := p.next() // identifier
p.next() // (
call := &Call{Name: nameTok.Value, Insensitive: nameTok.Tilde}
for p.cur().Type != TokenRParen && p.cur().Type != TokenEOF && !p.tooDeep {
call.Args = append(call.Args, p.parseExpression())
if !p.accept(TokenComma) {
break
}
}
if _, ok := p.expect(TokenRParen); !ok {
p.skipTo(TokenRParen, TokenPipe, TokenRBrack)
p.accept(TokenRParen)
}
return call
}
// parseField parses a dotted, optionally indexed field path. The current
// token must be an identifier or backtick identifier.
func (p *parser) parseField(optional bool) Expr {
f := &Field{Optional: optional}
first := p.next()
if first.Type == TokenBacktickIdent {
f.Path = append(f.Path, PathSeg{Name: first.Value})
} else {
f.Path = append(f.Path, PathSeg{Name: first.Text})
}
for {
switch {
case p.cur().Type == TokenDot:
nt := p.peekType(1)
if nt != TokenIdent && nt != TokenBacktickIdent && !p.toks[minInt(p.pos+1, len(p.toks)-1)].isKeyword() {
p.errorAt(p.cur(), "expected field name after '.'")
p.next()
return f
}
p.next()
seg := p.next()
name := seg.Text
if seg.Type == TokenBacktickIdent {
name = seg.Value
}
f.Path = append(f.Path, PathSeg{Name: name})
case p.cur().Type == TokenLBrack && p.peekType(1) == TokenNumber && p.peekType(2) == TokenRBrack:
p.next()
numTok := p.next()
p.next()
idx, err := strconv.Atoi(numTok.Text)
if err != nil {
p.errorAt(numTok, "array index must be an integer")
idx = 0
}
f.Path = append(f.Path, PathSeg{Index: idx, IsIndex: true})
default:
return f
}
}
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}