2020-03-08 12:54:02 +01:00
|
|
|
//! Module containing error definitions for the parsing process.
|
|
|
|
|
2020-11-20 09:52:28 +01:00
|
|
|
use crate::{EvalAltResult, Position};
|
2021-04-17 09:15:54 +02:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use core_error::Error;
|
|
|
|
#[cfg(not(feature = "no_std"))]
|
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt;
|
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
2020-03-04 15:00:01 +01:00
|
|
|
|
2020-11-20 09:52:28 +01:00
|
|
|
/// _(INTERNALS)_ Error encountered when tokenizing the script text.
|
2020-07-25 09:52:27 +02:00
|
|
|
/// Exported under the `internals` feature only.
|
|
|
|
///
|
2021-01-16 07:46:03 +01:00
|
|
|
/// # Volatile Data Structure
|
2020-07-25 09:52:27 +02:00
|
|
|
///
|
|
|
|
/// This type is volatile and may change.
|
2020-05-07 06:25:09 +02:00
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
|
2020-06-25 05:07:46 +02:00
|
|
|
#[non_exhaustive]
|
2020-03-04 15:00:01 +01:00
|
|
|
pub enum LexError {
|
2020-07-05 09:23:51 +02:00
|
|
|
/// An unexpected symbol is encountered when tokenizing the script text.
|
|
|
|
UnexpectedInput(String),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// A string literal is not terminated before a new-line or EOF.
|
|
|
|
UnterminatedString,
|
2020-06-14 08:25:47 +02:00
|
|
|
/// An identifier is in an invalid format.
|
|
|
|
StringTooLong(usize),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// 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-06-14 10:56:36 +02:00
|
|
|
/// Bad symbol encountered when tokenizing the script text.
|
2020-11-21 08:08:18 +01:00
|
|
|
ImproperSymbol(String, 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 {
|
2021-03-10 06:32:09 +01:00
|
|
|
#[inline(always)]
|
2020-03-04 15:00:01 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
2020-07-05 09:23:51 +02:00
|
|
|
Self::UnexpectedInput(s) => write!(f, "Unexpected '{}'", s),
|
2020-11-02 05:50:27 +01:00
|
|
|
Self::MalformedEscapeSequence(s) => write!(f, "{}: '{}'", self.desc(), s),
|
|
|
|
Self::MalformedNumber(s) => write!(f, "{}: '{}'", self.desc(), s),
|
|
|
|
Self::MalformedChar(s) => write!(f, "{}: '{}'", self.desc(), s),
|
|
|
|
Self::MalformedIdentifier(s) => write!(f, "{}: '{}'", self.desc(), s),
|
|
|
|
Self::UnterminatedString => f.write_str(self.desc()),
|
|
|
|
Self::StringTooLong(max) => write!(f, "{} ({})", self.desc(), max),
|
2020-11-21 08:08:18 +01:00
|
|
|
Self::ImproperSymbol(s, d) if d.is_empty() => {
|
|
|
|
write!(f, "Invalid symbol encountered: '{}'", s)
|
|
|
|
}
|
|
|
|
Self::ImproperSymbol(_, d) => f.write_str(d),
|
2020-03-04 15:00:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 10:56:36 +02:00
|
|
|
impl LexError {
|
2021-06-12 16:47:43 +02:00
|
|
|
#[must_use]
|
2020-11-02 05:50:27 +01:00
|
|
|
pub(crate) fn desc(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
Self::UnexpectedInput(_) => "Unexpected character encountered",
|
|
|
|
Self::UnterminatedString => "Open string is not terminated",
|
|
|
|
Self::StringTooLong(_) => "Length of string literal exceeds the maximum limit",
|
|
|
|
Self::MalformedEscapeSequence(_) => "Invalid escape sequence",
|
|
|
|
Self::MalformedNumber(_) => "Invalid number",
|
|
|
|
Self::MalformedChar(_) => "Invalid character",
|
|
|
|
Self::MalformedIdentifier(_) => "Variable name is not proper",
|
2020-11-21 08:08:18 +01:00
|
|
|
Self::ImproperSymbol(_, _) => "Invalid symbol encountered",
|
2020-11-02 05:50:27 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 02:36:06 +01:00
|
|
|
/// Convert a [`LexError`] into a [`ParseError`].
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2021-06-12 16:47:43 +02:00
|
|
|
#[must_use]
|
2020-11-25 02:36:06 +01:00
|
|
|
pub fn into_err(self, pos: Position) -> ParseError {
|
|
|
|
ParseError(Box::new(self.into()), pos)
|
2020-06-14 10:56:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-07 06:25:09 +02:00
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
|
2020-06-25 05:07:46 +02:00
|
|
|
#[non_exhaustive]
|
2020-03-04 15:00:01 +01:00
|
|
|
pub enum ParseErrorType {
|
|
|
|
/// The script ends prematurely.
|
2020-03-20 12:50:58 +01:00
|
|
|
UnexpectedEOF,
|
2020-11-02 05:50:27 +01:00
|
|
|
/// Error in the script text. Wrapped value is the lex error.
|
|
|
|
BadInput(LexError),
|
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.
|
2020-03-29 07:44:27 +02:00
|
|
|
MissingToken(String, String),
|
2021-06-10 04:16:39 +02:00
|
|
|
/// Expecting a particular symbol but not finding one. Wrapped value is the description.
|
|
|
|
MissingSymbol(String),
|
2020-12-26 06:05:57 +01: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-12-26 06:05:57 +01:00
|
|
|
/// An expression in indexing brackets `[]` has syntax error. Wrapped value is the error
|
|
|
|
/// description (if any).
|
2020-04-03 13:42:01 +02:00
|
|
|
///
|
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-12-26 06:05:57 +01: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-07-30 12:18:28 +02:00
|
|
|
/// A capturing has syntax error. Wrapped value is the error description (if any).
|
|
|
|
///
|
2020-08-03 06:10:20 +02:00
|
|
|
/// Never appears under the `no_closure` feature.
|
2020-07-30 12:18:28 +02:00
|
|
|
MalformedCapture(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),
|
2021-04-16 03:41:02 +02:00
|
|
|
/// A `switch` case is duplicated.
|
2020-11-13 11:32:18 +01:00
|
|
|
DuplicatedSwitchCase,
|
2021-06-07 05:01:16 +02:00
|
|
|
/// A variable name is duplicated. Wrapped value is the variable name.
|
|
|
|
DuplicatedVariable(String),
|
2021-04-16 03:41:02 +02:00
|
|
|
/// The default case of a `switch` statement is not the last.
|
|
|
|
WrongSwitchDefaultCase,
|
2021-04-16 07:28:36 +02:00
|
|
|
/// The case condition of a `switch` statement is not appropriate.
|
|
|
|
WrongSwitchCaseCondition,
|
2020-03-30 06:14:59 +02:00
|
|
|
/// 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-10-20 17:16:03 +02:00
|
|
|
/// Missing a variable name after the `let`, `const`, `for` or `catch` keywords.
|
2020-03-16 05:38:01 +01:00
|
|
|
VariableExpected,
|
2020-07-17 04:18:07 +02:00
|
|
|
/// An identifier is a reserved keyword.
|
|
|
|
Reserved(String),
|
2020-04-03 13:42:01 +02:00
|
|
|
/// Missing an expression. Wrapped value is the expression type.
|
2020-03-20 12:50:58 +01:00
|
|
|
ExprExpected(String),
|
2020-12-12 13:09:29 +01:00
|
|
|
/// Defining a doc-comment in an appropriate place (e.g. not at global level).
|
|
|
|
///
|
|
|
|
/// Never appears under the `no_function` feature.
|
|
|
|
WrongDocComment,
|
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.
|
2021-03-30 05:39:00 +02:00
|
|
|
WrongFnDefinition,
|
2021-01-03 13:54:08 +01:00
|
|
|
/// Defining a function with a name that conflicts with an existing function.
|
|
|
|
/// Wrapped values are the function name and number of parameters.
|
|
|
|
///
|
|
|
|
/// Never appears under the `no_object` feature.
|
|
|
|
FnDuplicatedDefinition(String, usize),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// 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),
|
2020-12-26 06:05:57 +01:00
|
|
|
/// 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.
|
2020-03-29 07:44:27 +02:00
|
|
|
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-05-08 10:49:24 +02:00
|
|
|
/// Export statement not at global level.
|
|
|
|
///
|
|
|
|
/// Never appears under the `no_module` feature.
|
|
|
|
WrongExport,
|
2020-06-14 08:25:47 +02:00
|
|
|
/// Assignment to an a constant variable. Wrapped value is the constant variable name.
|
2020-03-13 11:12:41 +01:00
|
|
|
AssignmentToConstant(String),
|
2020-10-28 03:26:36 +01:00
|
|
|
/// Assignment to an inappropriate LHS (left-hand-side) expression.
|
|
|
|
/// Wrapped value is the error message (if any).
|
|
|
|
AssignmentToInvalidLHS(String),
|
2020-05-18 13:32:22 +02:00
|
|
|
/// Expression exceeding the maximum levels of complexity.
|
|
|
|
///
|
|
|
|
/// Never appears under the `unchecked` feature.
|
|
|
|
ExprTooDeep,
|
2020-06-14 08:25:47 +02:00
|
|
|
/// Literal exceeding the maximum size. Wrapped values are the data type name and the maximum size.
|
|
|
|
///
|
|
|
|
/// Never appears under the `unchecked` feature.
|
|
|
|
LiteralTooLarge(String, usize),
|
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-11-20 09:52:28 +01:00
|
|
|
/// Make a [`ParseError`] using the current type and position.
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2021-06-12 16:47:43 +02:00
|
|
|
#[must_use]
|
2020-05-22 12:28:13 +02:00
|
|
|
pub(crate) fn into_err(self, pos: Position) -> ParseError {
|
|
|
|
ParseError(Box::new(self), pos)
|
2020-03-24 09:46:47 +01:00
|
|
|
}
|
2020-03-04 15:00:01 +01:00
|
|
|
|
2021-06-12 16:47:43 +02:00
|
|
|
#[must_use]
|
2020-03-14 16:39:45 +01:00
|
|
|
pub(crate) fn desc(&self) -> &str {
|
2020-06-12 12:04:16 +02:00
|
|
|
match self {
|
|
|
|
Self::UnexpectedEOF => "Script is incomplete",
|
2021-01-03 13:54:08 +01:00
|
|
|
Self::BadInput(err) => err.desc(),
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::UnknownOperator(_) => "Unknown operator",
|
|
|
|
Self::MissingToken(_, _) => "Expecting a certain token that is missing",
|
2021-06-10 04:16:39 +02:00
|
|
|
Self::MissingSymbol(_) => "Expecting a certain symbol that is missing",
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::MalformedCallExpr(_) => "Invalid expression in function call arguments",
|
|
|
|
Self::MalformedIndexExpr(_) => "Invalid index in indexing expression",
|
|
|
|
Self::MalformedInExpr(_) => "Invalid 'in' expression",
|
2020-07-30 12:18:28 +02:00
|
|
|
Self::MalformedCapture(_) => "Invalid capturing",
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::DuplicatedProperty(_) => "Duplicated property in object map literal",
|
2020-11-13 11:32:18 +01:00
|
|
|
Self::DuplicatedSwitchCase => "Duplicated switch case",
|
2021-06-07 05:01:16 +02:00
|
|
|
Self::DuplicatedVariable(_) => "Duplicated variable name",
|
2021-04-16 03:41:02 +02:00
|
|
|
Self::WrongSwitchDefaultCase => "Default switch case is not the last",
|
2021-04-16 07:28:36 +02:00
|
|
|
Self::WrongSwitchCaseCondition => "Default switch case cannot have condition",
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::PropertyExpected => "Expecting name of a property",
|
|
|
|
Self::VariableExpected => "Expecting name of a variable",
|
2020-07-17 04:18:07 +02:00
|
|
|
Self::Reserved(_) => "Invalid use of reserved keyword",
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::ExprExpected(_) => "Expecting an expression",
|
2021-03-30 05:39:00 +02:00
|
|
|
Self::WrongFnDefinition => "Function definitions must be at global level and cannot be inside a block or another function",
|
2021-01-03 13:54:08 +01:00
|
|
|
Self::FnDuplicatedDefinition(_, _) => "Function already exists",
|
2020-07-26 16:25:30 +02:00
|
|
|
Self::FnMissingName => "Expecting function name in function declaration",
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::FnMissingParams(_) => "Expecting parameters in function declaration",
|
|
|
|
Self::FnDuplicatedParam(_,_) => "Duplicated parameters in function declaration",
|
|
|
|
Self::FnMissingBody(_) => "Expecting body statement block for function declaration",
|
2020-12-12 13:09:29 +01:00
|
|
|
Self::WrongDocComment => "Doc-comment must be followed immediately by a function definition",
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::WrongExport => "Export statement can only appear at global level",
|
|
|
|
Self::AssignmentToConstant(_) => "Cannot assign to a constant value",
|
2020-10-28 03:26:36 +01:00
|
|
|
Self::AssignmentToInvalidLHS(_) => "Expression cannot be assigned to",
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::ExprTooDeep => "Expression exceeds maximum complexity",
|
2020-06-14 08:25:47 +02:00
|
|
|
Self::LiteralTooLarge(_, _) => "Literal exceeds maximum limit",
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::LoopBreak => "Break statement should only be used inside a loop"
|
2020-03-04 15:00:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 12:04:16 +02:00
|
|
|
impl fmt::Display for ParseErrorType {
|
2020-06-13 11:03:49 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-06-12 12:04:16 +02:00
|
|
|
match self {
|
2020-11-02 05:50:27 +01:00
|
|
|
Self::BadInput(err) => write!(f, "{}", err),
|
|
|
|
|
|
|
|
Self::MalformedCallExpr(s) => f.write_str(if s.is_empty() { self.desc() } else { s }),
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::UnknownOperator(s) => write!(f, "{}: '{}'", self.desc(), s),
|
2020-03-16 05:38:01 +01:00
|
|
|
|
2020-07-30 12:18:28 +02:00
|
|
|
Self::MalformedIndexExpr(s) | Self::MalformedInExpr(s) | Self::MalformedCapture(s) => {
|
|
|
|
f.write_str(if s.is_empty() { self.desc() } else { s })
|
|
|
|
}
|
2020-04-06 11:47:34 +02:00
|
|
|
|
2021-01-03 13:54:08 +01:00
|
|
|
Self::FnDuplicatedDefinition(s, n) => {
|
|
|
|
write!(f, "Function '{}' with ", s)?;
|
|
|
|
match n {
|
|
|
|
0 => f.write_str("no parameters already exists"),
|
|
|
|
1 => f.write_str("1 parameter already exists"),
|
|
|
|
_ => write!(f, "{} parameters already exists", n),
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::DuplicatedProperty(s) => {
|
|
|
|
write!(f, "Duplicated property '{}' for object map literal", s)
|
2020-03-29 17:53:35 +02:00
|
|
|
}
|
2020-11-13 11:32:18 +01:00
|
|
|
Self::DuplicatedSwitchCase => f.write_str(self.desc()),
|
2021-06-07 05:01:16 +02:00
|
|
|
Self::DuplicatedVariable(s) => write!(f, "Duplicated variable name '{}'", s),
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::ExprExpected(s) => write!(f, "Expecting {} expression", s),
|
2020-03-20 12:50:58 +01:00
|
|
|
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::FnMissingParams(s) => write!(f, "Expecting parameters for function '{}'", s),
|
2020-03-16 05:38:01 +01:00
|
|
|
|
2020-07-19 11:14:55 +02:00
|
|
|
Self::FnMissingBody(s) if s.is_empty() => {
|
|
|
|
f.write_str("Expecting body statement block for anonymous function")
|
|
|
|
}
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::FnMissingBody(s) => {
|
|
|
|
write!(f, "Expecting body statement block for function '{}'", s)
|
2020-03-17 10:33:37 +01:00
|
|
|
}
|
|
|
|
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::FnDuplicatedParam(s, arg) => {
|
|
|
|
write!(f, "Duplicated parameter '{}' for function '{}'", arg, s)
|
2020-03-27 09:46:08 +01:00
|
|
|
}
|
|
|
|
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::MissingToken(token, s) => write!(f, "Expecting '{}' {}", token, s),
|
2021-06-10 04:16:39 +02:00
|
|
|
Self::MissingSymbol(s) => f.write_str(s),
|
2020-03-16 16:51:32 +01:00
|
|
|
|
2020-07-02 15:46:08 +02:00
|
|
|
Self::AssignmentToConstant(s) if s.is_empty() => f.write_str(self.desc()),
|
2020-06-12 12:04:16 +02:00
|
|
|
Self::AssignmentToConstant(s) => write!(f, "Cannot assign to constant '{}'", s),
|
2020-10-28 03:26:36 +01:00
|
|
|
|
|
|
|
Self::AssignmentToInvalidLHS(s) if s.is_empty() => f.write_str(self.desc()),
|
|
|
|
Self::AssignmentToInvalidLHS(s) => f.write_str(s),
|
|
|
|
|
2020-06-14 08:25:47 +02:00
|
|
|
Self::LiteralTooLarge(typ, max) => {
|
|
|
|
write!(f, "{} exceeds the maximum limit ({})", typ, max)
|
|
|
|
}
|
2020-12-12 13:09:29 +01:00
|
|
|
|
2020-07-17 04:18:07 +02:00
|
|
|
Self::Reserved(s) => write!(f, "'{}' is a reserved keyword", s),
|
2020-12-12 13:09:29 +01:00
|
|
|
|
2020-07-02 15:46:08 +02:00
|
|
|
_ => f.write_str(self.desc()),
|
2020-03-04 15:00:01 +01:00
|
|
|
}
|
2020-06-12 12:04:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 05:50:27 +01:00
|
|
|
impl From<LexError> for ParseErrorType {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-11-02 05:50:27 +01:00
|
|
|
fn from(err: LexError) -> Self {
|
2020-06-14 10:56:36 +02:00
|
|
|
match err {
|
|
|
|
LexError::StringTooLong(max) => {
|
2020-11-02 05:50:27 +01:00
|
|
|
Self::LiteralTooLarge("Length of string literal".to_string(), max)
|
2020-06-14 10:56:36 +02:00
|
|
|
}
|
2020-11-02 05:50:27 +01:00
|
|
|
_ => Self::BadInput(err),
|
2020-06-14 10:56:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 12:04:16 +02:00
|
|
|
/// Error when parsing a script.
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
|
|
|
|
pub struct ParseError(pub Box<ParseErrorType>, pub Position);
|
2020-03-04 15:00:01 +01:00
|
|
|
|
2020-06-12 12:04:16 +02:00
|
|
|
impl Error for ParseError {}
|
|
|
|
|
|
|
|
impl fmt::Display for ParseError {
|
2021-03-10 06:32:09 +01:00
|
|
|
#[inline(always)]
|
2020-06-12 12:04:16 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(&self.0, f)?;
|
|
|
|
|
|
|
|
// Do not write any position if None
|
2020-04-17 14:01:41 +02:00
|
|
|
if !self.1.is_none() {
|
2020-06-12 12:04:16 +02:00
|
|
|
write!(f, " ({})", self.1)?;
|
2020-03-04 15:00:01 +01:00
|
|
|
}
|
2020-06-12 12:04:16 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseErrorType> for Box<EvalAltResult> {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-06-12 12:04:16 +02:00
|
|
|
fn from(err: ParseErrorType) -> Self {
|
2020-12-29 15:04:31 +01:00
|
|
|
Box::new(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseErrorType> for EvalAltResult {
|
|
|
|
#[inline(always)]
|
|
|
|
fn from(err: ParseErrorType) -> Self {
|
|
|
|
EvalAltResult::ErrorParsing(err, Position::NONE)
|
2020-06-12 12:04:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseError> for Box<EvalAltResult> {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-06-12 12:04:16 +02:00
|
|
|
fn from(err: ParseError) -> Self {
|
2020-12-29 15:04:31 +01:00
|
|
|
Box::new(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseError> for EvalAltResult {
|
|
|
|
#[inline(always)]
|
|
|
|
fn from(err: ParseError) -> Self {
|
|
|
|
EvalAltResult::ErrorParsing(*err.0, err.1)
|
2020-03-04 15:00:01 +01:00
|
|
|
}
|
|
|
|
}
|