-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.go
More file actions
509 lines (459 loc) · 10.7 KB
/
Copy pathast.go
File metadata and controls
509 lines (459 loc) · 10.7 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
package eql
import (
"strconv"
"strings"
)
// Query is a parsed EQL statement: one body (event query, sequence, join, or
// sample) followed by zero or more pipes.
type Query struct {
Body QueryBody
Pipes []*Pipe
}
// QueryBody is implemented by *EventQuery and *Sequence.
type QueryBody interface {
render(b *strings.Builder)
}
// EventQuery is `category where condition`, `any where condition`, or — as a
// tolerated extension for extraction of stored rule fragments — a bare
// boolean expression without a category (Bare=true).
type EventQuery struct {
Category string // literal category text ("" when Any or Bare)
CategoryAny bool // `any where ...`
Bare bool // no `category where` prefix, just an expression
Where Expr
}
// SequenceKind discriminates the three correlation constructs that share the
// Sequence node shape.
type SequenceKind string
const (
KindSequence SequenceKind = "sequence"
KindJoin SequenceKind = "join"
KindSample SequenceKind = "sample"
)
// Sequence is `sequence ...`, `join ...`, or `sample ...`.
type Sequence struct {
Kind SequenceKind
By []Expr // global join keys (sequence/join/sample `by`)
MaxSpan string // raw duration text, e.g. "30s" ("" when absent)
WithByOrder bool // header was written `with maxspan=... by ...` (parsed-then-tolerated order)
Steps []*SequenceStep
Until *SequenceStep
}
// SequenceStep is one bracketed term of a sequence/join/sample.
type SequenceStep struct {
Missing bool // opened with ![ — matches the absence of the event
Query *EventQuery
By []Expr // per-step join keys
Runs int // `with runs=N`, 0 when absent
WithKey string // raw key of a `with key=value` modifier ("runs" normally)
}
// Pipe is `| name arg1, arg2`.
type Pipe struct {
Name string
Args []Expr
}
// Expr is the interface implemented by all expression nodes.
type Expr interface {
render(b *strings.Builder)
}
// Binary covers logical (and/or), comparison (== != < <= > >= : treated
// separately), and arithmetic (+ - * / %) operators. Op is the canonical
// lowercase operator text.
type Binary struct {
Op string
L, R Expr
}
// Not is prefix `not expr`.
type Not struct {
X Expr
}
// Paren preserves explicit grouping from the source.
type Paren struct {
X Expr
}
// Unary is prefix arithmetic minus/plus.
type Unary struct {
Op string
X Expr
}
// InExpr is `x [not] in[~] (a, b, c)`.
type InExpr struct {
X Expr
List []Expr
Negated bool
Insensitive bool
}
// PatternKind distinguishes the string-matching predicates.
type PatternKind string
const (
PatternSeq PatternKind = ":" // case-insensitive equality with wildcards
PatternLike PatternKind = "like" // wildcard match
PatternRegex PatternKind = "regex" // regular expression match
)
// PatternExpr is `x : v`, `x like v`, `x regex v`, or their list forms
// `x like ("a*", "b*")`. Insensitive marks the ~ variants (":" is inherently
// case-insensitive and keeps Insensitive=false).
type PatternExpr struct {
Kind PatternKind
X Expr
Patterns []Expr
Parenthesized bool // list form was written with parentheses
Insensitive bool
}
// Call is a function call. Insensitive marks the ~ suffix (endsWith~).
type Call struct {
Name string
Insensitive bool
Args []Expr
}
// PathSeg is one segment of a dotted field path: a name or an array index.
type PathSeg struct {
Name string
Index int
IsIndex bool
}
// Field is a (possibly optional, possibly indexed) field reference such as
// `?process.args[0]` or “ `host-name`.ip “.
type Field struct {
Optional bool
Path []PathSeg
}
// Name returns the canonical dotted path without the optional marker,
// e.g. "process.args[0]".
func (f *Field) Name() string {
var b strings.Builder
for i, s := range f.Path {
if s.IsIndex {
b.WriteByte('[')
b.WriteString(strconv.Itoa(s.Index))
b.WriteByte(']')
continue
}
if i > 0 {
b.WriteByte('.')
}
b.WriteString(s.Name)
}
return b.String()
}
// LiteralKind discriminates literal values.
type LiteralKind int
const (
LitString LiteralKind = iota
LitNumber
LitBool
LitNull
)
// Literal is a constant value. For strings, Value holds the decoded text;
// for numbers, Text holds the original notation.
type Literal struct {
Kind LiteralKind
Value string // decoded string value
Text string // raw number text
Bool bool
Raw bool // originated from a raw string form
}
// Lineage is the legacy Endgame relationship predicate:
// `child of [process where ...]`, `descendant of [...]`, `event of [...]`.
type Lineage struct {
Kind string // "child", "descendant", "event"
Sub *EventQuery
}
// BadExpr is a placeholder emitted during error recovery.
type BadExpr struct {
Near string
}
// ---------------------------------------------------------------------------
// Rendering. String() output is canonical EQL that re-parses to the same AST
// (idempotent under parse∘render), which the round-trip tests rely on.
// ---------------------------------------------------------------------------
// String renders the query as canonical single-line EQL.
func (q *Query) String() string {
var b strings.Builder
if q.Body != nil {
q.Body.render(&b)
}
for _, p := range q.Pipes {
b.WriteString(" | ")
b.WriteString(p.Name)
for i, a := range p.Args {
if i == 0 {
b.WriteByte(' ')
} else {
b.WriteString(", ")
}
a.render(&b)
}
}
return b.String()
}
func (e *EventQuery) render(b *strings.Builder) {
if e.Bare {
if e.Where != nil {
e.Where.render(b)
}
return
}
if e.CategoryAny {
b.WriteString("any")
} else {
renderCategory(b, e.Category)
}
b.WriteString(" where ")
if e.Where != nil {
e.Where.render(b)
}
}
func renderCategory(b *strings.Builder, cat string) {
if isPlainIdent(cat) && keywords[strings.ToLower(cat)] == 0 {
b.WriteString(cat)
return
}
b.WriteString(quoteString(cat))
}
func (s *Sequence) render(b *strings.Builder) {
b.WriteString(string(s.Kind))
if len(s.By) > 0 {
b.WriteString(" by ")
renderExprList(b, s.By)
}
if s.MaxSpan != "" {
b.WriteString(" with maxspan=")
b.WriteString(s.MaxSpan)
}
for _, st := range s.Steps {
b.WriteByte(' ')
st.render(b)
}
if s.Until != nil {
b.WriteString(" until ")
s.Until.render(b)
}
}
func (s *SequenceStep) render(b *strings.Builder) {
if s.Missing {
b.WriteString("![")
} else {
b.WriteByte('[')
}
s.Query.render(b)
b.WriteByte(']')
if len(s.By) > 0 {
b.WriteString(" by ")
renderExprList(b, s.By)
}
if s.WithKey != "" {
b.WriteString(" with ")
b.WriteString(s.WithKey)
b.WriteByte('=')
b.WriteString(strconv.Itoa(s.Runs))
}
}
func renderExprList(b *strings.Builder, list []Expr) {
for i, e := range list {
if i > 0 {
b.WriteString(", ")
}
e.render(b)
}
}
// precedence returns the binding strength of an expression node for
// parenthesization during rendering; higher binds tighter.
func precedence(e Expr) int {
switch v := e.(type) {
case *Binary:
switch v.Op {
case "or":
return 1
case "and":
return 2
case "==", "!=", "<", "<=", ">", ">=":
return 5
case "+", "-":
return 6
default: // * / %
return 7
}
case *Not:
return 3
case *InExpr, *PatternExpr:
return 4
case *Unary:
return 8
default:
return 9
}
}
func renderChild(b *strings.Builder, child Expr, parentPrec int, rightSide bool) {
p := precedence(child)
need := p < parentPrec || (rightSide && p == parentPrec)
if need {
b.WriteByte('(')
}
child.render(b)
if need {
b.WriteByte(')')
}
}
func (e *Binary) render(b *strings.Builder) {
p := precedence(e)
renderChild(b, e.L, p, false)
b.WriteByte(' ')
b.WriteString(e.Op)
b.WriteByte(' ')
renderChild(b, e.R, p, true)
}
func (e *Not) render(b *strings.Builder) {
b.WriteString("not ")
renderChild(b, e.X, precedence(e), false)
}
func (e *Paren) render(b *strings.Builder) {
b.WriteByte('(')
e.X.render(b)
b.WriteByte(')')
}
func (e *Unary) render(b *strings.Builder) {
b.WriteString(e.Op)
renderChild(b, e.X, precedence(e), false)
}
func (e *InExpr) render(b *strings.Builder) {
renderChild(b, e.X, 4, false)
if e.Negated {
b.WriteString(" not")
}
b.WriteString(" in")
if e.Insensitive {
b.WriteByte('~')
}
b.WriteString(" (")
renderExprList(b, e.List)
b.WriteByte(')')
}
func (e *PatternExpr) render(b *strings.Builder) {
renderChild(b, e.X, 4, false)
b.WriteByte(' ')
b.WriteString(string(e.Kind))
if e.Insensitive {
b.WriteByte('~')
}
b.WriteByte(' ')
if len(e.Patterns) == 1 && !e.Parenthesized {
e.Patterns[0].render(b)
return
}
b.WriteByte('(')
renderExprList(b, e.Patterns)
b.WriteByte(')')
}
func (e *Call) render(b *strings.Builder) {
b.WriteString(e.Name)
if e.Insensitive {
b.WriteByte('~')
}
b.WriteByte('(')
renderExprList(b, e.Args)
b.WriteByte(')')
}
func (e *Field) render(b *strings.Builder) {
if e.Optional {
b.WriteByte('?')
}
for i, s := range e.Path {
if s.IsIndex {
b.WriteByte('[')
b.WriteString(strconv.Itoa(s.Index))
b.WriteByte(']')
continue
}
if i > 0 {
b.WriteByte('.')
}
if isPlainIdent(s.Name) {
b.WriteString(s.Name)
} else {
b.WriteByte('`')
b.WriteString(strings.ReplaceAll(s.Name, "`", "``"))
b.WriteByte('`')
}
}
}
func (e *Literal) render(b *strings.Builder) {
switch e.Kind {
case LitString:
b.WriteString(quoteString(e.Value))
case LitNumber:
b.WriteString(e.Text)
case LitBool:
if e.Bool {
b.WriteString("true")
} else {
b.WriteString("false")
}
case LitNull:
b.WriteString("null")
}
}
func (e *Lineage) render(b *strings.Builder) {
b.WriteString(e.Kind)
b.WriteString(" of [")
e.Sub.render(b)
b.WriteByte(']')
}
func (e *BadExpr) render(b *strings.Builder) {
b.WriteString("true /* unparsed */")
}
// quoteString renders a decoded string value as a canonical double-quoted
// EQL string literal.
func quoteString(s string) string {
var b strings.Builder
b.WriteByte('"')
for _, r := range s {
switch r {
case '"':
b.WriteString(`\"`)
case '\\':
b.WriteString(`\\`)
case '\n':
b.WriteString(`\n`)
case '\r':
b.WriteString(`\r`)
case '\t':
b.WriteString(`\t`)
default:
if r < 0x20 {
b.WriteString(`\u{` + strconv.FormatInt(int64(r), 16) + `}`)
} else {
b.WriteRune(r)
}
}
}
b.WriteByte('"')
return b.String()
}
// isPlainIdent reports whether s can appear unquoted as an identifier.
func isPlainIdent(s string) bool {
if s == "" {
return false
}
for i, r := range s {
switch {
case r == '_' || (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z'):
case r >= '0' && r <= '9':
if i == 0 {
return false
}
default:
return false
}
}
return true
}
// ExprString renders any expression node as canonical EQL text.
func ExprString(e Expr) string {
if e == nil {
return ""
}
var b strings.Builder
e.render(&b)
return b.String()
}