Improve error messages to lists.

This commit is contained in:
Stephen Chung
2020-03-16 23:51:32 +08:00
parent d4311bddb0
commit abe5365bfd
5 changed files with 433 additions and 453 deletions

View File

@@ -1031,30 +1031,29 @@ impl Engine<'_> {
// Block scope
Stmt::Block(block, _) => {
let prev_len = scope.len();
let mut last_result: Result<Dynamic, EvalAltResult> = Ok(().into_dynamic());
let mut result: Result<Dynamic, EvalAltResult> = Ok(().into_dynamic());
for block_stmt in block.iter() {
last_result = self.eval_stmt(scope, block_stmt);
for stmt in block.iter() {
result = self.eval_stmt(scope, stmt);
if let Err(x) = last_result {
last_result = Err(x);
if result.is_err() {
break;
}
}
scope.rewind(prev_len);
last_result
result
}
// If-else statement
Stmt::IfElse(guard, body, else_body) => self
Stmt::IfElse(guard, if_body, else_body) => self
.eval_expr(scope, guard)?
.downcast::<bool>()
.map_err(|_| EvalAltResult::ErrorIfGuard(guard.position()))
.and_then(|guard_val| {
if *guard_val {
self.eval_stmt(scope, body)
self.eval_stmt(scope, if_body)
} else if let Some(stmt) = else_body {
self.eval_stmt(scope, stmt.as_ref())
} else {

View File

@@ -58,6 +58,8 @@ pub enum ParseErrorType {
/// An open `[` is missing the corresponding closing `]`.
#[cfg(not(feature = "no_index"))]
MissingRightBracket(String),
/// A list of expressions is missing the separating ','.
MissingComma(String),
/// An expression in function call arguments `()` has syntax error.
MalformedCallExpr(String),
/// An expression in indexing brackets `[]` has syntax error.
@@ -116,6 +118,7 @@ impl ParseError {
ParseErrorType::MissingRightBrace(_) => "Expecting '}'",
#[cfg(not(feature = "no_index"))]
ParseErrorType::MissingRightBracket(_) => "Expecting ']'",
ParseErrorType::MissingComma(_) => "Expecting ','",
ParseErrorType::MalformedCallExpr(_) => "Invalid expression in function call arguments",
#[cfg(not(feature = "no_index"))]
ParseErrorType::MalformedIndexExpr(_) => "Invalid index in indexing expression",
@@ -165,6 +168,8 @@ impl fmt::Display for ParseError {
#[cfg(not(feature = "no_index"))]
ParseErrorType::MissingRightBracket(ref s) => write!(f, "{} for {}", self.desc(), s)?,
ParseErrorType::MissingComma(ref s) => write!(f, "{} for {}", self.desc(), s)?,
ParseErrorType::AssignmentToConstant(ref s) if s.is_empty() => {
write!(f, "{}", self.desc())?
}

File diff suppressed because it is too large Load Diff