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
/// Shared SQL tokenizer for completion and diagnostics engines.
/// Provides position-preserving tokenization of SQL text.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum TokenKind {
Word,
Whitespace,
Dot,
Comma,
Other,
}
#[derive(Debug)]
pub struct Token<'a> {
pub text: &'a str,
pub kind: TokenKind,
pub row: usize,
pub col: usize,
}
/// Tokenize SQL lines, skipping line comments (`--`).
pub fn tokenize_sql<'a>(lines: &[&'a str]) -> Vec<Token<'a>> {
let mut tokens = Vec::new();
for (row, line) in lines.iter().enumerate() {
// Skip line comments
if let Some(pos) = line.find("--") {
let effective = &line[..pos];
tokenize_line(effective, row, &mut tokens);
continue;
}
tokenize_line(line, row, &mut tokens);
}
tokens
}
fn tokenize_line<'a>(line: &'a str, row: usize, tokens: &mut Vec<Token<'a>>) {
let bytes = line.as_bytes();
let mut col = 0;
while col < bytes.len() {
let b = bytes[col];
if b.is_ascii_whitespace() {
let start = col;
while col < bytes.len() && bytes[col].is_ascii_whitespace() {
col += 1;
}
tokens.push(Token {
text: &line[start..col],
kind: TokenKind::Whitespace,
row,
col: start,
});
} else if b == b'.' {
tokens.push(Token {
text: ".",
kind: TokenKind::Dot,
row,
col,
});
col += 1;
} else if b == b',' {
tokens.push(Token {
text: ",",
kind: TokenKind::Comma,
row,
col,
});
col += 1;
} else if b == b'\'' {
// Skip string literals
let start = col;
col += 1;
while col < bytes.len() && bytes[col] != b'\'' {
col += 1;
}
if col < bytes.len() {
col += 1;
}
tokens.push(Token {
text: &line[start..col],
kind: TokenKind::Other,
row,
col: start,
});
} else if b.is_ascii_alphanumeric() || b == b'_' {
let start = col;
while col < bytes.len() && (bytes[col].is_ascii_alphanumeric() || bytes[col] == b'_') {
col += 1;
}
tokens.push(Token {
text: &line[start..col],
kind: TokenKind::Word,
row,
col: start,
});
} else {
tokens.push(Token {
text: &line[col..col + 1],
kind: TokenKind::Other,
row,
col,
});
col += 1;
}
}
// Newline acts as whitespace between lines
tokens.push(Token {
text: " ",
kind: TokenKind::Whitespace,
row,
col: line.len(),
});
}
/// Check if a word (already UPPERCASE) is a common SQL keyword.
pub fn is_sql_keyword(upper: &str) -> bool {
matches!(
upper,
"SELECT"
| "FROM"
| "WHERE"
| "INSERT"
| "INTO"
| "VALUES"
| "UPDATE"
| "SET"
| "DELETE"
| "CREATE"
| "ALTER"
| "DROP"
| "TABLE"
| "VIEW"
| "INDEX"
| "JOIN"
| "INNER"
| "LEFT"
| "RIGHT"
| "FULL"
| "OUTER"
| "CROSS"
| "NATURAL"
| "ON"
| "USING"
| "AND"
| "OR"
| "NOT"
| "IN"
| "EXISTS"
| "BETWEEN"
| "LIKE"
| "IS"
| "NULL"
| "ORDER"
| "BY"
| "GROUP"
| "HAVING"
| "LIMIT"
| "OFFSET"
| "ASC"
| "DESC"
| "DISTINCT"
| "ALL"
| "AS"
| "CASE"
| "WHEN"
| "THEN"
| "ELSE"
| "END"
| "UNION"
| "INTERSECT"
| "EXCEPT"
| "WITH"
| "RECURSIVE"
| "BEGIN"
| "COMMIT"
| "ROLLBACK"
| "DECLARE"
| "RETURN"
| "IF"
| "LOOP"
| "FOR"
| "WHILE"
| "EXCEPTION"
| "RAISE"
| "PRAGMA"
| "EXEC"
| "EXECUTE"
| "CALL"
)
}