-
Notifications
You must be signed in to change notification settings - Fork 563
/
lexer.rs
496 lines (474 loc) · 15.5 KB
/
lexer.rs
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
use crate::errinput;
use crate::error::Result;
/// The lexer (lexical analyzer) preprocesses raw SQL strings into a sequence of
/// lexical tokens (e.g. keyword, number, string, etc) that are passed onto the
/// SQL parser. In doing so, it strips away basic syntactic noise such as
/// whitespace, case, and quotes, and performs initial validation of symbols.
pub struct Lexer<'a> {
chars: std::iter::Peekable<std::str::Chars<'a>>,
}
/// A lexical token.
///
/// For simplicity, these carry owned String copies rather than &str references
/// into the original input string. This causes frequent allocations, but that's
/// fine for our purposes here.
#[derive(Clone, Debug, PartialEq)]
pub enum Token {
/// A number, with digits, decimal points, and/or exponents. Leading signs
/// (e.g. -) are separate tokens.
Number(String),
/// A Unicode string, with quotes stripped and escape sequences resolved.
String(String),
/// An identifier, with any quotes stripped.
Ident(String),
/// A SQL keyword.
Keyword(Keyword),
Period, // .
Equal, // =
NotEqual, // !=
GreaterThan, // >
GreaterThanOrEqual, // >=
LessThan, // <
LessThanOrEqual, // <=
LessOrGreaterThan, // <>
Plus, // +
Minus, // -
Asterisk, // *
Slash, // /
Caret, // ^
Percent, // %
Exclamation, // !
Question, // ?
Comma, // ,
Semicolon, // ;
OpenParen, // (
CloseParen, // )
}
impl std::fmt::Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(match self {
Self::Number(n) => n,
Self::String(s) => s,
Self::Ident(s) => s,
Self::Keyword(k) => return k.fmt(f),
Self::Period => ".",
Self::Equal => "=",
Self::NotEqual => "!=",
Self::GreaterThan => ">",
Self::GreaterThanOrEqual => ">=",
Self::LessThan => "<",
Self::LessThanOrEqual => "<=",
Self::LessOrGreaterThan => "<>",
Self::Plus => "+",
Self::Minus => "-",
Self::Asterisk => "*",
Self::Slash => "/",
Self::Caret => "^",
Self::Percent => "%",
Self::Exclamation => "!",
Self::Question => "?",
Self::Comma => ",",
Self::Semicolon => ";",
Self::OpenParen => "(",
Self::CloseParen => ")",
})
}
}
impl From<Keyword> for Token {
fn from(keyword: Keyword) -> Self {
Self::Keyword(keyword)
}
}
/// Reserved SQL keywords.
#[derive(Clone, Debug, PartialEq)]
pub enum Keyword {
And,
As,
Asc,
Begin,
Bool,
Boolean,
By,
Char,
Commit,
Create,
Cross,
Default,
Delete,
Desc,
Double,
Drop,
Exists,
Explain,
False,
Float,
From,
Group,
Having,
If,
Index,
Infinity,
Inner,
Insert,
Int,
Integer,
Into,
Is,
Join,
Key,
Left,
Like,
Limit,
NaN,
Not,
Null,
Of,
Offset,
On,
Only,
Or,
Order,
Outer,
Primary,
Read,
References,
Right,
Rollback,
Select,
Set,
String,
System,
Table,
Text,
Time,
Transaction,
True,
Unique,
Update,
Values,
Varchar,
Where,
Write,
}
impl TryFrom<&str> for Keyword {
// The error just indicates this isn't a keyword, so use a cheap string.
type Error = &'static str;
fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
// Only compare lowercase, which is enforced by the lexer. This avoids
// allocating a string to change the case. Assert this.
debug_assert!(value.chars().all(|c| !c.is_uppercase()), "keyword must be lowercase");
Ok(match value {
"as" => Self::As,
"asc" => Self::Asc,
"and" => Self::And,
"begin" => Self::Begin,
"bool" => Self::Bool,
"boolean" => Self::Boolean,
"by" => Self::By,
"char" => Self::Char,
"commit" => Self::Commit,
"create" => Self::Create,
"cross" => Self::Cross,
"default" => Self::Default,
"delete" => Self::Delete,
"desc" => Self::Desc,
"double" => Self::Double,
"drop" => Self::Drop,
"exists" => Self::Exists,
"explain" => Self::Explain,
"false" => Self::False,
"float" => Self::Float,
"from" => Self::From,
"group" => Self::Group,
"having" => Self::Having,
"if" => Self::If,
"index" => Self::Index,
"infinity" => Self::Infinity,
"inner" => Self::Inner,
"insert" => Self::Insert,
"int" => Self::Int,
"integer" => Self::Integer,
"into" => Self::Into,
"is" => Self::Is,
"join" => Self::Join,
"key" => Self::Key,
"left" => Self::Left,
"like" => Self::Like,
"limit" => Self::Limit,
"nan" => Self::NaN,
"not" => Self::Not,
"null" => Self::Null,
"of" => Self::Of,
"offset" => Self::Offset,
"on" => Self::On,
"only" => Self::Only,
"or" => Self::Or,
"order" => Self::Order,
"outer" => Self::Outer,
"primary" => Self::Primary,
"read" => Self::Read,
"references" => Self::References,
"right" => Self::Right,
"rollback" => Self::Rollback,
"select" => Self::Select,
"set" => Self::Set,
"string" => Self::String,
"system" => Self::System,
"table" => Self::Table,
"text" => Self::Text,
"time" => Self::Time,
"transaction" => Self::Transaction,
"true" => Self::True,
"unique" => Self::Unique,
"update" => Self::Update,
"values" => Self::Values,
"varchar" => Self::Varchar,
"where" => Self::Where,
"write" => Self::Write,
_ => return Err("not a keyword"),
})
}
}
impl std::fmt::Display for Keyword {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
// Display keywords as uppercase.
f.write_str(match self {
Self::As => "AS",
Self::Asc => "ASC",
Self::And => "AND",
Self::Begin => "BEGIN",
Self::Bool => "BOOL",
Self::Boolean => "BOOLEAN",
Self::By => "BY",
Self::Char => "CHAR",
Self::Commit => "COMMIT",
Self::Create => "CREATE",
Self::Cross => "CROSS",
Self::Default => "DEFAULT",
Self::Delete => "DELETE",
Self::Desc => "DESC",
Self::Double => "DOUBLE",
Self::Drop => "DROP",
Self::Exists => "EXISTS",
Self::Explain => "EXPLAIN",
Self::False => "FALSE",
Self::Float => "FLOAT",
Self::From => "FROM",
Self::Group => "GROUP",
Self::Having => "HAVING",
Self::If => "IF",
Self::Index => "INDEX",
Self::Infinity => "INFINITY",
Self::Inner => "INNER",
Self::Insert => "INSERT",
Self::Int => "INT",
Self::Integer => "INTEGER",
Self::Into => "INTO",
Self::Is => "IS",
Self::Join => "JOIN",
Self::Key => "KEY",
Self::Left => "LEFT",
Self::Like => "LIKE",
Self::Limit => "LIMIT",
Self::NaN => "NAN",
Self::Not => "NOT",
Self::Null => "NULL",
Self::Of => "OF",
Self::Offset => "OFFSET",
Self::On => "ON",
Self::Only => "ONLY",
Self::Outer => "OUTER",
Self::Or => "OR",
Self::Order => "ORDER",
Self::Primary => "PRIMARY",
Self::Read => "READ",
Self::References => "REFERENCES",
Self::Right => "RIGHT",
Self::Rollback => "ROLLBACK",
Self::Select => "SELECT",
Self::Set => "SET",
Self::String => "STRING",
Self::System => "SYSTEM",
Self::Table => "TABLE",
Self::Text => "TEXT",
Self::Time => "TIME",
Self::Transaction => "TRANSACTION",
Self::True => "TRUE",
Self::Unique => "UNIQUE",
Self::Update => "UPDATE",
Self::Values => "VALUES",
Self::Varchar => "VARCHAR",
Self::Where => "WHERE",
Self::Write => "WRITE",
})
}
}
/// The lexer is used as a token iterator.
impl<'a> Iterator for Lexer<'a> {
type Item = Result<Token>;
fn next(&mut self) -> Option<Result<Token>> {
match self.scan() {
Ok(Some(token)) => Some(Ok(token)),
// If there's any remaining chars, the lexer didn't recognize them.
// Otherwise, we're done lexing.
Ok(None) => self.chars.peek().map(|c| errinput!("unexpected character {c}")),
Err(err) => Some(Err(err)),
}
}
}
impl<'a> Lexer<'a> {
/// Creates a new lexer for the given string.
pub fn new(input: &'a str) -> Lexer<'a> {
Lexer { chars: input.chars().peekable() }
}
/// Returns the next character if it satisfies the predicate.
fn next_if(&mut self, predicate: impl Fn(char) -> bool) -> Option<char> {
self.chars.peek().filter(|&&c| predicate(c))?;
self.chars.next()
}
/// Applies a function to the next character, returning its result and
/// consuming the next character if it's Some.
fn next_if_map<T>(&mut self, map: impl Fn(char) -> Option<T>) -> Option<T> {
let value = self.chars.peek().and_then(|&c| map(c))?;
self.chars.next();
Some(value)
}
/// Returns true if the next character is the given character, consuming it.
fn next_is(&mut self, c: char) -> bool {
self.next_if(|n| n == c).is_some()
}
/// Scans the next token, if any.
fn scan(&mut self) -> Result<Option<Token>> {
// Ignore whitespace. The first character tells us the token type.
self.skip_whitespace();
match self.chars.peek() {
Some('\'') => self.scan_string(),
Some('"') => self.scan_ident_quoted(),
Some(c) if c.is_ascii_digit() => Ok(self.scan_number()),
Some(c) if c.is_alphabetic() => Ok(self.scan_ident_or_keyword()),
Some(_) => Ok(self.scan_symbol()),
None => Ok(None),
}
}
/// Scans the next identifier or keyword, if any. It's converted to
/// lowercase, by SQL convention.
fn scan_ident_or_keyword(&mut self) -> Option<Token> {
// The first character must be alphabetic. The rest can be numeric.
let mut name = self.next_if(|c| c.is_alphabetic())?.to_lowercase().to_string();
while let Some(c) = self.next_if(|c| c.is_alphanumeric() || c == '_') {
name.extend(c.to_lowercase())
}
// Check if the identifier matches a keyword.
match Keyword::try_from(name.as_str()).ok() {
Some(keyword) => Some(Token::Keyword(keyword)),
None => Some(Token::Ident(name)),
}
}
/// Scans the next quoted identifier, if any. Case is preserved, keywords
/// are ignored.
fn scan_ident_quoted(&mut self) -> Result<Option<Token>> {
if !self.next_is('"') {
return Ok(None);
}
let mut ident = String::new();
loop {
match self.chars.next() {
// "" is the escape sequence for ".
Some('"') if self.next_is('"') => ident.push('"'),
Some('"') => break,
Some(c) => ident.push(c),
None => return errinput!("unexpected end of quoted identifier"),
}
}
Ok(Some(Token::Ident(ident)))
}
/// Scans the next number, if any.
fn scan_number(&mut self) -> Option<Token> {
// Scan the integer part. There must be one digit.
let mut number = self.next_if(|c| c.is_ascii_digit())?.to_string();
while let Some(c) = self.next_if(|c| c.is_ascii_digit()) {
number.push(c)
}
// Scan the fractional part, if any.
if let Some(sep) = self.next_if(|c| c == '.') {
number.push(sep);
while let Some(dec) = self.next_if(|c| c.is_ascii_digit()) {
number.push(dec)
}
}
// Scan the exponent, if any.
if let Some(exp) = self.next_if(|c| c == 'e' || c == 'E') {
number.push(exp);
if let Some(sign) = self.next_if(|c| c == '+' || c == '-') {
number.push(sign)
}
while let Some(c) = self.next_if(|c| c.is_ascii_digit()) {
number.push(c)
}
}
Some(Token::Number(number))
}
/// Scans the next string literal, if any.
fn scan_string(&mut self) -> Result<Option<Token>> {
if !self.next_is('\'') {
return Ok(None);
}
let mut string = String::new();
loop {
match self.chars.next() {
// '' is the escape sequence for '.
Some('\'') if self.next_is('\'') => string.push('\''),
Some('\'') => break,
Some(c) => string.push(c),
None => return errinput!("unexpected end of string literal"),
}
}
Ok(Some(Token::String(string)))
}
/// Scans the next symbol token, if any.
fn scan_symbol(&mut self) -> Option<Token> {
let mut token = self.next_if_map(|c| {
Some(match c {
'.' => Token::Period,
'=' => Token::Equal,
'>' => Token::GreaterThan,
'<' => Token::LessThan,
'+' => Token::Plus,
'-' => Token::Minus,
'*' => Token::Asterisk,
'/' => Token::Slash,
'^' => Token::Caret,
'%' => Token::Percent,
'!' => Token::Exclamation,
'?' => Token::Question,
',' => Token::Comma,
';' => Token::Semicolon,
'(' => Token::OpenParen,
')' => Token::CloseParen,
_ => return None,
})
})?;
// Handle two-character tokens, e.g. !=.
token = match token {
Token::Exclamation if self.next_is('=') => Token::NotEqual,
Token::GreaterThan if self.next_is('=') => Token::GreaterThanOrEqual,
Token::LessThan if self.next_is('>') => Token::LessOrGreaterThan,
Token::LessThan if self.next_is('=') => Token::LessThanOrEqual,
token => token,
};
Some(token)
}
/// Skips any whitespace.
fn skip_whitespace(&mut self) {
while self.next_if(|c| c.is_whitespace()).is_some() {}
}
}
/// Returns true if the entire given string is a single valid identifier.
pub fn is_ident(ident: &str) -> bool {
let mut lexer = Lexer::new(ident);
let token = lexer.next();
if lexer.next().is_some() {
return false; // multiple tokens, so not an identifier
}
matches!(token, Some(Ok(Token::Ident(_))))
}