Add custom syntax.

This commit is contained in:
Stephen Chung
2020-07-09 19:54:28 +08:00
parent e33760a7d4
commit 99164ebceb
10 changed files with 483 additions and 132 deletions

View File

@@ -21,7 +21,7 @@ fn test_map_indexing() -> Result<(), Box<EvalAltResult>> {
r#"
let y = #{d: 1, "e": #{a: 42, b: 88, "": "hello"}, " 123 xyz": 9};
y.e[""][4]
"#
"#
)?,
'o'
);
@@ -47,7 +47,7 @@ fn test_map_indexing() -> Result<(), Box<EvalAltResult>> {
let x = #{a: 1, b: 2, c: 3};
let c = x.remove("c");
x.len() + c
"#
"#
)?,
5
);
@@ -58,7 +58,7 @@ fn test_map_indexing() -> Result<(), Box<EvalAltResult>> {
let y = #{b: 42, d: 9};
x.mixin(y);
x.len() + x.b
"
"
)?,
46
);
@@ -68,7 +68,7 @@ fn test_map_indexing() -> Result<(), Box<EvalAltResult>> {
let x = #{a: 1, b: 2, c: 3};
x += #{b: 42, d: 9};
x.len() + x.b
"
"
)?,
46
);
@@ -79,7 +79,7 @@ fn test_map_indexing() -> Result<(), Box<EvalAltResult>> {
let x = #{a: 1, b: 2, c: 3};
let y = #{b: 42, d: 9};
x + y
"
"
)?
.len(),
4
@@ -94,27 +94,9 @@ fn test_map_assign() -> Result<(), Box<EvalAltResult>> {
let x = engine.eval::<Map>(r#"let x = #{a: 1, b: true, "c$": "hello"}; x"#)?;
assert_eq!(
x.get("a")
.cloned()
.expect("should have property a")
.cast::<INT>(),
1
);
assert_eq!(
x.get("b")
.cloned()
.expect("should have property b")
.cast::<bool>(),
true
);
assert_eq!(
x.get("c$")
.cloned()
.expect("should have property c$")
.cast::<String>(),
"hello"
);
assert_eq!(x["a"].clone().cast::<INT>(), 1);
assert_eq!(x["b"].clone().cast::<bool>(), true);
assert_eq!(x["c$"].clone().cast::<String>(), "hello");
Ok(())
}
@@ -125,27 +107,9 @@ fn test_map_return() -> Result<(), Box<EvalAltResult>> {
let x = engine.eval::<Map>(r#"#{a: 1, b: true, "c$": "hello"}"#)?;
assert_eq!(
x.get("a")
.cloned()
.expect("should have property a")
.cast::<INT>(),
1
);
assert_eq!(
x.get("b")
.cloned()
.expect("should have property b")
.cast::<bool>(),
true
);
assert_eq!(
x.get("c$")
.cloned()
.expect("should have property c$")
.cast::<String>(),
"hello"
);
assert_eq!(x["a"].clone().cast::<INT>(), 1);
assert_eq!(x["b"].clone().cast::<bool>(), true);
assert_eq!(x["c$"].clone().cast::<String>(), "hello");
Ok(())
}
@@ -167,7 +131,7 @@ fn test_map_for() -> Result<(), Box<EvalAltResult>> {
}
s
"#
"#
)?
.len(),
11
@@ -188,41 +152,11 @@ fn test_map_json() -> Result<(), Box<EvalAltResult>> {
assert!(!map.contains_key("x"));
assert_eq!(
map.get("a")
.cloned()
.expect("should have property a")
.cast::<INT>(),
1
);
assert_eq!(
map.get("b")
.cloned()
.expect("should have property b")
.cast::<bool>(),
true
);
assert_eq!(
map.get("c")
.cloned()
.expect("should have property a")
.cast::<INT>(),
42
);
assert_eq!(
map.get("$d e f!")
.cloned()
.expect("should have property $d e f!")
.cast::<String>(),
"hello"
);
assert_eq!(
map.get("z")
.cloned()
.expect("should have property z")
.cast::<()>(),
()
);
assert_eq!(map["a"].clone().cast::<INT>(), 1);
assert_eq!(map["b"].clone().cast::<bool>(), true);
assert_eq!(map["c"].clone().cast::<INT>(), 42);
assert_eq!(map["$d e f!"].clone().cast::<String>(), "hello");
assert_eq!(map["z"].clone().cast::<()>(), ());
#[cfg(not(feature = "no_index"))]
{
@@ -241,7 +175,7 @@ fn test_map_json() -> Result<(), Box<EvalAltResult>> {
}
s
"#
"#
)?
.len(),
11
@@ -265,7 +199,7 @@ fn test_map_oop() -> Result<(), Box<EvalAltResult>> {
obj.action(2);
obj.data
"#,
"#,
)?,
42
);

69
tests/syntax.rs Normal file
View File

@@ -0,0 +1,69 @@
#![cfg(feature = "internals")]
use rhai::{
Dynamic, Engine, EvalAltResult, EvalState, Expr, Imports, LexError, Module, Scope, INT,
};
#[test]
fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine
.add_custom_syntax(
&["do", "$ident$", "$block$", "while", "$expr$"],
1,
|engine: &Engine,
scope: &mut Scope,
mods: &mut Imports,
state: &mut EvalState,
lib: &Module,
this_ptr: &mut Option<&mut Dynamic>,
exprs: &[Expr],
level: usize| {
let var_name = match exprs.get(0).unwrap() {
Expr::Variable(s) => (s.0).0.clone(),
_ => unreachable!(),
};
let stmt = exprs.get(1).unwrap();
let expr = exprs.get(2).unwrap();
scope.push(var_name, 0 as INT);
loop {
engine.eval_expr_from_ast(scope, mods, state, lib, this_ptr, stmt, level)?;
if !engine
.eval_expr_from_ast(scope, mods, state, lib, this_ptr, expr, level)?
.as_bool()
.map_err(|_| {
EvalAltResult::ErrorBooleanArgMismatch(
"do-while".into(),
expr.position(),
)
})?
{
break;
}
}
Ok(().into())
},
)
.unwrap();
assert!(matches!(
*engine.add_custom_syntax(&["!"], 0, |_, _, _, _, _, _, _, _| Ok(().into())).expect_err("should error"),
LexError::ImproperSymbol(s) if s == "!"
));
assert_eq!(
engine.eval::<INT>(
r"
do x { x += 1 } while x < 42;
x
"
)?,
42
);
Ok(())
}