Skip to content

Commit

Permalink
thiserror for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nulluser0 committed Jun 17, 2024
1 parent 0338180 commit 63e2b6c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 87 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
criterion = "0.5.1"
thiserror = "1.0.61"

[lib]
name = "quiklang"
Expand Down Expand Up @@ -38,4 +39,4 @@ harness = false

[[bench]]
name = "interpret_factorial"
harness = false
harness = false
48 changes: 48 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use thiserror::Error;

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

#[derive(Error, Debug)]
pub enum ParserError {
#[error("Error at position {position}: {message}. Expected {expected:?}, but found {found:?}")]
UnexpectedToken {
expected: Token,
found: Token,
position: usize,
message: String,
},

#[error("Error: `async` must be followed by `fn`")]
MissingAsyncFn,

#[error("Error at position {0}: `break` found outside of loop context")]
BreakOutsideLoop(usize),

#[error("Error at position {0}: `return` found outside of function context")]
ReturnOutsideFunction(usize),

#[error("Error at position {0}: Missing function identifier")]
MissingFunctionIdentifier(usize),

#[error("Error at position {0}: Invalid function parameter")]
InvalidFunctionParameter(usize),

#[error("Error at position {0}: Cannot use `mut` with `const`")]
MutConstConflict(usize),

#[error("Error at position {0}: Missing identifier")]
MissingIdentifier(usize),

#[error("Error at position {0}: `const` must have an assigned value")]
ConstWithoutValue(usize),

#[error("Error at position {0}: Object literal key expected")]
ObjectLiteralKeyExpected(usize),

#[error("Error at position {0}: Invalid operator {1:?}")]
InvalidOperator(usize, Token),

#[error("Error at position {0}: Invalid property access using dot operator {1:?}")]
InvalidDotProperty(usize, Expr),
// Add other error variants here if needed
}
90 changes: 4 additions & 86 deletions src/frontend/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Parser

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

use super::{
ast::{Expr, Literal, Stmt, UnaryOp},
Expand Down Expand Up @@ -630,88 +633,3 @@ impl Parser {
Ok(program)
}
}

#[derive(Debug)]
pub enum ParserError {
UnexpectedToken {
expected: Token,
found: Token,
position: usize,
message: String,
},
MissingAsyncFn,
BreakOutsideLoop(usize),
ReturnOutsideFunction(usize),
MissingFunctionIdentifier(usize),
InvalidFunctionParameter(usize),
MutConstConflict(usize),
MissingIdentifier(usize),
ConstWithoutValue(usize),
ObjectLiteralKeyExpected(usize),
InvalidOperator(usize, Token),
InvalidDotProperty(usize, Expr),
// ... other error variants
}

impl std::fmt::Display for ParserError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ParserError::UnexpectedToken {
expected,
found,
position,
message,
} => {
write!(
f,
"Error at position {}: {}. Expected {:?}, but found {:?}",
position, message, expected, found
)
}
ParserError::MissingAsyncFn => write!(f, "Error: `async` must be followed by `fn`"),
ParserError::BreakOutsideLoop(pos) => write!(
f,
"Error at position {}: `break` found outside of loop context",
pos
),
ParserError::ReturnOutsideFunction(pos) => write!(
f,
"Error at position {}: `return` found outside of function context",
pos
),
ParserError::MissingFunctionIdentifier(pos) => {
write!(f, "Error at position {}: Missing function identifier", pos)
}
ParserError::InvalidFunctionParameter(pos) => {
write!(f, "Error at position {}: Invalid function parameter", pos)
}
ParserError::MutConstConflict(pos) => write!(
f,
"Error at position {}: Cannot use `mut` with `const`",
pos
),
ParserError::MissingIdentifier(pos) => {
write!(f, "Error at position {}: Missing identifier", pos)
}
ParserError::ConstWithoutValue(pos) => write!(
f,
"Error at position {}: `const` must have an assigned value",
pos
),
ParserError::ObjectLiteralKeyExpected(pos) => {
write!(f, "Error at position {}: Object literal key expected", pos)
}
ParserError::InvalidOperator(pos, token) => {
write!(f, "Error at position {}: Invalid operator {:?}", pos, token)
}
ParserError::InvalidDotProperty(pos, expr) => write!(
f,
"Error at position {}: Invalid property access using dot operator {:?}",
pos, expr
),
// ... other error variants
}
}
}

impl std::error::Error for ParserError {}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod backend;
pub mod errors;
pub mod frontend;
pub mod utils;

0 comments on commit 63e2b6c

Please sign in to comment.