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
// Match-value expression parsing (patterns / match arms), without struct-literal path.
use crate::lexer::Token;
use crate::parser::ast::*;
use crate::parser_impl::Parser;
impl Parser {
pub(in crate::parser) fn parse_match_value(
&mut self,
) -> Result<&'static Expression<'static>, String> {
// Parse a non-struct-literal expression for match values
// This is basically parse_binary_expression but without struct literal support
let mut left = match self.current_token() {
Token::LParen => {
self.advance();
// Check for empty tuple ()
if self.current_token() == &Token::RParen {
self.advance();
return Ok(self.alloc_expr(Expression::Tuple {
elements: vec![],
location: self.current_location(),
}));
}
// Parse the first expression inside parentheses
// Use parse_match_value recursively to avoid parsing assignment operators
let first_expr = self.parse_match_value()?;
// Check if it's a tuple (has comma) or just a parenthesized expression
if self.current_token() == &Token::Comma {
let mut elements = vec![first_expr];
while self.current_token() == &Token::Comma {
self.advance(); // consume comma
// Allow trailing comma
if self.current_token() == &Token::RParen {
break;
}
elements.push(self.parse_match_value()?);
}
self.expect(Token::RParen)?;
self.alloc_expr(Expression::Tuple {
elements,
location: self.current_location(),
})
} else {
// Just a parenthesized expression
self.expect(Token::RParen)?;
first_expr
}
}
Token::LBracket => {
// Array literal: [a, b, c] or array repeat: [value; count]
self.advance();
// Check for empty array []
if self.current_token() == &Token::RBracket {
self.advance();
return Ok(self.alloc_expr(Expression::Array {
elements: vec![],
location: self.current_location(),
}));
}
let first_element = self.parse_expression()?;
// Check for array repeat syntax: [value; count]
if self.current_token() == &Token::Semicolon {
self.advance();
let count = self.parse_expression()?;
self.expect(Token::RBracket)?;
// Represent as a macro invocation: vec![value; count]
return Ok(self.alloc_expr(Expression::MacroInvocation {
name: "vec".to_string(),
args: vec![first_element, count],
delimiter: MacroDelimiter::Brackets,
is_repeat: true, // This is vec![x; n] repeat syntax
location: self.current_location(),
}));
}
// Regular array literal
let mut elements = vec![first_element];
while self.current_token() == &Token::Comma {
self.advance(); // consume comma
// Allow trailing comma
if self.current_token() == &Token::RBracket {
break;
}
elements.push(self.parse_expression()?);
}
self.expect(Token::RBracket)?;
self.alloc_expr(Expression::Array {
elements,
location: self.current_location(),
})
}
Token::Ampersand => {
// Handle & and &mut unary operators
self.advance();
let is_mut = if self.current_token() == &Token::Mut {
self.advance();
true
} else {
false
};
let inner = self.parse_match_value()?;
self.alloc_expr(Expression::Unary {
op: if is_mut {
UnaryOp::MutRef
} else {
UnaryOp::Ref
},
operand: inner,
location: self.current_location(),
})
}
Token::Star => {
// Handle * dereference operator
self.advance();
let inner = self.parse_match_value()?;
self.alloc_expr(Expression::Unary {
op: UnaryOp::Deref,
operand: inner,
location: self.current_location(),
})
}
Token::Minus => {
// Handle - negation operator
self.advance();
let inner = self.parse_match_value()?;
self.alloc_expr(Expression::Unary {
op: UnaryOp::Neg,
operand: inner,
location: self.current_location(),
})
}
Token::Bang => {
// Handle ! not operator
self.advance();
let inner = self.parse_match_value()?;
self.alloc_expr(Expression::Unary {
op: UnaryOp::Not,
operand: inner,
location: self.current_location(),
})
}
Token::Ident(name) => {
let mut qualified_name = name.clone();
self.advance();
// Handle qualified paths with :: (e.g., std::fs::read)
while self.current_token() == &Token::ColonColon {
// Look ahead to see if there's an identifier after ::
if self.position + 1 < self.tokens.len() {
if let Token::Ident(next_name) = &self.tokens[self.position + 1].token {
// This is a qualified path segment
qualified_name.push_str("::");
qualified_name.push_str(next_name);
self.advance(); // consume ::
self.advance(); // consume identifier
} else if let Token::Lt = &self.tokens[self.position + 1].token {
// This is turbofish (e.g., Type::<T>), stop here
break;
} else {
// Unknown token after ::, stop here
break;
}
} else {
// No more tokens, stop
break;
}
}
// Don't check for { here - just create the identifier
// and continue to postfix operators
self.alloc_expr(Expression::Identifier {
name: qualified_name,
location: self.current_location(),
})
}
_ => self.parse_primary_expression()?,
};
// Handle postfix operators (., [, etc.) before binary operators
loop {
match self.current_token() {
Token::Dot => {
// Check for .await
if self.peek(1) == Some(&Token::Await) {
self.advance(); // consume '.'
self.advance(); // consume 'await'
left = self.alloc_expr(Expression::Await {
expr: left,
location: self.current_location(),
});
} else {
self.advance();
let field = match self.current_token() {
Token::Ident(name) => {
let name = name.clone();
self.advance();
name
}
Token::IntLiteral(n) | Token::IntLiteralSuffixed(n, _) => {
let field_name = n.to_string();
self.advance();
field_name
}
_ => {
return Err("Expected field or method name after .".to_string());
}
};
left = self.alloc_expr(Expression::FieldAccess {
object: left,
field,
location: self.current_location(),
});
}
}
Token::LBracket => {
self.advance();
// Check for slice syntax: [start..end], [start..], [..end]
if self.current_token() == &Token::DotDot {
// [..end] - slice from beginning
self.advance(); // consume '..'
let end = if self.current_token() != &Token::RBracket {
Some(self.parse_expression()?)
} else {
None
};
self.expect(Token::RBracket)?;
// Desugar [..end] to .slice(0, end)
// We need to compute end_expr without holding onto left
let len_call = self.alloc_expr(Expression::MethodCall {
object: left,
method: "len".to_string(),
type_args: None,
arguments: vec![],
location: self.current_location(),
});
let end_expr = end.unwrap_or(len_call);
let zero_lit = self.alloc_expr(Expression::Literal {
value: Literal::Int(0),
location: self.current_location(),
});
left = self.alloc_expr(Expression::MethodCall {
object: left,
method: "slice".to_string(),
type_args: None,
arguments: vec![(None, zero_lit), (None, end_expr)],
location: self.current_location(),
});
} else {
let start_or_index = self.parse_expression()?;
// Check if this is a slice or regular index
if self.current_token() == &Token::DotDot {
// [start..] or [start..end] - slice syntax
self.advance(); // consume '..'
let end = if self.current_token() != &Token::RBracket {
Some(self.parse_expression()?)
} else {
None
};
self.expect(Token::RBracket)?;
// Desugar [start..end] to .slice(start, end)
let end_expr = end.unwrap_or_else(|| {
self.alloc_expr(Expression::MethodCall {
object: left,
method: "len".to_string(),
type_args: None,
arguments: vec![],
location: self.current_location(),
})
});
left = self.alloc_expr(Expression::MethodCall {
object: left,
method: "slice".to_string(),
type_args: None,
arguments: vec![(None, start_or_index), (None, end_expr)],
location: self.current_location(),
});
} else {
// Regular index: [i]
self.expect(Token::RBracket)?;
left = self.alloc_expr(Expression::Index {
object: left,
index: start_or_index,
location: self.current_location(),
});
}
}
}
Token::ColonColon => {
// Handle turbofish and static method calls in match values
self.advance(); // consume ::
if self.current_token() == &Token::Lt {
// Turbofish: expr::<Type>
self.advance(); // consume <
let mut types = vec![self.parse_type()?];
while self.current_token() == &Token::Comma {
self.advance();
if self.current_token() != &Token::Gt
&& self.current_token() != &Token::Shr
{
types.push(self.parse_type()?);
}
}
self.expect_gt_or_split_shr()?; // Handle nested generics
// Expect function call after turbofish
if self.current_token() == &Token::LParen {
self.advance();
let arguments = self.parse_arguments()?;
self.expect(Token::RParen)?;
left = self.alloc_expr(Expression::MethodCall {
object: left,
method: String::new(), // Empty method name signals turbofish call
type_args: Some(types),
arguments,
location: self.current_location(),
});
} else {
return Err("Expected '(' after turbofish".to_string());
}
} else if let Token::Ident(method) = self.current_token() {
// Static method or path continuation
let method = method.clone();
self.advance();
// Check for turbofish on this method
let type_args = if self.current_token() == &Token::ColonColon {
// Peek ahead to see if this is turbofish or path continuation
if self.peek(1) == Some(&Token::Lt) {
// Turbofish: Type::<T>
self.advance(); // consume ::
self.advance(); // consume <
let mut types = vec![self.parse_type()?];
while self.current_token() == &Token::Comma {
self.advance();
if self.current_token() != &Token::Gt
&& self.current_token() != &Token::Shr
{
types.push(self.parse_type()?);
}
}
self.expect_gt_or_split_shr()?; // Handle nested generics
Some(types)
} else {
// Not turbofish - don't consume ::, let the loop handle it
None
}
} else {
None
};
if self.current_token() == &Token::LParen {
self.advance();
let arguments = self.parse_arguments()?;
self.expect(Token::RParen)?;
left = self.alloc_expr(Expression::MethodCall {
object: left,
method,
type_args,
arguments,
location: self.current_location(),
});
} else {
// Just a path, treat as field access
left = self.alloc_expr(Expression::FieldAccess {
object: left,
field: method,
location: self.current_location(),
});
}
} else {
return Err("Expected '<' or identifier after '::'".to_string());
}
}
Token::LParen => {
// Function call
self.advance();
let mut arguments = Vec::new();
while self.current_token() != &Token::RParen {
let arg = self.parse_expression()?;
arguments.push((None, arg));
if self.current_token() == &Token::Comma {
self.advance();
}
}
self.expect(Token::RParen)?;
left = self.alloc_expr(Expression::Call {
function: left,
arguments,
location: self.current_location(),
});
}
_ => break,
}
}
// Handle binary operators
while let Some((op, precedence)) = self.get_binary_op() {
self.advance();
let right = self.parse_binary_expression(precedence + 1)?;
left = self.alloc_expr(Expression::Binary {
left,
op,
right,
location: self.current_location(),
});
}
Ok(left)
}
}