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
43
44
45
46
47
48
49
50
|
//! Boilerplate error definitions.
use std::fmt;
use crate::lexer::Location;
/// A type alias for std's Result with the Error as our error type.
pub type Result<T, E = Error> = std::result::Result<T, E>;
/// An error encountered when parsing a Grammar.
#[derive(Debug)]
pub struct Error {
pub(crate) message: String,
pub(crate) location: Option<Location>,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(loc) = self.location {
// Report 1-based indices, to match text editors
write!(f, "{}:{}: ", loc.line + 1, loc.column + 1)?
}
write!(f, "{}", self.message)
}
}
impl std::error::Error for Error {}
impl Error {
pub(crate) fn with_location(self, location: Location) -> Error {
Error {
location: Some(location),
..self
}
}
}
macro_rules! _format_err {
($($tt:tt)*) => {
$crate::error::Error {
message: format!($($tt)*),
location: None,
}
};
}
pub(crate) use _format_err as format_err;
macro_rules! _bail {
($($tt:tt)*) => { return Err($crate::error::format_err!($($tt)*)) };
}
pub(crate) use _bail as bail;
|