use super::expression::expression_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, } 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, ) -> StdResult, Vec> { let mut tokens = Vec::new(); let mut errors = Vec::new(); while self.token_iter.peek().is_some() { match self.expression() { Ok(token) => { if errors.is_empty() { tokens.push(token) } } Err(e) => errors.push(e), } } if errors.is_empty() { Ok(tokens) } else { Err(errors) } } }