From d8ec7ed14183b980d8107ef44f883680504d63b5 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 6 Mar 2020 10:50:20 +0800 Subject: [PATCH] Rephrase error messages. --- src/engine.rs | 30 ++++++++++++++++-------------- src/error.rs | 2 +- src/result.rs | 6 +++--- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 2b9abf1c..48b57199 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -202,7 +202,7 @@ impl Engine<'_> { Expr::Index(lhs, idx_expr) => { let idx = self.eval_index_value(scope, idx_expr)?; - let (lhs_value, pos) = match lhs.as_ref() { + let (lhs_value, _) = match lhs.as_ref() { Expr::Identifier(id, pos) => { let get_fn_name = format!("get${}", id); ( @@ -213,7 +213,7 @@ impl Engine<'_> { expr => return Err(EvalAltResult::ErrorDotExpr(expr.position())), }; - Self::get_indexed_value(lhs_value, idx, pos).map(|(v, _)| v) + Self::get_indexed_value(lhs_value, idx, idx_expr.position()).map(|(v, _)| v) } // xxx.lhs.rhs @@ -229,7 +229,7 @@ impl Engine<'_> { Expr::Index(lhs, idx_expr) => { let idx = self.eval_index_value(scope, idx_expr)?; - let (lhs_value, pos) = match lhs.as_ref() { + let (lhs_value, _) = match lhs.as_ref() { Expr::Identifier(id, pos) => { let get_fn_name = format!("get${}", id); ( @@ -240,9 +240,9 @@ impl Engine<'_> { expr => return Err(EvalAltResult::ErrorDotExpr(expr.position())), }; - Self::get_indexed_value(lhs_value, idx, pos).and_then(|(mut value, _)| { - self.get_dot_val_helper(scope, value.as_mut(), rhs) - }) + Self::get_indexed_value(lhs_value, idx, idx_expr.position()).and_then( + |(mut value, _)| self.get_dot_val_helper(scope, value.as_mut(), rhs), + ) } // Syntax error _ => Err(EvalAltResult::ErrorDotExpr(lhs.position())), @@ -285,7 +285,7 @@ impl Engine<'_> { pos: Position, ) -> Result<(Dynamic, VariableType), EvalAltResult> { if val.is::() { - let arr = val.downcast::().expect("Array expected"); + let arr = val.downcast::().expect("array expected"); if idx >= 0 { arr.get(idx as usize) @@ -296,7 +296,7 @@ impl Engine<'_> { Err(EvalAltResult::ErrorArrayBounds(arr.len(), idx, pos)) } } else if val.is::() { - let s = val.downcast::().expect("String expected"); + let s = val.downcast::().expect("string expected"); if idx >= 0 { s.chars() @@ -311,7 +311,7 @@ impl Engine<'_> { )) } } else { - Err(EvalAltResult::ErrorIndexing(pos)) + Err(EvalAltResult::ErrorIndexingType(pos)) } } @@ -325,17 +325,19 @@ impl Engine<'_> { let idx = self.eval_index_value(scope, idx_expr)?; match lhs { + // id[idx_expr] Expr::Identifier(id, _) => Self::search_scope( scope, &id, - |val| Self::get_indexed_value(val, idx, lhs.position()), + |val| Self::get_indexed_value(val, idx, idx_expr.position()), lhs.position(), ) .map(|(src_idx, (val, source_type))| { (source_type, Some((id.clone(), src_idx)), idx as usize, val) }), - expr => Self::get_indexed_value(self.eval_expr(scope, expr)?, idx, lhs.position()) + // (expr)[idx_expr] + expr => Self::get_indexed_value(self.eval_expr(scope, expr)?, idx, idx_expr.position()) .map(|(val, _)| (VariableType::Expression, None, idx as usize, val)), } } @@ -377,7 +379,7 @@ impl Engine<'_> { // Value must be a character let ch = *val .downcast::() - .expect("value to update an index position in a string must be a char"); + .expect("char value expected to update an index position in a string"); Some(Self::str_replace_char(s, idx as usize, ch).into_dynamic()) } @@ -422,7 +424,7 @@ impl Engine<'_> { idx, target, ) - .expect("source_type must be either Array or String"); + .expect("array or string source type expected for indexing"); } value @@ -516,7 +518,7 @@ impl Engine<'_> { idx, target, ) - .expect("source_type must be either Array or String"); + .expect("array or string source_type expected for indexing"); } value diff --git a/src/error.rs b/src/error.rs index d6410031..3096040e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -128,7 +128,7 @@ impl fmt::Display for ParseError { ParseErrorType::BadInput(ref s) => write!(f, "{}", s)?, ParseErrorType::UnknownOperator(ref s) => write!(f, "{}: '{}'", self.description(), s)?, ParseErrorType::FnMissingParams(ref s) => { - write!(f, "Missing parameters for function '{}'", s)? + write!(f, "Expecting parameters for function '{}'", s)? } ParseErrorType::MissingRightParen(ref s) | ParseErrorType::MissingRightBrace(ref s) diff --git a/src/result.rs b/src/result.rs index ae05ee4b..1d869c8b 100644 --- a/src/result.rs +++ b/src/result.rs @@ -26,7 +26,7 @@ pub enum EvalAltResult { /// Wrapped values are the current number of characters in the string and the index number. ErrorStringBounds(usize, i64, Position), /// Trying to index into a type that is not an array and not a string. - ErrorIndexing(Position), + ErrorIndexingType(Position), /// Trying to index into an array or string with an index that is not `i64`. ErrorIndexExpr(Position), /// The guard expression in an `if` statement does not return a boolean value. @@ -65,7 +65,7 @@ impl Error for EvalAltResult { } Self::ErrorBooleanArgMismatch(_, _) => "Boolean operator expects boolean operands", Self::ErrorIndexExpr(_) => "Indexing into an array or string expects an integer index", - Self::ErrorIndexing(_) => "Indexing can only be performed on an array or a string", + Self::ErrorIndexingType(_) => "Indexing can only be performed on an array or a string", Self::ErrorArrayBounds(_, index, _) if *index < 0 => { "Array access expects non-negative index" } @@ -104,7 +104,7 @@ impl std::fmt::Display for EvalAltResult { match self { Self::ErrorFunctionNotFound(s, pos) => write!(f, "{}: '{}' ({})", desc, s, pos), Self::ErrorVariableNotFound(s, pos) => write!(f, "{}: '{}' ({})", desc, s, pos), - Self::ErrorIndexing(pos) => write!(f, "{} ({})", desc, pos), + Self::ErrorIndexingType(pos) => write!(f, "{} ({})", desc, pos), Self::ErrorIndexExpr(pos) => write!(f, "{} ({})", desc, pos), Self::ErrorIfGuard(pos) => write!(f, "{} ({})", desc, pos), Self::ErrorFor(pos) => write!(f, "{} ({})", desc, pos),