2020-03-08 19:54:02 +08:00
|
|
|
//! Module containing error definitions for the parsing process.
|
|
|
|
|
2020-03-04 22:00:01 +08:00
|
|
|
use crate::parser::Position;
|
2020-03-18 22:03:50 +08:00
|
|
|
|
2020-03-17 19:26:11 +01:00
|
|
|
use crate::stdlib::{char, error::Error, fmt, string::String};
|
2020-03-04 22:00:01 +08: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 12:38:01 +08:00
|
|
|
/// An identifier is in an invalid format.
|
|
|
|
MalformedIdentifier(String),
|
2020-03-04 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
2020-03-14 23:39:45 +08:00
|
|
|
impl Error for LexError {}
|
2020-03-04 22:00:01 +08: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 12:38:01 +08:00
|
|
|
Self::MalformedIdentifier(s) => {
|
|
|
|
write!(f, "Variable name is not in a legal format: '{}'", s)
|
|
|
|
}
|
2020-03-14 23:39:45 +08:00
|
|
|
Self::UnterminatedString => write!(f, "Open string is not terminated"),
|
2020-03-04 22:00:01 +08: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 19:50:58 +08:00
|
|
|
UnexpectedEOF,
|
2020-03-04 22:00:01 +08:00
|
|
|
/// An unknown operator is encountered. Wrapped value is the operator.
|
|
|
|
UnknownOperator(String),
|
2020-03-29 13:44:27 +08:00
|
|
|
/// Expecting a particular token but not finding one. Wrapped values are the token and usage.
|
|
|
|
MissingToken(String, String),
|
2020-03-04 22:00:01 +08:00
|
|
|
/// An expression in function call arguments `()` has syntax error.
|
2020-03-09 10:41:17 +08:00
|
|
|
MalformedCallExpr(String),
|
2020-03-04 22:00:01 +08:00
|
|
|
/// An expression in indexing brackets `[]` has syntax error.
|
2020-03-16 12:38:01 +08:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-03-09 10:41:17 +08:00
|
|
|
MalformedIndexExpr(String),
|
2020-03-29 23:53:35 +08:00
|
|
|
/// A map definition has duplicated property names. Wrapped is the property name.
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
DuplicatedProperty(String),
|
2020-03-13 18:12:41 +08:00
|
|
|
/// Invalid expression assigned to constant.
|
|
|
|
ForbiddenConstantExpr(String),
|
2020-03-30 12:14:59 +08:00
|
|
|
/// Missing a property name for custom types and maps.
|
2020-03-29 23:53:35 +08:00
|
|
|
PropertyExpected,
|
2020-03-16 12:38:01 +08:00
|
|
|
/// Missing a variable name after the `let`, `const` or `for` keywords.
|
|
|
|
VariableExpected,
|
2020-03-20 19:50:58 +08:00
|
|
|
/// Missing an expression.
|
|
|
|
ExprExpected(String),
|
2020-03-04 22:00:01 +08:00
|
|
|
/// Defining a function `fn` in an appropriate place (e.g. inside another function).
|
2020-03-16 12:38:01 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-03-04 22:00:01 +08:00
|
|
|
WrongFnDefinition,
|
|
|
|
/// Missing a function name after the `fn` keyword.
|
2020-03-16 12:38:01 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-03-04 22:00:01 +08:00
|
|
|
FnMissingName,
|
|
|
|
/// A function definition is missing the parameters list. Wrapped value is the function name.
|
2020-03-16 12:38:01 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-03-04 22:00:01 +08:00
|
|
|
FnMissingParams(String),
|
2020-03-27 16:46:08 +08:00
|
|
|
/// A function definition has duplicated parameters. Wrapped values are the function name and parameter name.
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-03-29 13:44:27 +08:00
|
|
|
FnDuplicatedParam(String, String),
|
2020-03-17 17:33:37 +08:00
|
|
|
/// A function definition is missing the body. Wrapped value is the function name.
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
FnMissingBody(String),
|
2020-03-07 10:15:42 +08:00
|
|
|
/// Assignment to an inappropriate LHS (left-hand-side) expression.
|
|
|
|
AssignmentToInvalidLHS,
|
2020-03-13 18:12:41 +08:00
|
|
|
/// Assignment to a copy of a value.
|
|
|
|
AssignmentToCopy,
|
|
|
|
/// Assignment to an a constant variable.
|
|
|
|
AssignmentToConstant(String),
|
2020-03-17 17:33:37 +08:00
|
|
|
/// Break statement not inside a loop.
|
|
|
|
LoopBreak,
|
2020-03-04 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
2020-03-24 16:46:47 +08:00
|
|
|
impl ParseErrorType {
|
2020-03-25 11:27:09 +08:00
|
|
|
/// Make a `ParseError` using the current type and position.
|
2020-03-24 16:46:47 +08:00
|
|
|
pub(crate) fn into_err(self, pos: Position) -> ParseError {
|
|
|
|
ParseError(self, pos)
|
|
|
|
}
|
2020-03-25 11:27:09 +08:00
|
|
|
|
|
|
|
/// Make a `ParseError` using the current type and EOF position.
|
2020-03-24 16:46:47 +08:00
|
|
|
pub(crate) fn into_err_eof(self) -> ParseError {
|
|
|
|
ParseError(self, Position::eof())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 22:00:01 +08:00
|
|
|
/// Error when parsing a script.
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2020-03-10 09:30:12 +08:00
|
|
|
pub struct ParseError(pub(crate) ParseErrorType, pub(crate) Position);
|
2020-03-04 22:00:01 +08: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 23:39:45 +08:00
|
|
|
pub(crate) fn desc(&self) -> &str {
|
2020-03-04 22:00:01 +08:00
|
|
|
match self.0 {
|
|
|
|
ParseErrorType::BadInput(ref p) => p,
|
2020-03-20 19:50:58 +08:00
|
|
|
ParseErrorType::UnexpectedEOF => "Script is incomplete",
|
2020-03-04 22:00:01 +08:00
|
|
|
ParseErrorType::UnknownOperator(_) => "Unknown operator",
|
2020-03-29 13:44:27 +08:00
|
|
|
ParseErrorType::MissingToken(_, _) => "Expecting a certain token that is missing",
|
2020-03-09 10:41:17 +08:00
|
|
|
ParseErrorType::MalformedCallExpr(_) => "Invalid expression in function call arguments",
|
2020-03-16 12:38:01 +08:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-03-09 10:41:17 +08:00
|
|
|
ParseErrorType::MalformedIndexExpr(_) => "Invalid index in indexing expression",
|
2020-03-29 23:53:35 +08:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
ParseErrorType::DuplicatedProperty(_) => "Duplicated property in object map literal",
|
2020-03-13 18:12:41 +08:00
|
|
|
ParseErrorType::ForbiddenConstantExpr(_) => "Expecting a constant",
|
2020-03-29 23:53:35 +08:00
|
|
|
ParseErrorType::PropertyExpected => "Expecting name of a property",
|
2020-03-16 12:38:01 +08:00
|
|
|
ParseErrorType::VariableExpected => "Expecting name of a variable",
|
2020-03-20 19:50:58 +08:00
|
|
|
ParseErrorType::ExprExpected(_) => "Expecting an expression",
|
2020-03-16 12:38:01 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-03-04 22:00:01 +08:00
|
|
|
ParseErrorType::FnMissingName => "Expecting name in function declaration",
|
2020-03-16 12:38:01 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-03-04 22:00:01 +08:00
|
|
|
ParseErrorType::FnMissingParams(_) => "Expecting parameters in function declaration",
|
2020-03-16 12:38:01 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-03-29 13:44:27 +08:00
|
|
|
ParseErrorType::FnDuplicatedParam(_,_) => "Duplicated parameters in function declaration",
|
2020-03-27 16:46:08 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-03-17 17:33:37 +08:00
|
|
|
ParseErrorType::FnMissingBody(_) => "Expecting body statement block for function declaration",
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-03-17 14:29:22 +08:00
|
|
|
ParseErrorType::WrongFnDefinition => "Function definitions must be at global level and cannot be inside a block or another function",
|
2020-03-13 18:12:41 +08: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 17:33:37 +08:00
|
|
|
ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant variable.",
|
|
|
|
ParseErrorType::LoopBreak => "Break statement should only be used inside a loop"
|
2020-03-04 22:00:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 23:39:45 +08:00
|
|
|
impl Error for ParseError {}
|
|
|
|
|
2020-03-04 22:00:01 +08:00
|
|
|
impl fmt::Display for ParseError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self.0 {
|
2020-03-16 12:38:01 +08:00
|
|
|
ParseErrorType::BadInput(ref s) | ParseErrorType::MalformedCallExpr(ref s) => {
|
2020-03-14 23:39:45 +08:00
|
|
|
write!(f, "{}", if s.is_empty() { self.desc() } else { s })?
|
2020-03-09 10:41:17 +08:00
|
|
|
}
|
2020-03-13 18:12:41 +08:00
|
|
|
ParseErrorType::ForbiddenConstantExpr(ref s) => {
|
|
|
|
write!(f, "Expecting a constant to assign to '{}'", s)?
|
|
|
|
}
|
2020-03-14 23:39:45 +08:00
|
|
|
ParseErrorType::UnknownOperator(ref s) => write!(f, "{}: '{}'", self.desc(), s)?,
|
2020-03-16 12:38:01 +08:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
ParseErrorType::MalformedIndexExpr(ref s) => {
|
|
|
|
write!(f, "{}", if s.is_empty() { self.desc() } else { s })?
|
|
|
|
}
|
|
|
|
|
2020-03-29 23:53:35 +08:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
ParseErrorType::DuplicatedProperty(ref s) => {
|
|
|
|
write!(f, "Duplicated property '{}' for object map literal", s)?
|
|
|
|
}
|
|
|
|
|
2020-03-20 19:50:58 +08:00
|
|
|
ParseErrorType::ExprExpected(ref s) => write!(f, "Expecting {} expression", s)?,
|
|
|
|
|
2020-03-16 12:38:01 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-03-04 22:00:01 +08:00
|
|
|
ParseErrorType::FnMissingParams(ref s) => {
|
2020-03-06 10:50:20 +08:00
|
|
|
write!(f, "Expecting parameters for function '{}'", s)?
|
2020-03-04 22:00:01 +08:00
|
|
|
}
|
2020-03-16 12:38:01 +08:00
|
|
|
|
2020-03-17 17:33:37 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
ParseErrorType::FnMissingBody(ref s) => {
|
|
|
|
write!(f, "Expecting body statement block for function '{}'", s)?
|
|
|
|
}
|
|
|
|
|
2020-03-27 16:46:08 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-03-29 13:44:27 +08:00
|
|
|
ParseErrorType::FnDuplicatedParam(ref s, ref arg) => {
|
2020-03-27 16:46:08 +08:00
|
|
|
write!(f, "Duplicated parameter '{}' for function '{}'", arg, s)?
|
|
|
|
}
|
|
|
|
|
2020-03-29 13:44:27 +08:00
|
|
|
ParseErrorType::MissingToken(ref token, ref s) => {
|
|
|
|
write!(f, "Expecting '{}' {}", token, s)?
|
2020-03-17 14:29:22 +08:00
|
|
|
}
|
2020-03-16 23:51:32 +08:00
|
|
|
|
2020-03-13 18:12:41 +08:00
|
|
|
ParseErrorType::AssignmentToConstant(ref s) if s.is_empty() => {
|
2020-03-14 23:39:45 +08:00
|
|
|
write!(f, "{}", self.desc())?
|
2020-03-13 18:12:41 +08:00
|
|
|
}
|
|
|
|
ParseErrorType::AssignmentToConstant(ref s) => {
|
|
|
|
write!(f, "Cannot assign to constant '{}'", s)?
|
|
|
|
}
|
2020-03-14 23:39:45 +08:00
|
|
|
_ => write!(f, "{}", self.desc())?,
|
2020-03-04 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if !self.1.is_eof() {
|
|
|
|
write!(f, " ({})", self.1)
|
2020-03-08 22:47:13 +08:00
|
|
|
} else if !self.1.is_none() {
|
|
|
|
// Do not write any position if None
|
|
|
|
Ok(())
|
2020-03-04 22:00:01 +08:00
|
|
|
} else {
|
|
|
|
write!(f, " at the end of the script but there is no more input")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|