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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
pub mod ast;
pub mod precedence;
pub mod rules;
pub mod tests;
use crate::scanner::token::*;
use crate::scanner::*;
use ast::expr::*;
use ast::stmt::*;
use ast::*;
use self::precedence::Precedence;
type ParseError = String;
type ParseErrors = Vec<ParseError>;
#[derive(Default)]
pub struct Parser {
scanner: Scanner,
previous: Token,
current: Token,
peek_next: Token,
errors: ParseErrors,
in_match_pattern: bool,
}
impl Parser {
pub fn new(scanner: Scanner) -> Self {
let mut parser = Self {
scanner,
..Default::default()
};
parser.next_token();
parser.next_token();
parser
}
fn next_token(&mut self) {
self.previous = self.current.clone();
self.current = self.peek_next.clone();
self.peek_next = self.scanner.next_token();
}
fn prev_token_is(&self, ttype: &TokenType) -> bool {
self.previous.ttype == *ttype
}
fn curr_token_is(&self, ttype: &TokenType) -> bool {
self.current.ttype == *ttype
}
fn peek_token_is(&self, ttype: &TokenType) -> bool {
self.peek_next.ttype == *ttype
}
fn expect_peek(&mut self, ttype: &TokenType) -> bool {
if self.peek_token_is(ttype) {
self.next_token();
true
} else {
self.peek_error(ttype);
false
}
}
// Report error at a specified line. This is used for reporting errors
// in expressions that had an error in a prior sub expression which
// causes the parser to synchronize and skip tokens until the next
// statement. This helps report error at a line prior to synchronization.
pub fn push_error_at(&mut self, err: &str, line: usize) {
self.errors.push(format!("[line {}] {}", line, err));
self.synchronize();
}
pub fn push_error(&mut self, err: &str) {
self.push_error_at(err, self.scanner.get_line())
}
pub fn parse_errors(&self) -> &Vec<String> {
&self.errors
}
pub fn peek_error(&mut self, ttype: &TokenType) {
let msg = format!(
"expected token {}, got {} instead",
ttype, self.peek_next.ttype
);
self.push_error(&msg);
}
// Synchronize parser upon encountering error
fn synchronize(&mut self) {
while !self.peek_token_is(&TokenType::Eof) {
if self.prev_token_is(&TokenType::Semicolon) {
return;
}
if matches!(
self.peek_next.ttype,
TokenType::Function
| TokenType::Let
| TokenType::If
| TokenType::Return
| TokenType::Loop
| TokenType::While
| TokenType::Break
| TokenType::Continue
| TokenType::Match
) {
return;
}
self.next_token();
}
}
pub fn parse_program(&mut self) -> Program {
let mut program = Program::default();
while self.current.ttype != TokenType::Eof {
// TODO: Revisit error handling
if let Ok(stmt) = self.parse_statement() {
program.statements.push(stmt)
}
self.next_token();
}
program
}
fn parse_statement(&mut self) -> Result<Statement, ParseError> {
match self.current.ttype {
TokenType::Let => self.parse_let_statement(),
TokenType::Return => self.parse_return_statement(),
TokenType::Loop => self.parse_loop_statement(None),
TokenType::While => self.parse_while_statement(None),
TokenType::Break => self.parse_break_statement(),
TokenType::Continue => self.parse_continue_statement(),
TokenType::Function => self.parse_function_statement(),
TokenType::LeftBrace => self.parse_block_begin(),
TokenType::Filter => self.parse_filter_statement(),
_ => self.parse_expr_statement(),
}
}
fn parse_let_statement(&mut self) -> Result<Statement, ParseError> {
let token_let = self.current.clone();
if !self.expect_peek(&TokenType::Identifier) {
return Ok(Statement::Invalid);
}
let token_ident = self.current.clone();
if !self.expect_peek(&TokenType::Assign) {
return Ok(Statement::Invalid);
}
self.next_token();
let value = self.parse_expression(Precedence::Lowest, false);
// If the value expression is a function, the update the function node
// with the identifier contained from compiling the let statement since
// the name of the function is not available within the function
// expression. This is needed so that self references to recursive
// functions can be parsed effectively.
let value = if let Expression::Function(mut func) = value.clone() {
func.name = token_ident.literal.clone();
// use the updated function
Expression::Function(func)
} else {
// if any other expression, use the original value
value
};
if self.peek_token_is(&TokenType::Semicolon) {
self.next_token();
}
let identifier = Identifier {
token: token_ident.clone(),
value: token_ident.literal,
context: ParseContext {
access: AccessType::Set,
},
};
let let_stmt = LetStmt {
token: token_let,
name: identifier,
value,
};
Ok(Statement::Let(let_stmt))
}
fn parse_return_statement(&mut self) -> Result<Statement, ParseError> {
let token_ret = self.current.clone();
let value = if self.peek_token_is(&TokenType::Semicolon)
|| self.peek_token_is(&TokenType::RightBrace)
{
// No return value
None
} else {
self.next_token();
Some(self.parse_expression(Precedence::Lowest, false))
};
if self.peek_token_is(&TokenType::Semicolon) {
self.next_token();
}
let ret_stmt = ReturnStmt {
token: token_ret,
value,
};
Ok(Statement::Return(ret_stmt))
}
fn parse_block_begin(&mut self) -> Result<Statement, ParseError> {
Ok(Statement::Block(self.parse_block_statement()))
}
fn parse_loop_statement(&mut self, label: Option<Token>) -> Result<Statement, ParseError> {
let token = self.current.clone();
if !self.expect_peek(&TokenType::LeftBrace) {
return Ok(Statement::Invalid);
}
let body = self.parse_block_statement();
Ok(Statement::Loop(LoopStmt { token, label, body }))
}
fn parse_while_statement(&mut self, label: Option<Token>) -> Result<Statement, ParseError> {
let token = self.current.clone();
self.next_token();
let condition = self.parse_expression(Precedence::Lowest, false);
if !self.expect_peek(&TokenType::LeftBrace) {
return Ok(Statement::Invalid);
}
let body = self.parse_block_statement();
Ok(Statement::While(WhileStmt {
token,
label,
condition,
body,
}))
}
fn parse_break_statement(&mut self) -> Result<Statement, ParseError> {
// The break token
let token = self.current.clone();
// If there is a label, parse it
let label = if self.peek_token_is(&TokenType::Identifier) {
self.next_token();
Some(self.current.clone())
} else {
None
};
if self.peek_token_is(&TokenType::Semicolon) {
self.next_token();
}
let break_stmt = BreakStmt { token, label };
Ok(Statement::Break(break_stmt))
}
fn parse_continue_statement(&mut self) -> Result<Statement, ParseError> {
// The continue token
let token = self.current.clone();
// If there is a label, parse it
let label = if self.peek_token_is(&TokenType::Identifier) {
self.next_token();
Some(self.current.clone())
} else {
None
};
if self.peek_token_is(&TokenType::Semicolon) {
self.next_token();
}
let con_stmt = ContinueStmt { token, label };
Ok(Statement::Continue(con_stmt))
}
// Function statements are of the form 'fn <name>(<params>) { <body> }'.
// They differ from function expressions and are parsed differently.
// However, the underlying implementations are the same.
fn parse_function_statement(&mut self) -> Result<Statement, ParseError> {
let token = self.current.clone(); // fn keyword
if !self.peek_token_is(&TokenType::Identifier) {
return self.parse_expr_statement();
}
// Advance to the function name
self.next_token();
let name = self.current.clone(); // fn name
if !self.expect_peek(&TokenType::LeftParen) {
return Ok(Statement::Invalid);
}
let params = self.parse_function_params();
if !self.expect_peek(&TokenType::LeftBrace) {
return Ok(Statement::Invalid);
}
let body = self.parse_block_statement();
// The name of the string is known here
Ok(Statement::Function(FunctionLiteral {
name: name.literal,
token,
params,
body,
}))
}
/// Parse a filter statement. The filter pattern and action are optional.
/// But either one of them must be present.
fn parse_filter_statement(&mut self) -> Result<Statement, ParseError> {
let token: Token = self.current.clone();
// advance to the filter pattern expression or to the action
self.next_token();
let pattern = if self.curr_token_is(&TokenType::LeftBrace) {
FilterPattern::None
} else if self.curr_token_is(&TokenType::End) {
self.next_token();
if !self.curr_token_is(&TokenType::LeftBrace) {
self.push_error("expected '{' after 'end'");
return Ok(Statement::Invalid);
}
FilterPattern::End
} else {
let filter = FilterPattern::Expr(Box::new(
self.parse_expression(Precedence::Assignment, false),
));
// advance to the left brace
if self.peek_token_is(&TokenType::LeftBrace) {
self.next_token();
}
filter
};
let action = if self.curr_token_is(&TokenType::LeftBrace) {
Some(self.parse_block_statement())
} else {
None
};
if pattern.is_none() && action.is_none() {
self.push_error("a filter statement must have a pattern or an action");
return Ok(Statement::Invalid);
}
Ok(Statement::Filter(FilterStmt {
token,
pattern,
action,
}))
}
// Parse a statement as expression statement if it is none of the
// other statement types. However, if the statement begins with
// an idenifier and a colon, it is a label and should be followed
// by a loop statement.
fn parse_expr_statement(&mut self) -> Result<Statement, ParseError> {
let token_expr = self.current.clone();
if self.curr_token_is(&TokenType::Identifier) && self.peek_token_is(&TokenType::Colon) {
// Advance the token to the colon (':')
self.next_token();
// match peek token to be loop and while
return match self.peek_next.ttype {
TokenType::Loop => {
// Advance the token to the loop/while keyword
self.next_token();
// pass the label token to the loop statement
return self.parse_loop_statement(Some(token_expr));
}
TokenType::While => {
self.next_token();
// pass the label token to the while statement
return self.parse_while_statement(Some(token_expr));
}
_ => {
// only loops support labels for now
Ok(Statement::Invalid)
}
};
}
let expr = self.parse_expression(Precedence::Assignment, false);
if self.peek_token_is(&TokenType::Semicolon) {
self.next_token();
}
// Mark if the statement is an assignment expression statement
let is_assign = matches!(expr, Expression::Assign(_));
Ok(Statement::Expr(ExpressionStmt {
token: token_expr,
value: expr,
is_assign,
}))
}
/// Parsing an expression statement starts with 'parse_expression()'
/// It first tries to find a prefix parser for the current token called.
/// with 'Precedence::Lowest' as the parameter. The first token will always
/// belong to some kind of prefix expression. It may turn out to be nested
/// as an operand inside one or more infix expressions but as the code is
/// read from left to right, the first token always belong to a prefix
/// expression. It can be as simple as an identifier or a numeric
/// expression, or as complicated as prefix expression such as '-' that
/// accepts an arbitrary expression as its operand. If there is no prefix
/// parse function, it is a syntax error. Otherwise, call the prefix
/// parse function to parse the the current token and assign the resulting
/// AST node into 'left_expr'. After parsing that, the prefix expression
/// is done. Now look for an infix parser for the next token. If one is found,
/// it means the prefix expression that was already compiled might be an
/// operand to the infix operator, but only if 'precedence' is low enough
/// to permit the infix operator. If the next token is too low precedence,
/// or isn't an infix operator at all, the parsing is done. Otherwise,
/// consume the operator and hand off control to the infix parser that was
/// found. It consumes whatever other tokens it needs (the operator and
/// the right operand) and returns back to parse_expression(). The infix
/// parse function then creates a binary operator ast node with the left
/// and right operand and the operator. Note that the infix parse function
/// is passed the left operand as argument since it was already consumed.
/// Also note that the right operand itself can be an prefix expression
/// in itself (e.g. a numeric expression) or another infix expression
/// such as a binary '+'. Then the loop continues and see if the next
/// token is also a valid infix operator that can take the entire preceding
/// expression as its operand. Continue the loop crunching through infix
/// operators and their operands until a token is hit that that isn't an
/// infix operator or is too low precedence.
///
/// The associativity of infix expressions depends on the precedence
/// condition used in the loop.
/// 'a + b + c' -->> ((a + b) + c) when 'precedence < self.peek_precedence()'
/// 'a + b + c' -->> (a + (b + c)) when 'precedence <= self.peek_precedence()'
///
fn parse_expression(&mut self, precedence: Precedence, property: bool) -> Expression {
let can_assign = precedence <= Precedence::Assignment;
self.peek_invalid_assignment(can_assign);
// If there is a prefix parser for the current token
if let Some(prefix) = self.curr_prefix() {
let mut left_expr = prefix(self, property);
// Continue parsing if the next token is not a semi-colon and has
// a valid precedence for the current infix expression
while self.peek_valid_expression(precedence) {
if let Some(infix) = &self.peek_infix() {
self.next_token();
left_expr = infix(self, left_expr);
}
}
left_expr
} else {
self.no_prefix_parse_error();
Expression::Invalid
}
}
fn no_prefix_parse_error(&mut self) {
let msg = format!("failed to parse token '{}'", self.current);
self.push_error(&msg);
}
pub fn print_errors(&self) -> bool {
if self.errors.is_empty() {
return false;
}
for msg in &self.errors {
eprintln!("{}", msg);
}
true
}
pub fn peek_invalid_assignment(&mut self, can_assign: bool) {
// Check if the precedence is low enough to allow assignment
if !can_assign && self.peek_token_is(&TokenType::Assign) {
self.push_error("Invalid assignment target");
}
}
}