diff --git a/CHANGELOG.md b/CHANGELOG.md index 98281abf..a057eebe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ Rhai Release Notes ================== +Version 1.11.0 +============== + +Enhancements + +* The look-ahead symbol for custom syntax now renders a string literal in quotes (instead of the generic term `string`). This facilitates more accurate parsing by separating strings and identifiers. + + Version 1.10.1 ============== diff --git a/src/parser.rs b/src/parser.rs index 20600e0b..621a1b47 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2461,7 +2461,7 @@ impl Engine { use crate::api::custom_syntax::markers::*; let mut settings = settings; - let mut inputs = StaticVec::::new(); + let mut inputs = StaticVec::new_const(); let mut segments = StaticVec::new_const(); let mut tokens = StaticVec::new_const(); diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 71305dec..6cc7eb57 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -699,7 +699,7 @@ impl Token { FloatConstant(f) => f.to_string().into(), #[cfg(feature = "decimal")] DecimalConstant(d) => d.to_string().into(), - StringConstant(..) => "string".into(), + StringConstant(s) => format!("\"{s}\"").into(), InterpolatedString(..) => "string".into(), CharConstant(c) => c.to_string().into(), Identifier(s) => s.to_string().into(), diff --git a/tests/custom_syntax.rs b/tests/custom_syntax.rs index 2c8db7f1..dacca728 100644 --- a/tests/custom_syntax.rs +++ b/tests/custom_syntax.rs @@ -256,10 +256,18 @@ fn test_custom_syntax_raw() -> Result<(), Box> { engine.register_custom_syntax_with_state_raw( "hello", - |stream, _, state| match stream.len() { + |stream, look_ahead, state| match stream.len() { 0 => unreachable!(), - 1 => Ok(Some("$ident$".into())), + 1 if look_ahead == "\"world\"" => { + *state = Dynamic::TRUE; + Ok(Some("$string$".into())) + } + 1 => { + *state = Dynamic::FALSE; + Ok(Some("$ident$".into())) + } 2 => match stream[1].as_str() { + "world" if state.as_bool().unwrap_or(false) => Ok(Some("$$world".into())), "world" => Ok(Some("$$hello".into())), "kitty" => { *state = (42 as INT).into(); @@ -276,16 +284,11 @@ fn test_custom_syntax_raw() -> Result<(), Box> { context.scope_mut().push("foo", 999 as INT); Ok(match inputs[0].get_string_value().unwrap() { - "world" - if inputs - .last() - .unwrap() - .get_string_value() - .map_or(false, |s| s == "$$hello") => - { - 0 as INT - } - "world" => 123 as INT, + "world" => match inputs.last().unwrap().get_string_value().unwrap_or("") { + "$$hello" => 0 as INT, + "$$world" => 123456 as INT, + _ => 123 as INT, + }, "kitty" if inputs.len() > 1 => 999 as INT, "kitty" => state.as_int().unwrap(), _ => unreachable!(), @@ -294,6 +297,7 @@ fn test_custom_syntax_raw() -> Result<(), Box> { }, ); + assert_eq!(engine.eval::(r#"hello "world""#)?, 123456); assert_eq!(engine.eval::("hello world")?, 0); assert_eq!(engine.eval::("hello kitty")?, 42); assert_eq!(