2020-03-08 12:54:02 +01:00
|
|
|
//! Module containing error definitions for the evaluation process.
|
2020-03-04 15:00:01 +01:00
|
|
|
|
|
|
|
use crate::any::Dynamic;
|
|
|
|
use crate::error::ParseError;
|
2020-03-10 16:06:20 +01:00
|
|
|
use crate::parser::{Position, INT};
|
2020-03-11 04:03:18 +01:00
|
|
|
|
2020-03-17 19:26:11 +01:00
|
|
|
use crate::stdlib::{
|
|
|
|
error::Error,
|
|
|
|
fmt,
|
|
|
|
string::{String, ToString},
|
|
|
|
};
|
|
|
|
|
2020-03-18 05:04:26 +01:00
|
|
|
#[cfg(not(feature = "no_std"))]
|
2020-03-17 19:26:11 +01:00
|
|
|
use crate::stdlib::path::PathBuf;
|
2020-03-04 15:00:01 +01:00
|
|
|
|
|
|
|
/// Evaluation result.
|
|
|
|
///
|
|
|
|
/// All wrapped `Position` values represent the location in the script where the error occurs.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum EvalAltResult {
|
|
|
|
/// Syntax error.
|
|
|
|
ErrorParsing(ParseError),
|
|
|
|
/// Call to an unknown function. Wrapped value is the name of the function.
|
|
|
|
ErrorFunctionNotFound(String, Position),
|
|
|
|
/// Function call has incorrect number of arguments.
|
|
|
|
/// Wrapped values are the name of the function, the number of parameters required
|
|
|
|
/// and the actual number of arguments passed.
|
|
|
|
ErrorFunctionArgsMismatch(String, usize, usize, Position),
|
|
|
|
/// Non-boolean operand encountered for boolean operator. Wrapped value is the operator.
|
|
|
|
ErrorBooleanArgMismatch(String, Position),
|
2020-03-07 15:50:46 +01:00
|
|
|
/// Non-character value encountered where a character is required.
|
|
|
|
ErrorCharMismatch(Position),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Array access out-of-bounds.
|
|
|
|
/// Wrapped values are the current number of elements in the array and the index number.
|
2020-03-10 16:06:20 +01:00
|
|
|
ErrorArrayBounds(usize, INT, Position),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// String indexing out-of-bounds.
|
|
|
|
/// Wrapped values are the current number of characters in the string and the index number.
|
2020-03-10 16:06:20 +01:00
|
|
|
ErrorStringBounds(usize, INT, Position),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Trying to index into a type that is not an array and not a string.
|
2020-03-07 10:32:15 +01:00
|
|
|
ErrorIndexingType(String, Position),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Trying to index into an array or string with an index that is not `i64`.
|
|
|
|
ErrorIndexExpr(Position),
|
2020-03-20 12:50:58 +01:00
|
|
|
/// The guard expression in an `if` or `while` statement does not return a boolean value.
|
|
|
|
ErrorLogicGuard(Position),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// The `for` statement encounters a type that is not an iterator.
|
|
|
|
ErrorFor(Position),
|
|
|
|
/// Usage of an unknown variable. Wrapped value is the name of the variable.
|
|
|
|
ErrorVariableNotFound(String, Position),
|
|
|
|
/// Assignment to an inappropriate LHS (left-hand-side) expression.
|
|
|
|
ErrorAssignmentToUnknownLHS(Position),
|
2020-03-13 11:12:41 +01:00
|
|
|
/// Assignment to a constant variable.
|
|
|
|
ErrorAssignmentToConstant(String, Position),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Returned type is not the same as the required output type.
|
|
|
|
/// Wrapped value is the type of the actual result.
|
|
|
|
ErrorMismatchOutputType(String, Position),
|
|
|
|
/// Error reading from a script file. Wrapped value is the path of the script file.
|
2020-03-18 05:04:26 +01:00
|
|
|
#[cfg(not(feature = "no_std"))]
|
2020-03-13 09:17:55 +01:00
|
|
|
ErrorReadingScriptFile(PathBuf, std::io::Error),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Inappropriate member access.
|
2020-03-07 10:32:15 +01:00
|
|
|
ErrorDotExpr(String, Position),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Arithmetic error encountered. Wrapped value is the error message.
|
|
|
|
ErrorArithmetic(String, Position),
|
|
|
|
/// Run-time error encountered. Wrapped value is the error message.
|
|
|
|
ErrorRuntime(String, Position),
|
2020-03-17 10:33:37 +01:00
|
|
|
/// Breaking out of loops - not an error if within a loop.
|
|
|
|
ErrorLoopBreak(Position),
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Not an error: Value returned from a script via the `return` keyword.
|
|
|
|
/// Wrapped value is the result value.
|
|
|
|
Return(Dynamic, Position),
|
|
|
|
}
|
|
|
|
|
2020-03-14 16:39:45 +01:00
|
|
|
impl EvalAltResult {
|
|
|
|
pub(crate) fn desc(&self) -> &str {
|
2020-03-04 15:00:01 +01:00
|
|
|
match self {
|
2020-03-14 16:39:45 +01:00
|
|
|
Self::ErrorParsing(p) => p.desc(),
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorFunctionNotFound(_, _) => "Function not found",
|
|
|
|
Self::ErrorFunctionArgsMismatch(_, _, _, _) => {
|
|
|
|
"Function call with wrong number of arguments"
|
|
|
|
}
|
|
|
|
Self::ErrorBooleanArgMismatch(_, _) => "Boolean operator expects boolean operands",
|
2020-03-07 15:50:46 +01:00
|
|
|
Self::ErrorCharMismatch(_) => "Character expected",
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorIndexExpr(_) => "Indexing into an array or string expects an integer index",
|
2020-03-07 10:32:15 +01:00
|
|
|
Self::ErrorIndexingType(_, _) => {
|
|
|
|
"Indexing can only be performed on an array or a string"
|
|
|
|
}
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorArrayBounds(_, index, _) if *index < 0 => {
|
|
|
|
"Array access expects non-negative index"
|
|
|
|
}
|
2020-03-11 04:03:18 +01:00
|
|
|
Self::ErrorArrayBounds(0, _, _) => "Access of empty array",
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorArrayBounds(_, _, _) => "Array index out of bounds",
|
|
|
|
Self::ErrorStringBounds(_, index, _) if *index < 0 => {
|
|
|
|
"Indexing a string expects a non-negative index"
|
|
|
|
}
|
2020-03-11 04:03:18 +01:00
|
|
|
Self::ErrorStringBounds(0, _, _) => "Indexing of empty string",
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorStringBounds(_, _, _) => "String index out of bounds",
|
2020-03-20 12:50:58 +01:00
|
|
|
Self::ErrorLogicGuard(_) => "Boolean expression expected",
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorFor(_) => "For loop expects array or range",
|
|
|
|
Self::ErrorVariableNotFound(_, _) => "Variable not found",
|
|
|
|
Self::ErrorAssignmentToUnknownLHS(_) => {
|
|
|
|
"Assignment to an unsupported left-hand side expression"
|
|
|
|
}
|
2020-03-13 11:12:41 +01:00
|
|
|
Self::ErrorAssignmentToConstant(_, _) => "Assignment to a constant variable",
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorMismatchOutputType(_, _) => "Output type is incorrect",
|
2020-03-18 05:04:26 +01:00
|
|
|
#[cfg(not(feature = "no_std"))]
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorReadingScriptFile(_, _) => "Cannot read from script file",
|
2020-03-07 10:32:15 +01:00
|
|
|
Self::ErrorDotExpr(_, _) => "Malformed dot expression",
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorArithmetic(_, _) => "Arithmetic error",
|
|
|
|
Self::ErrorRuntime(_, _) => "Runtime error",
|
2020-03-17 10:33:37 +01:00
|
|
|
Self::ErrorLoopBreak(_) => "Break statement not inside a loop",
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::Return(_, _) => "[Not Error] Function returns value",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 16:39:45 +01:00
|
|
|
impl Error for EvalAltResult {}
|
|
|
|
|
2020-03-08 12:54:02 +01:00
|
|
|
impl fmt::Display for EvalAltResult {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-03-14 16:39:45 +01:00
|
|
|
let desc = self.desc();
|
2020-03-04 15:00:01 +01:00
|
|
|
|
|
|
|
match self {
|
|
|
|
Self::ErrorFunctionNotFound(s, pos) => write!(f, "{}: '{}' ({})", desc, s, pos),
|
|
|
|
Self::ErrorVariableNotFound(s, pos) => write!(f, "{}: '{}' ({})", desc, s, pos),
|
2020-03-07 10:32:15 +01:00
|
|
|
Self::ErrorIndexingType(_, pos) => write!(f, "{} ({})", desc, pos),
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorIndexExpr(pos) => write!(f, "{} ({})", desc, pos),
|
2020-03-20 12:50:58 +01:00
|
|
|
Self::ErrorLogicGuard(pos) => write!(f, "{} ({})", desc, pos),
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorFor(pos) => write!(f, "{} ({})", desc, pos),
|
|
|
|
Self::ErrorAssignmentToUnknownLHS(pos) => write!(f, "{} ({})", desc, pos),
|
2020-03-13 11:12:41 +01:00
|
|
|
Self::ErrorAssignmentToConstant(s, pos) => write!(f, "{}: '{}' ({})", desc, s, pos),
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorMismatchOutputType(s, pos) => write!(f, "{}: {} ({})", desc, s, pos),
|
2020-03-07 10:32:15 +01:00
|
|
|
Self::ErrorDotExpr(s, pos) if !s.is_empty() => write!(f, "{} {} ({})", desc, s, pos),
|
|
|
|
Self::ErrorDotExpr(_, pos) => write!(f, "{} ({})", desc, pos),
|
2020-03-08 15:47:13 +01:00
|
|
|
Self::ErrorArithmetic(s, pos) => write!(f, "{} ({})", s, pos),
|
|
|
|
Self::ErrorRuntime(s, pos) => {
|
|
|
|
write!(f, "{} ({})", if s.is_empty() { desc } else { s }, pos)
|
|
|
|
}
|
2020-03-17 10:33:37 +01:00
|
|
|
Self::ErrorLoopBreak(pos) => write!(f, "{} ({})", desc, pos),
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::Return(_, pos) => write!(f, "{} ({})", desc, pos),
|
2020-03-18 05:04:26 +01:00
|
|
|
#[cfg(not(feature = "no_std"))]
|
2020-03-13 11:07:51 +01:00
|
|
|
Self::ErrorReadingScriptFile(path, err) => {
|
|
|
|
write!(f, "{} '{}': {}", desc, path.display(), err)
|
2020-03-04 15:00:01 +01:00
|
|
|
}
|
|
|
|
Self::ErrorParsing(p) => write!(f, "Syntax error: {}", p),
|
2020-03-11 04:03:18 +01:00
|
|
|
Self::ErrorFunctionArgsMismatch(fun, 0, n, pos) => write!(
|
|
|
|
f,
|
|
|
|
"Function '{}' expects no argument but {} found ({})",
|
|
|
|
fun, n, pos
|
|
|
|
),
|
|
|
|
Self::ErrorFunctionArgsMismatch(fun, 1, n, pos) => write!(
|
|
|
|
f,
|
|
|
|
"Function '{}' expects one argument but {} found ({})",
|
|
|
|
fun, n, pos
|
|
|
|
),
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorFunctionArgsMismatch(fun, need, n, pos) => write!(
|
|
|
|
f,
|
|
|
|
"Function '{}' expects {} argument(s) but {} found ({})",
|
|
|
|
fun, need, n, pos
|
|
|
|
),
|
|
|
|
Self::ErrorBooleanArgMismatch(op, pos) => {
|
|
|
|
write!(f, "{} operator expects boolean operands ({})", op, pos)
|
|
|
|
}
|
2020-03-07 15:50:46 +01:00
|
|
|
Self::ErrorCharMismatch(pos) => {
|
|
|
|
write!(f, "string indexing expects a character value ({})", pos)
|
|
|
|
}
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorArrayBounds(_, index, pos) if *index < 0 => {
|
|
|
|
write!(f, "{}: {} < 0 ({})", desc, index, pos)
|
|
|
|
}
|
2020-03-11 04:03:18 +01:00
|
|
|
Self::ErrorArrayBounds(0, _, pos) => write!(f, "{} ({})", desc, pos),
|
|
|
|
Self::ErrorArrayBounds(1, index, pos) => write!(
|
|
|
|
f,
|
|
|
|
"Array index {} is out of bounds: only one element in the array ({})",
|
|
|
|
index, pos
|
|
|
|
),
|
2020-03-05 13:28:03 +01:00
|
|
|
Self::ErrorArrayBounds(max, index, pos) => write!(
|
|
|
|
f,
|
2020-03-11 04:03:18 +01:00
|
|
|
"Array index {} is out of bounds: only {} elements in the array ({})",
|
|
|
|
index, max, pos
|
2020-03-05 13:28:03 +01:00
|
|
|
),
|
2020-03-04 15:00:01 +01:00
|
|
|
Self::ErrorStringBounds(_, index, pos) if *index < 0 => {
|
|
|
|
write!(f, "{}: {} < 0 ({})", desc, index, pos)
|
|
|
|
}
|
2020-03-11 04:03:18 +01:00
|
|
|
Self::ErrorStringBounds(0, _, pos) => write!(f, "{} ({})", desc, pos),
|
|
|
|
Self::ErrorStringBounds(1, index, pos) => write!(
|
|
|
|
f,
|
|
|
|
"String index {} is out of bounds: only one character in the string ({})",
|
|
|
|
index, pos
|
|
|
|
),
|
2020-03-05 13:28:03 +01:00
|
|
|
Self::ErrorStringBounds(max, index, pos) => write!(
|
|
|
|
f,
|
2020-03-11 04:03:18 +01:00
|
|
|
"String index {} is out of bounds: only {} characters in the string ({})",
|
|
|
|
index, max, pos
|
2020-03-05 13:28:03 +01:00
|
|
|
),
|
2020-03-04 15:00:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseError> for EvalAltResult {
|
|
|
|
fn from(err: ParseError) -> Self {
|
|
|
|
Self::ErrorParsing(err)
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 15:47:13 +01:00
|
|
|
|
2020-03-10 10:10:33 +01:00
|
|
|
impl<T: AsRef<str>> From<T> for EvalAltResult {
|
|
|
|
fn from(err: T) -> Self {
|
|
|
|
Self::ErrorRuntime(err.as_ref().to_string(), Position::none())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 15:47:13 +01:00
|
|
|
impl EvalAltResult {
|
2020-03-10 02:30:12 +01:00
|
|
|
pub fn position(&self) -> Position {
|
|
|
|
match self {
|
2020-03-18 05:04:26 +01:00
|
|
|
#[cfg(not(feature = "no_std"))]
|
2020-03-17 19:26:11 +01:00
|
|
|
Self::ErrorReadingScriptFile(_, _) => Position::none(),
|
2020-03-10 02:30:12 +01:00
|
|
|
|
|
|
|
Self::ErrorParsing(err) => err.position(),
|
|
|
|
|
|
|
|
Self::ErrorFunctionNotFound(_, pos)
|
|
|
|
| Self::ErrorFunctionArgsMismatch(_, _, _, pos)
|
|
|
|
| Self::ErrorBooleanArgMismatch(_, pos)
|
|
|
|
| Self::ErrorCharMismatch(pos)
|
|
|
|
| Self::ErrorArrayBounds(_, _, pos)
|
|
|
|
| Self::ErrorStringBounds(_, _, pos)
|
|
|
|
| Self::ErrorIndexingType(_, pos)
|
|
|
|
| Self::ErrorIndexExpr(pos)
|
2020-03-20 12:50:58 +01:00
|
|
|
| Self::ErrorLogicGuard(pos)
|
2020-03-10 02:30:12 +01:00
|
|
|
| Self::ErrorFor(pos)
|
|
|
|
| Self::ErrorVariableNotFound(_, pos)
|
|
|
|
| Self::ErrorAssignmentToUnknownLHS(pos)
|
2020-03-13 11:12:41 +01:00
|
|
|
| Self::ErrorAssignmentToConstant(_, pos)
|
2020-03-10 02:30:12 +01:00
|
|
|
| Self::ErrorMismatchOutputType(_, pos)
|
|
|
|
| Self::ErrorDotExpr(_, pos)
|
|
|
|
| Self::ErrorArithmetic(_, pos)
|
|
|
|
| Self::ErrorRuntime(_, pos)
|
2020-03-17 10:33:37 +01:00
|
|
|
| Self::ErrorLoopBreak(pos)
|
2020-03-10 02:30:12 +01:00
|
|
|
| Self::Return(_, pos) => *pos,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 15:47:13 +01:00
|
|
|
pub(crate) fn set_position(&mut self, new_position: Position) {
|
|
|
|
match self {
|
2020-03-18 05:04:26 +01:00
|
|
|
#[cfg(not(feature = "no_std"))]
|
2020-03-17 19:26:11 +01:00
|
|
|
Self::ErrorReadingScriptFile(_, _) => (),
|
2020-03-08 15:47:13 +01:00
|
|
|
|
2020-03-10 02:30:12 +01:00
|
|
|
Self::ErrorParsing(ParseError(_, ref mut pos))
|
|
|
|
| Self::ErrorFunctionNotFound(_, ref mut pos)
|
|
|
|
| Self::ErrorFunctionArgsMismatch(_, _, _, ref mut pos)
|
|
|
|
| Self::ErrorBooleanArgMismatch(_, ref mut pos)
|
|
|
|
| Self::ErrorCharMismatch(ref mut pos)
|
|
|
|
| Self::ErrorArrayBounds(_, _, ref mut pos)
|
|
|
|
| Self::ErrorStringBounds(_, _, ref mut pos)
|
|
|
|
| Self::ErrorIndexingType(_, ref mut pos)
|
|
|
|
| Self::ErrorIndexExpr(ref mut pos)
|
2020-03-20 12:50:58 +01:00
|
|
|
| Self::ErrorLogicGuard(ref mut pos)
|
2020-03-10 02:30:12 +01:00
|
|
|
| Self::ErrorFor(ref mut pos)
|
|
|
|
| Self::ErrorVariableNotFound(_, ref mut pos)
|
|
|
|
| Self::ErrorAssignmentToUnknownLHS(ref mut pos)
|
2020-03-13 11:12:41 +01:00
|
|
|
| Self::ErrorAssignmentToConstant(_, ref mut pos)
|
2020-03-10 02:30:12 +01:00
|
|
|
| Self::ErrorMismatchOutputType(_, ref mut pos)
|
|
|
|
| Self::ErrorDotExpr(_, ref mut pos)
|
|
|
|
| Self::ErrorArithmetic(_, ref mut pos)
|
|
|
|
| Self::ErrorRuntime(_, ref mut pos)
|
2020-03-17 10:33:37 +01:00
|
|
|
| Self::ErrorLoopBreak(ref mut pos)
|
2020-03-10 02:30:12 +01:00
|
|
|
| Self::Return(_, ref mut pos) => *pos = new_position,
|
2020-03-08 15:47:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|