Skip to content

Commit

Permalink
Add universal error type
Browse files Browse the repository at this point in the history
  • Loading branch information
nulluser0 committed Jun 18, 2024
1 parent d610557 commit 59e50af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ use thiserror::Error;

use crate::frontend::{ast::Expr, lexer::Token};

#[derive(Error, Debug)]
pub enum Error {
#[error("Lexer Error: {0}")]
LexerError(LexerError),

#[error("Parser Error: {0}")]
ParserError(ParserError),
//
// #[error("Type Error: {0}")]
// TODO: TypeError(TypeError),

// #[error("Runtime Error: {0}")]
// TODO: RuntimeError(RuntimeError),
}

#[derive(Error, Debug)]
pub enum LexerError {
#[error("Unrecognized character '{0}'")]
Expand Down
10 changes: 6 additions & 4 deletions src/frontend/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Parser

use crate::{
errors::ParserError,
errors::{Error, ParserError},
frontend::ast::{BinaryOp, Program, Property},
};

Expand Down Expand Up @@ -622,12 +622,14 @@ impl Parser {
})
}

pub fn produce_ast(&mut self, source_code: String) -> Result<Program, ParserError> {
self.tokens = tokenize(&source_code).map_err(ParserError::LexerErrorAsParseError)?;
pub fn produce_ast(&mut self, source_code: String) -> Result<Program, Error> {
self.tokens = tokenize(&source_code).map_err(Error::LexerError)?;
let mut program = Program::new(Vec::new());

while self.not_eof() {
program.statements.push(self.parse_stmt()?); // SHOULD BE PARSE_STMT!!!
program
.statements
.push(self.parse_stmt().map_err(Error::ParserError)?);
}

Ok(program)
Expand Down

0 comments on commit 59e50af

Please sign in to comment.