Skip to content

Commit

Permalink
Merge pull request #209 from schungx/master
Browse files Browse the repository at this point in the history
General fixups for version 0.18.0
  • Loading branch information
schungx committed Aug 5, 2020
2 parents dab85c9 + f791c64 commit 75fd9f9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 41 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Expand Up @@ -19,7 +19,6 @@ jobs:
flags:
- ""
- "--features serde"
- "--features plugins"
- "--features unchecked"
- "--features sync"
- "--features no_optimize"
Expand Down
1 change: 1 addition & 0 deletions RELEASES.md
Expand Up @@ -36,6 +36,7 @@ Breaking changes
* `PackagesCollection::get_fn`, `PackagesCollection::contains_fn`, `Module::get_fn` and `Module::contains_fn` now take an additional `public_only` parameter indicating whether only public functions are accepted.
* The iterator returned by `Scope::iter` now contains a clone of the `Dynamic` value (unshared).
* `Engine::load_package` takes any type that is `Into<PackageLibrary>`.
* Error in `Engine::register_custom_syntax` is no longer `Box`-ed.

Housekeeping
------------
Expand Down
2 changes: 1 addition & 1 deletion src/syntax.rs
Expand Up @@ -100,7 +100,7 @@ impl Engine {
) -> Result<Dynamic, Box<EvalAltResult>>
+ SendSync
+ 'static,
) -> Result<&mut Self, Box<ParseError>> {
) -> Result<&mut Self, ParseError> {
let mut segments: StaticVec<_> = Default::default();

for s in keywords {
Expand Down
71 changes: 32 additions & 39 deletions tests/syntax.rs
@@ -1,6 +1,4 @@
use rhai::{
Engine, EvalAltResult, EvalContext, Expression, ParseError, ParseErrorType, Scope, INT,
};
use rhai::{Engine, EvalAltResult, EvalContext, Expression, ParseErrorType, Scope, INT};

#[test]
fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
Expand All @@ -19,43 +17,35 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
ParseErrorType::Reserved(err) if err == "while"
));

engine
.register_custom_syntax(
&[
"do", "|", "$ident$", "|", "->", "$block$", "while", "$expr$",
],
1,
|engine: &Engine,
context: &mut EvalContext,
scope: &mut Scope,
inputs: &[Expression]| {
let var_name = inputs[0].get_variable_name().unwrap().to_string();
let stmt = inputs.get(1).unwrap();
let expr = inputs.get(2).unwrap();
engine.register_custom_syntax(
&[
"do", "|", "$ident$", "|", "->", "$block$", "while", "$expr$",
],
1,
|engine: &Engine, context: &mut EvalContext, scope: &mut Scope, inputs: &[Expression]| {
let var_name = inputs[0].get_variable_name().unwrap().to_string();
let stmt = inputs.get(1).unwrap();
let expr = inputs.get(2).unwrap();

scope.push(var_name, 0 as INT);
scope.push(var_name, 0 as INT);

loop {
engine.eval_expression_tree(context, scope, stmt)?;
loop {
engine.eval_expression_tree(context, scope, stmt)?;

if !engine
.eval_expression_tree(context, scope, expr)?
.as_bool()
.map_err(|_| {
EvalAltResult::ErrorBooleanArgMismatch(
"do-while".into(),
expr.position(),
)
})?
{
break;
}
if !engine
.eval_expression_tree(context, scope, expr)?
.as_bool()
.map_err(|_| {
EvalAltResult::ErrorBooleanArgMismatch("do-while".into(), expr.position())
})?
{
break;
}
}

Ok(().into())
},
)
.unwrap();
Ok(().into())
},
)?;

// 'while' is now a custom keyword so this it can no longer be a variable
engine.consume("let while = 0").expect_err("should error");
Expand All @@ -71,10 +61,13 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
);

// The first symbol must be an identifier
assert!(matches!(
*engine.register_custom_syntax(&["!"], 0, |_, _, _, _| Ok(().into())).expect_err("should error"),
ParseError(err, _) if *err == ParseErrorType::BadInput("Improper symbol for custom syntax: '!'".to_string())
));
assert_eq!(
*engine
.register_custom_syntax(&["!"], 0, |_, _, _, _| Ok(().into()))
.expect_err("should error")
.0,
ParseErrorType::BadInput("Improper symbol for custom syntax: '!'".to_string())
);

Ok(())
}

0 comments on commit 75fd9f9

Please sign in to comment.