crftng-intrprtrs/src/ast/statement/expression_statement.rs

29 lines
742 B
Rust
Raw Normal View History

2022-05-02 11:38:20 +02:00
use crate::{
ast::expression::expression_node::ExpressionNode,
ast::parser::{InnerASTParsingError, Parser, Result},
lexer::token::{self, TokenType},
};
use super::Statement;
#[derive(Debug)]
pub struct ExpressionStatement(pub ExpressionNode);
impl ExpressionStatement {
pub fn new(expr: ExpressionNode) -> Self {
Self(expr)
}
}
impl<'a, T: Iterator<Item = token::Token<'a>>> Parser<'a, T> {
pub fn expression_statement(&mut self) -> Result<Statement> {
let expr = self.expression()?;
let token = self.token_iter.peek();
if let TokenType::Semicolon = token.token_type {
self.token_iter.next();
Ok(ExpressionStatement::new(expr).into())
} else {
Err(token.location.wrap(InnerASTParsingError::ExpectedSemi))
}
}
}