use crate::lexer::Token; #[derive(Debug)] pub enum RuntimeError { LexError { message: String, line: usize, column: usize }, ParseError { message: String, token: Token }, RuntimeError { message: String, token: Option }, } impl RuntimeError { pub fn lex(message: impl Into, line: usize, column: usize) -> Self { RuntimeError::LexError { message: message.into(), line, column, } } pub fn parse(message: impl Into, token: Token) -> Self { RuntimeError::ParseError { message: message.into(), token, } } } impl std::fmt::Display for RuntimeError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { RuntimeError::LexError { message, line, column } => { write!(f, "[Lex Error] {} at line {}, column {}", message, line, column) } RuntimeError::ParseError { message, token } => { write!(f, "[Parse Error] {} at line {}, column {}", message, token.line, token.column) } RuntimeError::RuntimeError { message, token } => { if let Some(tok) = token { write!(f, "[Runtime Error] {} at line {}, column {}", message, tok.line, tok.column) } else { write!(f, "[Runtime Error] {}", message) } } } } } impl std::error::Error for RuntimeError {}