parser: abstract block logic

This commit is contained in:
Ryan Chandler 2022-07-26 17:54:03 +01:00
parent dc7604f7ad
commit 013e2403ad
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 27 additions and 25 deletions

View File

@ -0,0 +1,17 @@
use trunk_lexer::TokenKind;
use crate::Block;
use super::{Parser, ParseResult};
impl Parser {
pub fn block(&mut self, until: &TokenKind) -> ParseResult<Block> {
let mut block = Block::new();
while ! self.is_eof() && &self.current.kind != until {
block.push(self.statement()?);
}
Ok(block)
}
}

View File

@ -23,6 +23,7 @@ macro_rules! expect {
}
mod params;
mod block;
#[derive(PartialEq)]
enum FunctionKind {
@ -271,14 +272,11 @@ impl Parser {
expect!(self, TokenKind::SemiColon, "expected semi-colon");
}
let mut body = Block::new();
while ! self.is_eof() {
if braced && self.current.kind == TokenKind::RightBrace {
break;
}
body.push(self.statement()?);
}
let body = if braced {
self.block(&TokenKind::RightBrace)?
} else {
Block::new()
};
if braced {
expect!(self, TokenKind::RightBrace, "expected }");
@ -303,10 +301,7 @@ impl Parser {
TokenKind::SemiColon
};
let mut then = Block::new();
while ! self.is_eof() && self.current.kind != body_end_token {
then.push(self.statement()?);
}
let then = self.block(&body_end_token)?;
if body_end_token == TokenKind::RightBrace {
expect!(self, TokenKind::RightBrace, "expected }");
@ -325,10 +320,7 @@ impl Parser {
expect!(self, TokenKind::LeftBrace, "expected {");
let mut body = Block::new();
while ! self.is_eof() && self.current.kind != TokenKind::RightBrace {
body.push(self.statement()?);
}
let body = self.block(&TokenKind::RightBrace)?;
expect!(self, TokenKind::RightBrace, "expected }");
@ -346,10 +338,7 @@ impl Parser {
expect!(self, TokenKind::LeftBrace, "expected {");
let mut r#else = Block::new();
while ! self.is_eof() && self.current.kind != TokenKind::RightBrace {
r#else.push(self.statement()?);
}
let mut r#else = self.block(&TokenKind::RightBrace)?;
expect!(self, TokenKind::RightBrace, "expected }");
@ -426,11 +415,7 @@ impl Parser {
expect!(self, TokenKind::LeftBrace, "expected {");
let mut body = Block::new();
while ! self.is_eof() && self.current.kind != TokenKind::RightBrace {
body.push(self.statement()?);
}
let mut body = self.block(&TokenKind::RightBrace)?;
expect!(self, TokenKind::RightBrace, "expected }");