FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlparser/token.go at master · dearcode/sqlparser · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
dearcode
/
sqlparser
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
4
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlparser
/
token.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
896 lines (833 loc) · 22 KB
Breadcrumbs
sqlparser
/
token.go
Copy path
File metadata and controls
896 lines (833 loc) · 22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
sqlparser
import
(
"bytes"
"errors"
"fmt"
"io"
"vitess.io/vitess/go/bytes2"
"vitess.io/vitess/go/sqltypes"
)
const
(
defaultBufSize
=
4096
eofChar
=
0x100
)
// Tokenizer is the struct used to generate SQL
// tokens for the parser.
type
Tokenizer
struct
{
InStream
io.
Reader
AllowComments
bool
ForceEOF
bool
lastChar
uint16
Position
int
lastToken
[]
byte
LastError
error
posVarIndex
int
ParseTree
Statement
partialDDL
*
DDL
nesting
int
multi
bool
buf
[]
byte
bufPos
int
bufSize
int
}
// NewStringTokenizer creates a new Tokenizer for the
// sql string.
func
NewStringTokenizer
(
sql
string
)
*
Tokenizer
{
buf
:=
[]
byte
(
sql
)
return
&
Tokenizer
{
buf
:
buf
,
bufSize
:
len
(
buf
),
}
}
// NewTokenizer creates a new Tokenizer reading a sql
// string from the io.Reader.
func
NewTokenizer
(
r
io.
Reader
)
*
Tokenizer
{
return
&
Tokenizer
{
InStream
:
r
,
buf
:
make
([]
byte
,
defaultBufSize
),
}
}
// keywords is a map of mysql keywords that fall into two categories:
// 1) keywords considered reserved by MySQL
// 2) keywords for us to handle specially in sql.y
//
// Those marked as UNUSED are likely reserved keywords. We add them here so that
// when rewriting queries we can properly backtick quote them so they don't cause issues
//
// NOTE: If you add new keywords, add them also to the reserved_keywords or
// non_reserved_keywords grammar in sql.y -- this will allow the keyword to be used
// in identifiers. See the docs for each grammar to determine which one to put it into.
var
keywords
=
map
[
string
]
int
{
"accessible"
:
UNUSED
,
"add"
:
ADD
,
"against"
:
AGAINST
,
"all"
:
ALL
,
"alter"
:
ALTER
,
"analyze"
:
ANALYZE
,
"and"
:
AND
,
"as"
:
AS
,
"asc"
:
ASC
,
"asensitive"
:
UNUSED
,
"auto_increment"
:
AUTO_INCREMENT
,
"before"
:
UNUSED
,
"begin"
:
BEGIN
,
"between"
:
BETWEEN
,
"bigint"
:
BIGINT
,
"binary"
:
BINARY
,
"_binary"
:
UNDERSCORE_BINARY
,
"bit"
:
BIT
,
"blob"
:
BLOB
,
"bool"
:
BOOL
,
"boolean"
:
BOOLEAN
,
"both"
:
UNUSED
,
"by"
:
BY
,
"call"
:
UNUSED
,
"case"
:
CASE
,
"cast"
:
CAST
,
"change"
:
CHANGE
,
"char"
:
CHAR
,
"character"
:
CHARACTER
,
"charset"
:
CHARSET
,
"check"
:
UNUSED
,
"collate"
:
COLLATE
,
"column"
:
COLUMN
,
"comment"
:
COMMENT_KEYWORD
,
"commit"
:
COMMIT
,
"condition"
:
UNUSED
,
"constraint"
:
CONSTRAINT
,
"continue"
:
UNUSED
,
"convert"
:
CONVERT
,
"create"
:
CREATE
,
"cross"
:
CROSS
,
"current_date"
:
CURRENT_DATE
,
"current_time"
:
CURRENT_TIME
,
"current_timestamp"
:
CURRENT_TIMESTAMP
,
"now"
:
NOW
,
"current_user"
:
UNUSED
,
"cursor"
:
UNUSED
,
"database"
:
DATABASE
,
"databases"
:
DATABASES
,
"day_hour"
:
UNUSED
,
"day_microsecond"
:
UNUSED
,
"day_minute"
:
UNUSED
,
"day_second"
:
UNUSED
,
"date"
:
DATE
,
"datetime"
:
DATETIME
,
"dec"
:
UNUSED
,
"decimal"
:
DECIMAL
,
"declare"
:
UNUSED
,
"default"
:
DEFAULT
,
"delayed"
:
UNUSED
,
"delete"
:
DELETE
,
"desc"
:
DESC
,
"describe"
:
DESCRIBE
,
"deterministic"
:
UNUSED
,
"distinct"
:
DISTINCT
,
"distinctrow"
:
UNUSED
,
"div"
:
DIV
,
"double"
:
DOUBLE
,
"precision"
:
UNUSED
,
"drop"
:
DROP
,
"modify"
:
MODIFY
,
"duplicate"
:
DUPLICATE
,
"each"
:
UNUSED
,
"else"
:
ELSE
,
"elseif"
:
UNUSED
,
"enclosed"
:
UNUSED
,
"end"
:
END
,
"enum"
:
ENUM
,
"escape"
:
ESCAPE
,
"escaped"
:
UNUSED
,
"exists"
:
EXISTS
,
"exit"
:
UNUSED
,
"explain"
:
EXPLAIN
,
"expansion"
:
EXPANSION
,
"false"
:
FALSE
,
"fetch"
:
UNUSED
,
"float"
:
FLOAT_TYPE
,
"float4"
:
UNUSED
,
"float8"
:
UNUSED
,
"for"
:
FOR
,
"force"
:
FORCE
,
"foreign"
:
FOREIGN
,
"no"
:
NO
,
"action"
:
ACTION
,
"references"
:
REFERENCES
,
"cascade"
:
CASCADE
,
"from"
:
FROM
,
"fulltext"
:
FULLTEXT
,
"generated"
:
UNUSED
,
"get"
:
UNUSED
,
"global"
:
GLOBAL
,
"grant"
:
UNUSED
,
"group"
:
GROUP
,
"group_concat"
:
GROUP_CONCAT
,
"having"
:
HAVING
,
"high_priority"
:
UNUSED
,
"hour_microsecond"
:
UNUSED
,
"hour_minute"
:
UNUSED
,
"hour_second"
:
UNUSED
,
"if"
:
IF
,
"ignore"
:
IGNORE
,
"in"
:
IN
,
"index"
:
INDEX
,
"infile"
:
UNUSED
,
"inout"
:
UNUSED
,
"inner"
:
INNER
,
"insensitive"
:
UNUSED
,
"insert"
:
INSERT
,
"int"
:
INT
,
"int1"
:
UNUSED
,
"int2"
:
UNUSED
,
"int3"
:
UNUSED
,
"int4"
:
UNUSED
,
"int8"
:
UNUSED
,
"integer"
:
INTEGER
,
"interval"
:
INTERVAL
,
"into"
:
INTO
,
"io_after_gtids"
:
UNUSED
,
"is"
:
IS
,
"iterate"
:
UNUSED
,
"join"
:
JOIN
,
"json"
:
JSON
,
"key"
:
KEY
,
"keys"
:
KEYS
,
"kill"
:
UNUSED
,
"language"
:
LANGUAGE
,
"last_insert_id"
:
LAST_INSERT_ID
,
"leading"
:
UNUSED
,
"leave"
:
UNUSED
,
"left"
:
LEFT
,
"less"
:
LESS
,
"like"
:
LIKE
,
"limit"
:
LIMIT
,
"linear"
:
UNUSED
,
"lines"
:
UNUSED
,
"load"
:
UNUSED
,
"localtime"
:
LOCALTIME
,
"localtimestamp"
:
LOCALTIMESTAMP
,
"lock"
:
LOCK
,
"long"
:
UNUSED
,
"longblob"
:
LONGBLOB
,
"longtext"
:
LONGTEXT
,
"loop"
:
UNUSED
,
"low_priority"
:
UNUSED
,
"master_bind"
:
UNUSED
,
"match"
:
MATCH
,
"maxvalue"
:
MAXVALUE
,
"mediumblob"
:
MEDIUMBLOB
,
"mediumint"
:
MEDIUMINT
,
"mediumtext"
:
MEDIUMTEXT
,
"middleint"
:
UNUSED
,
"minute_microsecond"
:
UNUSED
,
"minute_second"
:
UNUSED
,
"mod"
:
MOD
,
"mode"
:
MODE
,
"modifies"
:
UNUSED
,
"names"
:
NAMES
,
"natural"
:
NATURAL
,
"nchar"
:
NCHAR
,
"next"
:
NEXT
,
"not"
:
NOT
,
"no_write_to_binlog"
:
UNUSED
,
"null"
:
NULL
,
"numeric"
:
NUMERIC
,
"offset"
:
OFFSET
,
"on"
:
ON
,
"optimize"
:
OPTIMIZE
,
"optimizer_costs"
:
UNUSED
,
"option"
:
UNUSED
,
"optionally"
:
UNUSED
,
"or"
:
OR
,
"order"
:
ORDER
,
"out"
:
UNUSED
,
"outer"
:
OUTER
,
"outfile"
:
UNUSED
,
"partition"
:
PARTITION
,
"primary"
:
PRIMARY
,
"procedure"
:
PROCEDURE
,
"query"
:
QUERY
,
"range"
:
UNUSED
,
"read"
:
UNUSED
,
"reads"
:
UNUSED
,
"read_write"
:
UNUSED
,
"real"
:
REAL
,
"regexp"
:
REGEXP
,
"release"
:
UNUSED
,
"rename"
:
RENAME
,
"reorganize"
:
REORGANIZE
,
"repair"
:
REPAIR
,
"repeat"
:
UNUSED
,
"replace"
:
REPLACE
,
"require"
:
UNUSED
,
"resignal"
:
UNUSED
,
"restrict"
:
RESTRICT
,
"return"
:
UNUSED
,
"revoke"
:
UNUSED
,
"right"
:
RIGHT
,
"rlike"
:
REGEXP
,
"rollback"
:
ROLLBACK
,
"schema"
:
UNUSED
,
"schemas"
:
UNUSED
,
"second_microsecond"
:
UNUSED
,
"select"
:
SELECT
,
"sensitive"
:
UNUSED
,
"separator"
:
SEPARATOR
,
"session"
:
SESSION
,
"set"
:
SET
,
"share"
:
SHARE
,
"show"
:
SHOW
,
"signal"
:
UNUSED
,
"signed"
:
SIGNED
,
"smallint"
:
SMALLINT
,
"spatial"
:
SPATIAL
,
"specific"
:
UNUSED
,
"sql"
:
UNUSED
,
"sqlexception"
:
UNUSED
,
"sqlstate"
:
UNUSED
,
"sqlwarning"
:
UNUSED
,
"sql_big_result"
:
UNUSED
,
"sql_cache"
:
SQL_CACHE
,
"sql_calc_found_rows"
:
UNUSED
,
"sql_no_cache"
:
SQL_NO_CACHE
,
"sql_small_result"
:
UNUSED
,
"ssl"
:
UNUSED
,
"start"
:
START
,
"starting"
:
UNUSED
,
"status"
:
STATUS
,
"engine"
:
ENGINE
,
"stored"
:
UNUSED
,
"straight_join"
:
STRAIGHT_JOIN
,
"table"
:
TABLE
,
"tables"
:
TABLES
,
"terminated"
:
UNUSED
,
"text"
:
TEXT
,
"than"
:
THAN
,
"then"
:
THEN
,
"time"
:
TIME
,
"timestamp"
:
TIMESTAMP
,
"tinyblob"
:
TINYBLOB
,
"tinyint"
:
TINYINT
,
"tinytext"
:
TINYTEXT
,
"to"
:
TO
,
"trailing"
:
UNUSED
,
"transaction"
:
TRANSACTION
,
"trigger"
:
TRIGGER
,
"true"
:
TRUE
,
"truncate"
:
TRUNCATE
,
"undo"
:
UNUSED
,
"union"
:
UNION
,
"unique"
:
UNIQUE
,
"unlock"
:
UNUSED
,
"unsigned"
:
UNSIGNED
,
"update"
:
UPDATE
,
"usage"
:
UNUSED
,
"use"
:
USE
,
"using"
:
USING
,
"utc_date"
:
UTC_DATE
,
"utc_time"
:
UTC_TIME
,
"utc_timestamp"
:
UTC_TIMESTAMP
,
"values"
:
VALUES
,
"variables"
:
VARIABLES
,
"varbinary"
:
VARBINARY
,
"varchar"
:
VARCHAR
,
"varcharacter"
:
UNUSED
,
"varying"
:
UNUSED
,
"virtual"
:
UNUSED
,
"vindex"
:
VINDEX
,
"vindexes"
:
VINDEXES
,
"view"
:
VIEW
,
"vitess_keyspaces"
:
VITESS_KEYSPACES
,
"vitess_shards"
:
VITESS_SHARDS
,
"vitess_tablets"
:
VITESS_TABLETS
,
"vschema_tables"
:
VSCHEMA_TABLES
,
"when"
:
WHEN
,
"where"
:
WHERE
,
"while"
:
UNUSED
,
"with"
:
WITH
,
"write"
:
UNUSED
,
"xor"
:
UNUSED
,
"year"
:
YEAR
,
"year_month"
:
UNUSED
,
"zerofill"
:
ZEROFILL
,
}
// keywordStrings contains the reverse mapping of token to keyword strings
var
keywordStrings
=
map
[
int
]
string
{}
func
init
() {
for
str
,
id
:=
range
keywords
{
if
id
==
UNUSED
{
continue
}
keywordStrings
[
id
]
=
str
}
}
// KeywordString returns the string corresponding to the given keyword
func
KeywordString
(
id
int
)
string
{
str
,
ok
:=
keywordStrings
[
id
]
if
!
ok
{
return
""
}
return
str
}
// Lex returns the next token form the Tokenizer.
// This function is used by go yacc.
func
(
tkn
*
Tokenizer
)
Lex
(
lval
*
yySymType
)
int
{
typ
,
val
:=
tkn
.
Scan
()
for
typ
==
COMMENT
{
if
tkn
.
AllowComments
{
break
}
typ
,
val
=
tkn
.
Scan
()
}
lval
.
bytes
=
val
tkn
.
lastToken
=
val
return
typ
}
// Error is called by go yacc if there's a parsing error.
func
(
tkn
*
Tokenizer
)
Error
(
err
string
) {
buf
:=
&
bytes2.
Buffer
{}
if
tkn
.
lastToken
!=
nil
{
fmt
.
Fprintf
(
buf
,
"%s at position %v near '%s'"
,
err
,
tkn
.
Position
,
tkn
.
lastToken
)
}
else
{
fmt
.
Fprintf
(
buf
,
"%s at position %v"
,
err
,
tkn
.
Position
)
}
tkn
.
LastError
=
errors
.
New
(
buf
.
String
())
// Try and re-sync to the next statement
if
tkn
.
lastChar
!=
';'
{
tkn
.
skipStatement
()
}
}
// Scan scans the tokenizer for the next token and returns
// the token type and an optional value.
func
(
tkn
*
Tokenizer
)
Scan
() (
int
, []
byte
) {
if
tkn
.
lastChar
==
0
{
tkn
.
next
()
}
if
tkn
.
ForceEOF
{
tkn
.
skipStatement
()
return
0
,
nil
}
tkn
.
skipBlank
()
switch
ch
:=
tkn
.
lastChar
; {
case
isLetter
(
ch
):
tkn
.
next
()
if
ch
==
'X'
||
ch
==
'x'
{
if
tkn
.
lastChar
==
'\''
{
tkn
.
next
()
return
tkn
.
scanHex
()
}
}
if
ch
==
'B'
||
ch
==
'b'
{
if
tkn
.
lastChar
==
'\''
{
tkn
.
next
()
return
tkn
.
scanBitLiteral
()
}
}
return
tkn
.
scanIdentifier
([]
byte
{
byte
(
ch
)})
case
ch
==
':'
:
return
tkn
.
scanBindVar
()
case
isDigit
(
ch
):
return
tkn
.
scanNumber
(
false
,
false
)
case
ch
==
';'
&&
tkn
.
multi
:
return
0
,
nil
default
:
tkn
.
next
()
switch
ch
{
case
eofChar
:
return
0
,
nil
case
'='
,
','
,
';'
,
'('
,
')'
,
'+'
,
'*'
,
'%'
,
'^'
,
'~'
:
return
int
(
ch
),
nil
case
'&'
:
if
tkn
.
lastChar
==
'&'
{
tkn
.
next
()
return
AND
,
nil
}
return
int
(
ch
),
nil
case
'|'
:
if
tkn
.
lastChar
==
'|'
{
tkn
.
next
()
return
OR
,
nil
}
return
int
(
ch
),
nil
case
'?'
:
tkn
.
posVarIndex
++
buf
:=
new
(bytes2.
Buffer
)
fmt
.
Fprintf
(
buf
,
":v%d"
,
tkn
.
posVarIndex
)
return
VALUE_ARG
,
buf
.
Bytes
()
case
'.'
:
if
isDigit
(
tkn
.
lastChar
) {
return
tkn
.
scanNumber
(
true
,
false
)
}
return
int
(
ch
),
nil
case
'/'
:
switch
tkn
.
lastChar
{
case
'/'
:
tkn
.
next
()
return
tkn
.
scanCommentType1
(
"//"
)
case
'*'
:
tkn
.
next
()
return
tkn
.
scanCommentType2
()
default
:
return
int
(
ch
),
nil
}
case
'#'
:
return
tkn
.
scanCommentType1
(
"#"
)
case
'-'
:
switch
tkn
.
lastChar
{
case
'-'
:
tkn
.
next
()
return
tkn
.
scanCommentType1
(
"--"
)
case
'>'
:
tkn
.
next
()
if
tkn
.
lastChar
==
'>'
{
tkn
.
next
()
return
JSON_UNQUOTE_EXTRACT_OP
,
nil
}
return
JSON_EXTRACT_OP
,
nil
case
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
:
return
tkn
.
scanNumber
(
false
,
true
)
}
return
int
(
ch
),
nil
case
'<'
:
switch
tkn
.
lastChar
{
case
'>'
:
tkn
.
next
()
return
NE
,
nil
case
'<'
:
tkn
.
next
()
return
SHIFT_LEFT
,
nil
case
'='
:
tkn
.
next
()
switch
tkn
.
lastChar
{
case
'>'
:
tkn
.
next
()
return
NULL_SAFE_EQUAL
,
nil
default
:
return
LE
,
nil
}
default
:
return
int
(
ch
),
nil
}
case
'>'
:
switch
tkn
.
lastChar
{
case
'='
:
tkn
.
next
()
return
GE
,
nil
case
'>'
:
tkn
.
next
()
return
SHIFT_RIGHT
,
nil
default
:
return
int
(
ch
),
nil
}
case
'!'
:
if
tkn
.
lastChar
==
'='
{
tkn
.
next
()
return
NE
,
nil
}
return
int
(
ch
),
nil
case
'\''
,
'"'
:
return
tkn
.
scanString
(
ch
,
STRING
)
case
'`'
:
return
tkn
.
scanLiteralIdentifier
()
default
:
return
LEX_ERROR
, []
byte
{
byte
(
ch
)}
}
}
}
// skipStatement scans until the EOF, or end of statement is encountered.
func
(
tkn
*
Tokenizer
)
skipStatement
() {
ch
:=
tkn
.
lastChar
for
ch
!=
';'
&&
ch
!=
eofChar
{
tkn
.
next
()
ch
=
tkn
.
lastChar
}
}
func
(
tkn
*
Tokenizer
)
skipBlank
() {
ch
:=
tkn
.
lastChar
for
ch
==
' '
||
ch
==
'\n'
||
ch
==
'\r'
||
ch
==
'\t'
{
tkn
.
next
()
ch
=
tkn
.
lastChar
}
}
func
(
tkn
*
Tokenizer
)
scanIdentifier
(
lastBytes
[]
byte
) (
int
, []
byte
) {
buffer
:=
bytes2
.
NewBuffer
(
lastBytes
)
for
isLetter
(
tkn
.
lastChar
)
||
isDigit
(
tkn
.
lastChar
) {
buffer
.
WriteByte
(
byte
(
tkn
.
lastChar
))
tkn
.
next
()
}
lowered
:=
bytes
.
ToLower
(
buffer
.
Bytes
())
loweredStr
:=
string
(
lowered
)
if
keywordID
,
found
:=
keywords
[
loweredStr
];
found
{
return
keywordID
,
lowered
}
// dual must always be case-insensitive
if
loweredStr
==
"dual"
{
return
ID
,
lowered
}
return
ID
,
buffer
.
Bytes
()
}
func
(
tkn
*
Tokenizer
)
scanHex
() (
int
, []
byte
) {
buffer
:=
&
bytes2.
Buffer
{}
tkn
.
scanMantissa
(
16
,
buffer
)
if
tkn
.
lastChar
!=
'\''
{
return
LEX_ERROR
,
buffer
.
Bytes
()
}
tkn
.
next
()
if
buffer
.
Len
()
%
2
!=
0
{
return
LEX_ERROR
,
buffer
.
Bytes
()
}
return
HEX
,
buffer
.
Bytes
()
}
func
(
tkn
*
Tokenizer
)
scanBitLiteral
() (
int
, []
byte
) {
buffer
:=
&
bytes2.
Buffer
{}
tkn
.
scanMantissa
(
2
,
buffer
)
if
tkn
.
lastChar
!=
'\''
{
return
LEX_ERROR
,
buffer
.
Bytes
()
}
tkn
.
next
()
return
BIT_LITERAL
,
buffer
.
Bytes
()
}
func
(
tkn
*
Tokenizer
)
scanLiteralIdentifier
() (
int
, []
byte
) {
buffer
:=
&
bytes2.
Buffer
{}
backTickSeen
:=
false
for
{
if
backTickSeen
{
if
tkn
.
lastChar
!=
'`'
{
break
}
backTickSeen
=
false
buffer
.
WriteByte
(
'`'
)
tkn
.
next
()
continue
}
// The previous char was not a backtick.
switch
tkn
.
lastChar
{
case
'`'
:
backTickSeen
=
true
case
eofChar
:
// Premature EOF.
return
LEX_ERROR
,
buffer
.
Bytes
()
default
:
buffer
.
WriteByte
(
byte
(
tkn
.
lastChar
))
}
tkn
.
next
()
}
if
buffer
.
Len
()
==
0
{
return
LEX_ERROR
,
buffer
.
Bytes
()
}
return
ID
,
buffer
.
Bytes
()
}
func
(
tkn
*
Tokenizer
)
scanBindVar
() (
int
, []
byte
) {
buffer
:=
&
bytes2.
Buffer
{}
buffer
.
WriteByte
(
byte
(
tkn
.
lastChar
))
token
:=
VALUE_ARG
tkn
.
next
()
if
tkn
.
lastChar
==
':'
{
token
=
LIST_ARG
buffer
.
WriteByte
(
byte
(
tkn
.
lastChar
))
tkn
.
next
()
}
if
!
isLetter
(
tkn
.
lastChar
) {
return
LEX_ERROR
,
buffer
.
Bytes
()
}
for
isLetter
(
tkn
.
lastChar
)
||
isDigit
(
tkn
.
lastChar
)
||
tkn
.
lastChar
==
'.'
{
buffer
.
WriteByte
(
byte
(
tkn
.
lastChar
))
tkn
.
next
()
}
return
token
,
buffer
.
Bytes
()
}
func
(
tkn
*
Tokenizer
)
scanMantissa
(
base
int
,
buffer
*
bytes2.
Buffer
) {
for
digitVal
(
tkn
.
lastChar
)
<
base
{
tkn
.
consumeNext
(
buffer
)
}
}
func
(
tkn
*
Tokenizer
)
scanNumber
(
seenDecimalPoint
,
isNegative
bool
) (
int
, []
byte
) {
token
:=
INTEGRAL
buffer
:=
&
bytes2.
Buffer
{}
if
isNegative
{
buffer
.
WriteByte
(
'-'
)
}
if
seenDecimalPoint
{
token
=
FLOAT
buffer
.
WriteByte
(
'.'
)
tkn
.
scanMantissa
(
10
,
buffer
)
goto
exponent
}
// 0x construct.
if
tkn
.
lastChar
==
'0'
{
tkn
.
consumeNext
(
buffer
)
if
tkn
.
lastChar
==
'x'
||
tkn
.
lastChar
==
'X'
{
token
=
HEXNUM
tkn
.
consumeNext
(
buffer
)
tkn
.
scanMantissa
(
16
,
buffer
)
goto
exit
}
}
tkn
.
scanMantissa
(
10
,
buffer
)
if
tkn
.
lastChar
==
'.'
{
token
=
FLOAT
tkn
.
consumeNext
(
buffer
)
tkn
.
scanMantissa
(
10
,
buffer
)
}
exponent:
if
tkn
.
lastChar
==
'e'
||
tkn
.
lastChar
==
'E'
{
token
=
FLOAT
tkn
.
consumeNext
(
buffer
)
if
tkn
.
lastChar
==
'+'
||
tkn
.
lastChar
==
'-'
{
tkn
.
consumeNext
(
buffer
)
}
tkn
.
scanMantissa
(
10
,
buffer
)
}
exit:
// A letter cannot immediately follow a number.
if
isLetter
(
tkn
.
lastChar
) {
return
tkn
.
scanIdentifier
(
buffer
.
Bytes
())
}
return
token
,
buffer
.
Bytes
()
}
func
(
tkn
*
Tokenizer
)
scanString
(
delim
uint16
,
typ
int
) (
int
, []
byte
) {
var
buffer
bytes2.
Buffer
for
{
ch
:=
tkn
.
lastChar
if
ch
==
eofChar
{
// Unterminated string.
return
LEX_ERROR
,
buffer
.
Bytes
()
}
if
ch
!=
delim
&&
ch
!=
'\\'
{
buffer
.
WriteByte
(
byte
(
ch
))
// Scan ahead to the next interesting character.
start
:=
tkn
.
bufPos
for
;
tkn
.
bufPos
<
tkn
.
bufSize
;
tkn
.
bufPos
++
{
ch
=
uint16
(
tkn
.
buf
[
tkn
.
bufPos
])
if
ch
==
delim
||
ch
==
'\\'
{
break
}
}
buffer
.
Write
(
tkn
.
buf
[
start
:
tkn
.
bufPos
])
tkn
.
Position
+=
(
tkn
.
bufPos
-
start
)
if
tkn
.
bufPos
>=
tkn
.
bufSize
{
// Reached the end of the buffer without finding a delim or
// escape character.
tkn
.
next
()
continue
}
tkn
.
bufPos
++
tkn
.
Position
++
}
tkn
.
next
()
// Read one past the delim or escape character.
if
ch
==
'\\'
{
if
tkn
.
lastChar
==
eofChar
{
// String terminates mid escape character.
return
LEX_ERROR
,
buffer
.
Bytes
()
}
if
decodedChar
:=
sqltypes
.
SQLDecodeMap
[
byte
(
tkn
.
lastChar
)];
decodedChar
==
sqltypes
.
DontEscape
{
ch
=
tkn
.
lastChar
}
else
{
ch
=
uint16
(
decodedChar
)
}
}
else
if
ch
==
delim
&&
tkn
.
lastChar
!=
delim
{
// Correctly terminated string, which is not a double delim.
break
}
buffer
.
WriteByte
(
byte
(
ch
))
tkn
.
next
()
}
return
typ
,
buffer
.
Bytes
()
}
func
(
tkn
*
Tokenizer
)
scanCommentType1
(
prefix
string
) (
int
, []
byte
) {
buffer
:=
&
bytes2.
Buffer
{}
buffer
.
WriteString
(
prefix
)
for
tkn
.
lastChar
!=
eofChar
{
if
tkn
.
lastChar
==
'\n'
{
tkn
.
consumeNext
(
buffer
)
break
}
tkn
.
consumeNext
(
buffer
)
}
return
COMMENT
,
buffer
.
Bytes
()
}
func
(
tkn
*
Tokenizer
)
scanCommentType2
() (
int
, []
byte
) {
buffer
:=
&
bytes2.
Buffer
{}
buffer
.
WriteString
(
"/*"
)
for
{
if
tkn
.
lastChar
==
'*'
{
tkn
.
consumeNext
(
buffer
)
if
tkn
.
lastChar
==
'/'
{
tkn
.
consumeNext
(
buffer
)
break
}
continue
}
if
tkn
.
lastChar
==
eofChar
{
return
LEX_ERROR
,
buffer
.
Bytes
()
}
tkn
.
consumeNext
(
buffer
)
}
return
COMMENT
,
buffer
.
Bytes
()
}
func
(
tkn
*
Tokenizer
)
consumeNext
(
buffer
*
bytes2.
Buffer
) {
if
tkn
.
lastChar
==
eofChar
{
// This should never happen.
panic
(
"unexpected EOF"
)
}
buffer
.
WriteByte
(
byte
(
tkn
.
lastChar
))
tkn
.
next
()
}
func
(
tkn
*
Tokenizer
)
next
() {
if
tkn
.
bufPos
>=
tkn
.
bufSize
&&
tkn
.
InStream
!=
nil
{
// Try and refill the buffer
var
err
error
tkn
.
bufPos
=
0
if
tkn
.
bufSize
,
err
=
tkn
.
InStream
.
Read
(
tkn
.
buf
);
err
!=
io
.
EOF
&&
err
!=
nil
{
tkn
.
LastError
=
err
}
}
if
tkn
.
bufPos
>=
tkn
.
bufSize
{
if
tkn
.
lastChar
!=
eofChar
{
tkn
.
Position
++
tkn
.
lastChar
=
eofChar
}
}
else
{
tkn
.
Position
++
tkn
.
lastChar
=
uint16
(
tkn
.
buf
[
tkn
.
bufPos
])
tkn
.
bufPos
++
}
}
// reset clears any internal state.
func
(
tkn
*
Tokenizer
)
reset
() {
tkn
.
ParseTree
=
nil
tkn
.
partialDDL
=
nil
tkn
.
posVarIndex
=
0
tkn
.
nesting
=
0
tkn
.
ForceEOF
=
false
}
func
isLetter
(
ch
uint16
)
bool
{
return
'a'
<=
ch
&&
ch
<=
'z'
||
'A'
<=
ch
&&
ch
<=
'Z'
||
ch
==
'_'
||
ch
==
'@'
}
func
digitVal
(
ch
uint16
)
int
{
switch
{
case
'0'
<=
ch
&&
ch
<=
'9'
:
return
int
(
ch
)
-
'0'
case
'a'
<=
ch
&&
ch
<=
'f'
:
return
int
(
ch
)
-
'a'
+
10
case
'A'
<=
ch
&&
ch
<=
'F'
:
return
int
(
ch
)
-
'A'
+
10
}
return
16
// larger than any legal digit val
}
func
isDigit
(
ch
uint16
)
bool
{
return
'0'
<=
ch
&&
ch
<=
'9'
//return '0' <= ch && ch <= '9' || ch == '-' || ch == '.'
}
Back
|
FazBrowse Home
|
New Git URL