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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
//! SQL Lexer - Tokenization of SQL queries
//!
//! This module handles the conversion of raw SQL text into tokens
//! that can be consumed by the parser.
/// Lexer mode - controls whether comments are preserved or skipped
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LexerMode {
/// Standard mode - skip comments (current default behavior)
SkipComments,
/// Preserve mode - tokenize comments as tokens
PreserveComments,
}
impl Default for LexerMode {
fn default() -> Self {
LexerMode::SkipComments
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
// Keywords
Select,
From,
Where,
With, // WITH clause for CTEs
And,
Or,
In,
Not,
Between,
Like,
ILike, // Case-insensitive LIKE (PostgreSQL)
Is,
Null,
OrderBy,
GroupBy,
Having,
Qualify,
As,
Asc,
Desc,
Limit,
Offset,
Into, // INTO keyword for temporary tables
DateTime, // DateTime constructor
Case, // CASE expression
When, // WHEN clause
Then, // THEN clause
Else, // ELSE clause
End, // END keyword
Distinct, // DISTINCT keyword for aggregate functions
Over, // OVER keyword for window functions
Partition, // PARTITION keyword for window functions
By, // BY keyword (used with PARTITION BY, ORDER BY)
Exclude, // EXCLUDE keyword (for SELECT * EXCLUDE)
// Note: REPLACE is NOT a keyword - it's handled as a function name
// to avoid conflicting with the REPLACE() string function
// PIVOT/UNPIVOT keywords
Pivot, // PIVOT keyword for row-to-column transformation
Unpivot, // UNPIVOT keyword for column-to-row transformation
For, // FOR keyword (used in PIVOT: FOR column IN (...))
// Window frame keywords
Rows, // ROWS frame type
Range, // RANGE frame type
Unbounded, // UNBOUNDED for frame bounds
Preceding, // PRECEDING for frame bounds
Following, // FOLLOWING for frame bounds
Current, // CURRENT for CURRENT ROW
Row, // ROW for CURRENT ROW
// Set operation keywords
Union, // UNION
Intersect, // INTERSECT
Except, // EXCEPT
// Special CTE keywords
Web, // WEB (for WEB CTEs)
File, // FILE (for FILE CTEs — filesystem metadata)
// Row expansion functions
Unnest, // UNNEST (for expanding delimited strings into rows)
// JOIN keywords
Join, // JOIN keyword
Inner, // INNER JOIN
Left, // LEFT JOIN
Right, // RIGHT JOIN
Full, // FULL JOIN
Outer, // OUTER keyword (LEFT OUTER, RIGHT OUTER, FULL OUTER)
On, // ON keyword for join conditions
Cross, // CROSS JOIN
// Literals
Identifier(String),
QuotedIdentifier(String), // For "Customer Id" style identifiers
StringLiteral(String),
JsonBlock(String), // For $JSON$...$ JSON$ delimited blocks
NumberLiteral(String),
Star,
// Operators
Dot,
Comma,
Colon,
LeftParen,
RightParen,
Equal,
NotEqual,
LessThan,
GreaterThan,
LessThanOrEqual,
GreaterThanOrEqual,
// Arithmetic operators
Plus,
Minus,
Divide,
Modulo,
// String operators
Concat, // || for string concatenation
// Comments (preserved for formatting)
LineComment(String), // -- comment text (without the -- prefix)
BlockComment(String), // /* comment text */ (without delimiters)
// Special
Eof,
}
impl Token {
/// Check if a string is a SQL keyword and return corresponding token
pub fn from_keyword(s: &str) -> Option<Token> {
match s.to_uppercase().as_str() {
"SELECT" => Some(Token::Select),
"FROM" => Some(Token::From),
"WHERE" => Some(Token::Where),
"WITH" => Some(Token::With),
"AND" => Some(Token::And),
"OR" => Some(Token::Or),
"IN" => Some(Token::In),
"NOT" => Some(Token::Not),
"BETWEEN" => Some(Token::Between),
"LIKE" => Some(Token::Like),
"ILIKE" => Some(Token::ILike),
"IS" => Some(Token::Is),
"NULL" => Some(Token::Null),
"ORDER" => Some(Token::OrderBy),
"GROUP" => Some(Token::GroupBy),
"HAVING" => Some(Token::Having),
"QUALIFY" => Some(Token::Qualify),
"AS" => Some(Token::As),
"ASC" => Some(Token::Asc),
"DESC" => Some(Token::Desc),
"LIMIT" => Some(Token::Limit),
"OFFSET" => Some(Token::Offset),
"INTO" => Some(Token::Into),
"DISTINCT" => Some(Token::Distinct),
"EXCLUDE" => Some(Token::Exclude),
"PIVOT" => Some(Token::Pivot),
"UNPIVOT" => Some(Token::Unpivot),
"FOR" => Some(Token::For),
"CASE" => Some(Token::Case),
"WHEN" => Some(Token::When),
"THEN" => Some(Token::Then),
"ELSE" => Some(Token::Else),
"END" => Some(Token::End),
"OVER" => Some(Token::Over),
"PARTITION" => Some(Token::Partition),
"BY" => Some(Token::By),
"ROWS" => Some(Token::Rows),
"RANGE" => Some(Token::Range),
"UNBOUNDED" => Some(Token::Unbounded),
"PRECEDING" => Some(Token::Preceding),
"FOLLOWING" => Some(Token::Following),
"CURRENT" => Some(Token::Current),
"ROW" => Some(Token::Row),
"UNION" => Some(Token::Union),
"INTERSECT" => Some(Token::Intersect),
"EXCEPT" => Some(Token::Except),
"WEB" => Some(Token::Web),
"FILE" => Some(Token::File),
"UNNEST" => Some(Token::Unnest),
"JOIN" => Some(Token::Join),
"INNER" => Some(Token::Inner),
"LEFT" => Some(Token::Left),
"RIGHT" => Some(Token::Right),
"FULL" => Some(Token::Full),
"OUTER" => Some(Token::Outer),
"ON" => Some(Token::On),
"CROSS" => Some(Token::Cross),
_ => None,
}
}
/// Check if token is a logical operator
pub fn is_logical_operator(&self) -> bool {
matches!(self, Token::And | Token::Or)
}
/// Check if token is a join type
pub fn is_join_type(&self) -> bool {
matches!(
self,
Token::Inner | Token::Left | Token::Right | Token::Full | Token::Cross
)
}
/// Check if token ends a clause
pub fn is_clause_terminator(&self) -> bool {
matches!(
self,
Token::OrderBy
| Token::GroupBy
| Token::Having
| Token::Limit
| Token::Offset
| Token::Union
| Token::Intersect
| Token::Except
)
}
/// Get the string representation of a keyword token
/// Returns the keyword as it would appear in SQL (uppercase)
pub fn as_keyword_str(&self) -> Option<&'static str> {
match self {
Token::Select => Some("SELECT"),
Token::From => Some("FROM"),
Token::Where => Some("WHERE"),
Token::With => Some("WITH"),
Token::And => Some("AND"),
Token::Or => Some("OR"),
Token::In => Some("IN"),
Token::Not => Some("NOT"),
Token::Between => Some("BETWEEN"),
Token::Like => Some("LIKE"),
Token::ILike => Some("ILIKE"),
Token::Is => Some("IS"),
Token::Null => Some("NULL"),
Token::OrderBy => Some("ORDER BY"),
Token::GroupBy => Some("GROUP BY"),
Token::Having => Some("HAVING"),
Token::Qualify => Some("QUALIFY"),
Token::As => Some("AS"),
Token::Asc => Some("ASC"),
Token::Desc => Some("DESC"),
Token::Limit => Some("LIMIT"),
Token::Offset => Some("OFFSET"),
Token::Into => Some("INTO"),
Token::Distinct => Some("DISTINCT"),
Token::Exclude => Some("EXCLUDE"),
Token::Pivot => Some("PIVOT"),
Token::Unpivot => Some("UNPIVOT"),
Token::For => Some("FOR"),
Token::Case => Some("CASE"),
Token::When => Some("WHEN"),
Token::Then => Some("THEN"),
Token::Else => Some("ELSE"),
Token::End => Some("END"),
Token::Join => Some("JOIN"),
Token::Inner => Some("INNER"),
Token::Left => Some("LEFT"),
Token::Right => Some("RIGHT"),
Token::Full => Some("FULL"),
Token::Cross => Some("CROSS"),
Token::On => Some("ON"),
Token::Union => Some("UNION"),
Token::Intersect => Some("INTERSECT"),
Token::Except => Some("EXCEPT"),
Token::Over => Some("OVER"),
Token::Partition => Some("PARTITION"),
Token::By => Some("BY"),
Token::Rows => Some("ROWS"),
Token::Range => Some("RANGE"),
Token::Preceding => Some("PRECEDING"),
Token::Following => Some("FOLLOWING"),
Token::Current => Some("CURRENT"),
Token::Row => Some("ROW"),
Token::Unbounded => Some("UNBOUNDED"),
Token::DateTime => Some("DATETIME"),
_ => None,
}
}
}
#[derive(Debug, Clone)]
pub struct Lexer {
input: Vec<char>,
position: usize,
current_char: Option<char>,
mode: LexerMode,
}
impl Lexer {
#[must_use]
pub fn new(input: &str) -> Self {
Self::with_mode(input, LexerMode::default())
}
/// Create a new lexer with specified mode
#[must_use]
pub fn with_mode(input: &str, mode: LexerMode) -> Self {
let chars: Vec<char> = input.chars().collect();
let current = chars.first().copied();
Self {
input: chars,
position: 0,
current_char: current,
mode,
}
}
fn advance(&mut self) {
self.position += 1;
self.current_char = self.input.get(self.position).copied();
}
fn peek(&self, offset: usize) -> Option<char> {
self.input.get(self.position + offset).copied()
}
/// Peek ahead n characters and return as a string
fn peek_string(&self, n: usize) -> String {
let mut result = String::new();
for i in 0..n {
if let Some(ch) = self.input.get(self.position + i) {
result.push(*ch);
} else {
break;
}
}
result
}
/// Read a JSON block delimited by $JSON$...$JSON$
/// Consumes the opening delimiter and reads until closing $JSON$
fn read_json_block(&mut self) -> String {
let mut result = String::new();
// Skip opening $JSON$
for _ in 0..6 {
self.advance();
}
// Read until we find closing $JSON$
while let Some(ch) = self.current_char {
// Check if we're at the closing delimiter
if ch == '$' && self.peek_string(6) == "$JSON$" {
// Skip closing $JSON$
for _ in 0..6 {
self.advance();
}
break;
}
result.push(ch);
self.advance();
}
result
}
fn skip_whitespace(&mut self) {
while let Some(ch) = self.current_char {
if ch.is_whitespace() {
self.advance();
} else {
break;
}
}
}
/// Read a line comment and return its content (without the -- prefix)
fn read_line_comment(&mut self) -> String {
let mut result = String::new();
// Skip '--'
self.advance();
self.advance();
// Read until end of line or EOF
while let Some(ch) = self.current_char {
if ch == '\n' {
self.advance(); // consume the newline
break;
}
result.push(ch);
self.advance();
}
result
}
/// Read a block comment and return its content (without /* */ delimiters)
fn read_block_comment(&mut self) -> String {
let mut result = String::new();
// Skip '/*'
self.advance();
self.advance();
// Read until we find '*/'
while let Some(ch) = self.current_char {
if ch == '*' && self.peek(1) == Some('/') {
self.advance(); // skip '*'
self.advance(); // skip '/'
break;
}
result.push(ch);
self.advance();
}
result
}
/// Skip whitespace and comments (for backwards compatibility with parser)
/// This is the old behavior that discards comments
fn skip_whitespace_and_comments(&mut self) {
loop {
// Skip whitespace
while let Some(ch) = self.current_char {
if ch.is_whitespace() {
self.advance();
} else {
break;
}
}
// Check for comments
match self.current_char {
Some('-') if self.peek(1) == Some('-') => {
// Single-line comment: skip until end of line
self.advance(); // skip first '-'
self.advance(); // skip second '-'
while let Some(ch) = self.current_char {
self.advance();
if ch == '\n' {
break;
}
}
}
Some('/') if self.peek(1) == Some('*') => {
// Multi-line comment: skip until */
self.advance(); // skip '/'
self.advance(); // skip '*'
while let Some(ch) = self.current_char {
if ch == '*' && self.peek(1) == Some('/') {
self.advance(); // skip '*'
self.advance(); // skip '/'
break;
}
self.advance();
}
}
_ => {
// No more comments or whitespace
break;
}
}
}
}
fn read_identifier(&mut self) -> String {
let mut result = String::new();
while let Some(ch) = self.current_char {
if ch.is_alphanumeric() || ch == '_' {
result.push(ch);
self.advance();
} else {
break;
}
}
result
}
fn read_string(&mut self) -> String {
let mut result = String::new();
let quote_char = self.current_char.unwrap(); // ' or "
self.advance(); // skip opening quote
while let Some(ch) = self.current_char {
if ch == quote_char {
self.advance(); // skip closing quote
break;
}
result.push(ch);
self.advance();
}
result
}
fn read_number(&mut self) -> String {
let mut result = String::new();
let has_e = false;
// Read the main number part (including decimal point)
while let Some(ch) = self.current_char {
if !has_e && (ch.is_numeric() || ch == '.') {
result.push(ch);
self.advance();
} else if (ch == 'e' || ch == 'E') && !has_e && !result.is_empty() {
// Handle scientific notation
result.push(ch);
self.advance();
let _ = has_e; // We don't allow multiple 'e' characters, so break after this
// Check for optional sign after 'e'
if let Some(sign) = self.current_char {
if sign == '+' || sign == '-' {
result.push(sign);
self.advance();
}
}
// Read exponent digits
while let Some(digit) = self.current_char {
if digit.is_numeric() {
result.push(digit);
self.advance();
} else {
break;
}
}
break; // Done reading the number
} else {
break;
}
}
result
}
/// Get next token while preserving comments as tokens
/// This is the new behavior for comment-aware formatting
pub fn next_token_with_comments(&mut self) -> Token {
// Only skip whitespace, NOT comments
self.skip_whitespace();
match self.current_char {
None => Token::Eof,
// Handle comments as tokens
Some('-') if self.peek(1) == Some('-') => {
let comment_text = self.read_line_comment();
Token::LineComment(comment_text)
}
Some('/') if self.peek(1) == Some('*') => {
let comment_text = self.read_block_comment();
Token::BlockComment(comment_text)
}
Some('*') => {
self.advance();
Token::Star
}
Some('+') => {
self.advance();
Token::Plus
}
Some('/') => {
// Regular division (comment case handled above)
self.advance();
Token::Divide
}
Some('%') => {
self.advance();
Token::Modulo
}
Some('.') => {
self.advance();
Token::Dot
}
Some(',') => {
self.advance();
Token::Comma
}
Some(':') => {
self.advance();
Token::Colon
}
Some('(') => {
self.advance();
Token::LeftParen
}
Some(')') => {
self.advance();
Token::RightParen
}
Some('=') => {
self.advance();
Token::Equal
}
Some('<') => {
self.advance();
if self.current_char == Some('=') {
self.advance();
Token::LessThanOrEqual
} else if self.current_char == Some('>') {
self.advance();
Token::NotEqual
} else {
Token::LessThan
}
}
Some('>') => {
self.advance();
if self.current_char == Some('=') {
self.advance();
Token::GreaterThanOrEqual
} else {
Token::GreaterThan
}
}
Some('!') if self.peek(1) == Some('=') => {
self.advance();
self.advance();
Token::NotEqual
}
Some('|') if self.peek(1) == Some('|') => {
self.advance();
self.advance();
Token::Concat
}
Some('"') => {
let ident_val = self.read_string();
Token::QuotedIdentifier(ident_val)
}
Some('$') => {
if self.peek_string(6) == "$JSON$" {
let json_content = self.read_json_block();
Token::JsonBlock(json_content)
} else {
let ident = self.read_identifier();
Token::Identifier(ident)
}
}
Some('\'') => {
let string_val = self.read_string();
Token::StringLiteral(string_val)
}
Some('-') if self.peek(1).is_some_and(char::is_numeric) => {
self.advance();
let num = self.read_number();
Token::NumberLiteral(format!("-{num}"))
}
Some('-') => {
self.advance();
Token::Minus
}
Some(ch) if ch.is_numeric() => {
let num = self.read_number();
Token::NumberLiteral(num)
}
Some('#') => {
self.advance();
let table_name = self.read_identifier();
if table_name.is_empty() {
Token::Identifier("#".to_string())
} else {
Token::Identifier(format!("#{}", table_name))
}
}
Some(ch) if ch.is_alphabetic() || ch == '_' => {
let ident = self.read_identifier();
// Handle multi-word keywords like GROUP BY and ORDER BY
match ident.to_uppercase().as_str() {
"ORDER" if self.peek_keyword("BY") => {
self.skip_whitespace();
self.read_identifier(); // consume "BY"
Token::OrderBy
}
"GROUP" if self.peek_keyword("BY") => {
self.skip_whitespace();
self.read_identifier(); // consume "BY"
Token::GroupBy
}
_ => Token::from_keyword(&ident).unwrap_or_else(|| Token::Identifier(ident)),
}
}
Some(ch) => {
self.advance();
Token::Identifier(ch.to_string())
}
}
}
/// Get next token - dispatches based on lexer mode
pub fn next_token(&mut self) -> Token {
match self.mode {
LexerMode::SkipComments => self.next_token_skip_comments(),
LexerMode::PreserveComments => self.next_token_with_comments(),
}
}
/// Get next token skipping comments (original behavior)
fn next_token_skip_comments(&mut self) -> Token {
self.skip_whitespace_and_comments();
match self.current_char {
None => Token::Eof,
Some('*') => {
self.advance();
// Context-sensitive: could be SELECT * or multiplication
// The parser will distinguish based on context
Token::Star // We'll handle multiplication in parser
}
Some('+') => {
self.advance();
Token::Plus
}
Some('/') => {
// Check if this is a comment start
if self.peek(1) == Some('*') {
// This shouldn't happen as comments are skipped above,
// but handle it just in case
self.skip_whitespace_and_comments();
return self.next_token();
}
self.advance();
Token::Divide
}
Some('%') => {
self.advance();
Token::Modulo
}
Some('.') => {
self.advance();
Token::Dot
}
Some(',') => {
self.advance();
Token::Comma
}
Some(':') => {
self.advance();
Token::Colon
}
Some('(') => {
self.advance();
Token::LeftParen
}
Some(')') => {
self.advance();
Token::RightParen
}
Some('=') => {
self.advance();
Token::Equal
}
Some('<') => {
self.advance();
if self.current_char == Some('=') {
self.advance();
Token::LessThanOrEqual
} else if self.current_char == Some('>') {
self.advance();
Token::NotEqual
} else {
Token::LessThan
}
}
Some('>') => {
self.advance();
if self.current_char == Some('=') {
self.advance();
Token::GreaterThanOrEqual
} else {
Token::GreaterThan
}
}
Some('!') if self.peek(1) == Some('=') => {
self.advance();
self.advance();
Token::NotEqual
}
Some('|') if self.peek(1) == Some('|') => {
self.advance();
self.advance();
Token::Concat
}
Some('"') => {
// Double quotes = identifier
let ident_val = self.read_string();
Token::QuotedIdentifier(ident_val)
}
Some('$') => {
// Check if this is $JSON$ delimiter
if self.peek_string(6) == "$JSON$" {
let json_content = self.read_json_block();
Token::JsonBlock(json_content)
} else {
// Not a JSON block, could be part of identifier or parameter
// For now, treat as identifier start
let ident = self.read_identifier();
Token::Identifier(ident)
}
}
Some('\'') => {
// Single quotes = string literal
let string_val = self.read_string();
Token::StringLiteral(string_val)
}
Some('-') if self.peek(1) == Some('-') => {
// This is a comment, skip it and get next token
self.skip_whitespace_and_comments();
self.next_token()
}
Some('-') if self.peek(1).is_some_and(char::is_numeric) => {
// Handle negative numbers
self.advance(); // skip '-'
let num = self.read_number();
Token::NumberLiteral(format!("-{num}"))
}
Some('-') => {
// Handle subtraction operator
self.advance();
Token::Minus
}
Some(ch) if ch.is_numeric() => {
let num = self.read_number();
Token::NumberLiteral(num)
}
Some('#') => {
// Temporary table identifier: #tablename
self.advance(); // consume #
let table_name = self.read_identifier();
if table_name.is_empty() {
// Just # by itself
Token::Identifier("#".to_string())
} else {
// #tablename
Token::Identifier(format!("#{}", table_name))
}
}
Some(ch) if ch.is_alphabetic() || ch == '_' => {
let ident = self.read_identifier();
match ident.to_uppercase().as_str() {
"SELECT" => Token::Select,
"FROM" => Token::From,
"WHERE" => Token::Where,
"WITH" => Token::With,
"AND" => Token::And,
"OR" => Token::Or,
"IN" => Token::In,
"NOT" => Token::Not,
"BETWEEN" => Token::Between,
"LIKE" => Token::Like,
"ILIKE" => Token::ILike,
"IS" => Token::Is,
"NULL" => Token::Null,
"ORDER" if self.peek_keyword("BY") => {
self.skip_whitespace();
self.read_identifier(); // consume "BY"
Token::OrderBy
}
"GROUP" if self.peek_keyword("BY") => {
self.skip_whitespace();
self.read_identifier(); // consume "BY"
Token::GroupBy
}
"HAVING" => Token::Having,
"QUALIFY" => Token::Qualify,
"AS" => Token::As,
"ASC" => Token::Asc,
"DESC" => Token::Desc,
"LIMIT" => Token::Limit,
"OFFSET" => Token::Offset,
"INTO" => Token::Into,
"DATETIME" => Token::DateTime,
"CASE" => Token::Case,
"WHEN" => Token::When,
"THEN" => Token::Then,
"ELSE" => Token::Else,
"END" => Token::End,
"DISTINCT" => Token::Distinct,
"EXCLUDE" => Token::Exclude,
"PIVOT" => Token::Pivot,
"UNPIVOT" => Token::Unpivot,
"FOR" => Token::For,
"OVER" => Token::Over,
"PARTITION" => Token::Partition,
"BY" => Token::By,
// Window frame keywords
"ROWS" => Token::Rows,
// Note: RANGE is context-sensitive - it's both a window frame keyword and a table function
// We'll handle this in the parser based on context
"UNBOUNDED" => Token::Unbounded,
"PRECEDING" => Token::Preceding,
"FOLLOWING" => Token::Following,
"CURRENT" => Token::Current,
"ROW" => Token::Row,
// Set operation keywords
"UNION" => Token::Union,
"INTERSECT" => Token::Intersect,
"EXCEPT" => Token::Except,
// Special CTE keywords
"WEB" => Token::Web,
"FILE" => Token::File,
// Row expansion functions
"UNNEST" => Token::Unnest,
// JOIN keywords
"JOIN" => Token::Join,
"INNER" => Token::Inner,
"LEFT" => Token::Left,
"RIGHT" => Token::Right,
"FULL" => Token::Full,
"OUTER" => Token::Outer,
"ON" => Token::On,
"CROSS" => Token::Cross,
_ => Token::Identifier(ident),
}
}
Some(ch) => {
self.advance();
Token::Identifier(ch.to_string())
}
}
}
fn peek_keyword(&mut self, keyword: &str) -> bool {
let saved_pos = self.position;
let saved_char = self.current_char;
self.skip_whitespace_and_comments();
let next_word = self.read_identifier();
let matches = next_word.to_uppercase() == keyword;
// Restore position
self.position = saved_pos;
self.current_char = saved_char;
matches
}
#[must_use]
pub fn get_position(&self) -> usize {
self.position
}
pub fn tokenize_all(&mut self) -> Vec<Token> {
let mut tokens = Vec::new();
loop {
let token = self.next_token();
if matches!(token, Token::Eof) {
tokens.push(token);
break;
}
tokens.push(token);
}
tokens
}
pub fn tokenize_all_with_positions(&mut self) -> Vec<(usize, usize, Token)> {
let mut tokens = Vec::new();
loop {
self.skip_whitespace_and_comments();
let start_pos = self.position;
let token = self.next_token();
let end_pos = self.position;
if matches!(token, Token::Eof) {
break;
}
tokens.push((start_pos, end_pos, token));
}
tokens
}
/// Tokenize all tokens including comments
/// This is useful for formatting tools that need to preserve comments
pub fn tokenize_all_with_comments(&mut self) -> Vec<Token> {
let mut tokens = Vec::new();
loop {
let token = self.next_token_with_comments();
if matches!(token, Token::Eof) {
tokens.push(token);
break;
}
tokens.push(token);
}
tokens
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_line_comment_tokenization() {
let sql = "SELECT col1, -- this is a comment\ncol2 FROM table";
let mut lexer = Lexer::new(sql);
let tokens = lexer.tokenize_all_with_comments();
// Find the comment token
let comment_token = tokens.iter().find(|t| matches!(t, Token::LineComment(_)));
assert!(comment_token.is_some(), "Should find line comment token");
if let Some(Token::LineComment(text)) = comment_token {
assert_eq!(text.trim(), "this is a comment");
}
}
#[test]
fn test_block_comment_tokenization() {
let sql = "SELECT /* block comment */ col1 FROM table";
let mut lexer = Lexer::new(sql);
let tokens = lexer.tokenize_all_with_comments();
// Find the comment token
let comment_token = tokens.iter().find(|t| matches!(t, Token::BlockComment(_)));
assert!(comment_token.is_some(), "Should find block comment token");
if let Some(Token::BlockComment(text)) = comment_token {
assert_eq!(text.trim(), "block comment");
}
}
#[test]
fn test_multiple_comments() {
let sql = "-- First comment\nSELECT col1, /* inline */ col2\n-- Second comment\nFROM table";
let mut lexer = Lexer::new(sql);
let tokens = lexer.tokenize_all_with_comments();
let line_comments: Vec<_> = tokens
.iter()
.filter(|t| matches!(t, Token::LineComment(_)))
.collect();
let block_comments: Vec<_> = tokens
.iter()
.filter(|t| matches!(t, Token::BlockComment(_)))
.collect();
assert_eq!(line_comments.len(), 2, "Should find 2 line comments");
assert_eq!(block_comments.len(), 1, "Should find 1 block comment");
}
#[test]
fn test_backwards_compatibility() {
// Test that next_token() still skips comments
let sql = "SELECT -- comment\ncol1 FROM table";
let mut lexer = Lexer::new(sql);
let tokens = lexer.tokenize_all();
// Should NOT contain any comment tokens
let has_comments = tokens
.iter()
.any(|t| matches!(t, Token::LineComment(_) | Token::BlockComment(_)));
assert!(
!has_comments,
"next_token() should skip comments for backwards compatibility"
);
// Should still parse correctly
assert!(tokens.iter().any(|t| matches!(t, Token::Select)));
assert!(tokens.iter().any(|t| matches!(t, Token::From)));
}
// ===== Dual-Mode Lexer Tests (Phase 1) =====
#[test]
fn test_lexer_mode_skip_comments() {
let sql = "SELECT id -- comment\nFROM table";
// SkipComments mode (default)
let mut lexer = Lexer::with_mode(sql, LexerMode::SkipComments);
assert_eq!(lexer.next_token(), Token::Select);
assert_eq!(lexer.next_token(), Token::Identifier("id".into()));
// Comment should be skipped
assert_eq!(lexer.next_token(), Token::From);
assert_eq!(lexer.next_token(), Token::Identifier("table".into()));
assert_eq!(lexer.next_token(), Token::Eof);
}
#[test]
fn test_lexer_mode_preserve_comments() {
let sql = "SELECT id -- comment\nFROM table";
// PreserveComments mode
let mut lexer = Lexer::with_mode(sql, LexerMode::PreserveComments);
assert_eq!(lexer.next_token(), Token::Select);
assert_eq!(lexer.next_token(), Token::Identifier("id".into()));
// Comment should be preserved as a token
let comment_tok = lexer.next_token();
assert!(matches!(comment_tok, Token::LineComment(_)));
if let Token::LineComment(text) = comment_tok {
assert_eq!(text.trim(), "comment");
}
assert_eq!(lexer.next_token(), Token::From);
assert_eq!(lexer.next_token(), Token::Identifier("table".into()));
assert_eq!(lexer.next_token(), Token::Eof);
}
#[test]
fn test_lexer_mode_default_is_skip() {
let sql = "SELECT id -- comment\nFROM table";
// Default (using new()) should skip comments
let mut lexer = Lexer::new(sql);
let mut tok_count = 0;
loop {
let tok = lexer.next_token();
if matches!(tok, Token::Eof) {
break;
}
// Should never see a comment token
assert!(!matches!(
tok,
Token::LineComment(_) | Token::BlockComment(_)
));
tok_count += 1;
}
// SELECT, id, FROM, table = 4 tokens (no comment)
assert_eq!(tok_count, 4);
}
#[test]
fn test_lexer_mode_block_comments() {
let sql = "SELECT /* block */ id FROM table";
// Skip mode
let mut lexer_skip = Lexer::with_mode(sql, LexerMode::SkipComments);
assert_eq!(lexer_skip.next_token(), Token::Select);
assert_eq!(lexer_skip.next_token(), Token::Identifier("id".into()));
assert_eq!(lexer_skip.next_token(), Token::From);
// Preserve mode
let mut lexer_preserve = Lexer::with_mode(sql, LexerMode::PreserveComments);
assert_eq!(lexer_preserve.next_token(), Token::Select);
let comment_tok = lexer_preserve.next_token();
assert!(matches!(comment_tok, Token::BlockComment(_)));
if let Token::BlockComment(text) = comment_tok {
assert_eq!(text.trim(), "block");
}
assert_eq!(lexer_preserve.next_token(), Token::Identifier("id".into()));
}
#[test]
fn test_lexer_mode_mixed_comments() {
let sql = "-- leading\nSELECT /* inline */ id -- trailing\nFROM table";
let mut lexer = Lexer::with_mode(sql, LexerMode::PreserveComments);
// leading comment
assert!(matches!(lexer.next_token(), Token::LineComment(_)));
// SELECT
assert_eq!(lexer.next_token(), Token::Select);
// inline block comment
assert!(matches!(lexer.next_token(), Token::BlockComment(_)));
// id
assert_eq!(lexer.next_token(), Token::Identifier("id".into()));
// trailing comment
assert!(matches!(lexer.next_token(), Token::LineComment(_)));
// FROM table
assert_eq!(lexer.next_token(), Token::From);
assert_eq!(lexer.next_token(), Token::Identifier("table".into()));
assert_eq!(lexer.next_token(), Token::Eof);
}
#[test]
fn test_pivot_keywords() {
let sql = "PIVOT (MAX(amount) FOR month IN (val1, val2)) UNPIVOT";
let mut lexer = Lexer::new(sql);
// Test individual token recognition
assert_eq!(
lexer.next_token(),
Token::Pivot,
"First token should be PIVOT"
);
assert_eq!(lexer.next_token(), Token::LeftParen);
assert!(matches!(lexer.next_token(), Token::Identifier(_))); // MAX
assert_eq!(lexer.next_token(), Token::LeftParen);
assert!(matches!(lexer.next_token(), Token::Identifier(_))); // amount
assert_eq!(lexer.next_token(), Token::RightParen);
assert_eq!(lexer.next_token(), Token::For, "Should tokenize FOR");
assert!(matches!(lexer.next_token(), Token::Identifier(_))); // month
assert_eq!(lexer.next_token(), Token::In, "Should tokenize IN");
assert_eq!(lexer.next_token(), Token::LeftParen);
assert!(matches!(lexer.next_token(), Token::Identifier(_))); // val1
assert_eq!(lexer.next_token(), Token::Comma);
assert!(matches!(lexer.next_token(), Token::Identifier(_))); // val2
assert_eq!(lexer.next_token(), Token::RightParen);
assert_eq!(lexer.next_token(), Token::RightParen);
assert_eq!(
lexer.next_token(),
Token::Unpivot,
"Should tokenize UNPIVOT"
);
assert_eq!(lexer.next_token(), Token::Eof);
}
}