use thiserror::Error;
#[derive(Debug, Error)]
pub enum ParseError {
#[error("Got unexpected token: '{token}'")]
UnexpectedToken {
token: String,
},
#[error("Parentheses didn't match")]
MismatchedParentheses,
#[error("Expected something that wasn't found: {expected}")]
Expected {
expected: Expected,
},
}
#[derive(Debug, Error)]
pub enum MathError {
#[error("Variable '{name}' is not defined")]
UndefinedVariable {
name: String,
},
#[error("Function '{name}' is not defined")]
UndefinedFunction {
name: String,
},
#[error("A function was passed incorrect arguments")]
IncorrectArguments,
#[error("Attempted to divide by zero")]
DivideByZero,
#[error("A NaN value was attempted to be used as an operand")]
NaN,
#[error("Tried to compare a value that can't be compared (eg NaN, Infinity, etc.)")]
CmpError,
#[error("The operation '{op}' is not supported for the type {num_type}")]
Unimplemented {
op: String,
num_type: String,
},
#[error("An unknown error occurred during evaluation")]
Other,
}
#[derive(Debug, Error)]
pub enum EvalError {
#[error("Failed to parse the expression: {error}")]
ParseError {
error: ParseError,
},
#[error("Failed to evaluate the expression: {error}")]
MathError {
error: MathError,
},
}
impl From<ParseError> for EvalError {
fn from(t: ParseError) -> EvalError {
EvalError::ParseError { error: t }
}
}
impl From<MathError> for EvalError {
fn from(t: MathError) -> EvalError {
EvalError::MathError { error: t }
}
}
#[derive(Debug, Error)]
pub enum Expected {
#[error("Expected another operator")]
Operator,
#[error("Expected another expression")]
Expression,
#[error("Expected a parenthesis")]
Paren,
#[error("Expected a function")]
Function,
}