1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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<T, E: std::error::Error> SpanParserError for Result<T, E> {
type Output = Result<T, ParserError>;
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<T>(self, msg: impl Into<String>) -> Result<T, ParserError>;
}
impl TokenError for Token<'_> {
fn token_err<T>(self, msg: impl Into<String>) -> Result<T, ParserError> {
Err(ParserError {
span: self.span,
msg: msg.into(),
file: None,
})
}
}
|