use std::num::ParseFloatError;
use thiserror::Error;
use crate::expression::ast::ExprNum;
#[derive(Error, Debug)]
pub enum AstError<'a> {
#[error("Failed to parse '{input}' as a number: {source}")]
ParseLiteralError {
input: &'a str,
#[source]
source: ParseFloatError,
},
#[error("Unknown operator: '{0}'")]
UnknownOperator(&'a str),
#[error("Syntax error: unexpected token '{token}' at position {position}")]
SyntaxError {
token: &'a str,
position: usize,
},
#[error("Invalid node index: {index} (total nodes: {total_nodes})")]
InvalidNodeIndex {
index: usize,
total_nodes: usize,
},
#[error("Invalid pre-value index: {index} (total pre-value: {total_nodes})")]
InvalidPrecalcIndex {
index: usize,
total_nodes: usize,
},
#[error("Unmatched parentheses")]
UnmatchedParentheses,
#[error("Operator '{operator}' expects {expected} operands, but received {actual}")]
InvalidOperandCount {
operator: &'a str,
expected: usize,
actual: usize,
},
#[error("Empty expression")]
EmptyExpression,
#[error("Expression evaluation error: {0}")]
ExpressionEvaluationError(&'a str),
#[error("Invalid expression structure: {0}")]
InvalidExpressionStructure(&'a str),
#[error("Division by zero")]
DivisionByZero,
#[error("The calculation result '{0}' is abnormal")]
CalculationResultError(ExprNum),
#[error("The evaluated variable '{0}' has not been initialized")]
UninitializedVariable(&'a str),
}