-
Notifications
You must be signed in to change notification settings - Fork 2
/
lexer_test.go
397 lines (389 loc) · 8.45 KB
/
lexer_test.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
package ksql
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test(t *testing.T) {
assert := require.New(t)
tests := []struct {
name string
input string
tokens []Token
err error
}{
{
name: "parse double quote string",
input: "\"quoted\"",
tokens: []Token{{Kind: QuotedString, Start: 0, Len: 8}},
},
{
name: "parse double quote string blank",
input: "\"\"",
tokens: []Token{{Kind: QuotedString, Start: 0, Len: 2}},
},
{
name: "parse double quote string unterminated",
input: "\"dfg",
err: ErrUnterminatedString{s: "\"dfg"},
},
{
name: "parse double quote string unterminated bare",
input: "\"",
err: ErrUnterminatedString{s: "\""},
},
{
name: "parse single quote string",
input: "'quoted'",
tokens: []Token{{Kind: QuotedString, Start: 0, Len: 8}},
},
{
name: "parse single quote string blank",
input: "''",
tokens: []Token{{Kind: QuotedString, Start: 0, Len: 2}},
},
{
name: "parse single quote string unterminated",
input: "'dfg",
err: ErrUnterminatedString{s: "'dfg"},
},
{
name: "parse single quote string unterminated bare",
input: "'",
err: ErrUnterminatedString{s: "'"},
},
{
name: "parse bool true",
input: "true",
tokens: []Token{{Kind: BooleanTrue, Start: 0, Len: 4}},
},
{
name: "parse bool false",
input: "false",
tokens: []Token{{Kind: BooleanFalse, Start: 0, Len: 5}},
},
{
name: "parse invalid bool",
input: "fool",
err: ErrInvalidBool{s: "fool"},
},
{
name: "parse number float",
input: "123.23",
tokens: []Token{{Kind: Number, Start: 0, Len: 6}},
},
{
name: "parse number Exp",
input: "1e-10",
tokens: []Token{{Kind: Number, Len: 5}},
},
{
name: "parse number int",
input: "123",
tokens: []Token{{Kind: Number, Len: 3}},
},
{
name: "parse number invalid",
input: "123.23.23",
err: ErrInvalidNumber{s: "123.23.23"},
},
{
name: "parse identifier",
input: ".properties.first_name",
tokens: []Token{{Kind: SelectorPath, Len: 22}},
},
{
name: "parse identifier blank",
input: ".",
err: ErrInvalidSelectorPath{s: "."},
},
{
name: "parse equals",
input: "==",
tokens: []Token{{Kind: Equals, Len: 2}},
},
{
name: "parse add",
input: "+",
tokens: []Token{{Kind: Add, Len: 1}},
},
{
name: "parse Substract",
input: "-",
tokens: []Token{{Kind: Subtract, Len: 1}},
},
{
name: "parse multiply",
input: "*",
tokens: []Token{{Kind: Multiply, Len: 1}},
},
{
name: "parse divide",
input: "/",
tokens: []Token{{Kind: Divide, Len: 1}},
},
{
name: "parse gt",
input: ">",
tokens: []Token{{Kind: Gt, Len: 1}},
},
{
name: "parse gte",
input: ">=",
tokens: []Token{{Kind: Gte, Len: 2}},
},
{
name: "parse lt",
input: "<",
tokens: []Token{{Kind: Lt, Len: 1}},
},
{
name: "parse lte",
input: "<=",
tokens: []Token{{Kind: Lte, Len: 2}},
},
{
name: "parse open paren",
input: "(",
tokens: []Token{{Kind: OpenParen, Len: 1}},
},
{
name: "parse close paren",
input: ")",
tokens: []Token{{Kind: CloseParen, Len: 1}},
},
{
name: "parse open bracket",
input: "[",
tokens: []Token{{Kind: OpenBracket, Len: 1}},
},
{
name: "parse close bracket",
input: "]",
tokens: []Token{{Kind: CloseBracket, Len: 1}},
},
{
name: "parse comma",
input: ",",
tokens: []Token{{Kind: Comma, Len: 1}},
},
{
name: "parse add selectorPath",
input: ".field1 + .field2",
tokens: []Token{
{Kind: SelectorPath, Len: 7},
{Kind: Add, Start: 8, Len: 1},
{Kind: SelectorPath, Start: 10, Len: 7},
},
},
{
name: "parse sub selectorPath",
input: ".field1 - .field2",
tokens: []Token{
{Kind: SelectorPath, Len: 7},
{Kind: Subtract, Start: 8, Len: 1},
{Kind: SelectorPath, Start: 10, Len: 7},
},
},
{
name: "parse brackets",
input: ".field1 - ( .field2 + .field3 )",
tokens: []Token{
{Kind: SelectorPath, Len: 7},
{Kind: Subtract, Start: 8, Len: 1},
{Kind: OpenParen, Start: 10, Len: 1},
{Kind: SelectorPath, Start: 12, Len: 7},
{Kind: Add, Start: 20, Len: 1},
{Kind: SelectorPath, Start: 22, Len: 7},
{Kind: CloseParen, Start: 30, Len: 1},
},
},
{
name: "parse or",
input: "||",
tokens: []Token{{Kind: Or, Len: 2}},
},
{
name: "parse in",
input: " IN ",
tokens: []Token{{Kind: In, Start: 1, Len: 2}},
},
{
name: "parse contains",
input: " CONTAINS ",
tokens: []Token{{Kind: Contains, Start: 1, Len: 8}},
},
{
name: "parse contains any",
input: " CONTAINS_ANY ",
tokens: []Token{{Kind: ContainsAny, Start: 1, Len: 12}},
},
{
name: "parse contains all",
input: " CONTAINS_ALL ",
tokens: []Token{{Kind: ContainsAll, Start: 1, Len: 12}},
},
{
name: "parse STARTSWITH",
input: " STARTSWITH ",
tokens: []Token{{Kind: StartsWith, Start: 1, Len: 10}},
},
{
name: "parse ENDSWITH",
input: " ENDSWITH ",
tokens: []Token{{Kind: EndsWith, Start: 1, Len: 8}},
},
{
name: "parse AND",
input: "&&",
tokens: []Token{{Kind: And, Start: 0, Len: 2}},
},
{
name: "parse NULL",
input: "NULL",
tokens: []Token{{Kind: Null, Start: 0, Len: 4}},
},
{
name: "parse bad or",
input: "|",
err: ErrInvalidKeyword{s: "|"},
},
{
name: "parse bad in",
input: " IN",
err: ErrInvalidKeyword{s: "IN"},
},
{
name: "parse bad contains",
input: " CONTAINS",
err: ErrInvalidKeyword{s: "CONTAINS"},
},
{
name: "parse bad STARTSWITH",
input: " STARTSWITH",
err: ErrInvalidKeyword{s: "STARTSWITH"},
},
{
name: "parse bad ENDSWITH",
input: " ENDSWITH",
err: ErrInvalidKeyword{s: "ENDSWITH"},
},
{
name: "parse bad AND",
input: "&",
err: ErrInvalidKeyword{s: "&"},
},
{
name: "parse bad NULL",
input: "NULLL",
err: ErrInvalidKeyword{s: "NULLL"},
},
{
name: "parse not",
input: "!",
tokens: []Token{{Kind: Not, Start: 0, Len: 1}},
},
{
name: "parse bad identifier",
input: "_datetime",
err: ErrInvalidIdentifier{s: "_datetime"},
},
{
name: "parse identifier",
input: "_datetime_",
tokens: []Token{{Kind: Identifier, Start: 0, Len: 10}},
},
{
name: "parse COERCE",
input: "COERCE ",
tokens: []Token{{Kind: Coerce, Start: 0, Len: 6}},
},
{
name: "parse COERCE",
input: `COERCE "2022-01-02" _datetime_`,
tokens: []Token{
{Kind: Coerce, Start: 0, Len: 6},
{Kind: QuotedString, Start: 7, Len: 12},
{Kind: Identifier, Start: 20, Len: 10},
},
},
{
name: "parse bad contains",
input: "CONTAINS",
err: ErrInvalidKeyword{s: "CONTAINS"},
},
{
name: "parse bad contains any",
input: "CONTAINS_ANY",
err: ErrInvalidKeyword{s: "CONTAINS_ANY"},
},
{
name: "parse bad contains all",
input: "CONTAINS_ALL",
err: ErrInvalidKeyword{s: "CONTAINS_ALL"},
},
{
name: "parse bad between",
input: "BETWEEN",
err: ErrInvalidKeyword{s: "BETWEEEN"},
},
{
name: "parse BETWEEN",
input: "BETWEEN ",
tokens: []Token{{Kind: Between, Start: 0, Len: 7}},
},
{
name: "parse negative number",
input: " -1.23 ",
tokens: []Token{{Kind: Number, Start: 1, Len: 5}},
},
{
name: "parse positive number",
input: " +1.23 ",
tokens: []Token{{Kind: Number, Start: 1, Len: 5}},
},
{
name: "parse positive number",
input: " +1.23 ",
tokens: []Token{{Kind: Number, Start: 1, Len: 5}},
},
{
name: "parse negative exponential number",
input: " -1e10 ",
tokens: []Token{{Kind: Number, Start: 1, Len: 5}},
},
{
name: "parse positive exponential number",
input: " +1e10 ",
tokens: []Token{{Kind: Number, Start: 1, Len: 5}},
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tokens, err := collect([]byte(tc.input))
if tc.err != nil {
assert.Error(err)
return
}
assert.NoError(err)
assert.Equal(tc.tokens, tokens)
})
}
}
// Collect tokenizes the input and returns tokens or error lexing them.
func collect(src []byte) (tokens []Token, err error) {
tokenizer := NewTokenizer(src)
for {
next := tokenizer.Next()
if next.IsNone() {
break
}
result := next.Unwrap()
if result.IsErr() {
return nil, result.Err()
}
tokens = append(tokens, result.Unwrap())
}
return
}