rocks_lang/
parser.rs

1use crate::error::{Error, ParseError};
2use crate::token::{Token, Type};
3use crate::literal::Literal;
4use crate::expr::*;
5use crate::stmt::*;
6
7type ParseResult<T> = Result<T, ParseError>;
8
9/// Returns if the next token is any of the given types.
10macro_rules! matches {
11    ( $self:ident, $( $type:expr ),+ ) => {
12        {
13            if $( $self.check($type) ) ||* {
14                $self.advance();
15                true
16            } else {
17                false
18            }
19        }
20    }
21}
22
23/// Represents the parses that parses the tokens and returns the resulting
24/// expression according to the following grammer. This grammer is traversed in
25/// a top-down, left-to-right manner. The lower rule is, the higher precedence
26/// it has. Therefore the parser will start parsing from the top rule but will
27/// go down to the lower rules if it finds a match.
28///
29/// # Syntax Grammer
30/// ```text
31/// Program     -> Decleration* EOF ;
32/// ```
33///
34/// ### Declarations
35/// ```text
36/// Declaration -> ClassDecl | FunDecl | VarDecl | Statement ;
37/// ClassDecl   -> "class" IDENTIFIER ( "<" IDENTIFIER )? "{" Function* "}" ;
38/// FunDecl     -> "fun" Function ;
39/// VarDecl     -> "var" IDENTIFIER ( "=" Expression )? ";" ;
40/// ```
41///
42/// ### Statements
43/// ```text
44/// Statement   -> ExprStmt | ForStmt | IfStmt | PrintStmt | ReturnStmt | BreakStmt | WhileStmt | Block ;
45/// ExprStmt    -> Expression ";" ;
46/// ForStmt     -> "for" "(" ( VarDecl | ExprStmt | ";" ) Expression? ";" Expression? ")" Statement ;
47/// IfStmt      -> "if" "(" Expression ")" Statement ( "else" Statement )? ;
48/// PrintStmt   -> "print" Expression ";" ;
49/// ReturnStmt  -> "return" Expression? ";" ;
50/// BreakStmt   -> "break" ";" ;
51/// WhileStmt   -> "while" "(" Expression ")" Statement ;
52/// Block       -> "{" Decleration* "}" ;
53/// ```
54///
55/// ### Expressions
56/// ```text
57/// Expression  -> Assignment ;
58/// Assignment  -> ( Call "." )? IDENTIFIER "=" Assignment | LogicOr ;
59/// LogicOr     -> LogicAnd ( "or" LogicAnd )* ;
60/// LogicAnd    -> Equality ( "and" Equality )* ;
61/// Equality    -> Comparison ( ( "!=" | "==" ) Comparison )* ;
62/// Comparison  -> Term ( ( ">" | ">=" | "<" | "<=" ) Term )* ;
63/// Term        -> Factor ( ( "+" | "-" ) Factor )* ;
64/// Factor      -> Unary ( ( "*" | "/" ) Unary )* ;
65/// Unary       -> ( "!" | "-" ) Unary | Call ;
66/// Call        -> Primary ( "(" Arguments? ")" | "." IDENTIFIER )* ;
67/// Primary     -> NUMBER | STRING | "false" | "true" | "null" | "this" | "(" Expression ")" | IDENTIFIER | "super" "." IDENTIFIER ;
68/// ```
69///
70/// ### Misc
71/// ```text
72/// Function    -> IDENTIFIER "(" Parameters? ")" Block ;
73/// Parameters  -> IDENTIFIER ( "," IDENTIFIER )* ;
74/// Arguments   -> Expression ( "," Expression )* ;
75/// ```
76pub struct Parser {
77    /// The tokens to parse.
78    tokens: Vec<Token>,
79    /// The current token index.
80    current: u32,
81}
82
83impl Parser {
84    /// Creates a new parser with the given tokens.
85    pub fn new(tokens: Vec<Token>) -> Self {
86        Parser { tokens, current: 0 }
87    }
88
89    /// Parses the tokens and returns the resulting expression.
90    pub fn parse(&mut self) -> Vec<Stmt> {
91        let mut statements = Vec::new();
92
93        while !self.is_at_end() {
94            if let Some(stmt) = self.decleration() {
95                statements.push(stmt);
96            }
97        }
98
99        statements
100    }
101
102    /// Returns the next token without consuming it.
103    fn peek(&mut self) -> &Token {
104        &self.tokens[self.current as usize]
105    }
106
107    /// Returns the previous token without consuming it.
108    fn previous(&mut self) -> &Token {
109        &self.tokens[(self.current - 1) as usize]
110    }
111
112    /// Returns if the parser has reached the end of the file.
113    fn is_at_end(&mut self) -> bool {
114        self.peek().r#type == Type::EOF
115    }
116
117    /// Returns if the next token is of the given type.
118    fn check(&mut self, r#type: Type) -> bool {
119        if self.is_at_end() {
120            return false
121        }
122
123        self.peek().r#type == r#type
124    }
125
126    /// Consumes the next token and returns it.
127    fn advance(&mut self) -> &Token {
128        if !self.is_at_end() {
129            self.current += 1;
130        }
131
132        self.previous()
133    }
134
135    /// Consumes the next token and returns it, if it is of the given type.
136    /// See `matches!` macro for matching multiple types.
137    fn consume(&mut self, r#type: Type, message: &str) -> ParseResult<&Token> {
138        if self.check(r#type) {
139            return Ok(self.advance());
140        }
141
142        Err(ParseError {
143            token: self.peek().clone(),
144            message: message.to_string(),
145        }) 
146    }
147
148    /// Parses a decleration.
149    fn decleration(&mut self) -> Option<Stmt> {
150        let statement = if matches!(self, Type::Class) {
151           self.class_decleration()
152        } else if matches!(self, Type::Fun) {
153            self.function("function")
154        } else if matches!(self, Type::Var) {
155            self.var_decleration()
156        } else {
157            self.statement()
158        };
159
160        match statement {
161            Ok(stmt) => Some(stmt),
162            Err(error) => {
163                error.throw();
164                self.synchronize();
165                None
166            }
167        }
168    }
169
170    /// Parses a class decleration
171    fn class_decleration(&mut self) -> ParseResult<Stmt> {
172        let name = self.consume(Type::Identifier, "Expected class name")?.clone();
173
174        let superclass = if matches!(self, Type::Less) {
175            self.consume(Type::Identifier, "Expected superclass name")?;
176            Some(Expr::Variable(VariableData { name: self.previous().clone() }))
177        } else {
178            None
179        };
180
181        self.consume(Type::LeftBrace, "Expected '{' before class body")?;
182
183        let mut methods: Vec<Stmt> = vec![];
184        while !self.check(Type::RightBrace) && !self.is_at_end() {
185            methods.push(self.function("method")?);
186        }
187
188        self.consume(Type::RightBrace, "Expected '}' after class body")?;
189
190        Ok(Stmt::Class(ClassData { name, superclass, methods }))
191    }
192
193    /// Parses a variable decleration.
194    fn var_decleration(&mut self) -> ParseResult<Stmt> {
195        let name = self.consume(Type::Identifier, "Expected variable name")?.clone();
196
197        let mut initializer: Option<Expr> = None;
198        if matches!(self, Type::Equal) {
199            match self.expression() {
200                Ok(expr) => initializer = Some(expr),
201                Err(error) => return Err(error),
202            };
203        }
204
205        self.consume(Type::Semicolon, "Expected ';' after variable decleration")?;
206        Ok(Stmt::Var(VarData { name, initializer }))
207    }
208
209    /// Parses a while statement.
210    fn while_statement(&mut self) -> ParseResult<Stmt> {
211        self.consume(Type::LeftParen, "Expected '(' after while.")?;
212        let condition = self.expression()?;
213        self.consume(Type::RightParen, "Expected ')' after condition.")?;
214
215        let body = match self.statement() {
216            Ok(stmt) => stmt,
217            Err(error) => {
218                return Err(error);
219            }
220        };
221
222        Ok(Stmt::While(WhileData {
223            condition,
224            body: Box::new(body),
225        }))
226    }
227
228    /// Parses an expression.
229    fn expression(&mut self) -> ParseResult<Expr> {
230        self.assignment()
231    }
232
233    /// Parses a statement.
234    fn statement(&mut self) -> ParseResult<Stmt> {
235        if matches!(self, Type::For) {
236            return self.for_statement();
237        }
238
239        if matches!(self, Type::If) {
240            return self.if_statement();
241        }
242
243        if matches!(self, Type::Print) {
244            return self.print_statement();
245        }
246
247        if matches!(self, Type::Return) {
248            return self.return_statement();
249        }
250
251        if matches!(self, Type::Break) {
252            return self.break_statement();
253        }
254
255        if matches!(self, Type::While) {
256            return self.while_statement();
257        }
258
259        if matches!(self, Type::LeftBrace) {
260            return Ok(Stmt::Block(BlockData { statements: self.block()? }));
261        }
262
263        self.expression_statement()
264    }
265
266    /// Parses a for statement.
267    fn for_statement(&mut self) -> ParseResult<Stmt> {
268        self.consume(Type::LeftParen, "Expected '(' after 'for'")?;
269
270        let initializer: Option<Stmt>;
271        if matches!(self, Type::Semicolon) {
272            initializer = None;
273        } else if matches!(self, Type::Var) {
274            initializer = Some(self.var_decleration()?);
275        } else {
276            initializer = Some(self.expression_statement()?);
277        }
278
279        let condition = match !self.check(Type::Semicolon) {
280            true => Some(self.expression()?),
281            false => None,
282        };
283        self.consume(Type::Semicolon, "Expected ';' after loop condition")?;
284
285        let increment = match !self.check(Type::RightParen) {
286            true => Some(self.expression()?),
287            false => None,
288        };
289        self.consume(Type::RightParen, "Expected ')' after loop clauses")?;
290
291        let mut body = match self.statement() {
292            Ok(stmt) => stmt,
293            Err(error) => {
294                return Err(error);
295            }
296        };
297
298        // Execute the increment after the body.
299        if let Some(increment) = increment {
300            body = Stmt::Block(BlockData {
301                statements: vec![
302                    body,
303                    Stmt::Expression(ExpressionData {
304                        expr: increment
305                    }),
306                ],
307            });
308        }
309
310        // Wrap the body into a while loop.
311        // If there is no condition, use true.
312        body = Stmt::While(WhileData {
313            condition: condition.unwrap_or(Expr::Literal(Literal::Bool(true))),
314            body: Box::new(body),
315        });
316
317        // Add the initializer before the loop if there is one.
318        if let Some(initializer) = initializer {
319            body = Stmt::Block(BlockData {
320                statements: vec![
321                    initializer,
322                    body,
323                ],
324            });
325        }
326
327        Ok(body)
328    }
329
330    /// Parses an if statement.
331    fn if_statement(&mut self) -> ParseResult<Stmt> {
332        self.consume(Type::LeftParen, "Expected '(' after 'if'")?;
333        let condition = self.expression()?;
334        self.consume(Type::RightParen, "Expected ')' after if condition")?;
335
336        let then_branch = Box::new(self.statement()?);
337        let mut else_branch: Option<Box<Stmt>> = None;
338        if matches!(self, Type::Else) {
339            else_branch = Some(Box::new(self.statement()?));
340        }
341
342        Ok(Stmt::If(IfData { condition, then_branch, else_branch }))
343    }
344
345    /// Parses a print statement.
346    fn print_statement(&mut self) -> ParseResult<Stmt> {
347        let expr = match self.expression() {
348            Ok(expr) => expr,
349            Err(error) => return Err(error),
350        };
351
352        self.consume(Type::Semicolon, "Expected ';' after value")?;
353
354        Ok(Stmt::Print(PrintData { expr }))
355    }
356
357    /// Parses a return statement.
358    fn return_statement(&mut self) -> ParseResult<Stmt> {
359        let keyword = self.previous().to_owned();
360
361        let value = match self.check(Type::Semicolon) {
362            true => None,
363            false => Some(self.expression()?),
364        };
365
366        self.consume(Type::Semicolon, "Expected ';' after return value")?;
367        Ok(Stmt::Return(ReturnData { keyword, value }))
368    }
369
370    /// Parses a break statement.
371    fn break_statement(&mut self) -> ParseResult<Stmt> {
372        let keyword = self.previous().clone();
373
374        self.consume(Type::Semicolon, "Expected ';' after break")?;
375
376        Ok(Stmt::Break(BreakData { keyword }))
377    }
378
379    /// Parses an expression statement.
380    fn expression_statement(&mut self) -> ParseResult<Stmt> {
381        let expr = match self.expression() {
382            Ok(expr) => expr,
383            Err(error) => return Err(error),
384        };
385
386        self.consume(Type::Semicolon, "Expected ';' after expression")?;
387
388        Ok(Stmt::Expression(ExpressionData { expr }))
389    }
390
391    /// Parses a function decleration.
392    fn function(&mut self, kind: &str) -> ParseResult<Stmt> {
393        let name = self.consume(Type::Identifier, &format!("Expected {kind} name"))?.to_owned();
394
395        self.consume(Type::LeftParen, &format!("Expected '(' after {kind} name"))?;
396
397        let mut params = vec![];
398
399        if !self.check(Type::RightParen) {
400            loop {
401                if params.len() >= 255 {
402                    return Err(ParseError {
403                        token: self.peek().to_owned(),
404                        message: "Cannot have more than 255 parameters".to_string(),
405                    });
406                }
407
408                params.push(self.consume(Type::Identifier, "Expected parameter name")?.to_owned());
409
410                if !matches!(self, Type::Comma) {
411                    break;
412                }
413            }
414        }
415
416        self.consume(Type::RightParen, "Expected ')' after parameters")?;
417
418        self.consume(Type::LeftBrace, &format!("Expected '{{' before {kind} body"))?;
419
420        let body = self.block()?;
421
422        Ok(Stmt::Function(FunctionData { name, params, body }))
423    }
424
425    /// Parses a block statement.
426    fn block(&mut self) -> ParseResult<Vec<Stmt>> {
427        let mut statements = Vec::new();
428
429        while !self.check(Type::RightBrace) && !self.is_at_end() {
430            if let Some(stmt) = self.decleration() {
431                statements.push(stmt);
432            }
433        }
434
435        self.consume(Type::RightBrace, "Expected '}' after block")?;
436
437        Ok(statements)
438    }
439
440    /// Parses an assignment expression.
441    fn assignment(&mut self) -> ParseResult<Expr> {
442        let expr = self.or()?;
443
444        if matches!(self, Type::Equal) {
445            let equals = self.previous().to_owned();
446            let value = self.assignment()?;
447
448            if let Expr::Variable(data) = expr {
449                let name = data.name;
450
451                return Ok(Expr::Assign(AssignData {
452                    name,
453                    value: Box::new(value)
454                }));
455            } else if let Expr::Get(data) = expr {
456                return Ok(Expr::Set(SetData {
457                    object: data.object,
458                    name: data.name,
459                    value: Box::new(value),
460                }));
461            }
462
463            ParseError {
464                token: equals,
465                message: "Invalid assignment target".to_string()
466            }.throw();
467        }
468
469        Ok(expr)
470    }
471
472    /// Parses an or expression.
473    fn or(&mut self) -> ParseResult<Expr> {
474        let mut expr = self.and()?;
475
476        while matches!(self, Type::Or) {
477            let operator = self.previous().clone();
478            let right = self.and()?;
479            expr = Expr::Logical(LogicalData {
480                left: Box::new(expr),
481                operator,
482                right: Box::new(right)
483            });
484        }
485
486        Ok(expr)
487    }
488
489    /// Parses and and expression.
490    fn and(&mut self) -> ParseResult<Expr> {
491        let mut expr = self.equality()?;
492
493        while matches!(self, Type::And) {
494            let operator = self.previous().clone();
495            let right = self.equality()?;
496            expr = Expr::Logical(LogicalData {
497                left: Box::new(expr),
498                operator,
499                right: Box::new(right),
500            });
501        }
502
503        Ok(expr)
504    }
505
506    /// Parses an equality expression.
507    fn equality(&mut self) -> ParseResult<Expr> {
508        let mut expr = match self.comparison() {
509            Ok(expr) => expr,
510            Err(error) => return Err(error),
511        };
512
513        while matches!(self, Type::BangEqual, Type::EqualEqual) {
514            let operator = self.previous().clone();
515            let right = match self.comparison() {
516                Ok(expr) => expr,
517                Err(error) => return Err(error),
518            };
519
520            expr = Expr::Binary(BinaryData {
521                left: Box::new(expr),
522                operator,
523                right: Box::new(right)
524            });
525        }
526
527        Ok(expr)
528    }
529
530    /// Parses a comparison expression.
531    fn comparison(&mut self) -> ParseResult<Expr> {
532        let mut expr = match self.term() {
533            Ok(expr) => expr,
534            Err(error) => return Err(error),
535        };
536
537        while matches!(self, Type::Greater, Type::GreaterEqual, Type::Less, Type::LessEqual) {
538            let operator = self.previous().clone();
539            let right = match self.term() {
540                Ok(expr) => expr,
541                Err(error) => return Err(error),
542            };
543
544            expr = Expr::Binary(BinaryData {
545                left: Box::new(expr),
546                operator,
547                right: Box::new(right)
548            });
549        }
550
551        Ok(expr)
552    }
553
554    /// Parses a term expression.
555    fn term(&mut self) -> ParseResult<Expr> {
556        let mut expr = match self.factor() {
557            Ok(expr) => expr,
558            Err(error) => return Err(error),
559        };
560
561        while matches!(self, Type::Minus, Type::Plus) {
562            let operator = self.previous().clone();
563            let right = match self.factor() {
564                Ok(expr) => expr,
565                Err(error) => return Err(error),
566            };
567
568            expr = Expr::Binary(BinaryData {
569                left: Box::new(expr),
570                operator,
571                right: Box::new(right)
572            });
573        }
574
575        Ok(expr)
576    }
577
578    /// Parses a factor expression.
579    fn factor(&mut self) -> ParseResult<Expr> {
580        let mut expr = match self.unary() {
581            Ok(expr) => expr,
582            Err(error) => return Err(error),
583        };
584
585        while matches!(self, Type::Slash, Type::Star) {
586            let operator = self.previous().clone();
587            let right = match self.unary() {
588                Ok(expr) => expr,
589                Err(error) => return Err(error),
590            };
591
592            expr = Expr::Binary(BinaryData {
593                left: Box::new(expr),
594                operator,
595                right: Box::new(right)
596            });
597        }
598
599        Ok(expr)
600    }
601
602    /// Parses a unary expression.
603    fn unary(&mut self) -> ParseResult<Expr> {
604        if matches!(self, Type::Bang, Type::Minus) {
605            let operator = self.previous().clone();
606            let right = match self.unary() {
607                Ok(expr) => expr,
608                Err(error) => return Err(error),
609            };
610
611            return Ok(Expr::Unary(UnaryData {
612                operator,
613                expr: Box::new(right)
614            }));
615        }
616
617        self.call()
618    }
619
620    /// Parses a call arguments.
621    fn finish_call(&mut self, callee: &Expr) -> ParseResult<Expr> {
622        let mut arguments = vec![];
623
624        if !self.check(Type::RightParen) {
625            while { 
626                if arguments.len() >= 255 {
627                    ParseError {
628                        token: self.peek().to_owned(),
629                        message: "Cannot have more than 255 arguments".to_string(),
630                    }.throw();
631                }
632
633                arguments.push(self.expression()?);
634                matches!(self, Type::Comma)
635            } {}
636        }
637
638        let paren = self.consume(Type::RightParen, "Expected ')' after arguments")?;
639
640        Ok(Expr::Call(CallData {
641            callee: Box::new(callee.to_owned()),
642            paren: paren.to_owned(),
643            arguments,
644        }))
645    }
646
647    /// Parses a call expression.
648    fn call(&mut self) -> ParseResult<Expr> {
649        let mut expr = self.primary()?;
650
651        loop {
652            if matches!(self, Type::LeftParen) {
653                expr = self.finish_call(&expr)?;
654            } else if matches!(self, Type::Dot) {
655                let name = self.consume(Type::Identifier, "Expected property name after '.'")?;
656                expr = Expr::Get(GetData { object: Box::new(expr), name: name.clone() });
657            } else {
658                break;
659            }
660        }
661
662        Ok(expr)
663    }
664
665    /// Parses a primary expression.
666    fn primary(&mut self) -> ParseResult<Expr> {
667        if matches!(self, Type::False) {
668            return Ok(Expr::Literal(Literal::Bool(false)));
669        }
670
671        if matches!(self, Type::True) {
672            return Ok(Expr::Literal(Literal::Bool(true)));
673        }
674
675        if matches!(self, Type::Null) {
676            return Ok(Expr::Literal(Literal::Null));
677        }
678
679        if matches!(self, Type::Number, Type::String) {
680            return Ok(Expr::Literal(self.previous().clone().literal
681                .expect("number or string to have a literal value")));
682        }
683
684        if matches!(self, Type::Super) { 
685            let keyword = self.previous().clone();
686            self.consume(Type::Dot, "Expected '.' after 'super'")?;
687            let method = self.consume(Type::Identifier, "Expected superclass method name")?.clone();
688
689            return Ok(Expr::Super(SuperData { keyword, method }))
690        }
691
692        if matches!(self, Type::This) {
693            return Ok(Expr::This(ThisData { keyword: self.previous().clone() }));
694        }
695
696        if matches!(self, Type::Identifier) {
697            return Ok(Expr::Variable(VariableData {
698                name: self.previous().clone()
699            }))
700        }
701
702        if matches!(self, Type::LeftParen) {
703            let expr = match self.expression() {
704                Ok(expr) => expr,
705                Err(error) => return Err(error),
706            };
707
708            match self.consume(Type::RightParen, "Expected ')' after expression") {
709                Ok(_) => (),
710                Err(error) => return Err(error),
711            };
712
713            return Ok(Expr::Grouping(GroupingData { expr: Box::new(expr) }));
714        }
715
716        Err(ParseError {
717            token: self.peek().clone(),
718            message: "Expected expression".to_string()
719        })
720    }
721
722    /// Tries to recover from a parse error.
723    fn synchronize(&mut self) {
724        self.advance();
725
726        while !self.is_at_end() {
727            if self.previous().r#type == Type::Semicolon {
728                return;
729            }
730
731            match self.peek().r#type {
732                Type::Class => return,
733                Type::Fun => return,
734                Type::Var => return,
735                Type::For => return,
736                Type::If => return,
737                Type::While => return,
738                Type::Print => return,
739                Type::Return => return,
740                _ => self.advance()
741            };
742        }
743    }
744}