forked from longbridgeapp/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
547 lines (523 loc) · 9.67 KB
/
token.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package sqlparser
import (
"fmt"
"strconv"
"strings"
)
var keywords map[string]Token
func init() {
keywords = make(map[string]Token)
for i := keyword_beg + 1; i < keyword_end; i++ {
keywords[tokens[i]] = i
}
keywords[tokens[NULL]] = NULL
keywords[tokens[TRUE]] = TRUE
keywords[tokens[FALSE]] = FALSE
}
// Token is the set of lexical tokens of the Go programming language.
type Token int
// The list of tokens.
const (
// Special tokens
ILLEGAL Token = iota
EOF
COMMENT
SPACE
literal_beg
IDENT // IDENT
QIDENT // "IDENT" or `IDENT`
STRING // 'string'
BLOB // ???
FLOAT // 123.45
INTEGER // 123
MLCOMMENT // multiline comment
NULL // NULL
TRUE // true
FALSE // false
BIND //? or ?NNN or :VVV or @VVV or $VVV
literal_end
operator_beg
SEMI // ;
LP // (
RP // )
COMMA // ,
NE // !=
EQ // =
LE // <=
LG // <>
LT // <
GT // >
GE // >=
BITAND // &
BITOR // |
BITNOT // !
LSHIFT // <<
RSHIFT // >>
PLUS // +
MINUS // -
STAR // *
SLASH // /
REM // %
CONCAT // ||
DOT // .
operator_end
keyword_beg
ABORT
ACTION
ADD
AFTER
AGG_COLUMN
AGG_FUNCTION
ALL
ALTER
ANALYZE
AND
AS
ASC
ASTERISK
ATTACH
AUTOINCREMENT
BEFORE
BEGIN
BETWEEN
BY
CASCADE
CASE
CAST
CHECK
COLUMN
COLUMNKW
COMMIT
CONFLICT
CONSTRAINT
CREATE
CROSS
CTIME_KW
CURRENT
CURRENT_TIME
CURRENT_DATE
CURRENT_TIMESTAMP
DATABASE
DEFAULT
DEFERRABLE
DEFERRED
DELETE
DESC
DETACH
DISTINCT
DO
DROP
EACH
ELSE
END
ESCAPE
EXCEPT
EXCLUDE
EXCLUSIVE
EXISTS
EXPLAIN
FAIL
FILTER
FIRST
FOLLOWING
FOR
FOREIGN
FROM
FUNCTION
GLOB
GROUP
GROUPS
HAVING
IF
IF_NULL_ROW
IGNORE
IMMEDIATE
IN
INDEX
INDEXED
INITIALLY
INNER
INSERT
INSTEAD
INTERSECT
INTO
IS
ISNOT
ISNULL // TODO: REMOVE?
JOIN
KEY
LAST
LEFT
LIKE
LIMIT
MATCH
NATURAL
NO
NOT
NOTBETWEEN
NOTEXISTS
NOTGLOB
NOTHING
NOTIN
NOTLIKE
NOTMATCH
NOTNULL
NOTREGEXP
NULLS
OF
OFFSET
ON
ONLY
OR
ORDER
OTHERS
OUTER
OVER
OVERRIDING
PARTITION
PLAN
PRAGMA
PRECEDING
PRIMARY
QUERY
RAISE
RANGE
RECURSIVE
REFERENCES
REGEXP
REGISTER
REINDEX
RELEASE
RENAME
RESTRICT
RETURNING
ROLLBACK
ROW
ROWS
SELECT
SELECT_COLUMN
SET
SPAN
SYSTEM
TABLE
TEMP
THEN
TIES
TO
TRANSACTION
TRIGGER
TRUTH
UNBOUNDED
UNION
UNIQUE
UPDATE
USER
USING
VACUUM
VALUE
VALUES
VARIABLE
VECTOR
VIEW
VIRTUAL
WHEN
WHERE
WINDOW
WITH
WITHOUT
keyword_end
ANY // ???
token_end
)
var tokens = [...]string{
ILLEGAL: "ILLEGAL",
EOF: "EOF",
COMMENT: "COMMENT",
SPACE: "SPACE",
IDENT: "IDENT",
QIDENT: "QIDENT",
STRING: "STRING",
BLOB: "BLOB",
FLOAT: "FLOAT",
INTEGER: "INTEGER",
MLCOMMENT: "MLCOMMENT",
NULL: "NULL",
TRUE: "TRUE",
FALSE: "FALSE",
BIND: "BIND",
SEMI: ";",
LP: "(",
RP: ")",
COMMA: ",",
NE: "!=",
EQ: "=",
LE: "<=",
LG: "<>",
LT: "<",
GT: ">",
GE: ">=",
BITAND: "&",
BITOR: "|",
BITNOT: "!",
LSHIFT: "<<",
RSHIFT: ">>",
PLUS: "+",
MINUS: "-",
STAR: "*",
SLASH: "/",
REM: "%",
CONCAT: "||",
DOT: ".",
ABORT: "ABORT",
ACTION: "ACTION",
ADD: "ADD",
AFTER: "AFTER",
AGG_COLUMN: "AGG_COLUMN",
AGG_FUNCTION: "AGG_FUNCTION",
ALL: "ALL",
ALTER: "ALTER",
ANALYZE: "ANALYZE",
AND: "AND",
AS: "AS",
ASC: "ASC",
ASTERISK: "ASTERISK",
ATTACH: "ATTACH",
AUTOINCREMENT: "AUTOINCREMENT",
BEFORE: "BEFORE",
BEGIN: "BEGIN",
BETWEEN: "BETWEEN",
BY: "BY",
CASCADE: "CASCADE",
CASE: "CASE",
CAST: "CAST",
CHECK: "CHECK",
COLUMN: "COLUMN",
COLUMNKW: "COLUMNKW",
COMMIT: "COMMIT",
CONFLICT: "CONFLICT",
CONSTRAINT: "CONSTRAINT",
CREATE: "CREATE",
CROSS: "CROSS",
CTIME_KW: "CTIME_KW",
CURRENT: "CURRENT",
CURRENT_TIME: "CURRENT_TIME",
CURRENT_DATE: "CURRENT_DATE",
CURRENT_TIMESTAMP: "CURRENT_TIMESTAMP",
DATABASE: "DATABASE",
DEFAULT: "DEFAULT",
DEFERRABLE: "DEFERRABLE",
DEFERRED: "DEFERRED",
DELETE: "DELETE",
DESC: "DESC",
DETACH: "DETACH",
DISTINCT: "DISTINCT",
DO: "DO",
DROP: "DROP",
EACH: "EACH",
ELSE: "ELSE",
END: "END",
ESCAPE: "ESCAPE",
EXCEPT: "EXCEPT",
EXCLUDE: "EXCLUDE",
EXCLUSIVE: "EXCLUSIVE",
EXISTS: "EXISTS",
EXPLAIN: "EXPLAIN",
FAIL: "FAIL",
FILTER: "FILTER",
FIRST: "FIRST",
FOLLOWING: "FOLLOWING",
FOR: "FOR",
FOREIGN: "FOREIGN",
FROM: "FROM",
FUNCTION: "FUNCTION",
GLOB: "GLOB",
GROUP: "GROUP",
GROUPS: "GROUPS",
HAVING: "HAVING",
IF: "IF",
IF_NULL_ROW: "IF_NULL_ROW",
IGNORE: "IGNORE",
IMMEDIATE: "IMMEDIATE",
IN: "IN",
INDEX: "INDEX",
INDEXED: "INDEXED",
INITIALLY: "INITIALLY",
INNER: "INNER",
INSERT: "INSERT",
INSTEAD: "INSTEAD",
INTERSECT: "INTERSECT",
INTO: "INTO",
IS: "IS",
ISNOT: "ISNOT",
ISNULL: "ISNULL",
JOIN: "JOIN",
KEY: "KEY",
LAST: "LAST",
LEFT: "LEFT",
LIKE: "LIKE",
LIMIT: "LIMIT",
MATCH: "MATCH",
NO: "NO",
NATURAL: "NATURAL",
NOT: "NOT",
NOTBETWEEN: "NOTBETWEEN",
NOTEXISTS: "NOTEXISTS",
NOTGLOB: "NOTGLOB",
NOTHING: "NOTHING",
NOTIN: "NOTIN",
NOTLIKE: "NOTLIKE",
NOTMATCH: "NOTMATCH",
NOTNULL: "NOTNULL",
NOTREGEXP: "NOTREGEXP",
NULLS: "NULLS",
OF: "OF",
OFFSET: "OFFSET",
ON: "ON",
ONLY: "ONLY",
OR: "OR",
ORDER: "ORDER",
OTHERS: "OTHERS",
OUTER: "OUTER",
OVER: "OVER",
OVERRIDING: "OVERRIDING",
PARTITION: "PARTITION",
PLAN: "PLAN",
PRAGMA: "PRAGMA",
PRECEDING: "PRECEDING",
PRIMARY: "PRIMARY",
QUERY: "QUERY",
RAISE: "RAISE",
RANGE: "RANGE",
RECURSIVE: "RECURSIVE",
REFERENCES: "REFERENCES",
REGEXP: "REGEXP",
REGISTER: "REGISTER",
REINDEX: "REINDEX",
RELEASE: "RELEASE",
RENAME: "RENAME",
RESTRICT: "RESTRICT",
RETURNING: "RETURNING",
ROLLBACK: "ROLLBACK",
ROW: "ROW",
ROWS: "ROWS",
SELECT: "SELECT",
SELECT_COLUMN: "SELECT_COLUMN",
SET: "SET",
SPAN: "SPAN",
SYSTEM: "SYSTEM",
TABLE: "TABLE",
TEMP: "TEMP",
THEN: "THEN",
TIES: "TIES",
TO: "TO",
TRANSACTION: "TRANSACTION",
TRIGGER: "TRIGGER",
TRUTH: "TRUTH",
UNBOUNDED: "UNBOUNDED",
UNION: "UNION",
UNIQUE: "UNIQUE",
UPDATE: "UPDATE",
USER: "USER",
USING: "USING",
VACUUM: "VACUUM",
VALUE: "VALUE",
VALUES: "VALUES",
VARIABLE: "VARIABLE",
VECTOR: "VECTOR",
VIEW: "VIEW",
VIRTUAL: "VIRTUAL",
WHEN: "WHEN",
WHERE: "WHERE",
WINDOW: "WINDOW",
WITH: "WITH",
WITHOUT: "WITHOUT",
}
func (tok Token) String() string {
s := ""
if 0 <= tok && tok < Token(len(tokens)) {
s = tokens[tok]
}
if s == "" {
s = "token(" + strconv.Itoa(int(tok)) + ")"
}
return s
}
func Lookup(ident string) Token {
if tok, ok := keywords[strings.ToUpper(ident)]; ok {
return tok
}
return IDENT
}
func (tok Token) IsLiteral() bool {
return tok > literal_beg && tok < literal_end
}
func (tok Token) IsBinaryOp() bool {
switch tok {
case PLUS, MINUS, STAR, SLASH, REM, CONCAT, NOT, BETWEEN,
LSHIFT, RSHIFT, BITAND, BITOR, LT, LG, LE, GT, GE, EQ, NE,
IS, IN, LIKE, GLOB, MATCH, REGEXP, AND, OR:
return true
default:
return false
}
}
func isIdentToken(tok Token) bool {
return tok == IDENT || tok == QIDENT
}
const (
LowestPrec = 0 // non-operators
UnaryPrec = 13
HighestPrec = 14
)
func (op Token) Precedence() int {
switch op {
case OR:
return 1
case AND:
return 2
case NOT:
return 3
case IS, MATCH, LIKE, GLOB, REGEXP, BETWEEN, IN, ISNULL, NOTNULL, NE, LG, EQ:
return 4
case GT, LE, LT, GE:
return 5
case ESCAPE:
return 6
case BITAND, BITOR, LSHIFT, RSHIFT:
return 7
case PLUS, MINUS:
return 8
case STAR, SLASH, REM:
return 9
case CONCAT:
return 10
case BITNOT:
return 11
}
return LowestPrec
}
type Pos struct {
Offset int // offset, starting at 0
Line int // line number, starting at 1
Column int // column number, starting at 1 (byte count)
}
// String returns a string representation of the position.
func (p Pos) String() string {
if !p.IsValid() {
return "-"
}
s := fmt.Sprintf("%d", p.Line)
if p.Column != 0 {
s += fmt.Sprintf(":%d", p.Column)
}
return s
}
// IsValid returns true if p is non-zero.
func (p Pos) IsValid() bool {
return p != Pos{}
}
func assert(condition bool) {
if !condition {
panic("assert failed")
}
}