Repository navigation
Expand file tree
/
Copy pathtokenizer_core.py
More file actions
1219 lines (1103 loc) · 35.1 KB
/
Copy pathtokenizer_core.py
File metadata and controls
1219 lines (1103 loc) · 35.1 KB
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import typing as t
from enum import IntEnum, auto
from sqlglot.errors import TokenError
# dict lookup is faster than .upper() and .isdigit()
_CHAR_UPPER: dict[str, str] = {chr(i): chr(i).upper() for i in range(97, 123)}
_DIGIT_CHARS: frozenset[str] = frozenset("0123456789")
class TokenType(IntEnum):
L_PAREN = auto()
R_PAREN = auto()
L_BRACKET = auto()
R_BRACKET = auto()
L_BRACE = auto()
R_BRACE = auto()
COMMA = auto()
DOT = auto()
DASH = auto()
PLUS = auto()
COLON = auto()
DOTCOLON = auto()
DOTCARET = auto()
DCOLON = auto()
DCOLONDOLLAR = auto()
DCOLONPERCENT = auto()
DCOLONQMARK = auto()
DQMARK = auto()
SEMICOLON = auto()
STAR = auto()
BACKSLASH = auto()
SLASH = auto()
LT = auto()
LTE = auto()
GT = auto()
GTE = auto()
NOT = auto()
EQ = auto()
NEQ = auto()
NULLSAFE_EQ = auto()
COLON_EQ = auto()
COLON_GT = auto()
NCOLON_GT = auto()
AND = auto()
OR = auto()
AMP = auto()
DPIPE = auto()
PIPE_GT = auto()
PIPE = auto()
PIPE_SLASH = auto()
DPIPE_SLASH = auto()
CARET = auto()
CARET_AT = auto()
TILDE = auto()
ARROW = auto()
DARROW = auto()
FARROW = auto()
HASH = auto()
HASH_ARROW = auto()
DHASH_ARROW = auto()
LR_ARROW = auto()
LLRR_ARROW = auto()
DAT = auto()
AT_QMARK = auto()
LT_AT = auto()
AT_GT = auto()
DOLLAR = auto()
PARAMETER = auto()
SESSION = auto()
SESSION_PARAMETER = auto()
SESSION_USER = auto()
DAMP = auto()
AMP_LT = auto()
AMP_GT = auto()
ADJACENT = auto()
XOR = auto()
DSTAR = auto()
QMARK_AMP = auto()
QMARK_PIPE = auto()
HASH_DASH = auto()
EXCLAMATION = auto()
URI_START = auto()
BLOCK_START = auto()
BLOCK_END = auto()
SPACE = auto()
BREAK = auto()
STRING = auto()
NUMBER = auto()
IDENTIFIER = auto()
DATABASE = auto()
COLUMN = auto()
COLUMN_DEF = auto()
SCHEMA = auto()
TABLE = auto()
WAREHOUSE = auto()
STAGE = auto()
STREAM = auto()
STREAMLIT = auto()
VAR = auto()
BIT_STRING = auto()
HEX_STRING = auto()
BYTE_STRING = auto()
NATIONAL_STRING = auto()
RAW_STRING = auto()
HEREDOC_STRING = auto()
UNICODE_STRING = auto()
# types
BIT = auto()
BOOLEAN = auto()
TINYINT = auto()
UTINYINT = auto()
SMALLINT = auto()
USMALLINT = auto()
MEDIUMINT = auto()
UMEDIUMINT = auto()
INT = auto()
UINT = auto()
BIGINT = auto()
UBIGINT = auto()
BIGNUM = auto()
INT128 = auto()
UINT128 = auto()
INT256 = auto()
UINT256 = auto()
FLOAT = auto()
DOUBLE = auto()
UDOUBLE = auto()
DECIMAL = auto()
DECIMAL32 = auto()
DECIMAL64 = auto()
DECIMAL128 = auto()
DECIMAL256 = auto()
DECFLOAT = auto()
UDECIMAL = auto()
BIGDECIMAL = auto()
CHAR = auto()
NCHAR = auto()
VARCHAR = auto()
NVARCHAR = auto()
BPCHAR = auto()
TEXT = auto()
MEDIUMTEXT = auto()
LONGTEXT = auto()
BLOB = auto()
MEDIUMBLOB = auto()
LONGBLOB = auto()
TINYBLOB = auto()
TINYTEXT = auto()
NAME = auto()
BINARY = auto()
VARBINARY = auto()
JSON = auto()
JSONB = auto()
TIME = auto()
TIMETZ = auto()
TIME_NS = auto()
TIMESTAMP = auto()
TIMESTAMPTZ = auto()
TIMESTAMPLTZ = auto()
TIMESTAMPNTZ = auto()
TIMESTAMP_S = auto()
TIMESTAMP_MS = auto()
TIMESTAMP_NS = auto()
DATETIME = auto()
DATETIME2 = auto()
DATETIME64 = auto()
SMALLDATETIME = auto()
DATE = auto()
DATE32 = auto()
INT4RANGE = auto()
INT4MULTIRANGE = auto()
INT8RANGE = auto()
INT8MULTIRANGE = auto()
NUMRANGE = auto()
NUMMULTIRANGE = auto()
TSRANGE = auto()
TSMULTIRANGE = auto()
TSTZRANGE = auto()
TSTZMULTIRANGE = auto()
DATERANGE = auto()
DATEMULTIRANGE = auto()
UUID = auto()
GEOGRAPHY = auto()
GEOGRAPHYPOINT = auto()
NULLABLE = auto()
GEOMETRY = auto()
POINT = auto()
RING = auto()
LINESTRING = auto()
LOCALTIME = auto()
LOCALTIMESTAMP = auto()
SYSTIMESTAMP = auto()
MULTILINESTRING = auto()
POLYGON = auto()
MULTIPOLYGON = auto()
HLLSKETCH = auto()
HSTORE = auto()
SUPER = auto()
SERIAL = auto()
SMALLSERIAL = auto()
BIGSERIAL = auto()
XML = auto()
YEAR = auto()
USERDEFINED = auto()
MONEY = auto()
SMALLMONEY = auto()
ROWVERSION = auto()
IMAGE = auto()
VARIANT = auto()
OBJECT = auto()
INET = auto()
IPADDRESS = auto()
IPPREFIX = auto()
IPV4 = auto()
IPV6 = auto()
ENUM = auto()
ENUM8 = auto()
ENUM16 = auto()
FIXEDSTRING = auto()
LOWCARDINALITY = auto()
NESTED = auto()
AGGREGATEFUNCTION = auto()
SIMPLEAGGREGATEFUNCTION = auto()
TDIGEST = auto()
UNKNOWN = auto()
VECTOR = auto()
DYNAMIC = auto()
VOID = auto()
# keywords
ALIAS = auto()
ALTER = auto()
ALL = auto()
ANTI = auto()
ANY = auto()
APPLY = auto()
ARRAY = auto()
ASC = auto()
ASOF = auto()
ATTACH = auto()
AUTO_INCREMENT = auto()
BEGIN = auto()
BETWEEN = auto()
BULK_COLLECT_INTO = auto()
CACHE = auto()
CASE = auto()
CHARACTER_SET = auto()
CLUSTER_BY = auto()
COLLATE = auto()
COMMAND = auto()
COMMENT = auto()
COMMIT = auto()
CONNECT_BY = auto()
CONSTRAINT = auto()
COPY = auto()
CREATE = auto()
CROSS = auto()
CUBE = auto()
CURRENT_DATE = auto()
CURRENT_DATETIME = auto()
CURRENT_SCHEMA = auto()
CURRENT_TIME = auto()
CURRENT_TIMESTAMP = auto()
CURRENT_USER = auto()
CURRENT_USER_ID = auto()
CURRENT_ROLE = auto()
CURRENT_CATALOG = auto()
DECLARE = auto()
DEFAULT = auto()
DELETE = auto()
DESC = auto()
DESCRIBE = auto()
DETACH = auto()
DICTIONARY = auto()
DISTINCT = auto()
DISTRIBUTE_BY = auto()
DIV = auto()
DROP = auto()
ELSE = auto()
END = auto()
ESCAPE = auto()
EXCEPT = auto()
EXECUTE = auto()
EXISTS = auto()
FALSE = auto()
FETCH = auto()
FILE = auto()
FILE_FORMAT = auto()
FILTER = auto()
FINAL = auto()
FIRST = auto()
FOR = auto()
FORCE = auto()
FOREIGN_KEY = auto()
FORMAT = auto()
FROM = auto()
FULL = auto()
FUNCTION = auto()
GET = auto()
GLOB = auto()
GLOBAL = auto()
GRANT = auto()
GROUP_BY = auto()
GROUPING_SETS = auto()
HAVING = auto()
HINT = auto()
IGNORE = auto()
ILIKE = auto()
IN = auto()
INDEX = auto()
INDEXED_BY = auto()
INNER = auto()
INSERT = auto()
INSTALL = auto()
INTEGRATION = auto()
INTERSECT = auto()
INTERVAL = auto()
INTO = auto()
INTRODUCER = auto()
IRLIKE = auto()
IS = auto()
ISNULL = auto()
JOIN = auto()
JOIN_MARKER = auto()
KEEP = auto()
KEY = auto()
KILL = auto()
LANGUAGE = auto()
LATERAL = auto()
LEFT = auto()
LIKE = auto()
LIMIT = auto()
LIST = auto()
LOAD = auto()
LOCK = auto()
MAP = auto()
MATCH = auto()
MATCH_CONDITION = auto()
MATCH_RECOGNIZE = auto()
MEMBER_OF = auto()
MERGE = auto()
MOD = auto()
MODEL = auto()
NATURAL = auto()
NEXT = auto()
NOTHING = auto()
NOTNULL = auto()
NULL = auto()
OBJECT_IDENTIFIER = auto()
OFFSET = auto()
ON = auto()
ONLY = auto()
OPERATOR = auto()
ORDER_BY = auto()
ORDER_SIBLINGS_BY = auto()
ORDERED = auto()
ORDINALITY = auto()
OUT = auto()
INOUT = auto()
OUTER = auto()
OVER = auto()
OVERLAPS = auto()
OVERWRITE = auto()
PACKAGE = auto()
PARTITION = auto()
PARTITION_BY = auto()
PERCENT = auto()
PIVOT = auto()
PLACEHOLDER = auto()
POLICY = auto()
POOL = auto()
POSITIONAL = auto()
PRAGMA = auto()
PREWHERE = auto()
PRIMARY_KEY = auto()
PROCEDURE = auto()
PROPERTIES = auto()
PROJECTION = auto()
PSEUDO_TYPE = auto()
PUT = auto()
QUALIFY = auto()
QUOTE = auto()
QDCOLON = auto()
RANGE = auto()
RECURSIVE = auto()
REFRESH = auto()
RENAME = auto()
REPLACE = auto()
RETURNING = auto()
REVOKE = auto()
REFERENCES = auto()
RIGHT = auto()
RLIKE = auto()
ROLE = auto()
ROLLBACK = auto()
ROLLUP = auto()
ROW = auto()
ROWS = auto()
RULE = auto()
SELECT = auto()
SEMI = auto()
SEPARATOR = auto()
SEQUENCE = auto()
SERDE_PROPERTIES = auto()
SET = auto()
SETTINGS = auto()
SHOW = auto()
SIMILAR_TO = auto()
SOME = auto()
SORT_BY = auto()
SOUNDS_LIKE = auto()
SQL_SECURITY = auto()
START_WITH = auto()
STORAGE_INTEGRATION = auto()
STRAIGHT_JOIN = auto()
STRUCT = auto()
SUMMARIZE = auto()
TABLE_SAMPLE = auto()
TAG = auto()
TEMPORARY = auto()
TOP = auto()
THEN = auto()
TRUE = auto()
TRUNCATE = auto()
TRIGGER = auto()
TYPE = auto()
UNCACHE = auto()
UNDROP = auto()
UNION = auto()
UNNEST = auto()
UNPIVOT = auto()
UPDATE = auto()
USE = auto()
USING = auto()
VALUES = auto()
VARIADIC = auto()
VIEW = auto()
SEMANTIC_VIEW = auto()
VOLATILE = auto()
VOLUME = auto()
WHEN = auto()
WHERE = auto()
WINDOW = auto()
WITH = auto()
UNIQUE = auto()
UTC_DATE = auto()
UTC_TIME = auto()
UTC_TIMESTAMP = auto()
VERSION_SNAPSHOT = auto()
TIMESTAMP_SNAPSHOT = auto()
OPTION = auto()
SINK = auto()
SOURCE = auto()
ANALYZE = auto()
NAMESPACE = auto()
EXPORT = auto()
# sentinels
HIVE_TOKEN_STREAM = auto()
SENTINEL = auto()
def __str__(self) -> str:
return f"TokenType.{self.name}"
class Token:
# mypyc doesn't expose slots
_attrs: t.ClassVar[tuple[str, ...]] = (
"token_type",
"text",
"line",
"col",
"start",
"end",
"comments",
)
__slots__ = _attrs
@classmethod
def number(cls, number: int) -> Token:
"""Returns a NUMBER token with `number` as its text."""
return cls(TokenType.NUMBER, str(number))
@classmethod
def string(cls, string: str) -> Token:
"""Returns a STRING token with `string` as its text."""
return cls(TokenType.STRING, string)
@classmethod
def identifier(cls, identifier: str) -> Token:
"""Returns an IDENTIFIER token with `identifier` as its text."""
return cls(TokenType.IDENTIFIER, identifier)
@classmethod
def var(cls, var: str) -> Token:
"""Returns an VAR token with `var` as its text."""
return cls(TokenType.VAR, var)
def __init__(
self,
token_type: TokenType,
text: str,
line: int = 1,
col: int = 1,
start: int = 0,
end: int = 0,
comments: list[str] | None = None,
) -> None:
self.token_type = token_type
self.text = text
self.line = line
self.col = col
self.start = start
self.end = end
self.comments = [] if comments is None else comments
def __bool__(self) -> bool:
return self.token_type != TokenType.SENTINEL
def __repr__(self) -> str:
attributes = ", ".join(
f"{k}: TokenType.{self.token_type.name}"
if k == "token_type"
else f"{k}: {getattr(self, k)}"
for k in self._attrs
)
return f"<Token {attributes}>"
class TokenizerCore:
__slots__ = (
"sql",
"size",
"tokens",
"_start",
"_current",
"_line",
"_col",
"_comments",
"_char",
"_end",
"_peek",
"_prev_token_line",
"single_tokens",
"keywords",
"quotes",
"format_strings",
"identifiers",
"comments",
"string_escapes",
"byte_string_escapes",
"identifier_escapes",
"escape_follow_chars",
"commands",
"command_prefix_tokens",
"nested_comments",
"hint_start",
"tokens_preceding_hint",
"has_bit_strings",
"has_hex_strings",
"numeric_literals",
"var_single_tokens",
"string_escapes_allowed_in_raw_strings",
"heredoc_tag_is_identifier",
"heredoc_string_alternative",
"keyword_trie",
"numbers_can_be_underscore_separated",
"numbers_can_have_decimals",
"identifiers_can_start_with_digit",
"unescaped_sequences",
)
def __init__(
self,
single_tokens: dict[str, TokenType],
keywords: dict[str, TokenType],
quotes: dict[str, str],
format_strings: dict[str, tuple[str, TokenType]],
identifiers: dict[str, str],
comments: dict[str, str | None],
string_escapes: set[str],
byte_string_escapes: set[str],
identifier_escapes: set[str],
escape_follow_chars: set[str],
commands: set[TokenType],
command_prefix_tokens: set[TokenType],
nested_comments: bool,
hint_start: str,
tokens_preceding_hint: set[TokenType],
has_bit_strings: bool,
has_hex_strings: bool,
numeric_literals: dict[str, str],
var_single_tokens: set[str],
string_escapes_allowed_in_raw_strings: bool,
heredoc_tag_is_identifier: bool,
heredoc_string_alternative: TokenType,
keyword_trie: dict,
numbers_can_be_underscore_separated: bool,
numbers_can_have_decimals: bool,
identifiers_can_start_with_digit: bool,
unescaped_sequences: dict[str, str],
) -> None:
self.single_tokens = single_tokens
self.keywords = keywords
self.quotes = quotes
self.format_strings = format_strings
self.identifiers = identifiers
self.comments = comments
self.string_escapes = string_escapes
self.byte_string_escapes = byte_string_escapes
self.identifier_escapes = identifier_escapes
self.escape_follow_chars = escape_follow_chars
self.commands = commands
self.command_prefix_tokens = command_prefix_tokens
self.nested_comments = nested_comments
self.hint_start = hint_start
self.tokens_preceding_hint = tokens_preceding_hint
self.has_bit_strings = has_bit_strings
self.has_hex_strings = has_hex_strings
self.numeric_literals = numeric_literals
self.var_single_tokens = var_single_tokens
self.string_escapes_allowed_in_raw_strings = string_escapes_allowed_in_raw_strings
self.heredoc_tag_is_identifier = heredoc_tag_is_identifier
self.heredoc_string_alternative = heredoc_string_alternative
self.keyword_trie = keyword_trie
self.numbers_can_be_underscore_separated = numbers_can_be_underscore_separated
self.numbers_can_have_decimals = numbers_can_have_decimals
self.identifiers_can_start_with_digit = identifiers_can_start_with_digit
self.unescaped_sequences = unescaped_sequences
self.sql = ""
self.size = 0
self.tokens: list[Token] = []
self._start = 0
self._current = 0
self._line = 1
self._col = 0
self._comments: list[str] = []
self._char = ""
self._end = False
self._peek = ""
self._prev_token_line = -1
def reset(self) -> None:
self.sql = ""
self.size = 0
self.tokens = []
self._start = 0
self._current = 0
self._line = 1
self._col = 0
self._comments = []
self._char = ""
self._end = False
self._peek = ""
self._prev_token_line = -1
def tokenize(self, sql: str) -> list[Token]:
"""Returns a list of tokens corresponding to the SQL string `sql`."""
self.reset()
self.sql = sql
self.size = len(sql)
try:
self._scan()
except Exception as e:
start = max(self._current - 50, 0)
end = min(self._current + 50, self.size - 1)
context = self.sql[start:end]
raise TokenError(f"Error tokenizing '{context}'") from e
return self.tokens
def _scan(self, check_semicolon: bool = False) -> None:
identifiers = self.identifiers
digit_chars = _DIGIT_CHARS
while self.size and not self._end:
current = self._current
# Skip spaces here rather than iteratively calling advance() for performance reasons
while current < self.size:
char = self.sql[current]
if char == " " or char == "\t":
current += 1
else:
break
offset = current - self._current if current > self._current else 1
self._start = current
self._advance(offset)
if not self._char.isspace():
if self._char in digit_chars:
self._scan_number()
elif self._char in identifiers:
self._scan_identifier(identifiers[self._char])
else:
self._scan_keywords()
if check_semicolon and self._peek == ";":
break
if self.tokens and self._comments:
self.tokens[-1].comments.extend(self._comments)
def _chars(self, size: int) -> str:
if size == 1:
return self._char
start = self._current - 1
end = start + size
return self.sql[start:end] if end <= self.size else ""
def _advance(self, i: int = 1, alnum: bool = False) -> None:
char = self._char
if char == "\n" or char == "\r":
# Ensures we don't count an extra line if we get a \r\n line break sequence
if not (char == "\r" and self._peek == "\n"):
self._col = i
self._line += 1
else:
self._col += i
self._current += i
sql = self.sql
size = self.size
self._end = self._current >= size
self._char = sql[self._current - 1]
self._peek = "" if self._end else sql[self._current]
if alnum and self._char.isalnum():
# Cache to local variables instead of attributes for better performance
_col = self._col
_current = self._current
_end = self._end
_peek = self._peek
while _peek.isalnum():
_col += 1
_current += 1
_end = _current >= size
_peek = "" if _end else sql[_current]
self._col = _col
self._current = _current
self._end = _end
self._peek = _peek
self._char = sql[_current - 1]
@property
def _text(self) -> str:
return self.sql[self._start : self._current]
def _add(self, token_type: TokenType, text: str | None = None) -> None:
self._prev_token_line = self._line
if self._comments and token_type == TokenType.SEMICOLON and self.tokens:
self.tokens[-1].comments.extend(self._comments)
self._comments = []
if text is None:
text = self.sql[self._start : self._current]
self.tokens.append(
Token(
token_type,
text=text,
line=self._line,
col=self._col,
start=self._start,
end=self._current - 1,
comments=self._comments,
)
)
self._comments = []
# If we have either a semicolon or a begin token before the command's token, we'll parse
# whatever follows the command's token as a string
if (
token_type in self.commands
and self._peek != ";"
and (len(self.tokens) == 1 or self.tokens[-2].token_type in self.command_prefix_tokens)
):
start = self._current
tokens = len(self.tokens)
self._scan(check_semicolon=True)
self.tokens = self.tokens[:tokens]
text = self.sql[start : self._current].strip()
if text:
self._add(TokenType.STRING, text)
def _scan_keywords(self) -> None:
sql = self.sql
sql_size = self.size
single_tokens = self.single_tokens
char_upper = _CHAR_UPPER
size = 0
word = None
chars = self._char
char = chars
prev_space = False
skip = False
trie = self.keyword_trie
single_token = char in single_tokens
while chars:
if not skip:
sub = trie.get(char_upper.get(char, char))
if sub is None:
break
trie = sub
if 0 in trie:
word = chars
end = self._current + size
size += 1
if end < sql_size:
char = sql[end]
single_token = single_token or char in single_tokens
is_space = char.isspace()
if not is_space or not prev_space:
if is_space:
char = " "
chars += char
prev_space = is_space
skip = False
else:
skip = True
else:
char = ""
break
if word:
if self._scan_string(word):
return
if self._scan_comment(word):
return
if prev_space or single_token or not char:
self._advance(size - 1)
word = word.upper()
self._add(self.keywords[word], text=word)
return
if self._char in single_tokens:
self._add(single_tokens[self._char], text=self._char)
return
self._scan_var()
def _scan_comment(self, comment_start: str) -> bool:
if comment_start not in self.comments:
return False
comment_start_line = self._line
comment_start_size = len(comment_start)
comment_end = self.comments[comment_start]
if comment_end:
# Skip the comment's start delimiter
self._advance(comment_start_size)
comment_count = 1
comment_end_size = len(comment_end)
nested_comments = self.nested_comments
while not self._end:
if self._chars(comment_end_size) == comment_end:
comment_count -= 1
if not comment_count:
break
self._advance(alnum=True)
# Nested comments are allowed by some dialects, e.g. databricks, duckdb, postgres
if (
nested_comments
and not self._end
and self._chars(comment_end_size) == comment_start
):
self._advance(comment_start_size)
comment_count += 1
self._comments.append(self._text[comment_start_size : -comment_end_size + 1])
self._advance(comment_end_size - 1)
else:
_peek = self._peek
while not self._end and _peek != "\n" and _peek != "\r":
self._advance(alnum=True)
_peek = self._peek
self._comments.append(self._text[comment_start_size:])
if (
comment_start == self.hint_start
and self.tokens
and self.tokens[-1].token_type in self.tokens_preceding_hint
):
self._add(TokenType.HINT)
# Leading comment is attached to the succeeding token, whilst trailing comment to the preceding.
# Multiple consecutive comments are preserved by appending them to the current comments list.
if comment_start_line == self._prev_token_line:
self.tokens[-1].comments.extend(self._comments)
self._comments = []
self._prev_token_line = self._line
return True
def _scan_number(self) -> None:
if self._char == "0":
peek = _CHAR_UPPER.get(self._peek, self._peek)
if peek == "B":
return self._scan_bits() if self.has_bit_strings else self._add(TokenType.NUMBER)
elif peek == "X":
return self._scan_hex() if self.has_hex_strings else self._add(TokenType.NUMBER)
decimal = False
scientific = 0
numbers_can_be_underscore_separated = self.numbers_can_be_underscore_separated
single_tokens = self.single_tokens
keywords = self.keywords
numeric_literals = self.numeric_literals
identifiers_can_start_with_digit = self.identifiers_can_start_with_digit
is_underscore_separated: bool = False
number_text: str = ""
numeric_literal: str = ""
numeric_type: TokenType | None = None
while True:
if self._peek in _DIGIT_CHARS:
# Batch consecutive digits: scan ahead to find how many
sql = self.sql
end = self._current + 1
size = self.size
while end < size and sql[end] in _DIGIT_CHARS:
end += 1
self._advance(end - self._current)
elif self._peek == "." and not decimal:
if (
self.tokens and self.tokens[-1].token_type == TokenType.PARAMETER
) or not self.numbers_can_have_decimals:
break
decimal = True
self._advance()
elif self._peek in ("-", "+") and scientific == 1:
# Only consume +/- if followed by a digit
if self._current + 1 < self.size and self.sql[self._current + 1] in _DIGIT_CHARS:
scientific += 1
self._advance()
else:
break
elif _CHAR_UPPER.get(self._peek, self._peek) == "E" and not scientific:
scientific += 1
self._advance()
elif self._peek == "_" and numbers_can_be_underscore_separated:
is_underscore_separated = True
self._advance()
elif self._peek.isidentifier():
number_text = self._text
while self._peek and not self._peek.isspace() and self._peek not in single_tokens:
numeric_literal += self._peek
self._advance()
numeric_type = keywords.get(numeric_literals.get(numeric_literal.upper(), ""))
if numeric_type:
break
elif identifiers_can_start_with_digit:
return self._add(TokenType.VAR)
self._advance(-len(numeric_literal))
break
else:
break
number_text = number_text or self.sql[self._start : self._current]
# Normalize inputs such as 100_000 to 100000
if is_underscore_separated:
number_text = number_text.replace("_", "")
self._add(TokenType.NUMBER, number_text)
# Normalize inputs such as 123L to 123::BIGINT so that they're parsed as casts
if numeric_type: