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
use std::borrow::Cow;
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Token<'a> {
/// An identifier (variable name).
///
/// Identifiers start with a letter or underscore and can contain
/// letters, digits, and underscores.
///
/// Examples: `name`, `user_id`, `_temp`
Ident(&'a str),
/// Assignment operator `=`.
Assign,
/// An integer literal.
///
/// Contains the string representation of the integer for parsing.
/// Examples: `"42"`, `"0"`, `"1000"`
Int(&'a str),
/// A floating-point literal.
///
/// Contains the string representation of the float for parsing.
/// Must contain a decimal point followed by digits.
/// Examples: `"3.14"`, `"0.5"`
Float(&'a str),
/// Left parenthesis `(`.
LParen,
/// Right parenthesis `)`.
RParen,
/// Left square bracket `[`.
LSquare,
/// Right square bracket `]`.
RSquare,
/// Left curly brace `{`.
LCurly,
/// Right curly brace `}`.
RCurly,
/// Colon `:`.
Colon,
/// Semicolon `;`.
Semicolon,
/// Question mark `?` (used in ternary conditionals).
Question,
/// Pipe `|` (single pipe, not logical OR).
Pipe,
/// Underscore `_`.
Underscore,
/// A string literal enclosed in quotes.
///
/// String literals can be enclosed in either single or double quotes
/// and support escape sequences like `\n`, `\t`, `\\`, etc.
///
/// The `Cow` allows for zero-copy when there are no escape sequences,
/// but allocates when escape processing is needed.
///
/// Examples: `"hello"`, `'world'`, `"line\nbreak"`
Literal(Cow<'a, str>),
// Unary and binary operations
/// Logical NOT `!`.
Not,
/// Addition `+`.
Plus,
/// Subtraction/negation `-`.
Minus,
/// Multiplication `*`.
Star,
/// Division `/`.
Slash,
// Comparison operators
/// Equality `==`.
Equals,
/// Inequality `!=`.
NotEquals,
/// Greater than `>`.
GreaterThan,
/// Greater than or equal `>=`.
GreaterThanEquals,
/// Less than `<`.
LessThan,
/// Less than or equal `<=`.
LessThanEquals,
/// Logical AND `&&`.
And,
/// Logical OR `||`.
Or,
/// An unknown/unexpected character.
///
/// Used when the lexer encounters a character it doesn't recognize.
/// This allows the parser to handle the error gracefully.
Unknown(char),
}
pub struct TemplateLexer<'a> {
input: &'a str,
bytes: &'a [u8],
cursor: usize,
}
impl<'a> TemplateLexer<'a> {
pub fn new(input: &'a str) -> Self {
Self {
input,
bytes: input.as_bytes(),
cursor: 0,
}
}
#[inline]
fn current(&self) -> u8 {
if self.cursor < self.bytes.len() {
self.bytes[self.cursor]
} else {
0
}
}
#[inline]
fn peek(&mut self) -> u8 {
if self.cursor + 1 < self.bytes.len() {
self.bytes[self.cursor + 1]
} else {
0
}
}
#[inline]
fn advance(&mut self) {
if self.cursor < self.bytes.len() {
self.cursor += 1;
}
}
#[inline]
fn skip_whitespace(&mut self) {
while self.cursor < self.bytes.len() && self.current().is_ascii_whitespace() {
self.cursor += 1;
}
}
fn read_literal(&mut self, del: char) -> Cow<'a, str> {
let del = del as u8;
let start = self.cursor;
let mut tmp_cursor = start;
let mut escaped = false;
while tmp_cursor < self.bytes.len() {
match self.bytes[tmp_cursor] {
c if c == del => break,
b'\\' => {
escaped = true;
tmp_cursor += 2;
}
_ => tmp_cursor += 1,
}
}
if !escaped {
let len = tmp_cursor - start;
self.cursor = tmp_cursor + 1; // skip closing quote
return Cow::Borrowed(&self.input[start..start + len]);
}
let mut out = String::with_capacity(tmp_cursor - start);
while self.cursor < self.bytes.len() {
match self.current() {
c if c == del => {
self.advance();
break;
}
b'\\' => {
self.advance();
if self.cursor < self.bytes.len() {
match self.current() {
b'n' => out.push('\n'),
b't' => out.push('\t'),
b'r' => out.push('\r'),
b'\\' => out.push('\\'),
b'0' => out.push('\0'),
c if c == del => out.push(del as char),
c => out.push(c as char),
}
self.advance();
}
}
c => {
out.push(c as char);
self.advance();
}
}
}
Cow::Owned(out)
}
fn read_ident(&mut self, start: usize) -> &'a str {
while self.cursor < self.bytes.len() {
let b = self.bytes[self.cursor];
if b.is_ascii_alphabetic() || b.is_ascii_digit() || b == b'_' {
self.cursor += 1;
} else {
break;
}
}
&self.input[start..self.cursor]
}
fn read_number(&mut self, start: usize) -> Token<'a> {
let mut is_float = false;
while self.cursor < self.bytes.len() && self.current().is_ascii_digit() {
self.cursor += 1;
}
if self.current() == b'.' && self.peek().is_ascii_digit() {
is_float = true;
self.cursor += 1; // skip '.'
while self.cursor < self.bytes.len() && self.current().is_ascii_digit() {
self.cursor += 1;
}
}
let slice = &self.input[start..self.cursor];
if is_float {
Token::Float(slice)
} else {
Token::Int(slice)
}
}
#[inline]
fn check_double(
&mut self,
expected: u8,
matched: Token<'a>,
unmatched: Token<'a>,
) -> Token<'a> {
if self.current() == expected {
self.advance();
matched
} else {
unmatched
}
}
fn next_token(&mut self) -> Option<Token<'a>> {
self.skip_whitespace();
if self.cursor >= self.bytes.len() {
return None;
}
let start = self.cursor;
let ch = self.current();
self.advance();
match ch {
b'(' => Some(Token::LParen),
b')' => Some(Token::RParen),
b'[' => Some(Token::LSquare),
b']' => Some(Token::RSquare),
b'{' => Some(Token::LCurly),
b'}' => Some(Token::RCurly),
b':' => Some(Token::Colon),
b';' => Some(Token::Semicolon),
b'?' => Some(Token::Question),
b'+' => Some(Token::Plus),
b'-' => Some(Token::Minus),
b'*' => Some(Token::Star),
b'/' => Some(Token::Slash),
b'=' => Some(self.check_double(b'=', Token::Equals, Token::Assign)),
b'!' => Some(self.check_double(b'=', Token::NotEquals, Token::Not)),
b'<' => Some(self.check_double(b'=', Token::LessThanEquals, Token::LessThan)),
b'>' => Some(self.check_double(b'=', Token::GreaterThanEquals, Token::GreaterThan)),
b'&' => Some(self.check_double(b'&', Token::And, Token::Unknown('&'))),
b'|' => Some(self.check_double(b'|', Token::Or, Token::Pipe)),
b'"' => Some(Token::Literal(self.read_literal('"'))),
b'\'' => Some(Token::Literal(self.read_literal('\''))),
b'_' => {
let next = self.current();
if next.is_ascii_alphabetic() || next.is_ascii_digit() || next == b'_' {
Some(Token::Ident(self.read_ident(start)))
} else {
Some(Token::Underscore)
}
}
b if b.is_ascii_alphabetic() => Some(Token::Ident(self.read_ident(start))),
b if b.is_ascii_digit() => Some(self.read_number(start)),
b => Some(Token::Unknown(b as char)),
}
}
}
impl<'a> Iterator for TemplateLexer<'a> {
type Item = Token<'a>;
fn next(&mut self) -> Option<Self::Item> {
self.next_token()
}
}