-
Notifications
You must be signed in to change notification settings - Fork 3
/
lexer.go
462 lines (446 loc) · 8.18 KB
/
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
package kvql
import (
"fmt"
"strconv"
"strings"
)
type TokenType byte
const (
SELECT TokenType = 1
WHERE TokenType = 2
KEY TokenType = 3
VALUE TokenType = 4
OPERATOR TokenType = 5
STRING TokenType = 6
LPAREN TokenType = 7
RPAREN TokenType = 8
NAME TokenType = 9
SEP TokenType = 10
NUMBER TokenType = 11
FLOAT TokenType = 12
LIMIT TokenType = 13
ORDER TokenType = 14
BY TokenType = 15
ASC TokenType = 16
DESC TokenType = 17
TRUE TokenType = 18
FALSE TokenType = 19
AS TokenType = 20
GROUP TokenType = 21
IN TokenType = 22
BETWEEN TokenType = 23
AND TokenType = 24
LBRACK TokenType = 25
RBRACK TokenType = 26
PUT TokenType = 27
REMOVE TokenType = 28
SEMI TokenType = 29
OR TokenType = 30
DELETE TokenType = 31
)
var (
TokenTypeToString = map[TokenType]string{
SELECT: "SELECT",
WHERE: "WHERE",
KEY: "KEY",
VALUE: "VALUE",
OPERATOR: "OP",
STRING: "STR",
LPAREN: "(",
RPAREN: ")",
NAME: "NAME",
SEP: "SEP",
NUMBER: "NUM",
FLOAT: "FLOAT",
LIMIT: "LIMIT",
ORDER: "ORDER",
BY: "BY",
ASC: "ASC",
DESC: "DESC",
TRUE: "true",
FALSE: "false",
AS: "AS",
GROUP: "GROUP",
IN: "IN",
BETWEEN: "BETWEEN",
AND: "AND",
LBRACK: "[",
RBRACK: "]",
PUT: "PUT",
REMOVE: "REMOVE",
SEMI: "SEMI",
OR: "OR",
DELETE: "DELETE",
}
)
const (
LowestPrec = 0 // non-operators
UnaryPrec = 6
HighestPrec = 7
)
type Token struct {
Tp TokenType
Data string
Pos int
}
func (t *Token) String() string {
tp := TokenTypeToString[t.Tp]
return fmt.Sprintf("Tp: %6s Data: %10s Pos: %d", tp, t.Data, t.Pos)
}
func (t *Token) Precedence() int {
switch t.Tp {
case OPERATOR:
switch t.Data {
case "|", "or":
return 1
case "&", "and":
return 2
case "=", "!=", "^=", "~=", ">", ">=", "<", "<=", "in", "between":
return 3
case "+", "-":
return 4
case "*", "/":
return 5
}
}
return LowestPrec
}
type Lexer struct {
Query string
Length int
}
func NewLexer(query string) *Lexer {
return &Lexer{
Query: query,
Length: len(query),
}
}
func (l *Lexer) Split() []*Token {
var (
curr string
prev byte
next byte
ret []*Token
strStart bool = false
strStartChar byte = 0
tokStart int = 0
tokLen int = 0
tokStartPos int
)
for i := 0; i < l.Length; i++ {
char := l.Query[i]
if i < l.Length-1 {
next = l.Query[i+1]
} else {
next = 0
}
switch char {
case ' ':
if strStart {
tokLen++
break
}
curr = l.Query[tokStart : tokStart+min(tokLen, l.Length-tokStart)]
if token := buildToken(curr, tokStartPos); token != nil {
ret = append(ret, token)
}
tokLen = 0
tokStartPos = i + 1
tokStart = i + 1
case '"', '\'':
if !strStart {
strStart = true
strStartChar = char
tokStartPos = i
tokStart = i + 1
} else if strStartChar == char {
strStart = false
curr = l.Query[tokStart : tokStart+min(tokLen, l.Length-tokStart)]
token := &Token{
Tp: STRING,
Data: curr,
Pos: tokStartPos,
}
ret = append(ret, token)
tokLen = 0
} else {
tokLen++
}
case '`':
if !strStart {
strStart = true
strStartChar = char
tokStartPos = i
tokStart = i + 1
} else if strStartChar == char {
strStart = false
curr = l.Query[tokStart : tokStart+min(tokLen, l.Length-tokStart)]
token := &Token{
Tp: NAME,
Data: curr,
Pos: tokStartPos,
}
ret = append(ret, token)
tokLen = 0
} else {
tokLen++
}
case '~', '^', '=', '!', '*', '+', '-', '/', '>', '<':
if strStart {
tokLen++
break
}
curr = l.Query[tokStart : tokStart+min(tokLen, l.Length-tokStart)]
if token := buildToken(curr, tokStartPos); token != nil {
ret = append(ret, token)
}
tokLen = 0
var token *Token = nil
if next != '=' {
switch char {
case '!', '*', '+', '-', '/':
token = &Token{
Tp: OPERATOR,
Data: string(char),
Pos: i,
}
case '>', '<':
token = &Token{
Tp: OPERATOR,
Data: string(char),
Pos: i,
}
}
}
if token != nil {
ret = append(ret, token)
tokStartPos = i + 1
tokStart = i + 1
break
}
if char == '=' {
switch prev {
case '^':
token = &Token{
Tp: OPERATOR,
Data: "^=",
Pos: i - 1,
}
case '~':
token = &Token{
Tp: OPERATOR,
Data: "~=",
Pos: i - 1,
}
case '!':
token = &Token{
Tp: OPERATOR,
Data: "!=",
Pos: i - 1,
}
case '<':
token = &Token{
Tp: OPERATOR,
Data: "<=",
Pos: i - 1,
}
case '>':
token = &Token{
Tp: OPERATOR,
Data: ">=",
Pos: i - 1,
}
default:
token = &Token{
Tp: OPERATOR,
Data: "=",
Pos: i,
}
}
if token != nil {
ret = append(ret, token)
}
}
tokStartPos = i + 1
tokStart = i + 1
case '&', '|', '(', ')', '[', ']':
if strStart {
tokLen++
break
}
curr = l.Query[tokStart : tokStart+min(tokLen, l.Length-tokStart)]
token := buildToken(curr, tokStartPos)
if token != nil {
ret = append(ret, token)
}
switch char {
case '(':
token = &Token{
Tp: LPAREN,
Data: string(char),
Pos: i,
}
case ')':
token = &Token{
Tp: RPAREN,
Data: string(char),
Pos: i,
}
case '[':
token = &Token{
Tp: LBRACK,
Data: string(char),
Pos: i,
}
case ']':
token = &Token{
Tp: RBRACK,
Data: string(char),
Pos: i,
}
default:
token = &Token{
Tp: OPERATOR,
Data: string(char),
Pos: i,
}
}
ret = append(ret, token)
tokLen = 0
tokStartPos = i + 1
tokStart = i + 1
case ',', ';':
if strStart {
tokLen++
break
}
curr = l.Query[tokStart : tokStart+min(tokLen, l.Length-tokStart)]
token := buildToken(curr, tokStartPos)
if token != nil {
ret = append(ret, token)
}
switch char {
case ',':
token = &Token{
Tp: SEP,
Data: string(char),
Pos: i,
}
case ';':
token = &Token{
Tp: SEMI,
Data: string(char),
Pos: i,
}
}
ret = append(ret, token)
tokLen = 0
tokStartPos = i + 1
tokStart = i + 1
default:
tokLen++
}
prev = char
}
if tokLen > 0 {
curr = l.Query[tokStart : tokStart+min(tokLen, l.Length-tokStart)]
if token := buildToken(curr, tokStartPos); token != nil {
ret = append(ret, token)
}
}
return ret
}
func isNumber(val string) bool {
if _, err := strconv.ParseInt(val, 10, 64); err == nil {
return true
}
return false
}
func isFloat(val string) bool {
if _, err := strconv.ParseFloat(val, 64); err == nil {
return true
}
return false
}
func buildToken(curr string, pos int) *Token {
curr = strings.ToLower(strings.TrimSpace(curr))
if len(curr) == 0 {
return nil
}
token := &Token{
Data: curr,
Pos: pos,
}
switch curr {
case "select":
token.Tp = SELECT
return token
case "where":
token.Tp = WHERE
return token
case "key":
token.Tp = KEY
return token
case "value":
token.Tp = VALUE
return token
case "limit":
token.Tp = LIMIT
return token
case "order":
token.Tp = ORDER
return token
case "by":
token.Tp = BY
return token
case "asc":
token.Tp = ASC
return token
case "desc":
token.Tp = DESC
return token
case "true":
token.Tp = TRUE
return token
case "false":
token.Tp = FALSE
return token
case "as":
token.Tp = AS
return token
case "group":
token.Tp = GROUP
return token
case "in":
token.Tp = OPERATOR
return token
case "between":
token.Tp = OPERATOR
return token
case "put":
token.Tp = PUT
return token
case "remove":
token.Tp = REMOVE
return token
case "and":
token.Tp = OPERATOR
return token
case "or":
token.Tp = OPERATOR
return token
case "delete":
token.Tp = DELETE
return token
default:
if isNumber(curr) {
token.Tp = NUMBER
return token
} else if isFloat(curr) {
token.Tp = FLOAT
return token
}
token.Tp = NAME
return token
}
}