-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlift.go
More file actions
394 lines (339 loc) · 9.84 KB
/
lift.go
File metadata and controls
394 lines (339 loc) · 9.84 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
package expr
import (
"fmt"
"strconv"
"strings"
)
const (
// VarPrefix is the lifted variable name used when extracting idents from an
// expression.
VarPrefix = "vars"
)
// replace is truly hack city. these are 20 variable names for values that are
// lifted out of expressions via liftLiterals.
var replace = []string{
"a", "b", "c", "d", "e",
"f", "g", "h", "i", "j",
"k", "l", "m", "n", "o",
"p", "q", "r", "s", "t",
"u", "v", "w", "x", "y",
"z",
}
// LiftedArgs represents a set of variables that have been lifted from expressions and
// replaced with identifiers, eg `id == "foo"` becomes `id == vars.a`, with "foo" lifted
// as "vars.a".
type LiftedArgs interface {
// Get a lifted variable argument from the parsed expression.
Get(val string) (any, bool)
// Return all lifted variables as a map.
Map() map[string]any
}
// liftLiterals lifts quoted literals into variables, allowing us to normalize
// expressions to increase cache hit rates.
func liftLiterals(expr string) (string, LiftedArgs) {
if strings.Contains(expr, VarPrefix+".") {
// Do not lift an expression twice, else we run the risk of using
// eg. `vars.a` to reference two separate strings, breaking the
// expression.
return expr, nil
}
lp := liftParser{expr: expr}
return lp.lift()
}
type liftParser struct {
expr string
idx int
rewritten *strings.Builder
// varCounter counts the number of variables lifted.
varCounter int
vars pointerArgMap
// prevChar distinguishes a numeric literal start from a digit within an identifier.
prevChar byte
// bracketDepth: array indices must not be lifted; parseArrayAccess expects integer literals.
bracketDepth int
}
func (l *liftParser) lift() (string, LiftedArgs) {
l.vars = pointerArgMap{
expr: l.expr,
vars: map[string]argMapValue{},
}
l.rewritten = &strings.Builder{}
comment := false
for l.idx < len(l.expr) {
char := l.expr[l.idx]
l.idx++
if comment && char == '\n' {
comment = false
}
if comment && char != '\n' {
continue
}
switch char {
case '/':
// if the next character is a slash, this is a comment line ("//")
if len(l.expr) > l.idx && string(l.expr[l.idx]) == "/" {
comment = true
continue
}
// prevChar must be '/' so digits immediately after (e.g. x/2) are lifted, not skipped.
l.rewritten.WriteByte(char)
l.prevChar = char
case '"':
// Consume the string arg.
val := l.consumeString('"')
l.addLiftedVar(val)
case '\'':
val := l.consumeString('\'')
l.addLiftedVar(val)
case '.':
// Leading-dot float (.5): if we wrote the dot then lifted the digit we'd produce ".vars.a".
if !isIdentChar(l.prevChar) && l.idx < len(l.expr) && l.expr[l.idx] >= '0' && l.expr[l.idx] <= '9' {
l.consumeLeadingDotFloat()
} else {
l.rewritten.WriteByte(char)
l.prevChar = char
}
case '[':
l.bracketDepth++
l.rewritten.WriteByte(char)
l.prevChar = char
case ']':
l.bracketDepth--
l.rewritten.WriteByte(char)
l.prevChar = char
default:
if char >= '0' && char <= '9' && !isIdentChar(l.prevChar) {
l.consumeNumeric(char)
} else {
l.rewritten.WriteByte(char)
l.prevChar = char
}
}
}
return strings.TrimSpace(l.rewritten.String()), &l.vars
}
// isIdentChar returns true if c can be part of an identifier (a-z, A-Z, 0-9, _).
func isIdentChar(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'
}
// consumeLeadingDotFloat lifts a leading-dot float literal (.5, .5e2).
// The dot has already been consumed; l.idx points to the first digit.
func (l *liftParser) consumeLeadingDotFloat() {
start := l.idx - 1 // include the leading dot
for l.idx < len(l.expr) && l.expr[l.idx] >= '0' && l.expr[l.idx] <= '9' {
l.idx++
}
if l.idx < len(l.expr) && (l.expr[l.idx] == 'e' || l.expr[l.idx] == 'E') {
l.idx++
if l.idx < len(l.expr) && (l.expr[l.idx] == '+' || l.expr[l.idx] == '-') {
l.idx++
}
for l.idx < len(l.expr) && l.expr[l.idx] >= '0' && l.expr[l.idx] <= '9' {
l.idx++
}
}
numStr := l.expr[start:l.idx]
f, err := strconv.ParseFloat(numStr, 64)
if err != nil {
l.rewritten.WriteString(numStr)
if len(numStr) > 0 {
l.prevChar = numStr[len(numStr)-1]
}
return
}
l.addLiftedVar(argMapValue{parsed: f})
}
// consumeNumeric lifts a numeric literal so expressions differing only in value share
// the same CEL cache entry.
func (l *liftParser) consumeNumeric(first byte) {
// Array index — parseArrayAccess expects an integer literal, not vars.X.
if l.bracketDepth > 0 {
l.rewritten.WriteByte(first)
l.prevChar = first
return
}
start := l.idx - 1 // first was already consumed (l.idx was incremented before the switch)
// 0x/0b/0o prefix — base-10 parsing would give the wrong value.
// TODO: we can lift those as well but not a priority?
if first == '0' && l.idx < len(l.expr) {
next := l.expr[l.idx]
if (next >= 'a' && next <= 'z') || (next >= 'A' && next <= 'Z') {
l.rewritten.WriteByte(first)
l.prevChar = first
return
}
}
for l.idx < len(l.expr) && l.expr[l.idx] >= '0' && l.expr[l.idx] <= '9' {
l.idx++
}
// u/U suffix — lifting the digits alone leaves "u" in the expression, producing "vars.au" which is completely wrong
// TODO: we can lift those as well but not a priority?
if l.idx < len(l.expr) && (l.expr[l.idx] == 'u' || l.expr[l.idx] == 'U') {
l.idx++ // consume the suffix as part of the token
numStr := l.expr[start:l.idx]
l.rewritten.WriteString(numStr)
l.prevChar = numStr[len(numStr)-1]
return
}
// Dot is fractional only when followed by a digit; trailing dot (1.) or field accessor (.field) are not.
isFloat := false
if l.idx < len(l.expr) && l.expr[l.idx] == '.' &&
l.idx+1 < len(l.expr) && l.expr[l.idx+1] >= '0' && l.expr[l.idx+1] <= '9' {
isFloat = true
l.idx++ // consume '.'
for l.idx < len(l.expr) && l.expr[l.idx] >= '0' && l.expr[l.idx] <= '9' {
l.idx++
}
}
// Consume e/E exponent whole; leaving "e10" would produce "vars.ae10" (field access, not a number) which is wrong as well
if l.idx < len(l.expr) && (l.expr[l.idx] == 'e' || l.expr[l.idx] == 'E') {
l.idx++
if l.idx < len(l.expr) && (l.expr[l.idx] == '+' || l.expr[l.idx] == '-') {
l.idx++
}
for l.idx < len(l.expr) && l.expr[l.idx] >= '0' && l.expr[l.idx] <= '9' {
l.idx++
}
numStr := l.expr[start:l.idx]
f, err := strconv.ParseFloat(numStr, 64)
if err != nil {
l.rewritten.WriteString(numStr)
if len(numStr) > 0 {
l.prevChar = numStr[len(numStr)-1]
}
return
}
l.addLiftedVar(argMapValue{parsed: f})
return
}
numStr := l.expr[start:l.idx]
if isFloat {
f, err := strconv.ParseFloat(numStr, 64)
if err != nil {
l.rewritten.WriteString(numStr)
if len(numStr) > 0 {
l.prevChar = numStr[len(numStr)-1]
}
return
}
l.addLiftedVar(argMapValue{parsed: f})
} else {
n, err := strconv.ParseInt(numStr, 10, 64)
if err != nil {
l.rewritten.WriteString(numStr)
if len(numStr) > 0 {
l.prevChar = numStr[len(numStr)-1]
}
return
}
l.addLiftedVar(argMapValue{parsed: n})
}
}
func (l *liftParser) addLiftedVar(val argMapValue) {
if l.varCounter >= len(replace) {
// Do nothing.
v := val.get(l.expr)
var s string
switch typed := v.(type) {
case string:
s = strconv.Quote(typed)
case int64:
s = strconv.FormatInt(typed, 10)
case float64:
s = strconv.FormatFloat(typed, 'f', -1, 64)
default:
s = fmt.Sprintf("%v", v)
}
l.rewritten.WriteString(s)
if len(s) > 0 {
l.prevChar = s[len(s)-1]
}
return
}
letter := replace[l.varCounter]
l.vars.vars[letter] = val
l.varCounter++
l.rewritten.WriteString(VarPrefix + "." + letter)
l.prevChar = letter[0]
}
func (l *liftParser) consumeString(quoteChar byte) argMapValue {
offset := l.idx
length := 0
for l.idx < len(l.expr) {
char := l.expr[l.idx]
if char == '\\' && l.idx+1 < len(l.expr) {
// Escape sequence: skip the backslash and whatever follows it.
// This correctly handles \\, \", \', \n, \t, etc.
l.idx += 2
length += 2
continue
}
if char == quoteChar {
// Skip over the end quote.
l.idx++
// Return the substring offset/length
return argMapValue{offset: offset, length: length}
}
// Grab the next char for evaluation.
l.idx++
// Only now has the length of the inner quote increased.
length++
}
// this is a grossly invalid expr, eg: `event.data.id == "foo\"`
// in this case, we can never parse this string. we always fix this by treating the last backslash
// as a \ literal, innit bruv
if length > 0 && offset+length <= len(l.expr) && l.expr[offset+length-1] == quoteChar {
length--
}
return argMapValue{offset: offset, length: length}
}
// nolint:unused
func (l *liftParser) peek() byte {
if (l.idx + 1) >= len(l.expr) {
return 0x0
}
return l.expr[l.idx+1]
}
// pointerArgMap takes the original expression, and adds pointers to the original expression
// in order to grab variables.
//
// It does this by pointing to the offset and length of data within the expression, as opposed
// to extracting the value into a new string. This greatly reduces memory growth & heap allocations.
type pointerArgMap struct {
expr string
vars map[string]argMapValue
}
func (p pointerArgMap) Map() map[string]any {
res := map[string]any{}
for k, v := range p.vars {
res[k] = v.get(p.expr)
}
return res
}
func (p pointerArgMap) Get(key string) (any, bool) {
val, ok := p.vars[key]
if !ok {
return nil, false
}
data := val.get(p.expr)
return data, true
}
// argMapValue is either a string slice (offset/length into expr) or a pre-parsed numeric (parsed != nil).
type argMapValue struct {
offset, length int
parsed any
}
func (a argMapValue) get(expr string) any {
if a.parsed != nil {
return a.parsed
}
return expr[a.offset : a.offset+a.length]
}
type regularArgMap map[string]any
func (p regularArgMap) Get(key string) (any, bool) {
val, ok := p[key]
return val, ok
}
func (p regularArgMap) Map() map[string]any {
return p
}