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
//! Lexical analyzer for Jsonnet
use crate::error::{JsonnetError, Result};
/// Token types for the Jsonnet lexer
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
/// Identifier
Identifier(String),
/// String literal
String(String),
/// Number literal
Number(f64),
/// Boolean literal
Boolean(bool),
/// Null literal
Null,
/// Left brace
LeftBrace,
/// Right brace
RightBrace,
/// Left bracket
LeftBracket,
/// Right bracket
RightBracket,
/// Left parenthesis
LeftParen,
/// Right parenthesis
RightParen,
/// Comma
Comma,
/// Colon
Colon,
/// Semicolon
Semicolon,
/// Dot
Dot,
/// Plus
Plus,
/// Minus
Minus,
/// Star
Star,
/// Slash
Slash,
/// Percent
Percent,
/// Equal
Equal,
/// Not equal
NotEqual,
/// Less than
LessThan,
/// Less than or equal
LessThanEqual,
/// Greater than
GreaterThan,
/// Greater than or equal
GreaterThanEqual,
/// And
And,
/// Or
Or,
/// Not
Not,
/// If
If,
/// Then
Then,
/// Else
Else,
/// For
For,
/// In
In,
/// Function
Function,
/// Local
Local,
/// Import
Import,
/// Importstr
Importstr,
/// Error
Error,
/// End of file
Eof,
}
/// Lexer for Jsonnet source code
pub struct Lexer {
/// Source code
source: String,
/// Current position
position: usize,
/// Current line
line: usize,
/// Current column
column: usize,
}
impl Lexer {
/// Create a new lexer
pub fn new(source: String) -> Self {
Self {
source,
position: 0,
line: 1,
column: 1,
}
}
/// Get the current character
fn current(&self) -> Option<char> {
self.source.chars().nth(self.position)
}
/// Advance to the next character
fn advance(&mut self) {
if let Some(ch) = self.current() {
if ch == '\n' {
self.line += 1;
self.column = 1;
} else {
self.column += 1;
}
self.position += 1;
}
}
/// Peek at the next character
fn peek(&self) -> Option<char> {
self.source.chars().nth(self.position + 1)
}
/// Check if the current position matches a string
fn matches(&self, s: &str) -> bool {
self.source[self.position..].starts_with(s)
}
/// Advance by a string
fn advance_by(&mut self, s: &str) {
for _ in 0..s.len() {
self.advance();
}
}
/// Tokenize the source code
pub fn tokenize(&mut self) -> Result<Vec<Token>> {
let mut tokens = Vec::new();
while let Some(ch) = self.current() {
match ch {
// Whitespace
' ' | '\t' | '\r' | '\n' => {
self.advance();
}
// Comments
'#' => {
while let Some(ch) = self.current() {
if ch == '\n' {
break;
}
self.advance();
}
}
// Identifiers and keywords
'a'..='z' | 'A'..='Z' | '_' => {
let start = self.position;
while let Some(ch) = self.current() {
if ch.is_alphanumeric() || ch == '_' {
self.advance();
} else {
break;
}
}
let ident = &self.source[start..self.position];
let token = match ident {
"null" => Token::Null,
"true" => Token::Boolean(true),
"false" => Token::Boolean(false),
"if" => Token::If,
"then" => Token::Then,
"else" => Token::Else,
"for" => Token::For,
"in" => Token::In,
"function" => Token::Function,
"local" => Token::Local,
"import" => Token::Import,
"importstr" => Token::Importstr,
"error" => Token::Error,
_ => Token::Identifier(ident.to_string()),
};
tokens.push(token);
}
// Numbers
'0'..='9' => {
let start = self.position;
while let Some(ch) = self.current() {
if ch.is_digit(10) || ch == '.' || ch == 'e' || ch == 'E' {
self.advance();
} else {
break;
}
}
let num_str = &self.source[start..self.position];
let num = num_str.parse().map_err(|_| {
JsonnetError::parse_error(self.line, self.column, format!("Invalid number: {}", num_str))
})?;
tokens.push(Token::Number(num));
}
// String literals
'"' => {
self.advance();
let start = self.position;
while let Some(ch) = self.current() {
if ch == '"' {
break;
}
if ch == '\\' {
self.advance(); // Skip escape character
}
self.advance();
}
if let Some('"') = self.current() {
self.advance();
let string = &self.source[start..self.position - 1];
tokens.push(Token::String(string.to_string()));
} else {
return Err(JsonnetError::parse_error(self.line, self.column, "Unterminated string"));
}
}
// Operators and punctuation
'{' => {
tokens.push(Token::LeftBrace);
self.advance();
}
'}' => {
tokens.push(Token::RightBrace);
self.advance();
}
'[' => {
tokens.push(Token::LeftBracket);
self.advance();
}
']' => {
tokens.push(Token::RightBracket);
self.advance();
}
'(' => {
tokens.push(Token::LeftParen);
self.advance();
}
')' => {
tokens.push(Token::RightParen);
self.advance();
}
',' => {
tokens.push(Token::Comma);
self.advance();
}
':' => {
tokens.push(Token::Colon);
self.advance();
}
';' => {
tokens.push(Token::Semicolon);
self.advance();
}
'.' => {
tokens.push(Token::Dot);
self.advance();
}
'+' => {
tokens.push(Token::Plus);
self.advance();
}
'-' => {
tokens.push(Token::Minus);
self.advance();
}
'*' => {
tokens.push(Token::Star);
self.advance();
}
'/' => {
tokens.push(Token::Slash);
self.advance();
}
'%' => {
tokens.push(Token::Percent);
self.advance();
}
'=' => {
if self.matches("==") {
tokens.push(Token::Equal);
self.advance_by("==");
} else {
tokens.push(Token::Equal);
self.advance();
}
}
'!' => {
if self.matches("!=") {
tokens.push(Token::NotEqual);
self.advance_by("!=");
} else {
tokens.push(Token::Not);
self.advance();
}
}
'<' => {
if self.matches("<=") {
tokens.push(Token::LessThanEqual);
self.advance_by("<=");
} else {
tokens.push(Token::LessThan);
self.advance();
}
}
'>' => {
if self.matches(">=") {
tokens.push(Token::GreaterThanEqual);
self.advance_by(">=");
} else {
tokens.push(Token::GreaterThan);
self.advance();
}
}
'&' => {
if self.matches("&&") {
tokens.push(Token::And);
self.advance_by("&&");
} else {
return Err(JsonnetError::parse_error(self.line, self.column, "Unexpected character: &"));
}
}
'|' => {
if self.matches("||") {
tokens.push(Token::Or);
self.advance_by("||");
} else {
return Err(JsonnetError::parse_error(self.line, self.column, "Unexpected character: |"));
}
}
_ => {
return Err(JsonnetError::parse_error(self.line, self.column, format!("Unexpected character: {}", ch)));
}
}
}
tokens.push(Token::Eof);
Ok(tokens)
}
}