diff options
author | Vincent Ambo <mail@tazj.in> | 2020-12-20T23·13+0100 |
---|---|---|
committer | tazjin <mail@tazj.in> | 2020-12-22T10·13+0000 |
commit | a104afa6ea72dbafdc687156aac7e392eb7786a9 (patch) | |
tree | 536b308f9af23bfbe5c83587735db021cbe2c90f /users/tazjin/rlox/src/parser.rs | |
parent | 75ae25daa9e7686bca60b518d9cf442bcfba3bf7 (diff) |
refactor(tazjin/rlox): Introduce declarations in parser r/2025
Change-Id: I873fdd53319ec36da18926d9477e809a69dbace7 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2288 Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/parser.rs')
-rw-r--r-- | users/tazjin/rlox/src/parser.rs | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/users/tazjin/rlox/src/parser.rs b/users/tazjin/rlox/src/parser.rs index d7155fff10a7..91000db9dd90 100644 --- a/users/tazjin/rlox/src/parser.rs +++ b/users/tazjin/rlox/src/parser.rs @@ -48,12 +48,20 @@ pub enum Statement<'a> { Print(Expr<'a>), } -pub type Program<'a> = Vec<Statement<'a>>; +#[derive(Debug)] +pub enum Declaration<'a> { + Stmt(Statement<'a>), +} + +pub type Program<'a> = Vec<Declaration<'a>>; // Parser /* -program → statement* EOF ; +program → declaration* EOF ; + +declaration → varDecl + | statement ; statement → exprStmt | printStmt ; @@ -79,10 +87,15 @@ struct Parser<'a> { type ExprResult<'a> = Result<Expr<'a>, Error>; type StmtResult<'a> = Result<Statement<'a>, Error>; +type DeclResult<'a> = Result<Declaration<'a>, Error>; impl<'a> Parser<'a> { // recursive-descent parser functions + fn declaration(&mut self) -> DeclResult<'a> { + Ok(Declaration::Stmt(self.statement()?)) + } + fn statement(&mut self) -> StmtResult<'a> { if self.match_token(&[TokenKind::Print]) { self.print_statement() @@ -274,13 +287,13 @@ pub fn parse<'a>(tokens: Vec<Token<'a>>) -> Result<Program<'a>, Vec<Error>> { let mut errors: Vec<Error> = vec![]; while !parser.is_at_end() { - match parser.statement() { + match parser.declaration() { Err(err) => { errors.push(err); parser.synchronise(); } - Ok(stmt) => { - program.push(stmt); + Ok(decl) => { + program.push(decl); } } } |