-
Notifications
You must be signed in to change notification settings - Fork 35
/
ast.go
274 lines (225 loc) · 6.32 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
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
package conditions
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
// DataType represents the primitive data types available in InfluxQL.
type DataType string
const (
Unknown = DataType("")
Number = DataType("number")
Boolean = DataType("boolean")
String = DataType("string")
Time = DataType("time")
Duration = DataType("duration")
)
// InspectDataType returns the data type of a given value.
func InspectDataType(v interface{}) DataType {
switch v.(type) {
case float64:
return Number
case bool:
return Boolean
case string:
return String
case time.Time:
return Time
case time.Duration:
return Duration
default:
return Unknown
}
}
// Node represents a node in the conditions abstract syntax tree.
type Node interface {
node()
String() string
}
func (_ *VarRef) node() {}
func (_ *NumberLiteral) node() {}
func (_ *StringLiteral) node() {}
func (_ *BooleanLiteral) node() {}
func (_ *TimeLiteral) node() {}
func (_ *DurationLiteral) node() {}
func (_ *BinaryExpr) node() {}
func (_ *ParenExpr) node() {}
func (_ *SliceStringLiteral) node() {}
func (_ *SliceNumberLiteral) node() {}
// Expr represents an expression that can be evaluated to a value.
type Expr interface {
Node
expr()
Args() []string
}
func (_ *VarRef) expr() {}
func (_ *NumberLiteral) expr() {}
func (_ *StringLiteral) expr() {}
func (_ *BooleanLiteral) expr() {}
func (_ *TimeLiteral) expr() {}
func (_ *DurationLiteral) expr() {}
func (_ *BinaryExpr) expr() {}
func (_ *ParenExpr) expr() {}
func (_ *SliceStringLiteral) expr() {}
func (_ *SliceNumberLiteral) expr() {}
// VarRef represents a reference to a variable.
type VarRef struct {
Val string
}
// String returns a string representation of the variable reference.
func (r *VarRef) String() string { return QuoteIdent(r.Val) }
func (r *VarRef) Args() []string {
return []string{r.Val}
}
// NumberLiteral represents a numeric literal.
type NumberLiteral struct {
Val float64
}
// String returns a string representation of the literal.
func (l *NumberLiteral) String() string { return strconv.FormatFloat(l.Val, 'f', 3, 64) }
func (n *NumberLiteral) Args() []string {
args := []string{}
return args
}
type SliceStringLiteral struct {
Val []string
}
// String returns a string representation of the literal.
func (l *SliceStringLiteral) String() string {
return fmt.Sprintf("%s", l.Val)
}
func (l *SliceStringLiteral) Args() []string {
args := []string{}
return args
}
type SliceNumberLiteral struct {
Val []float64
}
// String returns a string representation of the literal.
func (l *SliceNumberLiteral) String() string {
return fmt.Sprintf("%s", l.Val)
}
func (l *SliceNumberLiteral) Args() []string {
args := []string{}
return args
}
// BooleanLiteral represents a boolean literal.
type BooleanLiteral struct {
Val bool
}
// String returns a string representation of the literal.
func (l *BooleanLiteral) String() string {
if l.Val {
return "true"
}
return "false"
}
func (l *BooleanLiteral) Args() []string {
args := []string{}
return args
}
// StringLiteral represents a string literal.
type StringLiteral struct {
Val string
}
// String returns a string representation of the literal.
func (l *StringLiteral) String() string { return Quote(l.Val) }
// TimeLiteral represents a point-in-time literal.
type TimeLiteral struct {
Val time.Time
}
func (l *StringLiteral) Args() []string {
args := []string{}
return args
}
// String returns a string representation of the literal.
func (l *TimeLiteral) String() string { return l.Val.UTC().Format("2006-01-02 15:04:05.999") }
// DurationLiteral represents a duration literal.
type DurationLiteral struct {
Val time.Duration
}
// String returns a string representation of the literal.
func (l *DurationLiteral) String() string { return FormatDuration(l.Val) }
// BinaryExpr represents an operation between two expressions.
type BinaryExpr struct {
Op Token
LHS Expr
RHS Expr
}
// String returns a string representation of the binary expression.
func (e *BinaryExpr) String() string {
return fmt.Sprintf("%s %s %s", e.LHS.String(), e.Op, e.RHS.String())
}
func (e *BinaryExpr) Args() []string {
args := []string{}
args = append(e.LHS.Args(), args...)
args = append(e.RHS.Args(), args...)
return args
}
// ParenExpr represents a parenthesized expression.
type ParenExpr struct {
Expr Expr
}
// String returns a string representation of the parenthesized expression.
func (e *ParenExpr) String() string { return fmt.Sprintf("(%s)", e.Expr.String()) }
func (p *ParenExpr) Args() []string {
args := []string{}
args = append(p.Expr.Args(), args...)
return args
}
// Visitor can be called by Walk to traverse an AST hierarchy.
// The Visit() function is called once per node.
type Visitor interface {
Visit(Node) Visitor
}
// Walk traverses a node hierarchy in depth-first order.
func Walk(v Visitor, node Node) {
if v = v.Visit(node); v == nil {
return
}
switch n := node.(type) {
case *BinaryExpr:
Walk(v, n.LHS)
Walk(v, n.RHS)
case *ParenExpr:
Walk(v, n.Expr)
}
}
// WalkFunc traverses a node hierarchy in depth-first order.
func WalkFunc(node Node, fn func(Node)) {
Walk(walkFuncVisitor(fn), node)
}
type walkFuncVisitor func(Node)
func (fn walkFuncVisitor) Visit(n Node) Visitor { fn(n); return fn }
// Quote returns a quoted string.
func Quote(s string) string {
return `"` + strings.NewReplacer("\n", `\n`, `\`, `\\`, `"`, `\"`).Replace(s) + `"`
}
// QuoteIdent returns a quoted identifier if the identifier requires quoting.
// Otherwise returns the original string passed in.
func QuoteIdent(s string) string {
if s == "" || regexp.MustCompile(`[^a-zA-Z_.]`).MatchString(s) {
return Quote(s)
}
return s
}
// FormatDuration formats a duration to a string.
func FormatDuration(d time.Duration) string {
if d%(7*24*time.Hour) == 0 {
return fmt.Sprintf("%dw", d/(7*24*time.Hour))
} else if d%(24*time.Hour) == 0 {
return fmt.Sprintf("%dd", d/(24*time.Hour))
} else if d%time.Hour == 0 {
return fmt.Sprintf("%dh", d/time.Hour)
} else if d%time.Minute == 0 {
return fmt.Sprintf("%dm", d/time.Minute)
} else if d%time.Second == 0 {
return fmt.Sprintf("%ds", d/time.Second)
} else if d%time.Millisecond == 0 {
return fmt.Sprintf("%dms", d/time.Millisecond)
} else {
return fmt.Sprintf("%d", d/time.Microsecond)
}
}