#![macro_use]
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Location {
pub line_number: usize,
}
#[derive(Debug)]
pub struct ParseError {
pub location: Location,
pub message: String,
pub is_warning: bool,
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.location.line_number == 0 {
write!(f, "command-line arguments: {}", self.message)
} else {
write!(f, "{}: {}", self.location.line_number, self.message)
}
}
}
impl std::error::Error for ParseError {}
pub type ParseResult<T> = Result<T, ParseError>;
macro_rules! err {
( $loc:expr, $msg:expr ) => {
Err($crate::ParseError {
location: $loc.clone(),
message: $msg.to_string(),
is_warning: false,
})
};
( $loc:expr, $fmt:expr, $( $arg:expr ),+ ) => {
Err($crate::ParseError {
location: $loc.clone(),
message: format!( $fmt, $( $arg ),+ ),
is_warning: false,
})
};
}
macro_rules! warn {
( $loc:expr, $fmt:expr, $( $arg:expr ),+ ) => {
Err($crate::ParseError {
location: $loc.clone(),
message: format!($fmt, $( $arg ),+ ),
is_warning: true,
})
};
}