-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.rs
686 lines (598 loc) · 21 KB
/
parser.rs
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
use crate::ast::{AstNode, SpannedAstNode};
use crate::error::ParsingError;
use crate::scanner::Scanner;
use crate::token::{Kind, Span, Token};
use crate::value::Value;
use std::iter::Peekable;
#[derive(Debug)]
pub struct Parser<'a> {
scanner: Peekable<Scanner<'a>>,
}
impl<'a> Parser<'a> {
pub fn new(source: &'a str) -> Self {
let scanner = Scanner::new(&source).peekable();
Parser { scanner }
}
/// Parse the source into a program - a list of declaration `AstNode`s
pub fn parse_program(&mut self) -> Result<Vec<SpannedAstNode>, Vec<ParsingError>> {
let mut program = vec![];
let mut errors = vec![];
while self.has_next() {
match self.declaration() {
Ok(decl) => program.push(decl),
Err(err) => {
self.synchronize();
errors.push(err);
}
}
}
if errors.is_empty() {
Ok(program)
} else {
Err(errors.drain(0..).collect())
}
}
fn declaration(&mut self) -> Result<SpannedAstNode, ParsingError> {
match self.next().kind {
Kind::Var => self.var_declaration(),
Kind::Class => self.class_declaration(),
Kind::Fun => {
self.advance();
self.function_declaration()
}
_ => self.statement(),
}
}
fn var_declaration(&mut self) -> Result<SpannedAstNode, ParsingError> {
let keyword = self.advance();
let (name, _) = self.id_token()?;
let initializer = if self.next().kind == Kind::Equal {
self.advance();
let initializer = self.expression()?;
Some(Box::new(initializer))
} else {
None
};
let semi = self.eat(Kind::Semicolon)?;
let span = Span::merge(vec![&keyword.span, &semi.span]);
Ok(SpannedAstNode::new(
AstNode::VarDeclaration { name, initializer },
span,
))
}
fn class_declaration(&mut self) -> Result<SpannedAstNode, ParsingError> {
let keyword = self.eat(Kind::Class)?;
let (name, _) = self.id_token()?;
let superclass = if let Kind::Less = self.next().kind {
self.advance();
let (superclass_name, superclass_span) = self.id_token()?;
if superclass_name == name {
return Err(ParsingError::SelfInheritance {
span: superclass_span,
});
}
Some(superclass_name)
} else {
None
};
// Parse the class body (methods)
self.eat(Kind::LeftBrace)?;
let mut methods = vec![];
while self.next().kind != Kind::RightBrace {
methods.push(self.function_declaration()?);
}
let end_brace = self.eat(Kind::RightBrace)?;
let span = Span::merge(vec![&keyword.span, &end_brace.span]);
Ok(SpannedAstNode::new(
AstNode::ClassDeclaration {
name,
methods,
superclass,
},
span,
))
}
fn function_declaration(&mut self) -> Result<SpannedAstNode, ParsingError> {
let (name, name_span) = self.id_token()?;
self.eat(Kind::LeftParen)?;
let parameters = match self.next().kind {
Kind::RightParen => vec![],
Kind::IdentifierLiteral(_) => self.parameter_list()?,
_ => {
return Err(ParsingError::UnexpectedToken {
expected: "parameter list or ')'.".to_string(),
actual: self.advance(),
})
}
};
self.eat(Kind::RightParen)?;
let body = self.block_statement()?;
let span = Span::merge(vec![&name_span, &body.span]);
Ok(SpannedAstNode::new(
AstNode::FunDeclaration {
name,
parameters,
body: Box::new(body),
},
span,
))
}
fn statement(&mut self) -> Result<SpannedAstNode, ParsingError> {
match self.next().kind {
Kind::Print => self.print_statement(),
Kind::LeftBrace => self.block_statement(),
Kind::If => self.if_statement(),
Kind::While => self.while_statement(),
Kind::For => self.for_statement(),
Kind::Return => self.return_statement(),
_ => self.expression_statement(),
}
}
fn expression_statement(&mut self) -> Result<SpannedAstNode, ParsingError> {
let expression = self.expression()?;
let semi = self.eat(Kind::Semicolon)?;
let new_span = Span::merge(vec![&expression.span, &semi.span]);
Ok(SpannedAstNode::new(
AstNode::ExpressionStmt {
expression: Box::new(expression),
},
new_span,
))
}
fn return_statement(&mut self) -> Result<SpannedAstNode, ParsingError> {
let keyword = self.advance();
let (value, span) = match self.next().kind {
Kind::Semicolon => (None, keyword.span),
_ => {
let expr = self.expression()?;
let span = Span::merge(vec![&keyword.span, &expr.span]);
(Some(Box::new(expr)), span)
}
};
self.eat(Kind::Semicolon)?;
Ok(SpannedAstNode::new(AstNode::Return { value }, span))
}
fn for_statement(&mut self) -> Result<SpannedAstNode, ParsingError> {
let keyword = self.advance();
self.eat(Kind::LeftParen)?;
let initializer = match self.next().kind {
Kind::Var => Some(Box::new(self.var_declaration()?)),
Kind::Semicolon => {
self.advance();
None
}
_ => Some(Box::new(self.expression_statement()?)),
};
let condition = match self.next().kind {
Kind::Semicolon => None,
_ => Some(Box::new(self.expression()?)),
};
self.eat(Kind::Semicolon)?;
let update = match self.next().kind {
Kind::RightParen => None,
_ => Some(Box::new(self.expression()?)),
};
self.eat(Kind::RightParen)?;
let block = self.statement()?;
let span = Span::merge(vec![&keyword.span, &block.span]);
Ok(SpannedAstNode::new(
AstNode::For {
initializer,
condition,
update,
block: Box::new(block),
},
span,
))
}
fn while_statement(&mut self) -> Result<SpannedAstNode, ParsingError> {
let keyword = self.advance();
self.eat(Kind::LeftParen)?;
let condition = self.expression()?;
self.eat(Kind::RightParen)?;
let block = self.statement()?;
let span = Span::merge(vec![&keyword.span, &block.span]);
Ok(SpannedAstNode::new(
AstNode::While {
condition: Box::new(condition),
block: Box::new(block),
},
span,
))
}
fn if_statement(&mut self) -> Result<SpannedAstNode, ParsingError> {
let keyword = self.advance();
self.eat(Kind::LeftParen)?;
let condition = self.expression()?;
self.eat(Kind::RightParen)?;
let if_block = self.statement()?;
let mut span = Span::merge(vec![&keyword.span, &if_block.span]);
let else_block = if let Kind::Else = self.next().kind {
self.advance();
let stmt = self.statement()?;
span = Span::merge(vec![&span, &stmt.span]);
Some(Box::new(stmt))
} else {
None
};
Ok(SpannedAstNode::new(
AstNode::If {
condition: Box::new(condition),
if_block: Box::new(if_block),
else_block,
},
span,
))
}
fn block_statement(&mut self) -> Result<SpannedAstNode, ParsingError> {
let lbrace = self.advance();
let mut declarations = vec![];
loop {
match self.next().kind {
Kind::RightBrace => break,
_ => declarations.push(self.declaration()?),
}
}
let rbrace = self.eat(Kind::RightBrace)?;
let new_span = Span::merge(vec![&lbrace.span, &rbrace.span]);
Ok(SpannedAstNode::new(
AstNode::Block { declarations },
new_span,
))
}
fn print_statement(&mut self) -> Result<SpannedAstNode, ParsingError> {
let keyword = self.advance();
let expression = self.expression()?;
let semi = self.eat(Kind::Semicolon)?;
let new_span = Span::merge(vec![&keyword.span, &expression.span, &semi.span]);
Ok(SpannedAstNode::new(
AstNode::Print {
expression: Box::new(expression),
},
new_span,
))
}
fn expression(&mut self) -> Result<SpannedAstNode, ParsingError> {
self.assignment()
}
fn assignment(&mut self) -> Result<SpannedAstNode, ParsingError> {
let node = self.or()?;
if self.next().kind == Kind::Equal {
self.advance();
let rvalue = self.assignment()?;
let new_span = Span::merge(vec![&node.span, &rvalue.span]);
Ok(SpannedAstNode::new(
AstNode::Assignment {
lvalue: Box::new(node),
rvalue: Box::new(rvalue),
},
new_span,
))
} else {
Ok(node)
}
}
fn or(&mut self) -> Result<SpannedAstNode, ParsingError> {
let mut node = self.and()?;
while self.next().kind == Kind::Or {
self.advance();
let right = self.and()?;
let new_span = Span::merge(vec![&node.span, &right.span]);
node = SpannedAstNode::new(
AstNode::Or {
left: Box::new(node),
right: Box::new(right),
},
new_span,
);
}
Ok(node)
}
fn and(&mut self) -> Result<SpannedAstNode, ParsingError> {
let mut node = self.equality()?;
while self.next().kind == Kind::And {
self.advance();
let right = self.equality()?;
let new_span = Span::merge(vec![&node.span, &right.span]);
node = SpannedAstNode::new(
AstNode::And {
left: Box::new(node),
right: Box::new(right),
},
new_span,
);
}
Ok(node)
}
fn equality(&mut self) -> Result<SpannedAstNode, ParsingError> {
let mut node = self.comparison()?;
while let Kind::EqualEqual | Kind::BangEqual = self.next().kind {
let operator = self.advance();
let right = self.comparison()?;
let new_span = Span::merge(vec![&node.span, &operator.span, &right.span]);
node = SpannedAstNode::new(
AstNode::Binary {
left: Box::new(node),
operator,
right: Box::new(right),
},
new_span,
);
}
Ok(node)
}
fn comparison(&mut self) -> Result<SpannedAstNode, ParsingError> {
let mut node = self.addition()?;
while let Kind::Less | Kind::LessEqual | Kind::Greater | Kind::GreaterEqual =
self.next().kind
{
let operator = self.advance();
let right = self.addition()?;
let new_span = Span::merge(vec![&node.span, &operator.span, &right.span]);
node = SpannedAstNode::new(
AstNode::Binary {
left: Box::new(node),
operator,
right: Box::new(right),
},
new_span,
);
}
Ok(node)
}
fn addition(&mut self) -> Result<SpannedAstNode, ParsingError> {
let mut node = self.multiplication()?;
while self.next().kind == Kind::Plus || self.next().kind == Kind::Minus {
let operator = self.advance();
let right = self.multiplication()?;
let new_span = Span::merge(vec![&node.span, &operator.span, &right.span]);
node = SpannedAstNode::new(
AstNode::Binary {
left: Box::new(node),
operator,
right: Box::new(right),
},
new_span,
);
}
Ok(node)
}
fn multiplication(&mut self) -> Result<SpannedAstNode, ParsingError> {
let mut node = self.unary()?;
while self.next().kind == Kind::Star || self.next().kind == Kind::Slash {
let operator = self.advance();
let right = self.unary()?;
let new_span = Span::merge(vec![&node.span, &operator.span, &right.span]);
node = SpannedAstNode::new(
AstNode::Binary {
left: Box::new(node),
operator,
right: Box::new(right),
},
new_span,
);
}
Ok(node)
}
fn unary(&mut self) -> Result<SpannedAstNode, ParsingError> {
match self.next().kind {
Kind::Minus | Kind::Bang => {
let operator = self.advance();
let expression = self.unary()?;
let new_span = Span::new(expression.span.start - 1, expression.span.end);
Ok(SpannedAstNode::new(
AstNode::Unary {
operator,
expression: Box::new(expression),
},
new_span,
))
}
_ => self.call(),
}
}
fn argument_list(&mut self) -> Result<Vec<SpannedAstNode>, ParsingError> {
let mut args = vec![];
args.push(self.expression()?);
while self.next().kind == Kind::Comma {
self.advance();
args.push(self.expression()?);
}
Ok(args)
}
fn call(&mut self) -> Result<SpannedAstNode, ParsingError> {
let mut node = self.primary()?;
loop {
match self.next().kind {
Kind::LeftParen => {
self.advance();
let arguments = match self.next().kind {
Kind::RightParen => vec![],
_ => self.argument_list()?,
};
let rparen = self.eat(Kind::RightParen)?;
let new_span = Span::merge(vec![&node.span, &rparen.span]);
node = SpannedAstNode::new(
AstNode::Invokation {
target: Box::new(node),
arguments,
},
new_span,
)
}
Kind::Dot => {
self.advance();
let (field_name, field_span) = self.id_token()?;
node = SpannedAstNode::new(
AstNode::FieldAccess {
target: Box::new(node),
name: field_name,
},
field_span,
)
}
_ => break,
}
}
Ok(node)
}
fn primary(&mut self) -> Result<SpannedAstNode, ParsingError> {
match &self.next().kind {
Kind::LeftParen => {
let lparen = self.advance();
let expression = self.expression()?;
let rparen = self.eat(Kind::RightParen)?;
let new_span = Span::merge(vec![&lparen.span, &rparen.span]);
Ok(SpannedAstNode::respan(expression, new_span))
}
Kind::IdentifierLiteral(name) => Ok(SpannedAstNode::new(
AstNode::Variable {
name: name.to_string(),
},
self.advance().span,
)),
Kind::NumberLiteral(_) => self.number_literal(),
Kind::StringLiteral(_) => self.string_literal(),
Kind::True => Ok(SpannedAstNode::new(
AstNode::Constant {
value: Value::Bool(true),
},
self.advance().span,
)),
Kind::False => Ok(SpannedAstNode::new(
AstNode::Constant {
value: Value::Bool(false),
},
self.advance().span,
)),
Kind::Nil => {
let literal = self.advance();
let span = literal.span;
Ok(SpannedAstNode::new(
AstNode::Constant { value: Value::Nil },
span,
))
}
Kind::This => Ok(SpannedAstNode::new(
AstNode::Variable {
name: "this".to_string(),
},
self.advance().span,
)),
Kind::Super => {
let keyword_span = self.advance().span;
self.eat(Kind::Dot)?;
let (name, name_span) = self.id_token()?;
Ok(SpannedAstNode::new(
AstNode::SuperAccess { name },
Span::merge(vec![&keyword_span, &name_span]),
))
}
_ => Err(ParsingError::UnexpectedToken {
expected: "primary expression".to_string(),
actual: self.advance(),
}),
}
}
fn number_literal(&mut self) -> Result<SpannedAstNode, ParsingError> {
let token = self.advance();
if let Kind::NumberLiteral(n) = token.kind {
Ok(SpannedAstNode::new(
AstNode::Constant {
value: Value::from(n),
},
token.span,
))
} else {
Err(ParsingError::UnexpectedToken {
expected: "number".to_string(),
actual: token,
})
}
}
fn string_literal(&mut self) -> Result<SpannedAstNode, ParsingError> {
let token = self.advance();
if let Kind::StringLiteral(s) = token.kind {
Ok(SpannedAstNode::new(
AstNode::Constant {
value: Value::from(s),
},
token.span,
))
} else {
Err(ParsingError::UnexpectedToken {
expected: "string".to_string(),
actual: token,
})
}
}
/// Parse a parameter list and return a vector of the `Token`s that represent the paremeter names
fn parameter_list(&mut self) -> Result<Vec<Token>, ParsingError> {
let mut parameters = vec![];
parameters.push(self.advance());
while self.next().kind == Kind::Comma {
self.advance();
let param_name = self.advance();
if let Kind::IdentifierLiteral(_) = param_name.kind {
parameters.push(param_name);
} else {
return Err(ParsingError::UnexpectedToken {
expected: "identifier".to_string(),
actual: param_name,
});
}
}
Ok(parameters)
}
/// Parse an identifier literal and return it, destructured
fn id_token(&mut self) -> Result<(String, Span), ParsingError> {
let token = self.advance();
if let Kind::IdentifierLiteral(id) = token.kind {
Ok((id, token.span))
} else {
Err(ParsingError::UnexpectedToken {
expected: "identifier".to_string(),
actual: token,
})
}
}
fn has_next(&mut self) -> bool {
self.scanner.peek().is_some()
}
/// Get a reference to the next `Token` that will be returned by `advance`
fn next(&mut self) -> &Token {
self.scanner.peek().unwrap()
}
/// Return the next `Token` and advance `self.scanner` to the next `Token`
fn advance(&mut self) -> Token {
self.scanner.next().unwrap()
}
/// Advance if the current `Token` matches `kind`. Otherwise, return an error
fn eat(&mut self, kind: Kind) -> Result<Token, ParsingError> {
if self.next().kind == kind {
Ok(self.advance())
} else {
Err(ParsingError::UnexpectedToken {
expected: format!("'{}'", kind),
actual: self.advance(),
})
}
}
/// Consume tokens until current is '{', '}', or the token after a ';'
fn synchronize(&mut self) {
loop {
match self.next().kind {
Kind::Semicolon => {
self.advance();
break;
}
Kind::LeftBrace | Kind::RightBrace => {
break;
}
_ => {
self.advance();
}
}
}
}
}