rhai/src/error.rs

222 lines
9.4 KiB
Rust
Raw Normal View History

2020-03-08 12:54:02 +01:00
//! Module containing error definitions for the parsing process.
use crate::token::Position;
2020-03-18 15:03:50 +01:00
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),
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-04-03 13:42:01 +02:00
Self::MalformedIdentifier(s) => write!(f, "Variable name is not proper: '{}'", 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.
2020-04-10 06:16:39 +02:00
///
/// Some errors never appear when certain features are turned on.
/// They still exist so that the application can turn features on and off without going through
/// massive code changes to remove/add back enum variants in match statements.
2020-03-04 15:00:01 +01:00
#[derive(Debug, PartialEq, Clone)]
pub enum ParseErrorType {
/// Error in the script text. Wrapped value is the error message.
BadInput(String),
/// The script ends prematurely.
UnexpectedEOF,
2020-03-04 15:00:01 +01:00
/// An unknown operator is encountered. Wrapped value is the operator.
UnknownOperator(String),
2020-04-03 13:42:01 +02:00
/// Expecting a particular token but not finding one. Wrapped values are the token and description.
MissingToken(String, String),
2020-04-03 13:42:01 +02:00
/// An expression in function call arguments `()` has syntax error. Wrapped value is the error description (if any).
2020-03-09 03:41:17 +01:00
MalformedCallExpr(String),
2020-04-03 13:42:01 +02:00
/// An expression in indexing brackets `[]` has syntax error. Wrapped value is the error description (if any).
///
2020-04-10 06:16:39 +02:00
/// Never appears under the `no_index` feature.
2020-03-09 03:41:17 +01:00
MalformedIndexExpr(String),
2020-04-06 11:47:34 +02:00
/// An expression in an `in` expression has syntax error. Wrapped value is the error description (if any).
2020-04-10 06:16:39 +02:00
///
/// Never appears under the `no_object` and `no_index` features combination.
2020-04-06 11:47:34 +02:00
MalformedInExpr(String),
2020-04-03 13:42:01 +02:00
/// A map definition has duplicated property names. Wrapped value is the property name.
///
2020-04-10 06:16:39 +02:00
/// Never appears under the `no_object` feature.
2020-03-29 17:53:35 +02:00
DuplicatedProperty(String),
2020-04-03 13:42:01 +02:00
/// Invalid expression assigned to constant. Wrapped value is the name of the constant.
2020-03-13 11:12:41 +01:00
ForbiddenConstantExpr(String),
/// Missing a property name for custom types and maps.
2020-04-10 06:16:39 +02:00
///
/// Never appears under the `no_object` feature.
2020-03-29 17:53:35 +02:00
PropertyExpected,
2020-03-16 05:38:01 +01:00
/// Missing a variable name after the `let`, `const` or `for` keywords.
VariableExpected,
2020-04-03 13:42:01 +02:00
/// Missing an expression. Wrapped value is the expression type.
ExprExpected(String),
2020-03-04 15:00:01 +01:00
/// Defining a function `fn` in an appropriate place (e.g. inside another function).
2020-04-03 13:42:01 +02:00
///
2020-04-10 06:16:39 +02:00
/// Never appears under the `no_function` feature.
2020-03-04 15:00:01 +01:00
WrongFnDefinition,
/// Missing a function name after the `fn` keyword.
2020-04-03 13:42:01 +02:00
///
2020-04-10 06:16:39 +02:00
/// Never appears under the `no_function` feature.
2020-03-04 15:00:01 +01:00
FnMissingName,
/// A function definition is missing the parameters list. Wrapped value is the function name.
2020-04-03 13:42:01 +02:00
///
2020-04-10 06:16:39 +02:00
/// Never appears under the `no_function` feature.
2020-03-04 15:00:01 +01:00
FnMissingParams(String),
/// A function definition has duplicated parameters. Wrapped values are the function name and parameter name.
2020-04-03 13:42:01 +02:00
///
2020-04-10 06:16:39 +02:00
/// Never appears under the `no_function` feature.
FnDuplicatedParam(String, String),
2020-03-17 10:33:37 +01:00
/// A function definition is missing the body. Wrapped value is the function name.
2020-04-03 13:42:01 +02:00
///
2020-04-10 06:16:39 +02:00
/// Never appears under the `no_function` feature.
2020-03-17 10:33:37 +01:00
FnMissingBody(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-17 10:33:37 +01:00
/// Break statement not inside a loop.
LoopBreak,
2020-03-04 15:00:01 +01:00
}
2020-03-24 09:46:47 +01:00
impl ParseErrorType {
2020-03-25 04:27:09 +01:00
/// Make a `ParseError` using the current type and position.
2020-03-24 09:46:47 +01:00
pub(crate) fn into_err(self, pos: Position) -> ParseError {
ParseError(self, pos)
}
2020-03-25 04:27:09 +01:00
/// Make a `ParseError` using the current type and EOF position.
2020-03-24 09:46:47 +01:00
pub(crate) fn into_err_eof(self) -> ParseError {
ParseError(self, Position::eof())
}
}
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 {
/// 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-04-05 06:37:07 +02:00
match &self.0 {
ParseErrorType::BadInput(p) => p,
ParseErrorType::UnexpectedEOF => "Script is incomplete",
2020-03-04 15:00:01 +01:00
ParseErrorType::UnknownOperator(_) => "Unknown operator",
ParseErrorType::MissingToken(_, _) => "Expecting a certain token that is missing",
2020-03-09 03:41:17 +01:00
ParseErrorType::MalformedCallExpr(_) => "Invalid expression in function call arguments",
ParseErrorType::MalformedIndexExpr(_) => "Invalid index in indexing expression",
2020-04-06 11:47:34 +02:00
ParseErrorType::MalformedInExpr(_) => "Invalid 'in' expression",
2020-03-29 17:53:35 +02:00
ParseErrorType::DuplicatedProperty(_) => "Duplicated property in object map literal",
2020-03-13 11:12:41 +01:00
ParseErrorType::ForbiddenConstantExpr(_) => "Expecting a constant",
2020-03-29 17:53:35 +02:00
ParseErrorType::PropertyExpected => "Expecting name of a property",
2020-03-16 05:38:01 +01:00
ParseErrorType::VariableExpected => "Expecting name of a variable",
ParseErrorType::ExprExpected(_) => "Expecting an expression",
2020-03-04 15:00:01 +01:00
ParseErrorType::FnMissingName => "Expecting name in function declaration",
ParseErrorType::FnMissingParams(_) => "Expecting parameters in function declaration",
ParseErrorType::FnDuplicatedParam(_,_) => "Duplicated parameters in function declaration",
2020-03-17 10:33:37 +01:00
ParseErrorType::FnMissingBody(_) => "Expecting body statement block for function declaration",
ParseErrorType::WrongFnDefinition => "Function definitions must be at global 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",
2020-03-17 10:33:37 +01:00
ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant variable.",
ParseErrorType::LoopBreak => "Break statement should only be used inside a loop"
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 {
2020-04-05 06:37:07 +02:00
match &self.0 {
ParseErrorType::BadInput(s) | ParseErrorType::MalformedCallExpr(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-04-05 06:37:07 +02:00
ParseErrorType::ForbiddenConstantExpr(s) => {
2020-03-13 11:12:41 +01:00
write!(f, "Expecting a constant to assign to '{}'", s)?
}
2020-04-05 06:37:07 +02:00
ParseErrorType::UnknownOperator(s) => write!(f, "{}: '{}'", self.desc(), s)?,
2020-03-16 05:38:01 +01:00
2020-04-05 06:37:07 +02:00
ParseErrorType::MalformedIndexExpr(s) => {
2020-03-16 05:38:01 +01:00
write!(f, "{}", if s.is_empty() { self.desc() } else { s })?
}
2020-04-06 11:47:34 +02:00
ParseErrorType::MalformedInExpr(s) => {
write!(f, "{}", if s.is_empty() { self.desc() } else { s })?
}
2020-04-05 06:37:07 +02:00
ParseErrorType::DuplicatedProperty(s) => {
2020-03-29 17:53:35 +02:00
write!(f, "Duplicated property '{}' for object map literal", s)?
}
2020-04-05 06:37:07 +02:00
ParseErrorType::ExprExpected(s) => write!(f, "Expecting {} expression", s)?,
2020-04-05 06:37:07 +02:00
ParseErrorType::FnMissingParams(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
2020-04-05 06:37:07 +02:00
ParseErrorType::FnMissingBody(s) => {
2020-03-17 10:33:37 +01:00
write!(f, "Expecting body statement block for function '{}'", s)?
}
2020-04-05 06:37:07 +02:00
ParseErrorType::FnDuplicatedParam(s, arg) => {
write!(f, "Duplicated parameter '{}' for function '{}'", arg, s)?
}
2020-04-05 06:37:07 +02:00
ParseErrorType::MissingToken(token, s) => write!(f, "Expecting '{}' {}", token, s)?,
2020-03-16 16:51:32 +01:00
2020-04-05 06:37:07 +02:00
ParseErrorType::AssignmentToConstant(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
}
2020-04-05 06:37:07 +02:00
ParseErrorType::AssignmentToConstant(s) => {
2020-03-13 11:12:41 +01:00
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")
}
}
}