58 lines
1.5 KiB
Rust
58 lines
1.5 KiB
Rust
use super::expression::expression_node;
|
|
use super::statement::statement_node;
|
|
use crate::error::ErrorLocationWrapper;
|
|
use crate::lexer::{token, token::TokenType};
|
|
|
|
use std::iter;
|
|
use std::result::Result as StdResult;
|
|
|
|
#[derive(Debug)]
|
|
pub enum InnerASTParsingError {
|
|
IncorrectToken(TokenType),
|
|
UnmatchedBrace,
|
|
}
|
|
|
|
impl std::fmt::Display for InnerASTParsingError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match *self {
|
|
Self::UnmatchedBrace => write!(f, "Unmatched brace"),
|
|
Self::IncorrectToken(ref token) => write!(f, "Incorrect token {:?}", token),
|
|
}
|
|
}
|
|
}
|
|
impl std::error::Error for InnerASTParsingError {}
|
|
|
|
pub type ASTParsingError = ErrorLocationWrapper<InnerASTParsingError>;
|
|
pub(super) type Result<T> = StdResult<T, ASTParsingError>;
|
|
|
|
pub struct Parser<'a, T: Iterator<Item = token::Token<'a>>> {
|
|
pub(super) token_iter: iter::Peekable<T>,
|
|
}
|
|
|
|
pub type ParseAllResult = StdResult<Vec<statement_node::Statement>, Vec<ASTParsingError>>;
|
|
|
|
impl<'a, T: Iterator<Item = token::Token<'a>>> Parser<'a, T> {
|
|
pub fn new(iter: T) -> Parser<'a, T> {
|
|
Parser {
|
|
token_iter: iter.peekable(),
|
|
}
|
|
}
|
|
pub fn parse_all(&mut self) -> ParseAllResult {
|
|
let mut res = Ok(Vec::new());
|
|
|
|
while !matches!(self.token_iter.peek().unwrap().token_type, token::TokenType::Eof) {
|
|
match self.statement() {
|
|
Ok(s) => {
|
|
if let Ok(ref mut v) = res {
|
|
v.push(s)
|
|
}
|
|
}
|
|
Err(e) => match res {
|
|
Ok(_) => res = Err(vec![e]),
|
|
Err(ref mut v) => v.push(e),
|
|
},
|
|
}
|
|
}
|
|
res
|
|
}
|
|
}
|