Better convert LexError to ParseError.

This commit is contained in:
Stephen Chung
2020-06-14 16:56:36 +08:00
parent b51930031b
commit 0c6a939c66
4 changed files with 44 additions and 51 deletions

View File

@@ -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);