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; pub(super) type Result = StdResult; pub struct Parser<'a, T: Iterator>> { pub(super) token_iter: iter::Peekable, } pub type ParseAllResult = StdResult, Vec>; impl<'a, T: Iterator>> 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 } }