/
ast.go
119 lines (93 loc) · 3.53 KB
/
ast.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package eval
import (
"github.com/alecthomas/participle/lexer"
)
// Expression represents basic expression syntax that can be evaluated for an Instance
type Expression struct {
Pos lexer.Position
Comparison *Comparison `parser:"@@"`
Op *string `parser:"[ @( \"|\" \"|\" | \"&\" \"&\" )"`
Next *Expression `parser:" @@ ]"`
}
// IterableExpression represents an iterable expration that can be evaluated for an Iterator
type IterableExpression struct {
Pos lexer.Position
IterableComparison *IterableComparison `parser:"@@"`
Expression *Expression `parser:"| @@"`
}
// IterableComparison allows evaluating a builtin pseudo-funciion for an iterable expression
type IterableComparison struct {
Pos lexer.Position
Fn *string `parser:"@( \"count\" | \"all\" | \"none\" )"`
Expression *Expression `parser:"\"(\" @@ \")\""`
ScalarComparison *ScalarComparison `parser:"[ @@ ]"`
}
// PathExpression represents an expression evaluating to a file path or file glob
type PathExpression struct {
Pos lexer.Position
Path *string `parser:"@UnixSystemPath"`
Expression *Expression `parser:"| @@"`
}
// Comparison represents syntax for comparison operations
type Comparison struct {
Pos lexer.Position
Term *Term `parser:"@@"`
ScalarComparison *ScalarComparison `parser:"[ @@"`
ArrayComparison *ArrayComparison `parser:"| @@ ]"`
}
// ScalarComparison represents syntax for scalar comparison
type ScalarComparison struct {
Pos lexer.Position
Op *string `parser:"@( \">\" \"=\" | \"<\" \"=\" | \">\" | \"<\" | \"!\" \"=\" | \"=\" \"=\" | \"=\" \"~\" | \"!\" \"~\" )"`
Next *Comparison `parser:" @@"`
}
// ArrayComparison represents syntax for array comparison
type ArrayComparison struct {
Pos lexer.Position
Op *string `parser:"( @( \"in\" | \"not\" \"in\" )"`
// TODO: likely doesn't work with rhs expression
Array *Array `parser:"@@ )"`
}
// Term is an abstract term allowing optional binary bit operation syntax
type Term struct {
Pos lexer.Position
Unary *Unary `parser:"@@"`
Op *string `parser:"[ @( \"&\" | \"|\" | \"^\" | \"+\" )"`
Next *Term `parser:" @@ ]"`
}
// Unary is a unary bit operation syntax
type Unary struct {
Pos lexer.Position
Op *string `parser:"( @( \"!\" | \"-\" | \"^\" )"`
Unary *Unary `parser:" @@ )"`
Value *Value `parser:"| @@"`
}
// Array provides support for array syntax and may contain any valid Values (mixed allowed)
type Array struct {
Pos lexer.Position
Values []Value `parser:"\"[\" @@ { \",\" @@ } \"]\""`
Ident *string `parser:"| @Ident"`
}
// Value provides support for various value types in expression including
// integers in various form, strings, function calls, variables and
// subexpressions
type Value struct {
Pos lexer.Position
Hex *string `parser:" @Hex"`
Octal *string `parser:"| @Octal"`
Decimal *int64 `parser:"| @Decimal"`
String *string `parser:"| @String"`
Call *Call `parser:"| @@"`
Variable *string `parser:"| @Ident"`
Subexpression *Expression `parser:"| \"(\" @@ \")\""`
}
// Call implements function call syntax
type Call struct {
Pos lexer.Position
Name string `parser:"@Ident"`
Args []*Expression `parser:"\"(\" [ @@ { \",\" @@ } ] \")\""`
}