Better convert LexError to ParseError.
This commit is contained in:
24
src/error.rs
24
src/error.rs
@@ -22,8 +22,8 @@ pub enum LexError {
|
||||
MalformedChar(String),
|
||||
/// An identifier is in an invalid format.
|
||||
MalformedIdentifier(String),
|
||||
/// Bad keyword encountered when tokenizing the script text.
|
||||
ImproperKeyword(String),
|
||||
/// Bad symbol encountered when tokenizing the script text.
|
||||
ImproperSymbol(String),
|
||||
}
|
||||
|
||||
impl Error for LexError {}
|
||||
@@ -42,11 +42,18 @@ impl fmt::Display for LexError {
|
||||
"Length of string literal exceeds the maximum limit ({})",
|
||||
max
|
||||
),
|
||||
Self::ImproperKeyword(s) => write!(f, "{}", s),
|
||||
Self::ImproperSymbol(s) => write!(f, "{}", s),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LexError {
|
||||
/// Convert a `LexError` into a `ParseError`.
|
||||
pub fn into_err(&self, pos: Position) -> ParseError {
|
||||
ParseError(Box::new(self.into()), pos)
|
||||
}
|
||||
}
|
||||
|
||||
/// Type of error encountered when parsing a script.
|
||||
///
|
||||
/// Some errors never appear when certain features are turned on.
|
||||
@@ -217,6 +224,17 @@ impl fmt::Display for ParseErrorType {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&LexError> for ParseErrorType {
|
||||
fn from(err: &LexError) -> Self {
|
||||
match err {
|
||||
LexError::StringTooLong(max) => {
|
||||
Self::LiteralTooLarge("Length of string literal".to_string(), *max)
|
||||
}
|
||||
_ => Self::BadInput(err.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Error when parsing a script.
|
||||
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
|
||||
pub struct ParseError(pub Box<ParseErrorType>, pub Position);
|
||||
|
Reference in New Issue
Block a user