29 lines
742 B
Rust
29 lines
742 B
Rust
|
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))
|
||
|
}
|
||
|
}
|
||
|
}
|