diff --git a/src/error.rs b/src/error.rs index ea3ad170..61edc035 100644 --- a/src/error.rs +++ b/src/error.rs @@ -93,55 +93,13 @@ pub enum EvalAltResult { Return(Dynamic, Position), } -impl EvalAltResult { - #[must_use] - pub(crate) fn desc(&self) -> &str { - match self { - #[allow(deprecated)] - Self::ErrorSystem(_, s) => s.description(), - Self::ErrorParsing(p, _) => p.desc(), - Self::ErrorInFunctionCall(_, _, _, _) => "Error in called function", - Self::ErrorInModule(_, _, _) => "Error in module", - Self::ErrorFunctionNotFound(_, _) => "Function not found", - Self::ErrorUnboundThis(_) => "'this' is not bound", - Self::ErrorMismatchDataType(_, _, _) => "Data type is incorrect", - Self::ErrorIndexingType(_, _) => "No indexer of the appropriate types defined", - Self::ErrorArrayBounds(0, _, _) => "Empty array has nothing to access", - Self::ErrorArrayBounds(_, _, _) => "Array index out of bounds", - Self::ErrorStringBounds(0, _, _) => "Empty string has nothing to index", - Self::ErrorStringBounds(_, _, _) => "String index out of bounds", - Self::ErrorBitFieldBounds(_, _, _) => "Bit-field index out of bounds", - Self::ErrorFor(_) => "For loop expects a type with an iterator defined", - Self::ErrorVariableNotFound(_, _) => "Variable not found", - Self::ErrorModuleNotFound(_, _) => "Module not found", - Self::ErrorDataRace(_, _) => "Data race detected when accessing variable", - Self::ErrorAssignmentToConstant(_, _) => "Cannot modify a constant", - Self::ErrorMismatchOutputType(_, _, _) => "Output type is incorrect", - Self::ErrorDotExpr(_, _) => "Malformed dot expression", - Self::ErrorArithmetic(_, _) => "Arithmetic error", - Self::ErrorTooManyOperations(_) => "Too many operations", - Self::ErrorTooManyModules(_) => "Too many modules imported", - Self::ErrorStackOverflow(_) => "Stack overflow", - Self::ErrorDataTooLarge(_, _) => "Data size exceeds maximum limit", - Self::ErrorTerminated(_, _) => "Script terminated.", - Self::ErrorRuntime(_, _) => "Runtime error", - Self::LoopBreak(true, _) => "Break statement not inside a loop", - Self::LoopBreak(false, _) => "Continue statement not inside a loop", - Self::Return(_, _) => "[Not Error] Function returns value", - } - } -} - impl Error for EvalAltResult {} impl fmt::Display for EvalAltResult { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let desc = self.desc(); - let pos = self.position(); - match self { - Self::ErrorSystem(s, _) if s.is_empty() => f.write_str(desc)?, - Self::ErrorSystem(s, _) => write!(f, "{}: {}", s, desc)?, + Self::ErrorSystem(s, err) if s.is_empty() => write!(f, "{}", err)?, + Self::ErrorSystem(s, err) => write!(f, "{}: {}", s, err)?, Self::ErrorParsing(p, _) => write!(f, "Syntax error: {}", p)?, @@ -164,32 +122,35 @@ impl fmt::Display for EvalAltResult { } Self::ErrorInModule(s, err, _) => write!(f, "Error in module '{}': {}", s, err)?, - Self::ErrorFunctionNotFound(s, _) - | Self::ErrorVariableNotFound(s, _) - | Self::ErrorDataRace(s, _) => write!(f, "{}: {}", desc, s)?, - - Self::ErrorModuleNotFound(s, _) => write!(f, "{}: '{}'", desc, s)?, - + Self::ErrorFunctionNotFound(s, _) => write!(f, "Function not found: {}", s)?, + Self::ErrorVariableNotFound(s, _) => write!(f, "Variable not found: {}", s)?, + Self::ErrorModuleNotFound(s, _) => write!(f, "Module not found: '{}'", s)?, + Self::ErrorDataRace(s, _) => { + write!(f, "Data race detected when accessing variable: {}", s)? + } Self::ErrorDotExpr(s, _) if !s.is_empty() => f.write_str(s)?, - Self::ErrorIndexingType(s, _) => write!(f, "Indexer not registered for '{}'", s)?, - - Self::ErrorUnboundThis(_) - | Self::ErrorFor(_) - | Self::ErrorDotExpr(_, _) - | Self::ErrorTooManyOperations(_) - | Self::ErrorTooManyModules(_) - | Self::ErrorStackOverflow(_) - | Self::ErrorTerminated(_, _) => f.write_str(desc)?, + Self::ErrorUnboundThis(_) => f.write_str("'this' is not bound")?, + Self::ErrorFor(_) => f.write_str("For loop expects a type with an iterator defined")?, + Self::ErrorDotExpr(_, _) => f.write_str("Malformed dot expression")?, + Self::ErrorTooManyOperations(_) => f.write_str("Too many operations")?, + Self::ErrorTooManyModules(_) => f.write_str("Too many modules imported")?, + Self::ErrorStackOverflow(_) => f.write_str("Stack overflow")?, + Self::ErrorTerminated(_, _) => f.write_str("Script terminated")?, Self::ErrorRuntime(d, _) if d.is::() => { let s = &*d .read_lock::() .expect("never fails because the type was checked"); - write!(f, "{}: {}", desc, if s.is_empty() { desc } else { s })? + + if s.is_empty() { + f.write_str("Runtime error")? + } else { + write!(f, "Runtime error: {}", s)? + } } - Self::ErrorRuntime(d, _) if d.is::<()>() => f.write_str(desc)?, - Self::ErrorRuntime(d, _) => write!(f, "{}: {}", desc, d)?, + Self::ErrorRuntime(d, _) if d.is::<()>() => f.write_str("Runtime error")?, + Self::ErrorRuntime(d, _) => write!(f, "Runtime error: {}", d)?, Self::ErrorAssignmentToConstant(s, _) => write!(f, "Cannot modify constant {}", s)?, Self::ErrorMismatchOutputType(s, r, _) => { @@ -206,8 +167,10 @@ impl fmt::Display for EvalAltResult { } Self::ErrorArithmetic(s, _) => f.write_str(s)?, - Self::LoopBreak(_, _) => f.write_str(desc)?, - Self::Return(_, _) => f.write_str(desc)?, + Self::LoopBreak(true, _) => f.write_str("Break statement not inside a loop")?, + Self::LoopBreak(false, _) => f.write_str("Continue statement not inside a loop")?, + + Self::Return(_, _) => f.write_str("NOT AN ERROR - Function returns value")?, Self::ErrorArrayBounds(0, index, _) => { write!(f, "Array index {} out of bounds: array is empty", index)? @@ -244,8 +207,8 @@ impl fmt::Display for EvalAltResult { } // Do not write any position if None - if !pos.is_none() { - write!(f, " ({})", pos)?; + if !self.position().is_none() { + write!(f, " ({})", self.position())?; } Ok(()) diff --git a/src/error_parsing.rs b/src/error_parsing.rs index 795b85f3..d228190e 100644 --- a/src/error_parsing.rs +++ b/src/error_parsing.rs @@ -194,43 +194,6 @@ impl ParseErrorType { pub(crate) fn into_err(self, pos: Position) -> ParseError { ParseError(Box::new(self), pos) } - - #[must_use] - pub(crate) fn desc(&self) -> &str { - match self { - Self::UnexpectedEOF => "Script is incomplete", - Self::BadInput(err) => err.desc(), - Self::UnknownOperator(_) => "Unknown operator", - Self::MissingToken(_, _) => "Expecting a certain token that is missing", - Self::MissingSymbol(_) => "Expecting a certain symbol that is missing", - Self::MalformedCallExpr(_) => "Invalid expression in function call arguments", - Self::MalformedIndexExpr(_) => "Invalid index in indexing expression", - Self::MalformedInExpr(_) => "Invalid 'in' expression", - Self::MalformedCapture(_) => "Invalid capturing", - Self::DuplicatedProperty(_) => "Duplicated property in object map literal", - Self::DuplicatedSwitchCase => "Duplicated switch case", - Self::DuplicatedVariable(_) => "Duplicated variable name", - Self::WrongSwitchDefaultCase => "Default switch case is not the last", - Self::WrongSwitchCaseCondition => "Default switch case cannot have condition", - Self::PropertyExpected => "Expecting name of a property", - Self::VariableExpected => "Expecting name of a variable", - Self::Reserved(_) => "Invalid use of reserved keyword", - Self::ExprExpected(_) => "Expecting an expression", - Self::WrongFnDefinition => "Function definitions must be at global level and cannot be inside a block or another function", - Self::FnDuplicatedDefinition(_, _) => "Function already exists", - Self::FnMissingName => "Expecting function name in function declaration", - Self::FnMissingParams(_) => "Expecting parameters in function declaration", - Self::FnDuplicatedParam(_,_) => "Duplicated parameters in function declaration", - Self::FnMissingBody(_) => "Expecting body statement block for function declaration", - Self::WrongDocComment => "Doc-comment must be followed immediately by a function definition", - Self::WrongExport => "Export statement can only appear at global level", - Self::AssignmentToConstant(_) => "Cannot assign to a constant value", - Self::AssignmentToInvalidLHS(_) => "Expression cannot be assigned to", - Self::ExprTooDeep => "Expression exceeds maximum complexity", - Self::LiteralTooLarge(_, _) => "Literal exceeds maximum limit", - Self::LoopBreak => "Break statement should only be used inside a loop" - } - } } impl fmt::Display for ParseErrorType { @@ -238,12 +201,14 @@ impl fmt::Display for ParseErrorType { match self { Self::BadInput(err) => write!(f, "{}", err), - Self::MalformedCallExpr(s) => f.write_str(if s.is_empty() { self.desc() } else { s }), - Self::UnknownOperator(s) => write!(f, "{}: '{}'", self.desc(), s), + Self::UnknownOperator(s) => write!(f, "Unknown operator: '{}'", s), - Self::MalformedIndexExpr(s) | Self::MalformedInExpr(s) | Self::MalformedCapture(s) => { - f.write_str(if s.is_empty() { self.desc() } else { s }) - } + Self::MalformedCallExpr(s) if s.is_empty() => f.write_str("Invalid expression in function call arguments"), + Self::MalformedIndexExpr(s) if s.is_empty() => f.write_str("Invalid index in indexing expression"), + Self::MalformedInExpr(s) if s.is_empty() => f.write_str("Invalid 'in' expression"), + Self::MalformedCapture(s) if s.is_empty() => f.write_str("Invalid capturing"), + + Self::MalformedCallExpr(s) | Self::MalformedIndexExpr(s) | Self::MalformedInExpr(s) | Self::MalformedCapture(s) => f.write_str(s), Self::FnDuplicatedDefinition(s, n) => { write!(f, "Function '{}' with ", s)?; @@ -253,43 +218,39 @@ impl fmt::Display for ParseErrorType { _ => write!(f, "{} parameters already exists", n), } } - Self::DuplicatedProperty(s) => { - write!(f, "Duplicated property '{}' for object map literal", s) - } - Self::DuplicatedSwitchCase => f.write_str(self.desc()), + + Self::FnMissingBody(s) if s.is_empty() => f.write_str("Expecting body statement block for anonymous function"), + Self::FnMissingBody(s) => write!(f, "Expecting body statement block for function '{}'", s), + + Self::FnMissingParams(s) => write!(f, "Expecting parameters for function '{}'", s), + Self::FnDuplicatedParam(s, arg) => write!(f, "Duplicated parameter '{}' for function '{}'", arg, s), + Self::DuplicatedProperty(s) => write!(f, "Duplicated property '{}' for object map literal", s), + Self::DuplicatedSwitchCase => f.write_str("Duplicated switch case"), Self::DuplicatedVariable(s) => write!(f, "Duplicated variable name '{}'", s), Self::ExprExpected(s) => write!(f, "Expecting {} expression", s), - - Self::FnMissingParams(s) => write!(f, "Expecting parameters for function '{}'", s), - - Self::FnMissingBody(s) if s.is_empty() => { - f.write_str("Expecting body statement block for anonymous function") - } - Self::FnMissingBody(s) => { - write!(f, "Expecting body statement block for function '{}'", s) - } - - Self::FnDuplicatedParam(s, arg) => { - write!(f, "Duplicated parameter '{}' for function '{}'", arg, s) - } - Self::MissingToken(token, s) => write!(f, "Expecting '{}' {}", token, s), Self::MissingSymbol(s) => f.write_str(s), - Self::AssignmentToConstant(s) if s.is_empty() => f.write_str(self.desc()), + Self::AssignmentToConstant(s) if s.is_empty() => f.write_str("Cannot assign to a constant value"), Self::AssignmentToConstant(s) => write!(f, "Cannot assign to constant '{}'", s), - Self::AssignmentToInvalidLHS(s) if s.is_empty() => f.write_str(self.desc()), + Self::AssignmentToInvalidLHS(s) if s.is_empty() => f.write_str("Expression cannot be assigned to"), Self::AssignmentToInvalidLHS(s) => f.write_str(s), - Self::LiteralTooLarge(typ, max) => { - write!(f, "{} exceeds the maximum limit ({})", typ, max) - } - + Self::LiteralTooLarge(typ, max) => write!(f, "{} exceeds the maximum limit ({})", typ, max), Self::Reserved(s) => write!(f, "'{}' is a reserved keyword", s), - - _ => f.write_str(self.desc()), + Self::UnexpectedEOF => f.write_str("Script is incomplete"), + Self::WrongSwitchDefaultCase => f.write_str("Default switch case is not the last"), + Self::WrongSwitchCaseCondition => f.write_str("Default switch case cannot have condition"), + Self::PropertyExpected => f.write_str("Expecting name of a property"), + Self::VariableExpected => f.write_str("Expecting name of a variable"), + Self::WrongFnDefinition => f.write_str("Function definitions must be at global level and cannot be inside a block or another function"), + Self::FnMissingName => f.write_str("Expecting function name in function declaration"), + Self::WrongDocComment => f.write_str("Doc-comment must be followed immediately by a function definition"), + Self::WrongExport => f.write_str("Export statement can only appear at global level"), + Self::ExprTooDeep => f.write_str("Expression exceeds maximum complexity"), + Self::LoopBreak => f.write_str("Break statement should only be used inside a loop"), } } }