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
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
Number(f64),
Identifier(String),
Percentage, // %
Plus, // +
Minus, // -
Star, // *
Slash, // /
Caret, // ^
Equal, // =
Arrow, // =>
In, // 'in' or 'to'
LPar, // (
RPar, // )
Comma, // ,
LBrack, // [
RBrack, // ]
// Comparisons
Less, // <
LessEq, // <=
Greater, // >
GreaterEq, // >=
DoubleEq, // ==
NotEq, // !=
// Logical
And, // and
Or, // or
Not, // not
// Bitwise
Ampersand, // &
Pipe, // |
Tilde, // ~
LShift, // <<
RShift, // >>
}
pub struct Lexer<'a> {
chars: std::iter::Peekable<std::str::CharIndices<'a>>,
input: &'a str,
}
impl<'a> Lexer<'a> {
pub fn new(input: &'a str) -> Self {
Self {
chars: input.char_indices().peekable(),
input,
}
}
pub fn lex(mut self) -> Result<Vec<Token>, String> {
let mut tokens = Vec::new();
while let Some(&(idx, ch)) = self.chars.peek() {
if ch.is_whitespace() {
self.chars.next();
continue;
}
match ch {
'+' => {
self.chars.next();
tokens.push(Token::Plus);
}
'-' => {
self.chars.next();
tokens.push(Token::Minus);
}
'*' => {
self.chars.next();
tokens.push(Token::Star);
}
'/' => {
self.chars.next();
tokens.push(Token::Slash);
}
'^' => {
self.chars.next();
tokens.push(Token::Caret);
}
'&' => {
self.chars.next();
tokens.push(Token::Ampersand);
}
'|' => {
self.chars.next();
tokens.push(Token::Pipe);
}
'~' => {
self.chars.next();
tokens.push(Token::Tilde);
}
'(' => {
self.chars.next();
tokens.push(Token::LPar);
}
')' => {
self.chars.next();
tokens.push(Token::RPar);
}
',' => {
self.chars.next();
tokens.push(Token::Comma);
}
'[' => {
self.chars.next();
tokens.push(Token::LBrack);
}
']' => {
self.chars.next();
tokens.push(Token::RBrack);
}
'%' => {
self.chars.next();
tokens.push(Token::Percentage);
}
'=' => {
self.chars.next();
if let Some(&(_, '>')) = self.chars.peek() {
self.chars.next();
tokens.push(Token::Arrow);
} else if let Some(&(_, '=')) = self.chars.peek() {
self.chars.next();
tokens.push(Token::DoubleEq);
} else {
tokens.push(Token::Equal);
}
}
'<' => {
self.chars.next();
if let Some(&(_, '=')) = self.chars.peek() {
self.chars.next();
tokens.push(Token::LessEq);
} else if let Some(&(_, '<')) = self.chars.peek() {
self.chars.next();
tokens.push(Token::LShift);
} else {
tokens.push(Token::Less);
}
}
'>' => {
self.chars.next();
if let Some(&(_, '=')) = self.chars.peek() {
self.chars.next();
tokens.push(Token::GreaterEq);
} else if let Some(&(_, '>')) = self.chars.peek() {
self.chars.next();
tokens.push(Token::RShift);
} else {
tokens.push(Token::Greater);
}
}
'!' => {
self.chars.next();
if let Some(&(_, '=')) = self.chars.peek() {
self.chars.next();
tokens.push(Token::NotEq);
} else {
return Err(format!("Unexpected character '!' at position {}", idx));
}
}
'$' => {
// Treat currency sign as an identifier for standard parsing
self.chars.next();
tokens.push(Token::Identifier("$".to_string()));
}
_ if ch.is_ascii_digit() => {
// Check for hex or binary prefix
let mut chars_clone = self.chars.clone();
chars_clone.next(); // consume current digit (which would be '0' for hex/bin)
if ch == '0'
&& let Some((_, next_ch)) = chars_clone.peek() {
if *next_ch == 'x' || *next_ch == 'X' {
self.chars.next();
self.chars.next();
let token = self.lex_hex_number(idx + 2)?;
tokens.push(token);
continue;
} else if *next_ch == 'b' || *next_ch == 'B' {
self.chars.next();
self.chars.next();
let token = self.lex_bin_number(idx + 2)?;
tokens.push(token);
continue;
}
}
let token = self.lex_number(idx)?;
tokens.push(token);
}
_ if ch.is_alphabetic() || ch == '_' => {
let token = self.lex_identifier(idx);
tokens.push(token);
}
_ => {
return Err(format!("Unexpected character '{}' at position {}", ch, idx));
}
}
}
Ok(tokens)
}
fn lex_hex_number(&mut self, start_idx: usize) -> Result<Token, String> {
let mut end_idx = start_idx;
while let Some(&(idx, ch)) = self.chars.peek() {
if ch.is_ascii_hexdigit() {
self.chars.next();
end_idx = idx + ch.len_utf8();
} else {
break;
}
}
if end_idx == start_idx {
return Err(format!("Empty hexadecimal literal at position {}", start_idx));
}
let hex_str = &self.input[start_idx..end_idx];
match i64::from_str_radix(hex_str, 16) {
Ok(val) => Ok(Token::Number(val as f64)),
Err(e) => Err(format!("Failed to parse hex number '{}': {}", hex_str, e)),
}
}
fn lex_bin_number(&mut self, start_idx: usize) -> Result<Token, String> {
let mut end_idx = start_idx;
while let Some(&(idx, ch)) = self.chars.peek() {
if ch == '0' || ch == '1' {
self.chars.next();
end_idx = idx + ch.len_utf8();
} else {
break;
}
}
if end_idx == start_idx {
return Err(format!("Empty binary literal at position {}", start_idx));
}
let bin_str = &self.input[start_idx..end_idx];
match i64::from_str_radix(bin_str, 2) {
Ok(val) => Ok(Token::Number(val as f64)),
Err(e) => Err(format!("Failed to parse binary number '{}': {}", bin_str, e)),
}
}
fn lex_number(&mut self, start_idx: usize) -> Result<Token, String> {
let mut end_idx = start_idx;
let mut has_decimal = false;
while let Some(&(idx, ch)) = self.chars.peek() {
if ch.is_ascii_digit() {
self.chars.next();
end_idx = idx + ch.len_utf8();
} else if ch == '.' && !has_decimal {
// Peek ahead to ensure there is a digit after the dot
self.chars.next();
if let Some(&(_, next_ch)) = self.chars.peek() {
if next_ch.is_ascii_digit() {
has_decimal = true;
end_idx = idx + ch.len_utf8();
} else {
// The dot is not followed by a digit (e.g., standard punctuation or end of input)
// Treat the number as finished before the dot
break;
}
} else {
break;
}
} else {
break;
}
}
let num_str = &self.input[start_idx..end_idx];
match num_str.parse::<f64>() {
Ok(val) => Ok(Token::Number(val)),
Err(e) => Err(format!("Failed to parse number '{}': {}", num_str, e)),
}
}
fn lex_identifier(&mut self, start_idx: usize) -> Token {
let mut end_idx = start_idx;
while let Some(&(idx, ch)) = self.chars.peek() {
if ch.is_alphanumeric() || ch == '_' || ch == '/' {
// We allow '/' inside unit identifiers (e.g., m/s or km/h)
self.chars.next();
end_idx = idx + ch.len_utf8();
} else {
break;
}
}
let ident_str = &self.input[start_idx..end_idx];
match ident_str {
"in" | "to" => Token::In,
"and" => Token::And,
"or" => Token::Or,
"not" => Token::Not,
_ => Token::Identifier(ident_str.to_string()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic_lexing() {
let lexer = Lexer::new("x = 10 + 20.5 =>");
let tokens = lexer.lex().unwrap();
assert_eq!(
tokens,
vec![
Token::Identifier("x".to_string()),
Token::Equal,
Token::Number(10.0),
Token::Plus,
Token::Number(20.5),
Token::Arrow,
]
);
}
#[test]
fn test_units_lexing() {
let lexer = Lexer::new("10m + 50cm in feet");
let tokens = lexer.lex().unwrap();
assert_eq!(
tokens,
vec![
Token::Number(10.0),
Token::Identifier("m".to_string()),
Token::Plus,
Token::Number(50.0),
Token::Identifier("cm".to_string()),
Token::In,
Token::Identifier("feet".to_string()),
]
);
}
#[test]
fn test_currency_symbol_lexing() {
let lexer = Lexer::new("$100 to EUR");
let tokens = lexer.lex().unwrap();
assert_eq!(
tokens,
vec![
Token::Identifier("$".to_string()),
Token::Number(100.0),
Token::In,
Token::Identifier("EUR".to_string()),
]
);
}
#[test]
fn test_derived_units_lexing() {
let lexer = Lexer::new("50km/h");
let tokens = lexer.lex().unwrap();
assert_eq!(
tokens,
vec![
Token::Number(50.0),
Token::Identifier("km/h".to_string()),
]
);
}
#[test]
fn test_percentages_lexing() {
let lexer = Lexer::new("100 - 15%");
let tokens = lexer.lex().unwrap();
assert_eq!(
tokens,
vec![
Token::Number(100.0),
Token::Minus,
Token::Number(15.0),
Token::Percentage,
]
);
}
}