46 lines
1004 B
Rust
46 lines
1004 B
Rust
use itertools::PeekingNext;
|
|
use std::fmt::Debug;
|
|
|
|
use super::{ExpressionNode, Parser, Result};
|
|
|
|
use crate::{ast::statement::Statement, lexer::token};
|
|
|
|
#[derive(Default)]
|
|
pub struct BlockExpr {
|
|
pub statements: Vec<Statement>,
|
|
}
|
|
|
|
impl Debug for BlockExpr {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
writeln!(f, "(")?;
|
|
for statement in self.statements.iter() {
|
|
writeln!(f, "{:?}", statement)?;
|
|
}
|
|
writeln!(f, ")")?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl<'a, T: Iterator<Item = token::Token<'a>>> Parser<'a, T> {
|
|
pub(super) fn block_expr(&mut self) -> Result<ExpressionNode> {
|
|
if self
|
|
.token_iter
|
|
.peeking_next(|t| matches!(t.token_type, token::TokenType::LeftBrace))
|
|
.is_some()
|
|
{
|
|
let mut block_expr = BlockExpr::default();
|
|
while self
|
|
.token_iter
|
|
.peeking_next(|t| matches!(t.token_type, token::TokenType::RightBrace))
|
|
.is_none()
|
|
{
|
|
block_expr.statements.push(self.statement()?)
|
|
}
|
|
|
|
Ok(block_expr.into())
|
|
} else {
|
|
self.equality()
|
|
}
|
|
}
|
|
}
|