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-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),
|
|
|
|
/// 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.
|
2020-03-20 12:50:58 +01:00
|
|
|
UnexpectedEOF,
|
2020-03-04 15:00:01 +01:00
|
|
|
/// An unknown operator is encountered. Wrapped value is the operator.
|
|
|
|
UnknownOperator(String),
|
|
|
|
/// An open `(` is missing the corresponding closing `)`.
|
2020-03-05 18:05:02 +01:00
|
|
|
MissingRightParen(String),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Expecting `(` but not finding one.
|
|
|
|
MissingLeftBrace,
|
|
|
|
/// An open `{` is missing the corresponding closing `}`.
|
2020-03-05 18:05:02 +01:00
|
|
|
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"))]
|
2020-03-05 18:05:02 +01:00
|
|
|
MissingRightBracket(String),
|
2020-03-16 16:51:32 +01:00
|
|
|
/// A list of expressions is missing the separating ','.
|
|
|
|
MissingComma(String),
|
2020-03-17 07:29:22 +01:00
|
|
|
/// A statement is missing the ending ';'.
|
|
|
|
MissingSemicolon(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,
|
2020-03-20 12:50:58 +01:00
|
|
|
/// Missing an expression.
|
|
|
|
ExprExpected(String),
|
2020-03-16 05:38:01 +01:00
|
|
|
/// 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-17 10:33:37 +01:00
|
|
|
/// A function definition is missing the body. Wrapped value is the function name.
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// Error when parsing a script.
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2020-03-10 02:30:12 +01:00
|
|
|
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,
|
2020-03-20 12:50:58 +01:00
|
|
|
ParseErrorType::UnexpectedEOF => "Script is incomplete",
|
2020-03-04 15:00:01 +01:00
|
|
|
ParseErrorType::UnknownOperator(_) => "Unknown operator",
|
2020-03-05 18:05:02 +01:00
|
|
|
ParseErrorType::MissingRightParen(_) => "Expecting ')'",
|
2020-03-04 15:00:01 +01:00
|
|
|
ParseErrorType::MissingLeftBrace => "Expecting '{'",
|
2020-03-05 18:05:02 +01:00
|
|
|
ParseErrorType::MissingRightBrace(_) => "Expecting '}'",
|
2020-03-16 05:38:01 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-03-05 18:05:02 +01:00
|
|
|
ParseErrorType::MissingRightBracket(_) => "Expecting ']'",
|
2020-03-16 16:51:32 +01:00
|
|
|
ParseErrorType::MissingComma(_) => "Expecting ','",
|
2020-03-17 07:29:22 +01:00
|
|
|
ParseErrorType::MissingSemicolon(_) => "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",
|
2020-03-20 12:50:58 +01:00
|
|
|
ParseErrorType::ExprExpected(_) => "Expecting an expression",
|
2020-03-16 05:38:01 +01:00
|
|
|
#[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-17 10:33:37 +01:00
|
|
|
ParseErrorType::FnMissingBody(_) => "Expecting body statement block for function declaration",
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-03-17 07:29:22 +01:00
|
|
|
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 {
|
|
|
|
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 })?
|
|
|
|
}
|
|
|
|
|
2020-03-20 12:50:58 +01:00
|
|
|
ParseErrorType::ExprExpected(ref s) => write!(f, "Expecting {} expression", s)?,
|
|
|
|
|
2020-03-16 05:38:01 +01:00
|
|
|
#[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
|
|
|
|
2020-03-17 10:33:37 +01:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
ParseErrorType::FnMissingBody(ref s) => {
|
|
|
|
write!(f, "Expecting body statement block for function '{}'", s)?
|
|
|
|
}
|
|
|
|
|
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-17 07:29:22 +01:00
|
|
|
ParseErrorType::MissingSemicolon(ref s) | ParseErrorType::MissingComma(ref s) => {
|
|
|
|
write!(f, "{} for {}", self.desc(), s)?
|
|
|
|
}
|
2020-03-16 16:51:32 +01:00
|
|
|
|
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)
|
2020-03-08 15:47:13 +01:00
|
|
|
} 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|