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
use crate::Error;
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Token {
/// An identifier (variable name, field name, etc.).
Ident(String),
/// A string literal (single quotes or double quotes).
Str(String),
/// A float literal.
F64(f64),
/// A integer literal.
I64(i64),
/// A boolean literal.
Bool(bool),
/// A null literal.
Null,
/// The equal operator (`=`).
Eq,
/// The greater than operator (`>`).
Gt,
/// The less than operator (`<`).
Lt,
/// The greater than or equal to operator (`>=`).
Ge,
/// The less than or equal to operator (`<=`).
Le,
/// The not equal to operator (`!=`).
Ne,
/// The in operator (`IN`).
In,
/// The left parenthesis (`(`).
LParen,
/// The right parenthesis (`)`).
RParen,
/// The square bracket (`[`).
LBracket,
/// The square bracket (`]`).
RBracket,
/// The comma (`,`).
Comma,
/// The colon (`:`).
Colon,
/// The dot (`.`).
Dot,
/// The and operator (`&&` or `AND`).
And,
/// The or operator (`||` or `OR`).
Or,
/// The not operator (`!` or `NOT`).
Not,
}
pub(crate) fn parse_token(input: &str) -> Result<Vec<Token>, Error> {
let mut tokens = Vec::new();
let mut chars = input.chars().peekable();
while let Some(&ch) = chars.peek() {
match ch {
// Skip whitespace.
c if c.is_whitespace() => {
chars.next();
}
// String literal.
'\'' => {
chars.next(); // consume opening quote
let mut s = String::new();
while let Some(&c) = chars.peek() {
if c == '\'' {
chars.next(); // consume closing quote
break;
}
s.push(c);
chars.next();
}
tokens.push(Token::Str(s));
}
'"' => {
chars.next(); // consume opening quote
let mut s = String::new();
while let Some(&c) = chars.peek() {
if c == '"' {
chars.next(); // consume closing quote
break;
}
s.push(c);
chars.next();
}
tokens.push(Token::Str(s));
}
// Operators.
'=' => {
chars.next();
tokens.push(Token::Eq);
}
'>' => {
chars.next();
if let Some(&'=') = chars.peek() {
chars.next();
tokens.push(Token::Ge);
} else {
tokens.push(Token::Gt);
}
}
'<' => {
chars.next();
if let Some(&'=') = chars.peek() {
chars.next();
tokens.push(Token::Le);
} else {
tokens.push(Token::Lt);
}
}
'!' => {
chars.next();
if let Some(&'=') = chars.peek() {
chars.next();
tokens.push(Token::Ne);
} else {
tokens.push(Token::Not);
}
}
// Brackets and punctuation.
'[' => {
chars.next();
tokens.push(Token::LBracket);
}
']' => {
chars.next();
tokens.push(Token::RBracket);
}
'(' => {
chars.next();
tokens.push(Token::LParen);
}
')' => {
chars.next();
tokens.push(Token::RParen);
}
',' => {
chars.next();
tokens.push(Token::Comma);
}
':' => {
chars.next();
tokens.push(Token::Colon);
}
'.' => {
chars.next();
tokens.push(Token::Dot);
}
// Float or Int.
c if c.is_ascii_digit() || c == '.' => {
let mut text = String::new();
while let Some(&c) = chars.peek() {
if !(c.is_ascii_digit() || c == '.') {
break;
}
text.push(c);
chars.next();
}
if text.contains('.') {
tokens.push(Token::F64(text.parse::<f64>().unwrap()));
} else {
tokens.push(Token::I64(text.parse::<i64>().unwrap()));
}
}
// Ident.
c if c.is_alphanumeric() || c == '_' => {
let mut text = String::new();
while let Some(&c) = chars.peek() {
if !(c.is_alphanumeric() || c == '_') {
break;
}
text.push(c);
chars.next();
}
// Check if it's a keyword or literal
match text.as_str() {
"AND" => tokens.push(Token::And),
"OR" => tokens.push(Token::Or),
"IN" => tokens.push(Token::In),
"true" => tokens.push(Token::Bool(true)),
"false" => tokens.push(Token::Bool(false)),
"null" => tokens.push(Token::Null),
_ => tokens.push(Token::Ident(text)),
}
}
// Unsupported characters.
ch => {
return Err(Error::UnsupportedCharacter(ch));
}
}
}
Ok(tokens)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_token() {
let input = "name = 'John' AND age > 18";
let tokens = parse_token(input).unwrap();
assert_eq!(
tokens,
vec![
Token::Ident("name".to_string()),
Token::Eq,
Token::Str("John".to_string()),
Token::And,
Token::Ident("age".to_string()),
Token::Gt,
Token::I64(18),
]
);
let input = r#"name = "John" AND age IN [18, 19, 20, 21]"#;
let tokens = parse_token(input).unwrap();
assert_eq!(
tokens,
vec![
Token::Ident("name".to_string()),
Token::Eq,
Token::Str("John".to_string()),
Token::And,
Token::Ident("age".to_string()),
Token::In,
Token::LBracket,
Token::I64(18),
Token::Comma,
Token::I64(19),
Token::Comma,
Token::I64(20),
Token::Comma,
Token::I64(21),
Token::RBracket,
]
);
let input = r#"matches(name, "^J.*n$")"#;
let tokens = parse_token(input).unwrap();
assert_eq!(
tokens,
vec![
Token::Ident("matches".to_string()),
Token::LParen,
Token::Ident("name".to_string()),
Token::Comma,
Token::Str("^J.*n$".to_string()),
Token::RParen,
]
);
let input = r#"name != null"#;
let tokens = parse_token(input).unwrap();
assert_eq!(
tokens,
vec![Token::Ident("name".to_string()), Token::Ne, Token::Null,]
);
let input = r#"open = 1.5 AND age = 18 AND is_peter = true"#;
let tokens = parse_token(input).unwrap();
assert_eq!(
tokens,
vec![
Token::Ident("open".to_string()),
Token::Eq,
Token::F64(1.5),
Token::And,
Token::Ident("age".to_string()),
Token::Eq,
Token::I64(18),
Token::And,
Token::Ident("is_peter".to_string()),
Token::Eq,
Token::Bool(true),
]
);
let input = r#"name.to_uppercase() = 'JOHN'"#;
let tokens = parse_token(input).unwrap();
assert_eq!(
tokens,
vec![
Token::Ident("name".to_string()),
Token::Dot,
Token::Ident("to_uppercase".to_string()),
Token::LParen,
Token::RParen,
Token::Eq,
Token::Str("JOHN".to_string()),
]
);
let input = r#"name.contains('John')"#;
let tokens = parse_token(input).unwrap();
assert_eq!(
tokens,
vec![
Token::Ident("name".to_string()),
Token::Dot,
Token::Ident("contains".to_string()),
Token::LParen,
Token::Str("John".to_string()),
Token::RParen,
]
);
let input = r#"type(maybe_i64_or_f64) IN ['i64', 'f64']"#;
let tokens = parse_token(input).unwrap();
assert_eq!(
tokens,
vec![
Token::Ident("type".to_string()),
Token::LParen,
Token::Ident("maybe_i64_or_f64".to_string()),
Token::RParen,
Token::In,
Token::LBracket,
Token::Str("i64".to_string()),
Token::Comma,
Token::Str("f64".to_string()),
Token::RBracket,
]
);
let input = r#"type(foo.contains('bar')) = 'i64'"#;
let tokens = parse_token(input).unwrap();
assert_eq!(
tokens,
vec![
Token::Ident("type".to_string()),
Token::LParen,
Token::Ident("foo".to_string()),
Token::Dot,
Token::Ident("contains".to_string()),
Token::LParen,
Token::Str("bar".to_string()),
Token::RParen,
Token::RParen,
Token::Eq,
Token::Str("i64".to_string()),
]
);
}
}