crftng-intrprtrs/src/ast/parser.rs

59 lines
1.6 KiB
Rust

use super::ast_parser_iter::ParserIter;
use super::statement::statement_node;
use crate::error::ErrorLocationWrapper;
use crate::lexer::{token, token::TokenType};
use std::result::Result as StdResult;
#[derive(Debug)]
pub enum InnerASTParsingError {
IncorrectToken(TokenType),
UnmatchedBrace,
ExpectedSemi,
}
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),
Self::ExpectedSemi => write!(f, "Expected semicolon"),
}
}
}
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: super::ast_parser_iter::ParserIter<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: ParserIter::new(iter),
}
}
pub fn parse_all(&mut self) -> ParseAllResult {
let mut res = Ok(Vec::new());
while !matches!(self.token_iter.peek().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
}
}