rhai/src/error.rs

192 lines
7.9 KiB
Rust
Raw Normal View History

2020-03-08 12:54:02 +01:00
//! Module containing error definitions for the parsing process.
2020-03-04 15:00:01 +01:00
use crate::parser::Position;
2020-03-17 19:26:11 +01:00
use crate::stdlib::{char, error::Error, fmt, string::String};
2020-03-04 15:00:01 +01:00
/// Error when tokenizing the script text.
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum LexError {
/// An unexpected character is encountered when tokenizing the script text.
UnexpectedChar(char),
/// A string literal is not terminated before a new-line or EOF.
UnterminatedString,
/// An string/character/numeric escape sequence is in an invalid format.
MalformedEscapeSequence(String),
/// An numeric literal is in an invalid format.
MalformedNumber(String),
/// An character literal is in an invalid format.
MalformedChar(String),
/// Error in the script text.
InputError(String),
2020-03-16 05:38:01 +01:00
/// An identifier is in an invalid format.
MalformedIdentifier(String),
2020-03-04 15:00:01 +01:00
}
2020-03-14 16:39:45 +01:00
impl Error for LexError {}
2020-03-04 15:00:01 +01:00
impl fmt::Display for LexError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnexpectedChar(c) => write!(f, "Unexpected '{}'", c),
Self::MalformedEscapeSequence(s) => write!(f, "Invalid escape sequence: '{}'", s),
Self::MalformedNumber(s) => write!(f, "Invalid number: '{}'", s),
Self::MalformedChar(s) => write!(f, "Invalid character: '{}'", s),
2020-03-16 05:38:01 +01:00
Self::MalformedIdentifier(s) => {
write!(f, "Variable name is not in a legal format: '{}'", s)
}
2020-03-04 15:00:01 +01:00
Self::InputError(s) => write!(f, "{}", s),
2020-03-14 16:39:45 +01:00
Self::UnterminatedString => write!(f, "Open string is not terminated"),
2020-03-04 15:00:01 +01:00
}
}
}
/// Type of error encountered when parsing a script.
#[derive(Debug, PartialEq, Clone)]
pub enum ParseErrorType {
/// Error in the script text. Wrapped value is the error message.
BadInput(String),
/// The script ends prematurely.
InputPastEndOfFile,
/// An unknown operator is encountered. Wrapped value is the operator.
UnknownOperator(String),
/// An open `(` is missing the corresponding closing `)`.
MissingRightParen(String),
2020-03-04 15:00:01 +01:00
/// Expecting `(` but not finding one.
MissingLeftBrace,
/// An open `{` is missing the corresponding closing `}`.
MissingRightBrace(String),
2020-03-04 15:00:01 +01:00
/// An open `[` is missing the corresponding closing `]`.
2020-03-16 05:38:01 +01:00
#[cfg(not(feature = "no_index"))]
MissingRightBracket(String),
2020-03-16 16:51:32 +01:00
/// A list of expressions is missing the separating ','.
MissingComma(String),
2020-03-04 15:00:01 +01:00
/// An expression in function call arguments `()` has syntax error.
2020-03-09 03:41:17 +01:00
MalformedCallExpr(String),
2020-03-04 15:00:01 +01:00
/// An expression in indexing brackets `[]` has syntax error.
2020-03-16 05:38:01 +01:00
#[cfg(not(feature = "no_index"))]
2020-03-09 03:41:17 +01:00
MalformedIndexExpr(String),
2020-03-13 11:12:41 +01:00
/// Invalid expression assigned to constant.
ForbiddenConstantExpr(String),
2020-03-16 05:38:01 +01:00
/// Missing a variable name after the `let`, `const` or `for` keywords.
VariableExpected,
/// A `for` statement is missing the `in` keyword.
MissingIn,
2020-03-04 15:00:01 +01:00
/// Defining a function `fn` in an appropriate place (e.g. inside another function).
2020-03-16 05:38:01 +01:00
#[cfg(not(feature = "no_function"))]
2020-03-04 15:00:01 +01:00
WrongFnDefinition,
/// Missing a function name after the `fn` keyword.
2020-03-16 05:38:01 +01:00
#[cfg(not(feature = "no_function"))]
2020-03-04 15:00:01 +01:00
FnMissingName,
/// A function definition is missing the parameters list. Wrapped value is the function name.
2020-03-16 05:38:01 +01:00
#[cfg(not(feature = "no_function"))]
2020-03-04 15:00:01 +01:00
FnMissingParams(String),
2020-03-07 03:15:42 +01:00
/// Assignment to an inappropriate LHS (left-hand-side) expression.
AssignmentToInvalidLHS,
2020-03-13 11:12:41 +01:00
/// Assignment to a copy of a value.
AssignmentToCopy,
/// Assignment to an a constant variable.
AssignmentToConstant(String),
2020-03-04 15:00:01 +01:00
}
/// Error when parsing a script.
#[derive(Debug, PartialEq, Clone)]
pub struct ParseError(pub(crate) ParseErrorType, pub(crate) Position);
2020-03-04 15:00:01 +01:00
impl ParseError {
/// Create a new `ParseError`.
pub(crate) fn new(err: ParseErrorType, pos: Position) -> Self {
Self(err, pos)
}
/// Get the parse error.
pub fn error_type(&self) -> &ParseErrorType {
&self.0
}
/// Get the location in the script of the error.
pub fn position(&self) -> Position {
self.1
}
2020-03-14 16:39:45 +01:00
pub(crate) fn desc(&self) -> &str {
2020-03-04 15:00:01 +01:00
match self.0 {
ParseErrorType::BadInput(ref p) => p,
ParseErrorType::InputPastEndOfFile => "Script is incomplete",
ParseErrorType::UnknownOperator(_) => "Unknown operator",
ParseErrorType::MissingRightParen(_) => "Expecting ')'",
2020-03-04 15:00:01 +01:00
ParseErrorType::MissingLeftBrace => "Expecting '{'",
ParseErrorType::MissingRightBrace(_) => "Expecting '}'",
2020-03-16 05:38:01 +01:00
#[cfg(not(feature = "no_index"))]
ParseErrorType::MissingRightBracket(_) => "Expecting ']'",
2020-03-16 16:51:32 +01:00
ParseErrorType::MissingComma(_) => "Expecting ','",
2020-03-09 03:41:17 +01:00
ParseErrorType::MalformedCallExpr(_) => "Invalid expression in function call arguments",
2020-03-16 05:38:01 +01:00
#[cfg(not(feature = "no_index"))]
2020-03-09 03:41:17 +01:00
ParseErrorType::MalformedIndexExpr(_) => "Invalid index in indexing expression",
2020-03-13 11:12:41 +01:00
ParseErrorType::ForbiddenConstantExpr(_) => "Expecting a constant",
2020-03-16 05:38:01 +01:00
ParseErrorType::MissingIn => "Expecting 'in'",
ParseErrorType::VariableExpected => "Expecting name of a variable",
#[cfg(not(feature = "no_function"))]
2020-03-04 15:00:01 +01:00
ParseErrorType::FnMissingName => "Expecting name in function declaration",
2020-03-16 05:38:01 +01:00
#[cfg(not(feature = "no_function"))]
2020-03-04 15:00:01 +01:00
ParseErrorType::FnMissingParams(_) => "Expecting parameters in function declaration",
2020-03-16 05:38:01 +01:00
#[cfg(not(feature = "no_function"))]
2020-03-04 15:00:01 +01:00
ParseErrorType::WrongFnDefinition => "Function definitions must be at top level and cannot be inside a block or another function",
2020-03-13 11:12:41 +01:00
ParseErrorType::AssignmentToInvalidLHS => "Cannot assign to this expression",
ParseErrorType::AssignmentToCopy => "Cannot assign to this expression because it will only be changing a copy of the value",
ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant variable."
2020-03-04 15:00:01 +01:00
}
}
}
2020-03-14 16:39:45 +01:00
impl Error for ParseError {}
2020-03-04 15:00:01 +01:00
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
2020-03-16 05:38:01 +01:00
ParseErrorType::BadInput(ref s) | ParseErrorType::MalformedCallExpr(ref s) => {
2020-03-14 16:39:45 +01:00
write!(f, "{}", if s.is_empty() { self.desc() } else { s })?
2020-03-09 03:41:17 +01:00
}
2020-03-13 11:12:41 +01:00
ParseErrorType::ForbiddenConstantExpr(ref s) => {
write!(f, "Expecting a constant to assign to '{}'", s)?
}
2020-03-14 16:39:45 +01:00
ParseErrorType::UnknownOperator(ref s) => write!(f, "{}: '{}'", self.desc(), s)?,
2020-03-16 05:38:01 +01:00
#[cfg(not(feature = "no_index"))]
ParseErrorType::MalformedIndexExpr(ref s) => {
write!(f, "{}", if s.is_empty() { self.desc() } else { s })?
}
#[cfg(not(feature = "no_function"))]
2020-03-04 15:00:01 +01:00
ParseErrorType::FnMissingParams(ref s) => {
2020-03-06 03:50:20 +01:00
write!(f, "Expecting parameters for function '{}'", s)?
2020-03-04 15:00:01 +01:00
}
2020-03-16 05:38:01 +01:00
ParseErrorType::MissingRightParen(ref s) | ParseErrorType::MissingRightBrace(ref s) => {
write!(f, "{} for {}", self.desc(), s)?
}
#[cfg(not(feature = "no_index"))]
ParseErrorType::MissingRightBracket(ref s) => write!(f, "{} for {}", self.desc(), s)?,
2020-03-16 16:51:32 +01:00
ParseErrorType::MissingComma(ref s) => write!(f, "{} for {}", self.desc(), s)?,
2020-03-13 11:12:41 +01:00
ParseErrorType::AssignmentToConstant(ref s) if s.is_empty() => {
2020-03-14 16:39:45 +01:00
write!(f, "{}", self.desc())?
2020-03-13 11:12:41 +01:00
}
ParseErrorType::AssignmentToConstant(ref s) => {
write!(f, "Cannot assign to constant '{}'", s)?
}
2020-03-14 16:39:45 +01:00
_ => write!(f, "{}", self.desc())?,
2020-03-04 15:00:01 +01:00
}
if !self.1.is_eof() {
write!(f, " ({})", self.1)
} else if !self.1.is_none() {
// Do not write any position if None
Ok(())
2020-03-04 15:00:01 +01:00
} else {
write!(f, " at the end of the script but there is no more input")
}
}
}