forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtering_lexer.go
513 lines (438 loc) · 9.88 KB
/
filtering_lexer.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
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
510
511
512
513
package query
import (
"fmt"
"strconv"
"unicode"
)
// FilteringLexer is impemented by lexical analyzers that are used by filtering expression parsers.
type FilteringLexer interface {
NextToken() (Token, error)
}
// NewFilteringLexer returns a default FilteringLexer implementation.
// text is a filtering expression to analyze.
func NewFilteringLexer(text string) FilteringLexer {
var runes []rune
for _, r := range text {
runes = append(runes, r)
}
if len(runes) > 0 {
return &filteringLexer{runes, 0, runes[0], false}
}
return &filteringLexer{runes, 0, 0, true}
}
// UnexpectedSymbolError describes symbol S in position Pos that was not appropriate according to REST API Syntax Specification.
type UnexpectedSymbolError struct {
S rune
Pos int
}
func (e *UnexpectedSymbolError) Error() string {
return fmt.Sprintf("Unexpected symbol %c in %d position", e.S, e.Pos)
}
// Token is impelemented by all supported tokens in a filtering expression.
type Token interface {
Token()
}
// TokenBase is used as a base type for all types which are tokens.
type TokenBase struct{}
// Token distinguishes tokens from other types.
func (t TokenBase) Token() {}
// LparenToken represents left parenthesis.
type LparenToken struct {
TokenBase
}
func (t LparenToken) String() string {
return "("
}
// RparenToken represents right parenthesis.
type RparenToken struct {
TokenBase
}
func (t RparenToken) String() string {
return ")"
}
// NumberToken represents a number literal.
// Value is a value of the literal.
type NumberToken struct {
TokenBase
Value float64
}
func (t NumberToken) String() string {
return fmt.Sprint(t.Value)
}
// StringToken represents a string literal.
// Value is a value of the literal.
type StringToken struct {
TokenBase
Value string
}
func (t StringToken) String() string {
return fmt.Sprint(t.Value)
}
// FieldToken represents a reference to a value of a resource.
// Value is a value of the reference.
type FieldToken struct {
TokenBase
Value string
}
func (t FieldToken) String() string {
return fmt.Sprint(t.Value)
}
// AndToken represents logical and.
type AndToken struct {
TokenBase
}
func (t AndToken) String() string {
return "and"
}
// OrToken represents logical or.
type OrToken struct {
TokenBase
}
func (t OrToken) String() string {
return "or"
}
// NotToken represents logical not.
type NotToken struct {
TokenBase
}
func (t NotToken) String() string {
return "not"
}
// EqToken represents equals operator.
type EqToken struct {
TokenBase
}
func (t EqToken) String() string {
return "=="
}
type InsensitiveEqToken struct {
TokenBase
}
func (t InsensitiveEqToken) String() string {
return ":="
}
// NeToken represents not equals operator.
type NeToken struct {
TokenBase
}
func (t NeToken) String() string {
return "!="
}
// MatchToken represents regular expression match.
type MatchToken struct {
TokenBase
}
func (t MatchToken) String() string {
return "~"
}
// NmatchToken represents negation of regular expression match.
type NmatchToken struct {
TokenBase
}
func (t NmatchToken) String() string {
return "!~"
}
// GtToken represents greater than operator.
type GtToken struct {
TokenBase
}
func (t GtToken) String() string {
return ">"
}
// GeToken represents greater than or equals operator.
type GeToken struct {
TokenBase
}
func (t GeToken) String() string {
return ">="
}
// LtToken represents less than operator.
type LtToken struct {
TokenBase
}
func (t LtToken) String() string {
return "<"
}
// LeToken represents less than or equals operator.
type LeToken struct {
TokenBase
}
func (t LeToken) String() string {
return "<="
}
type Contains struct {
TokenBase
}
func (t Contains) String() string {
return "contains"
}
// NullToken represents null literal.
type NullToken struct {
TokenBase
}
func (t NullToken) String() string {
return "null"
}
// InToken represent in operation for string and numbers arrays
type InToken struct {
TokenBase
}
func (t InToken) String() string {
return "in"
}
//NumberArrayToken represent number array e.g. [1,2,5]
type StringArrayToken struct {
TokenBase
Values []string
}
func (t StringArrayToken) String() string {
return fmt.Sprintf("%v", t.Values)
}
//NumberArrayToken represent number array e.g. [1,2,5]
type NumberArrayToken struct {
TokenBase
Values []float64
}
func (t NumberArrayToken) String() string {
return fmt.Sprintf("%v", t.Values)
}
// EOFToken represents end of an expression.
type EOFToken struct {
TokenBase
}
func (t EOFToken) String() string {
return "EOF"
}
type filteringLexer struct {
text []rune
pos int
curChar rune
eof bool
}
func (lexer *filteringLexer) advance() {
lexer.pos++
if lexer.pos < len(lexer.text) {
lexer.curChar = lexer.text[lexer.pos]
} else {
lexer.eof = true
lexer.curChar = 0
}
}
func (lexer *filteringLexer) number() (Token, error) {
number := string(lexer.curChar)
metDot := false
lexer.advance()
for !lexer.eof {
if unicode.IsDigit(lexer.curChar) {
number += string(lexer.curChar)
} else if !metDot && lexer.curChar == '.' {
number += string(lexer.curChar)
metDot = true
} else {
break
}
lexer.advance()
}
parsed, err := strconv.ParseFloat(number, 64)
if err != nil {
return nil, err
}
return NumberToken{Value: parsed}, nil
}
func (lexer *filteringLexer) string() (Token, error) {
// Add quote escaping support
term := lexer.curChar
s := ""
lexer.advance()
for lexer.curChar != term {
if lexer.eof {
return nil, &UnexpectedSymbolError{lexer.curChar, lexer.pos}
}
s += string(lexer.curChar)
lexer.advance()
}
lexer.advance()
return StringToken{Value: s}, nil
}
func (lexer *filteringLexer) array() (Token, error) {
term := ']'
lexer.advance()
if lexer.curChar == term {
return nil, &UnexpectedSymbolError{lexer.curChar, lexer.pos}
}
if unicode.IsDigit(lexer.curChar) {
values := make([]float64, 0)
for lexer.curChar != term {
if unicode.IsSpace(lexer.curChar) || lexer.curChar == ',' {
lexer.advance()
continue
}
if lexer.eof {
return nil, &UnexpectedSymbolError{lexer.curChar, lexer.pos}
}
t, err := lexer.number()
if err != nil {
return nil, err
}
numberToken, ok := t.(NumberToken)
if !ok {
return nil, &UnexpectedTokenError{t}
}
values = append(values, numberToken.Value)
}
if lexer.eof {
return nil, &UnexpectedSymbolError{lexer.curChar, lexer.pos}
}
lexer.advance()
return NumberArrayToken{
Values: values,
}, nil
} else if lexer.curChar == '\'' || lexer.curChar == '"' {
values := make([]string, 0)
for lexer.curChar != term {
if unicode.IsSpace(lexer.curChar) || lexer.curChar == ',' {
lexer.advance()
continue
}
if lexer.eof {
return nil, &UnexpectedSymbolError{lexer.curChar, lexer.pos}
}
t, err := lexer.string()
if err != nil {
return nil, err
}
numberToken, ok := t.(StringToken)
if !ok {
return nil, &UnexpectedTokenError{t}
}
values = append(values, numberToken.Value)
}
if lexer.eof {
return nil, &UnexpectedSymbolError{lexer.curChar, lexer.pos}
}
lexer.advance()
return StringArrayToken{
Values: values,
}, nil
} else {
return nil, &UnexpectedSymbolError{lexer.curChar, lexer.pos}
}
}
func (lexer *filteringLexer) fieldOrReserved() (Token, error) {
s := string(lexer.curChar)
lexer.advance()
for !lexer.eof {
if unicode.IsDigit(lexer.curChar) ||
unicode.IsLetter(lexer.curChar) ||
lexer.curChar == '.' ||
lexer.curChar == '-' ||
lexer.curChar == '_' {
s += string(lexer.curChar)
} else {
break
}
lexer.advance()
}
switch s {
case "and":
return AndToken{}, nil
case "or":
return OrToken{}, nil
case "not":
return NotToken{}, nil
case "null":
return NullToken{}, nil
case "eq":
return EqToken{}, nil
case "ne":
return NeToken{}, nil
case "gt":
return GtToken{}, nil
case "ge":
return GeToken{}, nil
case "lt":
return LtToken{}, nil
case "le":
return LeToken{}, nil
case "contains":
return Contains{}, nil
case "match":
return MatchToken{}, nil
case "nomatch":
return NmatchToken{}, nil
case "in":
return InToken{}, nil
case "ieq":
return InsensitiveEqToken{}, nil
default:
return FieldToken{Value: s}, nil
}
}
// NextToken returns the next token from the expression.
func (lexer *filteringLexer) NextToken() (Token, error) {
for !lexer.eof {
switch {
case unicode.IsSpace(lexer.curChar):
lexer.advance()
case lexer.curChar == '(':
lexer.advance()
return LparenToken{}, nil
case lexer.curChar == ')':
lexer.advance()
return RparenToken{}, nil
case lexer.curChar == '~':
lexer.advance()
return MatchToken{}, nil
case lexer.curChar == '=':
lexer.advance()
if lexer.curChar == '=' {
lexer.advance()
return EqToken{}, nil
}
return nil, &UnexpectedSymbolError{lexer.curChar, lexer.pos}
case lexer.curChar == '!':
lexer.advance()
if lexer.curChar == '=' {
lexer.advance()
return NeToken{}, nil
} else if lexer.curChar == '~' {
lexer.advance()
return NmatchToken{}, nil
} else {
return nil, &UnexpectedSymbolError{lexer.curChar, lexer.pos}
}
case lexer.curChar == '>':
lexer.advance()
if lexer.curChar == '=' {
lexer.advance()
return GeToken{}, nil
}
return GtToken{}, nil
case lexer.curChar == '<':
lexer.advance()
if lexer.curChar == '=' {
lexer.advance()
return LeToken{}, nil
}
return LtToken{}, nil
case lexer.curChar == ':':
lexer.advance()
if lexer.curChar == '=' {
lexer.advance()
return InsensitiveEqToken{}, nil
}
return nil, &UnexpectedSymbolError{lexer.curChar, lexer.pos}
case lexer.curChar == '\'' || lexer.curChar == '"':
return lexer.string()
case lexer.curChar == '[':
return lexer.array()
case unicode.IsDigit(lexer.curChar):
return lexer.number()
case unicode.IsLetter(lexer.curChar):
return lexer.fieldOrReserved()
default:
return nil, &UnexpectedSymbolError{lexer.curChar, lexer.pos}
}
}
return EOFToken{}, nil
}