use super::{ParserError, lexer::Token, pos::Span}; use std::fmt; impl std::error::Error for ParserError {} impl fmt::Display for ParserError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if let Some(file) = &self.file { write!(f, "{file} | ")?; } write!(f, "{} | {}", self.span, self.msg) } } pub trait SpanParserError { type Output; fn span_err(self, span: Span) -> Self::Output; } impl SpanParserError for Result { type Output = Result; fn span_err(self, span: Span) -> Self::Output { self.map_err(|e| ParserError { span, msg: e.to_string(), file: None, }) } } pub trait TokenError { fn token_err(self, msg: impl Into) -> Result; } impl TokenError for Token<'_> { fn token_err(self, msg: impl Into) -> Result { Err(ParserError { span: self.span, msg: msg.into(), file: None, }) } }