forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.go
452 lines (392 loc) · 10.5 KB
/
grammar.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ottl // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
import (
"encoding/hex"
"fmt"
"github.com/alecthomas/participle/v2/lexer"
)
// parsedStatement represents a parsed statement. It is the entry point into the statement DSL.
type parsedStatement struct {
Editor editor `parser:"(@@"`
// If converter is matched then return error
Converter *converter `parser:"|@@)"`
WhereClause *booleanExpression `parser:"( 'where' @@ )?"`
}
func (p *parsedStatement) checkForCustomError() error {
if p.Converter != nil {
return fmt.Errorf("editor names must start with a lowercase letter but got '%v'", p.Converter.Function)
}
err := p.Editor.checkForCustomError()
if err != nil {
return err
}
if p.WhereClause != nil {
return p.WhereClause.checkForCustomError()
}
return nil
}
type constExpr struct {
Boolean *boolean `parser:"( @Boolean"`
Converter *converter `parser:"| @@ )"`
}
// booleanValue represents something that evaluates to a boolean --
// either an equality or inequality, explicit true or false, or
// a parenthesized subexpression.
type booleanValue struct {
Negation *string `parser:"@OpNot?"`
Comparison *comparison `parser:"( @@"`
ConstExpr *constExpr `parser:"| @@"`
SubExpr *booleanExpression `parser:"| '(' @@ ')' )"`
}
func (b *booleanValue) checkForCustomError() error {
if b.Comparison != nil {
return b.Comparison.checkForCustomError()
}
if b.SubExpr != nil {
return b.SubExpr.checkForCustomError()
}
return nil
}
// opAndBooleanValue represents the right side of an AND boolean expression.
type opAndBooleanValue struct {
Operator string `parser:"@OpAnd"`
Value *booleanValue `parser:"@@"`
}
func (b *opAndBooleanValue) checkForCustomError() error {
return b.Value.checkForCustomError()
}
// term represents an arbitrary number of boolean values joined by AND.
type term struct {
Left *booleanValue `parser:"@@"`
Right []*opAndBooleanValue `parser:"@@*"`
}
func (b *term) checkForCustomError() error {
err := b.Left.checkForCustomError()
if err != nil {
return err
}
for _, r := range b.Right {
err = r.checkForCustomError()
if err != nil {
return err
}
}
return nil
}
// opOrTerm represents the right side of an OR boolean expression.
type opOrTerm struct {
Operator string `parser:"@OpOr"`
Term *term `parser:"@@"`
}
func (b *opOrTerm) checkForCustomError() error {
return b.Term.checkForCustomError()
}
// booleanExpression represents a true/false decision expressed
// as an arbitrary number of terms separated by OR.
type booleanExpression struct {
Left *term `parser:"@@"`
Right []*opOrTerm `parser:"@@*"`
}
func (b *booleanExpression) checkForCustomError() error {
err := b.Left.checkForCustomError()
if err != nil {
return err
}
for _, r := range b.Right {
err = r.checkForCustomError()
if err != nil {
return err
}
}
return nil
}
// compareOp is the type of a comparison operator.
type compareOp int
// These are the allowed values of a compareOp
const (
eq compareOp = iota
ne
lt
lte
gte
gt
)
// a fast way to get from a string to a compareOp
var compareOpTable = map[string]compareOp{
"==": eq,
"!=": ne,
"<": lt,
"<=": lte,
">": gt,
">=": gte,
}
// Capture is how the parser converts an operator string to a compareOp.
func (c *compareOp) Capture(values []string) error {
op, ok := compareOpTable[values[0]]
if !ok {
return fmt.Errorf("'%s' is not a valid operator", values[0])
}
*c = op
return nil
}
// String() for compareOp gives us more legible test results and error messages.
func (c *compareOp) String() string {
switch *c {
case eq:
return "eq"
case ne:
return "ne"
case lt:
return "lt"
case lte:
return "lte"
case gte:
return "gte"
case gt:
return "gt"
default:
return "UNKNOWN OP!"
}
}
// comparison represents an optional boolean condition.
type comparison struct {
Left value `parser:"@@"`
Op compareOp `parser:"@OpComparison"`
Right value `parser:"@@"`
}
func (c *comparison) checkForCustomError() error {
err := c.Left.checkForCustomError()
if err != nil {
return err
}
err = c.Right.checkForCustomError()
return err
}
// editor represents the function call of a statement.
type editor struct {
Function string `parser:"@(Lowercase(Uppercase | Lowercase)*)"`
Arguments []argument `parser:"'(' ( @@ ( ',' @@ )* )? ')'"`
// If keys are matched return an error
Keys []key `parser:"( @@ )*"`
}
func (i *editor) checkForCustomError() error {
var err error
for _, arg := range i.Arguments {
err = arg.checkForCustomError()
if err != nil {
return err
}
}
if i.Keys != nil {
return fmt.Errorf("only paths and converters may be indexed, not editors, but got %v %v", i.Function, i.Keys)
}
return nil
}
// converter represents a converter function call.
type converter struct {
Function string `parser:"@(Uppercase(Uppercase | Lowercase)*)"`
Arguments []argument `parser:"'(' ( @@ ( ',' @@ )* )? ')'"`
Keys []key `parser:"( @@ )*"`
}
type argument struct {
Name string `parser:"(@(Lowercase(Uppercase | Lowercase)*) Equal)?"`
Value value `parser:"( @@"`
FunctionName *string `parser:"| @(Uppercase(Uppercase | Lowercase)*) )"`
}
func (a *argument) checkForCustomError() error {
return a.Value.checkForCustomError()
}
// value represents a part of a parsed statement which is resolved to a value of some sort. This can be a telemetry path
// mathExpression, function call, or literal.
type value struct {
IsNil *isNil `parser:"( @'nil'"`
Literal *mathExprLiteral `parser:"| @@ (?! OpAddSub | OpMultDiv)"`
MathExpression *mathExpression `parser:"| @@"`
Bytes *byteSlice `parser:"| @Bytes"`
String *string `parser:"| @String"`
Bool *boolean `parser:"| @Boolean"`
Enum *enumSymbol `parser:"| @Uppercase (?! Lowercase)"`
List *list `parser:"| @@)"`
}
func (v *value) checkForCustomError() error {
if v.Literal != nil {
return v.Literal.checkForCustomError()
}
if v.MathExpression != nil {
return v.MathExpression.checkForCustomError()
}
return nil
}
// path represents a telemetry path mathExpression.
type path struct {
Fields []field `parser:"@@ ( '.' @@ )*"`
}
// field is an item within a path.
type field struct {
Name string `parser:"@Lowercase"`
Keys []key `parser:"( @@ )*"`
}
type key struct {
String *string `parser:"'[' (@String "`
Int *int64 `parser:"| @Int) ']'"`
}
type list struct {
Values []value `parser:"'[' (@@)* (',' @@)* ']'"`
}
// byteSlice type for capturing byte slices
type byteSlice []byte
func (b *byteSlice) Capture(values []string) error {
rawStr := values[0][2:]
newBytes, err := hex.DecodeString(rawStr)
if err != nil {
return err
}
*b = newBytes
return nil
}
// boolean Type for capturing booleans, see:
// https://github.com/alecthomas/participle#capturing-boolean-value
type boolean bool
func (b *boolean) Capture(values []string) error {
*b = values[0] == "true"
return nil
}
type isNil bool
func (n *isNil) Capture(_ []string) error {
*n = true
return nil
}
type mathExprLiteral struct {
// If editor is matched then error
Editor *editor `parser:"( @@"`
Converter *converter `parser:"| @@"`
Float *float64 `parser:"| @Float"`
Int *int64 `parser:"| @Int"`
Path *path `parser:"| @@ )"`
}
func (m *mathExprLiteral) checkForCustomError() error {
if m.Editor != nil {
return fmt.Errorf("converter names must start with an uppercase letter but got '%v'", m.Editor.Function)
}
return nil
}
type mathValue struct {
Literal *mathExprLiteral `parser:"( @@"`
SubExpression *mathExpression `parser:"| '(' @@ ')' )"`
}
func (m *mathValue) checkForCustomError() error {
if m.Literal != nil {
return m.Literal.checkForCustomError()
}
return m.SubExpression.checkForCustomError()
}
type opMultDivValue struct {
Operator mathOp `parser:"@OpMultDiv"`
Value *mathValue `parser:"@@"`
}
func (m *opMultDivValue) checkForCustomError() error {
return m.Value.checkForCustomError()
}
type addSubTerm struct {
Left *mathValue `parser:"@@"`
Right []*opMultDivValue `parser:"@@*"`
}
func (m *addSubTerm) checkForCustomError() error {
err := m.Left.checkForCustomError()
if err != nil {
return err
}
for _, r := range m.Right {
err = r.checkForCustomError()
if err != nil {
return err
}
}
return nil
}
type opAddSubTerm struct {
Operator mathOp `parser:"@OpAddSub"`
Term *addSubTerm `parser:"@@"`
}
func (m *opAddSubTerm) checkForCustomError() error {
return m.Term.checkForCustomError()
}
type mathExpression struct {
Left *addSubTerm `parser:"@@"`
Right []*opAddSubTerm `parser:"@@*"`
}
func (m *mathExpression) checkForCustomError() error {
err := m.Left.checkForCustomError()
if err != nil {
return err
}
for _, r := range m.Right {
err = r.checkForCustomError()
if err != nil {
return err
}
}
return nil
}
type mathOp int
const (
add mathOp = iota
sub
mult
div
)
var mathOpTable = map[string]mathOp{
"+": add,
"-": sub,
"*": mult,
"/": div,
}
func (m *mathOp) Capture(values []string) error {
op, ok := mathOpTable[values[0]]
if !ok {
return fmt.Errorf("'%s' is not a valid operator", values[0])
}
*m = op
return nil
}
func (m *mathOp) String() string {
switch *m {
case add:
return "+"
case sub:
return "-"
case mult:
return "*"
case div:
return "/"
default:
return "UNKNOWN OP!"
}
}
type enumSymbol string
// buildLexer constructs a SimpleLexer definition.
// Note that the ordering of these rules matters.
// It's in a separate function so it can be easily tested alone (see lexer_test.go).
func buildLexer() *lexer.StatefulDefinition {
return lexer.MustSimple([]lexer.SimpleRule{
{Name: `Bytes`, Pattern: `0x[a-fA-F0-9]+`},
{Name: `Float`, Pattern: `[-+]?\d*\.\d+([eE][-+]?\d+)?`},
{Name: `Int`, Pattern: `[-+]?\d+`},
{Name: `String`, Pattern: `"(\\.|[^\\"])*"`},
{Name: `OpNot`, Pattern: `\b(not)\b`},
{Name: `OpOr`, Pattern: `\b(or)\b`},
{Name: `OpAnd`, Pattern: `\b(and)\b`},
{Name: `OpComparison`, Pattern: `==|!=|>=|<=|>|<`},
{Name: `OpAddSub`, Pattern: `\+|\-`},
{Name: `OpMultDiv`, Pattern: `\/|\*`},
{Name: `Boolean`, Pattern: `\b(true|false)\b`},
{Name: `Equal`, Pattern: `=`},
{Name: `LParen`, Pattern: `\(`},
{Name: `RParen`, Pattern: `\)`},
{Name: `Punct`, Pattern: `[,.\[\]]`},
{Name: `Uppercase`, Pattern: `[A-Z][A-Z0-9_]*`},
{Name: `Lowercase`, Pattern: `[a-z][a-z0-9_]*`},
{Name: "whitespace", Pattern: `\s+`},
})
}