Debian Codegraph @

src/error.rs

raw symbols SCIP
1 //! Boilerplate error definitions.
2 use std::fmt;
3
4 use crate::lexer::Location;
5
6 /// A type alias for std's Result with the Error as our error type.
7 pub type Result<T, E = Error> = std::result::Result<T, E>;
8
9 /// An error encountered when parsing a Grammar.
10 #[derive(Debug)]
11 pub struct Error {
12 pub(crate) message: String,
13 pub(crate) location: Option<Location>,
14 }
15
16 impl fmt::Display for Error {
17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18 if let Some(loc) = self.location {
19 // Report 1-based indices, to match text editors
20 write!(f, "{}:{}: ", loc.line + 1, loc.column + 1)?
21 }
22 write!(f, "{}", self.message)
23 }
24 }
25
26 impl std::error::Error for Error {}
27
28 impl Error {
29 pub(crate) fn with_location(self, location: Location) -> Error {
30 Error {
31 location: Some(location),
32 ..self
33 }
34 }
35 }
36
37 macro_rules! _format_err {
38 ($($tt:tt)*) => {
39 $crate::error::Error {
40 message: format!($($tt)*),
41 location: None,
42 }
43 };
44 }
45 pub(crate) use _format_err as format_err;
46
47 macro_rules! _bail {
48 ($($tt:tt)*) => { return Err($crate::error::format_err!($($tt)*)) };
49 }
50 pub(crate) use _bail as bail;