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-08 12:54:02 +01:00
|
|
|
use std::{char, error::Error, fmt};
|
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),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for LexError {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
match *self {
|
|
|
|
Self::UnexpectedChar(_) => "Unexpected character",
|
|
|
|
Self::UnterminatedString => "Open string is not terminated",
|
|
|
|
Self::MalformedEscapeSequence(_) => "Unexpected values in escape sequence",
|
|
|
|
Self::MalformedNumber(_) => "Unexpected characters in number",
|
|
|
|
Self::MalformedChar(_) => "Char constant not a single character",
|
|
|
|
Self::InputError(_) => "Input error",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
Self::InputError(s) => write!(f, "{}", s),
|
|
|
|
_ => write!(f, "{}", self.description()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 `)`.
|
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-05 18:05:02 +01:00
|
|
|
MissingRightBracket(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-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-04 15:00:01 +01:00
|
|
|
/// Missing a variable name after the `let` keyword.
|
|
|
|
VarExpectsIdentifier,
|
|
|
|
/// Defining a function `fn` in an appropriate place (e.g. inside another function).
|
|
|
|
WrongFnDefinition,
|
|
|
|
/// Missing a function name after the `fn` keyword.
|
|
|
|
FnMissingName,
|
|
|
|
/// A function definition is missing the parameters list. Wrapped value is the function name.
|
|
|
|
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)]
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for ParseError {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
match self.0 {
|
|
|
|
ParseErrorType::BadInput(ref p) => p,
|
|
|
|
ParseErrorType::InputPastEndOfFile => "Script is incomplete",
|
|
|
|
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 '}'",
|
|
|
|
ParseErrorType::MissingRightBracket(_) => "Expecting ']'",
|
2020-03-09 03:41:17 +01:00
|
|
|
ParseErrorType::MalformedCallExpr(_) => "Invalid expression in function call arguments",
|
|
|
|
ParseErrorType::MalformedIndexExpr(_) => "Invalid index in indexing expression",
|
2020-03-13 11:12:41 +01:00
|
|
|
ParseErrorType::ForbiddenConstantExpr(_) => "Expecting a constant",
|
2020-03-04 15:00:01 +01:00
|
|
|
ParseErrorType::VarExpectsIdentifier => "Expecting name of a variable",
|
|
|
|
ParseErrorType::FnMissingName => "Expecting name in function declaration",
|
|
|
|
ParseErrorType::FnMissingParams(_) => "Expecting parameters in function declaration",
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&dyn Error> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ParseError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self.0 {
|
2020-03-09 03:41:17 +01:00
|
|
|
ParseErrorType::BadInput(ref s)
|
|
|
|
| ParseErrorType::MalformedIndexExpr(ref s)
|
|
|
|
| ParseErrorType::MalformedCallExpr(ref s) => {
|
|
|
|
write!(f, "{}", if s.is_empty() { self.description() } else { s })?
|
|
|
|
}
|
2020-03-13 11:12:41 +01:00
|
|
|
ParseErrorType::ForbiddenConstantExpr(ref s) => {
|
|
|
|
write!(f, "Expecting a constant to assign to '{}'", s)?
|
|
|
|
}
|
2020-03-04 15:00:01 +01:00
|
|
|
ParseErrorType::UnknownOperator(ref s) => write!(f, "{}: '{}'", self.description(), s)?,
|
|
|
|
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-05 18:05:02 +01:00
|
|
|
ParseErrorType::MissingRightParen(ref s)
|
|
|
|
| ParseErrorType::MissingRightBrace(ref s)
|
|
|
|
| ParseErrorType::MissingRightBracket(ref s) => {
|
|
|
|
write!(f, "{} for {}", self.description(), s)?
|
|
|
|
}
|
2020-03-13 11:12:41 +01:00
|
|
|
ParseErrorType::AssignmentToConstant(ref s) if s.is_empty() => {
|
|
|
|
write!(f, "{}", self.description())?
|
|
|
|
}
|
|
|
|
ParseErrorType::AssignmentToConstant(ref s) => {
|
|
|
|
write!(f, "Cannot assign to constant '{}'", s)?
|
|
|
|
}
|
2020-03-04 15:00:01 +01:00
|
|
|
_ => write!(f, "{}", self.description())?,
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|