Add ParseError:err_type and ParseError::position.

This commit is contained in:
Stephen Chung 2022-07-29 09:42:30 +08:00
parent 6fab9f5b63
commit ea38185cac
19 changed files with 147 additions and 104 deletions

View File

@ -47,6 +47,7 @@ Enhancements
* `Iterator<Item=T>` type for functions metadata is simplified to `Iterator<T>`.
* `Scope::remove` is added to remove a variable from a `Scope`, returning its value.
* The code base is cleaner by running it through Clippy.
* `ParseError::err_type` and `ParseError::position` are added for convenience.
Version 1.8.0

View File

@ -323,21 +323,21 @@ fn export_by_prefix_test() -> Result<(), Box<EvalAltResult>> {
assert_eq!(&output_array[3].as_int().unwrap(), &42);
assert!(matches!(*engine.eval::<FLOAT>(
r#"
"
let ex = 41.0;
let fx = Math::Advanced::foo_add_float2(ex, 1.0);
fx
"#).unwrap_err(),
").unwrap_err(),
EvalAltResult::ErrorFunctionNotFound(s, p)
if s == "Math::Advanced::foo_add_float2 (f64, f64)"
&& p == rhai::Position::new(3, 34)));
assert!(matches!(*engine.eval::<FLOAT>(
r#"
"
let ex = 41.0;
let fx = Math::Advanced::bar_m(ex, 1.0);
fx
"#).unwrap_err(),
").unwrap_err(),
EvalAltResult::ErrorFunctionNotFound(s, p)
if s == "Math::Advanced::bar_m (f64, f64)"
&& p == rhai::Position::new(3, 34)));
@ -405,11 +405,11 @@ fn export_all_test() -> Result<(), Box<EvalAltResult>> {
assert_eq!(&output_array[3].as_int().unwrap(), &42);
assert!(matches!(*engine.eval::<INT>(
r#"
"
let ex = 41;
let fx = Math::Advanced::foo_p(ex, 1);
fx
"#).unwrap_err(),
").unwrap_err(),
EvalAltResult::ErrorFunctionNotFound(s, p)
if s == "Math::Advanced::foo_p (i64, i64)"
&& p == rhai::Position::new(3, 34)));

View File

@ -143,41 +143,41 @@ fn export_nested_by_prefix_test() -> Result<(), Box<EvalAltResult>> {
assert_eq!(&output_array[3].as_int().unwrap(), &42);
assert!(matches!(*engine.eval::<FLOAT>(
r#"
"
let ex = 41.0;
let fx = Math::Advanced::foo_third_adders::add_float(ex, 1.0);
fx
"#).unwrap_err(),
").unwrap_err(),
EvalAltResult::ErrorFunctionNotFound(s, p)
if s == "Math::Advanced::foo_third_adders::add_float (f64, f64)"
&& p == rhai::Position::new(3, 52)));
assert!(matches!(*engine.eval::<FLOAT>(
r#"
"
let ex = 41;
let fx = Math::Advanced::foo_third_adders::add_int(ex, 1);
fx
"#).unwrap_err(),
").unwrap_err(),
EvalAltResult::ErrorFunctionNotFound(s, p)
if s == "Math::Advanced::foo_third_adders::add_int (i64, i64)"
&& p == rhai::Position::new(3, 52)));
assert!(matches!(*engine.eval::<FLOAT>(
r#"
"
let ex = 41;
let fx = Math::Advanced::bar_fourth_adders::add_int(ex, 1);
fx
"#).unwrap_err(),
").unwrap_err(),
EvalAltResult::ErrorFunctionNotFound(s, p)
if s == "Math::Advanced::bar_fourth_adders::add_int (i64, i64)"
&& p == rhai::Position::new(3, 53)));
assert!(matches!(*engine.eval::<FLOAT>(
r#"
"
let ex = 41.0;
let fx = Math::Advanced::bar_fourth_adders::add_float(ex, 1.0);
fx
"#).unwrap_err(),
").unwrap_err(),
EvalAltResult::ErrorFunctionNotFound(s, p)
if s == "Math::Advanced::bar_fourth_adders::add_float (f64, f64)"
&& p == rhai::Position::new(3, 53)));

View File

@ -299,6 +299,21 @@ impl fmt::Display for ParseError {
}
}
impl ParseError {
/// Get the [type][ParseErrorType] of this parse error.
#[inline(always)]
#[must_use]
pub fn err_type(&self) -> &ParseErrorType {
&self.0
}
/// Get the [position][Position] of this parse error.
#[inline(always)]
#[must_use]
pub fn position(&self) -> Position {
self.1
}
}
impl From<ParseErrorType> for RhaiError {
#[inline(always)]
fn from(err: ParseErrorType) -> Self {

View File

@ -179,40 +179,43 @@ fn test_array_index_types() -> Result<(), Box<EvalAltResult>> {
engine.compile("[1, 2, 3][0]['x']")?;
assert!(matches!(
*engine
engine
.compile("[1, 2, 3]['x']")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::MalformedIndexExpr(..)
));
#[cfg(not(feature = "no_float"))]
assert!(matches!(
*engine
engine
.compile("[1, 2, 3][123.456]")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::MalformedIndexExpr(..)
));
assert!(matches!(
*engine.compile("[1, 2, 3][()]").expect_err("should error").0,
engine
.compile("[1, 2, 3][()]")
.expect_err("should error")
.err_type(),
ParseErrorType::MalformedIndexExpr(..)
));
assert!(matches!(
*engine
engine
.compile(r#"[1, 2, 3]["hello"]"#)
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::MalformedIndexExpr(..)
));
assert!(matches!(
*engine
engine
.compile("[1, 2, 3][true && false]")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::MalformedIndexExpr(..)
));

View File

@ -21,40 +21,55 @@ fn test_assignments_bad_lhs() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
*engine.compile("(x+y) = 42;").expect_err("should error").0,
*engine
.compile("(x+y) = 42;")
.expect_err("should error")
.err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string())
);
assert_eq!(
*engine.compile("foo(x) = 42;").expect_err("should error").0,
*engine
.compile("foo(x) = 42;")
.expect_err("should error")
.err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string())
);
assert_eq!(
*engine.compile("true = 42;").expect_err("should error").0,
*engine
.compile("true = 42;")
.expect_err("should error")
.err_type(),
ParseErrorType::AssignmentToConstant("".to_string())
);
assert_eq!(
*engine.compile("123 = 42;").expect_err("should error").0,
*engine
.compile("123 = 42;")
.expect_err("should error")
.err_type(),
ParseErrorType::AssignmentToConstant("".to_string())
);
#[cfg(not(feature = "no_object"))]
{
assert_eq!(
*engine.compile("x.foo() = 42;").expect_err("should error").0,
*engine
.compile("x.foo() = 42;")
.expect_err("should error")
.err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string())
);
assert_eq!(
*engine
.compile("x.foo().x.y = 42;")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string())
);
assert_eq!(
*engine
.compile("x.y.z.foo() = 42;")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string())
);
#[cfg(not(feature = "no_index"))]
@ -62,7 +77,7 @@ fn test_assignments_bad_lhs() -> Result<(), Box<EvalAltResult>> {
*engine
.compile("x.foo()[0] = 42;")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string())
);
#[cfg(not(feature = "no_index"))]
@ -70,7 +85,7 @@ fn test_assignments_bad_lhs() -> Result<(), Box<EvalAltResult>> {
*engine
.compile("x[y].z.foo() = 42;")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string())
);
}

View File

@ -13,11 +13,11 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
// Disable 'while' and make sure it still works with custom syntax
engine.disable_symbol("while");
assert!(matches!(
*engine.compile("while false {}").expect_err("should error").0,
engine.compile("while false {}").expect_err("should error").err_type(),
ParseErrorType::Reserved(err) if err == "while"
));
assert!(matches!(
*engine.compile("let while = 0").expect_err("should error").0,
engine.compile("let while = 0").expect_err("should error").err_type(),
ParseErrorType::Reserved(err) if err == "while"
));
@ -157,7 +157,7 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
*engine
.register_custom_syntax(&["!"], false, |_, _| Ok(Dynamic::UNIT))
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::BadInput(LexError::ImproperSymbol(
"!".to_string(),
"Improper symbol for custom syntax at position #1: '!'".to_string()
@ -261,7 +261,10 @@ fn test_custom_syntax_raw() -> Result<(), Box<EvalAltResult>> {
);
assert_eq!(engine.eval::<INT>("(hello kitty) + foo")?, 1041);
assert_eq!(
*engine.compile("hello hey").expect_err("should error").0,
*engine
.compile("hello hey")
.expect_err("should error")
.err_type(),
ParseErrorType::BadInput(LexError::ImproperSymbol("hey".to_string(), "".to_string()))
);

View File

@ -16,7 +16,7 @@ fn test_max_string_size() -> Result<(), Box<EvalAltResult>> {
*engine
.compile(r#"let x = "hello, world!";"#)
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::LiteralTooLarge("Length of string literal".to_string(), 10)
);
@ -24,7 +24,7 @@ fn test_max_string_size() -> Result<(), Box<EvalAltResult>> {
*engine
.compile(r#"let x = "朝に紅顔、暮に白骨";"#)
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::LiteralTooLarge("Length of string literal".to_string(), 10)
);
@ -84,7 +84,7 @@ fn test_max_array_size() -> Result<(), Box<EvalAltResult>> {
*engine
.compile("let x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::LiteralTooLarge("Size of array literal".to_string(), 10)
);
@ -266,7 +266,7 @@ fn test_max_map_size() -> Result<(), Box<EvalAltResult>> {
"let x = #{a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8,i:9,j:10,k:11,l:12,m:13,n:14,o:15};"
)
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::LiteralTooLarge(
"Number of properties in object map literal".to_string(),
10

View File

@ -167,10 +167,10 @@ fn test_eval_disabled() -> Result<(), Box<EvalAltResult>> {
engine.disable_symbol("eval");
assert!(matches!(
*engine
engine
.compile(r#"eval("40 + 2")"#)
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::BadInput(LexError::ImproperSymbol(err, ..)) if err == "eval"
));

View File

@ -84,7 +84,8 @@ fn test_functions_global_module() -> Result<(), Box<EvalAltResult>> {
42
);
assert!(matches!(*engine.run("
assert!(matches!(*engine.run(
"
fn foo() { global::ANSWER }
{
@ -104,7 +105,8 @@ fn test_functions_global_module() -> Result<(), Box<EvalAltResult>> {
);
#[cfg(not(feature = "no_closure"))]
assert!(matches!(*engine.run("
assert!(matches!(*engine.run(
"
do_stuff(|| {
const LOCAL_VALUE = 42;
global::LOCAL_VALUE

View File

@ -110,7 +110,7 @@ fn test_internal_fn_overloading() -> Result<(), Box<EvalAltResult>> {
"
)
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::FnDuplicatedDefinition("abc".to_string(), 1)
);
@ -125,8 +125,8 @@ fn test_internal_fn_params() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
*engine
.compile("fn hello(x, x) { x }")
.expect_err("should be error")
.0,
.expect_err("should error")
.err_type(),
ParseErrorType::FnDuplicatedParam("hello".to_string(), "x".to_string())
);
@ -236,7 +236,7 @@ fn test_internal_fn_bang() -> Result<(), Box<EvalAltResult>> {
#[cfg(not(feature = "no_object"))]
assert!(matches!(
*engine
engine
.compile(
"
fn foo() { this += x; }
@ -248,7 +248,7 @@ fn test_internal_fn_bang() -> Result<(), Box<EvalAltResult>> {
"
)
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::MalformedCapture(..)
));

View File

@ -30,7 +30,7 @@ fn test_loop() -> Result<(), Box<EvalAltResult>> {
*engine
.compile("let x = 0; break;")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::LoopBreak
);
@ -38,7 +38,7 @@ fn test_loop() -> Result<(), Box<EvalAltResult>> {
*engine
.compile("let x = 0; if x > 0 { continue; }")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::LoopBreak
);

View File

@ -132,43 +132,43 @@ fn test_map_index_types() -> Result<(), Box<EvalAltResult>> {
engine.compile(r#"#{a:1, b:2, c:3}["a"]['x']"#)?;
assert!(matches!(
*engine
engine
.compile("#{a:1, b:2, c:3}['x']")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::MalformedIndexExpr(..)
));
assert!(matches!(
*engine
engine
.compile("#{a:1, b:2, c:3}[1]")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::MalformedIndexExpr(..)
));
#[cfg(not(feature = "no_float"))]
assert!(matches!(
*engine
engine
.compile("#{a:1, b:2, c:3}[123.456]")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::MalformedIndexExpr(..)
));
assert!(matches!(
*engine
engine
.compile("#{a:1, b:2, c:3}[()]")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::MalformedIndexExpr(..)
));
assert!(matches!(
*engine
engine
.compile("#{a:1, b:2, c:3}[true && false]")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::MalformedIndexExpr(..)
));

View File

@ -30,7 +30,8 @@ fn test_mismatched_op_custom_type() -> Result<(), Box<EvalAltResult>> {
.register_type_with_name::<TestStruct>("TestStruct")
.register_fn("new_ts", TestStruct::new);
assert!(matches!(*engine.eval::<bool>("
assert!(matches!(*engine.eval::<bool>(
"
let x = new_ts();
let y = new_ts();
x == y

File diff suppressed because one or more lines are too long

View File

@ -115,10 +115,10 @@ fn test_switch_errors() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert!(matches!(
*engine
engine
.compile("switch x { _ => 123, 1 => 42 }")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::WrongSwitchDefaultCase
));
@ -179,10 +179,10 @@ fn test_switch_condition() -> Result<(), Box<EvalAltResult>> {
);
assert!(matches!(
*engine
engine
.compile("switch x { 1 => 123, _ if true => 42 }")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::WrongSwitchCaseCondition
));
@ -274,8 +274,9 @@ fn test_switch_ranges() -> Result<(), Box<EvalAltResult>> {
'x'
);
assert!(matches!(
*engine.compile("switch x { 10..20 => (), 20..=42 => 'a', 25..45 => 'z', 42 => 'x', 30..100 => true }")
.expect_err("should error").0,
engine.compile(
"switch x { 10..20 => (), 20..=42 => 'a', 25..45 => 'z', 42 => 'x', 30..100 => true }"
).expect_err("should error").err_type(),
ParseErrorType::WrongSwitchIntegerCase
));
assert_eq!(

View File

@ -7,10 +7,10 @@ fn test_tokens_disabled() {
engine.disable_symbol("if"); // disable the 'if' keyword
assert!(matches!(
*engine
engine
.compile("let x = if true { 42 } else { 0 };")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::Reserved(err) if err == "if"
));
@ -20,12 +20,12 @@ fn test_tokens_disabled() {
*engine
.compile("let x = 40 + 2; x += 1;")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::UnknownOperator("+=".to_string())
);
assert!(matches!(
*engine.compile("let x = += 0;").expect_err("should error").0,
engine.compile("let x = += 0;").expect_err("should error").err_type(),
ParseErrorType::Reserved(err) if err == "+="
));
}

View File

@ -233,7 +233,7 @@ fn test_var_def_filter() -> Result<(), Box<EvalAltResult>> {
);
assert!(matches!(
*engine.compile("let x = 42;").expect_err("should error").0,
engine.compile("let x = 42;").expect_err("should error").err_type(),
ParseErrorType::ForbiddenVariable(s) if s == "x"
));
assert!(matches!(