rhai 1.27.0

Embedded scripting for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//! Module implementing custom syntax for [`Engine`].
#![cfg(not(feature = "no_custom_syntax"))]
#![cfg(any(not(feature = "no_ast"), feature = "grain"))]

#[cfg(not(feature = "no_ast"))]
use crate::ast::Expr;
use crate::func::SendSync;
#[cfg(not(feature = "no_ast"))]
use crate::parser::ParseResult;
use crate::types::dynamic::Variant;
#[cfg(not(feature = "no_ast"))]
use crate::types::token::{is_reserved_keyword_or_symbol, is_valid_identifier, Token};
#[cfg(any(not(feature = "no_ast"), feature = "grain"))]
use crate::ImmutableString;
#[cfg(not(feature = "no_ast"))]
use crate::LexError;
use crate::{Dynamic, Engine, EvalContext, Identifier, Position, RhaiResult};
#[cfg(not(feature = "no_ast"))]
use std::borrow::Borrow;
#[cfg(not(feature = "no_ast"))]
use std::ops::Deref;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;

/// Collection of special markers for custom syntax definition.
#[cfg(not(feature = "no_ast"))]
pub mod markers {
    /// Special marker for matching an expression.
    pub const CUSTOM_SYNTAX_MARKER_EXPR: &str = "$expr$";
    /// Special marker for matching a statements block.
    pub const CUSTOM_SYNTAX_MARKER_BLOCK: &str = "$block$";
    /// Special marker for matching a statements block after the starting `{`.
    pub const CUSTOM_SYNTAX_MARKER_INNER: &str = "$inner$";
    /// Special marker for matching a function body.
    #[cfg(not(feature = "no_function"))]
    pub const CUSTOM_SYNTAX_MARKER_FUNC: &str = "$func$";
    /// Special marker for matching a single character from the input stream.
    pub const CUSTOM_SYNTAX_MARKER_RAW: &str = "$raw$";
    /// Special marker for matching an identifier.
    pub const CUSTOM_SYNTAX_MARKER_IDENT: &str = "$ident$";
    /// Special marker for matching a single symbol.
    pub const CUSTOM_SYNTAX_MARKER_SYMBOL: &str = "$symbol$";
    /// Special marker for matching a single token.
    pub const CUSTOM_SYNTAX_MARKER_TOKEN: &str = "$token$";
    /// Special marker for matching a string literal.
    pub const CUSTOM_SYNTAX_MARKER_STRING: &str = "$string$";
    /// Special marker for matching an integer number.
    pub const CUSTOM_SYNTAX_MARKER_INT: &str = "$int$";
    /// Special marker for matching a floating-point number.
    #[cfg(not(feature = "no_float"))]
    pub const CUSTOM_SYNTAX_MARKER_FLOAT: &str = "$float$";
    /// Special marker for matching a boolean value.
    pub const CUSTOM_SYNTAX_MARKER_BOOL: &str = "$bool$";
    /// Special marker for identifying the custom syntax variant.
    pub const CUSTOM_SYNTAX_MARKER_SYNTAX_VARIANT: &str = "$$";
}

/// A general expression evaluation trait object.
#[cfg(not(feature = "sync"))]
pub type FnCustomSyntaxEval = dyn Fn(&mut EvalContext, &[Expression], &Dynamic) -> RhaiResult;
/// A general expression evaluation trait object.
#[cfg(feature = "sync")]
pub type FnCustomSyntaxEval =
    dyn Fn(&mut EvalContext, &[Expression], &Dynamic) -> RhaiResult + Send + Sync;

/// A general expression parsing trait object.
///
/// Not available under `no_ast`.
#[cfg(not(feature = "no_ast"))]
#[cfg(not(feature = "sync"))]
pub type FnCustomSyntaxParse =
    dyn Fn(&[ImmutableString], &str, &mut Dynamic) -> ParseResult<Option<ImmutableString>>;
/// A general expression parsing trait object.
///
/// Not available under `no_ast`.
#[cfg(not(feature = "no_ast"))]
#[cfg(feature = "sync")]
pub type FnCustomSyntaxParse = dyn Fn(&[ImmutableString], &str, &mut Dynamic) -> ParseResult<Option<ImmutableString>>
    + Send
    + Sync;

/// An expression sub-tree in an [`AST`][crate::AST], or a compiled [Rhai Grain](crate::grain)
/// chunk (requires `grain`).
#[derive(Debug, Clone)]
pub struct Expression<'a>(pub(crate) ExpressionNode<'a>);

#[derive(Debug, Clone)]
pub(crate) enum ExpressionNode<'a> {
    /// An [`Expr`].
    #[cfg(not(feature = "no_ast"))]
    AST(&'a Expr),
    /// A Rhai Grain compiled expression.
    #[cfg(feature = "grain")]
    Grain {
        /// The compiled Rhai Grain program.
        program: &'a crate::grain::Program<'a>,
        /// Program chunk.
        chunk: crate::grain::bytecode::Chunk,
        /// The local-slot base the chunk's slot numbering is relative to.
        ///
        /// The chunk was lowered while reusing the *surrounding* code's `slots`
        /// table rather than starting a fresh one as a normal function body does,
        /// so its slots only resolve correctly against the same base the enclosing
        /// frame is running with.
        base: usize,
        /// The literal value of the expression, if any.
        literal: Option<Dynamic>,
    },
}

#[cfg(not(feature = "no_ast"))]
impl<'a> From<&'a Expr> for Expression<'a> {
    #[inline(always)]
    fn from(expr: &'a Expr) -> Self {
        Self(ExpressionNode::AST(expr))
    }
}

#[cfg(feature = "grain")]
impl<'a> Expression<'a> {
    /// Create an [`Expression`] backed by a compiled Rhai Grain chunk.
    #[inline(always)]
    #[must_use]
    pub(crate) const fn from_grain(
        program: &'a crate::grain::Program<'a>,
        chunk: crate::grain::bytecode::Chunk,
        base: usize,
        literal: Option<Dynamic>,
    ) -> Self {
        Self(ExpressionNode::Grain {
            program,
            chunk,
            base,
            literal,
        })
    }
}

impl Expression<'_> {
    /// Evaluate this [expression tree][Expression] within an [evaluation context][`EvalContext`].
    ///
    /// # WARNING - Low Level API
    ///
    /// This function is very low level.  It evaluates an expression from an [`AST`][crate::AST],
    /// or runs a compiled Rhai Grain chunk (requires `grain`).
    #[inline(always)]
    pub fn eval_with_context(&self, context: &mut EvalContext) -> RhaiResult {
        context.eval_expression_tree(self)
    }
    /// Evaluate this [expression tree][Expression] within an [evaluation context][`EvalContext`].
    ///
    /// The following option is available:
    ///
    /// * whether to rewind the [`Scope`][crate::Scope] after evaluation if the expression is a [`StmtBlock`][crate::ast::StmtBlock]
    ///
    /// # WARNING - Unstable API
    ///
    /// This API is volatile and may change in the future.
    ///
    /// # WARNING - Low Level API
    ///
    /// This function is _extremely_ low level.  It evaluates an expression from an [`AST`][crate::AST].
    #[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
    #[inline(always)]
    pub fn eval_with_context_raw(
        &self,
        context: &mut EvalContext,
        rewind_scope: bool,
    ) -> RhaiResult {
        #[allow(deprecated)]
        context.eval_expression_tree_raw(self, rewind_scope)
    }
    /// Get the value of this expression if it is a variable name or a string constant.
    ///
    /// Returns [`None`] also if the constant is not of the specified type.
    ///
    /// For a compiled Rhai Grain chunk (requires `grain`), returns a value only when
    /// the compiler identified a string literal or variable name.
    #[inline]
    #[must_use]
    pub fn get_string_value(&self) -> Option<&str> {
        match self.0 {
            #[cfg(not(feature = "no_ast"))]
            ExpressionNode::AST(expr) => {
                return match expr {
                    #[cfg(not(feature = "no_module"))]
                    Expr::Variable(x, ..) if !x.2.is_empty() => None,
                    Expr::Variable(x, ..) => Some(&x.1),
                    #[cfg(not(feature = "no_function"))]
                    Expr::ThisPtr(..) => Some(crate::engine::KEYWORD_THIS),
                    Expr::StringConstant(x, ..) => Some(x),
                    _ => None,
                };
            }
            #[cfg(feature = "grain")]
            ExpressionNode::Grain { ref literal, .. } => {
                return literal
                    .as_ref()
                    .and_then(|v| v.downcast_ref::<ImmutableString>())
                    .map(ImmutableString::as_str);
            }
        }
    }
    /// Get the position of this expression.
    #[inline]
    #[must_use]
    pub fn position(&self) -> Position {
        match self.0 {
            #[cfg(not(feature = "no_ast"))]
            ExpressionNode::AST(expr) => {
                return expr.position();
            }
            #[cfg(feature = "grain")]
            ExpressionNode::Grain { program, chunk, .. } => {
                return program.position(chunk.entry() as usize);
            }
        }
    }
    /// Get the value of this expression if it is a literal constant.
    ///
    /// Supports [`INT`][crate::INT], [`FLOAT`][crate::FLOAT], `()`, `char`, `bool` and
    /// [`ImmutableString`][crate::ImmutableString].
    ///
    /// Returns [`None`] if the constant is not of the specified type, or if this
    /// Grain-compiled expression is not a known literal.
    #[inline]
    #[must_use]
    pub fn get_literal_value<T: Variant + Clone>(&self) -> Option<T> {
        match self.0 {
            // Coded this way in order to maximally leverage potentials for dead-code removal.
            #[cfg(not(feature = "no_ast"))]
            ExpressionNode::AST(expr) => {
                return match expr {
                    Expr::DynamicConstant(x, ..) => x.clone().try_cast::<T>(),
                    Expr::IntegerConstant(x, ..) => reify! { *x => Option<T> },

                    #[cfg(not(feature = "no_float"))]
                    Expr::FloatConstant(x, ..) => reify! { *x => Option<T> },

                    Expr::CharConstant(x, ..) => reify! { *x => Option<T> },
                    Expr::StringConstant(x, ..) => reify! { x.clone() => Option<T> },
                    Expr::Variable(x, ..) => reify! { x.1.clone() => Option<T> },
                    Expr::BoolConstant(x, ..) => reify! { *x => Option<T> },
                    Expr::Unit(..) => reify! { () => Option<T> },

                    _ => None,
                };
            }
            #[cfg(feature = "grain")]
            ExpressionNode::Grain { ref literal, .. } => {
                return literal.clone().and_then(|value| value.try_cast::<T>());
            }
        }
    }
}

/// Borrowing as an [`Expr`] is only meaningful for an [`Expression`] backed by one.
///
/// # Panics
///
/// Panics if this [`Expression`] instead holds a compiled Rhai Grain chunk (requires `grain`) --
/// which can only happen for a custom-syntax input Rhai Grain lowered itself.
#[cfg(not(feature = "no_ast"))]
impl Borrow<Expr> for Expression<'_> {
    #[inline(always)]
    fn borrow(&self) -> &Expr {
        match &self.0 {
            ExpressionNode::AST(expr) => expr,
            #[cfg(feature = "grain")]
            ExpressionNode::Grain { .. } => {
                panic!("a Grain-backed custom-syntax expression has no AST")
            }
        }
    }
}

#[cfg(not(feature = "no_ast"))]
impl AsRef<Expr> for Expression<'_> {
    #[inline(always)]
    fn as_ref(&self) -> &Expr {
        self.borrow()
    }
}

#[cfg(not(feature = "no_ast"))]
impl Deref for Expression<'_> {
    type Target = Expr;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        self.borrow()
    }
}

/// Definition of a custom syntax definition.
pub struct CustomSyntax {
    /// A parsing function to return the next token in a custom syntax based on the
    /// symbols parsed so far.
    ///
    /// Not available under `no_ast`.
    #[cfg(not(feature = "no_ast"))]
    pub parse: Box<FnCustomSyntaxParse>,
    /// Custom syntax implementation function.
    pub func: Box<FnCustomSyntaxEval>,
    /// Any variables added/removed in the scope?
    pub scope_may_be_changed: bool,
    /// Is look-ahead enabled when parsing this custom syntax?
    ///
    /// Not available under `no_ast`.
    #[cfg(not(feature = "no_ast"))]
    pub use_look_ahead: bool,
}

impl Engine {
    /// Register a custom syntax executor with the [`Engine`], without a parsing function.
    ///
    /// Not available under `no_custom_syntax`.
    ///
    /// This is the primitive every other `register_custom_syntax*` method is built on top of.
    /// Under `no_ast`, it is the *only* way to register a custom syntax, because there is no
    /// parser present to drive a `parse` callback -- the parsing/lowering has already happened
    /// once, upstream, on a host build that does have a parser. A `no_ast` (+ `grain`) build
    /// only ever needs the `key` -> `func` mapping, to run a
    /// [`Program`][crate::grain::Program] that already contains an
    /// `Op::CustomSyntax`[^op] referencing this `key`.
    ///
    /// [^op]: [`Op::CustomSyntax`](crate::grain::bytecode::Op::CustomSyntax), under the
    /// `internals` feature.
    ///
    /// * `key` is the discriminator symbol -- the first token of the custom syntax.
    /// * `scope_may_be_changed` specifies variables _may_ be added/removed by this custom syntax.
    /// * `func` is the implementation function.
    ///
    /// # Errors
    ///
    /// Under `no_ast`, returns an error if `scope_may_be_changed` is `true`. Without an AST
    /// interpreter present, there is no way to ever run the whole-node residual fallback a
    /// scope-changing custom syntax would require, so registering one that claims to need it is
    /// a logic error in the calling code, not a runtime possibility: a
    /// [`Program`][crate::grain::Program] that reaches a `no_ast` device has necessarily already
    /// been lowered (or rejected) by a host build that does have a parser.
    pub fn register_custom_syntax_handler(
        &mut self,
        key: impl Into<Identifier>,
        scope_may_be_changed: bool,
        func: impl Fn(&mut EvalContext, &[Expression], &Dynamic) -> RhaiResult + SendSync + 'static,
    ) -> Result<&mut Self, String> {
        #[cfg(feature = "no_ast")]
        if scope_may_be_changed {
            return Err(
                "cannot register a custom syntax with `scope_may_be_changed == true` under \
                 `no_ast`: there is no AST interpreter present to ever run its whole-node \
                 residual fallback"
                    .to_string(),
            );
        }

        self.custom_syntax.insert(
            key.into(),
            CustomSyntax {
                #[cfg(not(feature = "no_ast"))]
                parse: Box::new(|_, _, _| Ok(None)),
                func: Box::new(func),
                scope_may_be_changed,
                #[cfg(not(feature = "no_ast"))]
                use_look_ahead: true,
            }
            .into(),
        );
        Ok(self)
    }
    /// Register a custom syntax with the [`Engine`].
    ///
    /// Not available under `no_custom_syntax` or `no_ast`.
    ///
    /// * `symbols` holds a slice of strings that define the custom syntax.
    /// * `scope_may_be_changed` specifies variables _may_ be added/removed by this custom syntax.
    /// * `func` is the implementation function.
    ///
    /// ## Note on `symbols`
    ///
    /// * Whitespaces around symbols are stripped.
    /// * Symbols that are all-whitespace or empty are ignored.
    /// * If `symbols` does not contain at least one valid token, then the custom syntax registration
    ///   is simply ignored.
    ///
    /// ## Note on `scope_may_be_changed`
    ///
    /// If `scope_may_be_changed` is `true`, then _size_ of the current [`Scope`][crate::Scope]
    /// _may_ be modified by this custom syntax.
    ///
    /// Adding new variables and/or removing variables count.
    ///
    /// Simply modifying the values of existing variables does NOT count, as the _size_ of the
    /// current [`Scope`][crate::Scope] is unchanged, so `false` should be passed.
    ///
    /// Replacing one variable with another (i.e. adding a new variable and removing one variable at
    /// the same time so that the total _size_ of the [`Scope`][crate::Scope] is unchanged) also
    /// does NOT count, so `false` should be passed.
    #[cfg(not(feature = "no_ast"))]
    pub fn register_custom_syntax<S: AsRef<str> + Into<Identifier>>(
        &mut self,
        symbols: impl AsRef<[S]>,
        scope_may_be_changed: bool,
        func: impl Fn(&mut EvalContext, &[Expression]) -> RhaiResult + SendSync + 'static,
    ) -> ParseResult<&mut Self> {
        #[allow(clippy::wildcard_imports)]
        use markers::*;

        let mut segments = Vec::<ImmutableString>::new();

        for s in symbols.as_ref() {
            let s = s.as_ref().trim();

            // Skip empty symbols
            if s.is_empty() {
                continue;
            }

            let token = Token::lookup_symbol_from_syntax(s).or_else(|| {
                is_reserved_keyword_or_symbol(s)
                    .0
                    .then(|| Token::Reserved(Box::new(s.into())))
            });

            let seg = match s {
                CUSTOM_SYNTAX_MARKER_RAW => {
                    return Err(LexError::ImproperSymbol(
                        String::new(),
                        "`register_custom_syntax` does not support `$raw$`".to_string(),
                    )
                    .into_err(Position::NONE));
                }

                // Markers not in first position
                CUSTOM_SYNTAX_MARKER_IDENT
                | CUSTOM_SYNTAX_MARKER_SYMBOL
                | CUSTOM_SYNTAX_MARKER_TOKEN
                | CUSTOM_SYNTAX_MARKER_EXPR
                | CUSTOM_SYNTAX_MARKER_BLOCK
                | CUSTOM_SYNTAX_MARKER_BOOL
                | CUSTOM_SYNTAX_MARKER_INT
                | CUSTOM_SYNTAX_MARKER_STRING
                    if !segments.is_empty() =>
                {
                    s.into()
                }

                // Markers not in first position
                #[cfg(not(feature = "no_function"))]
                CUSTOM_SYNTAX_MARKER_FUNC if !segments.is_empty() => s.into(),

                // Markers not in first position
                #[cfg(not(feature = "no_float"))]
                CUSTOM_SYNTAX_MARKER_FLOAT if !segments.is_empty() => s.into(),

                // Identifier not in first position
                _ if !segments.is_empty() && is_valid_identifier(s) => s.into(),

                // Keyword/symbol not in first position
                _ if !segments.is_empty() && token.is_some() => {
                    // Make it a custom keyword/symbol if it is disabled or reserved
                    if (self.is_symbol_disabled(s)
                        || token.as_ref().map_or(false, Token::is_reserved))
                        && !self.custom_keywords.contains_key(s)
                    {
                        self.custom_keywords.insert(s.into(), None);
                    }
                    s.into()
                }

                // Standard keyword in first position but not disabled
                _ if segments.is_empty()
                    && token.as_ref().map_or(false, Token::is_standard_keyword)
                    && !self.is_symbol_disabled(s) =>
                {
                    return Err(LexError::ImproperSymbol(
                        s.to_string(),
                        format!("Improper symbol for custom syntax at position #0: '{s}'"),
                    )
                    .into_err(Position::NONE));
                }

                // Identifier or symbol in first position
                _ if segments.is_empty()
                    && (is_valid_identifier(s) || is_reserved_keyword_or_symbol(s).0) =>
                {
                    // Make it a custom keyword/symbol if it is disabled or reserved
                    if self.is_symbol_disabled(s)
                        || (token.as_ref().map_or(false, Token::is_reserved)
                            && !self.custom_keywords.contains_key(s))
                    {
                        self.custom_keywords.insert(s.into(), None);
                    }
                    s.into()
                }

                // Anything else is an error
                _ => {
                    return Err(LexError::ImproperSymbol(
                        s.to_string(),
                        format!(
                            "Improper symbol for custom syntax at position #{}: '{s}'",
                            segments.len() + 1,
                        ),
                    )
                    .into_err(Position::NONE));
                }
            };

            segments.push(seg);
        }

        // If the syntax has nothing, just ignore the registration
        if segments.is_empty() {
            return Ok(self);
        }

        // The first keyword/symbol is the discriminator
        let key = segments[0].clone();

        self.register_custom_syntax_with_state_raw(
            key,
            // Construct the parsing function
            move |stream, _, _| match stream.len() {
                len if len >= segments.len() => Ok(None),
                len => Ok(Some(segments[len].clone())),
            },
            scope_may_be_changed,
            move |context, expressions, _| func(context, expressions),
        );

        Ok(self)
    }
    /// Register a custom syntax with the [`Engine`] with custom user-defined state.
    ///
    /// Not available under `no_custom_syntax` or `no_ast`.
    ///
    /// # WARNING - Low Level API
    ///
    /// This function is very low level.
    ///
    /// * `scope_may_be_changed` specifies variables have been added/removed by this custom syntax.
    /// * `parse` is the parsing function.
    /// * `func` is the implementation function.
    ///
    /// All custom keywords used as symbols must be manually registered via [`Engine::register_custom_operator`].
    /// Otherwise, they won't be recognized.
    ///
    /// # Parsing Function Signature
    ///
    /// The parsing function has the following signature:
    ///
    /// `Fn(symbols: &[ImmutableString], look_ahead: &str, state: &mut Dynamic) -> Result<Option<ImmutableString>, ParseError>`
    ///
    /// where:
    /// * `symbols`: a slice of symbols that have been parsed so far, possibly containing `$expr$` and/or `$block$`;
    ///   `$ident$` and other literal markers are replaced by the actual text
    /// * `look_ahead`: a string slice containing the next symbol that is about to be read
    /// * `state`: a [`Dynamic`] value that contains a user-defined state
    ///
    /// ## Return value
    ///
    /// * `Ok(None)`: parsing complete and there are no more symbols to match.
    /// * `Ok(Some(symbol))`: the next symbol to match, which can also be `$expr$`, `$ident$` or `$block$` etc.
    /// * `Err(ParseError)`: error that is reflected back to the [`Engine`], normally `ParseError(ParseErrorType::BadInput(LexError::ImproperSymbol(message)), Position::NONE)` to indicate a syntax error, but it can be any [`ParseError`][crate::ParseError].
    #[cfg(not(feature = "no_ast"))]
    pub fn register_custom_syntax_with_state_raw(
        &mut self,
        key: impl Into<Identifier>,
        parse: impl Fn(&[ImmutableString], &str, &mut Dynamic) -> ParseResult<Option<ImmutableString>>
            + SendSync
            + 'static,
        scope_may_be_changed: bool,
        func: impl Fn(&mut EvalContext, &[Expression], &Dynamic) -> RhaiResult + SendSync + 'static,
    ) -> &mut Self {
        let key = key.into();

        // Not under `no_ast` here (see the `cfg` on this function), so
        // `scope_may_be_changed` can never trip the `no_ast` restriction.
        self.register_custom_syntax_handler(key.clone(), scope_may_be_changed, func)
            .expect("cannot fail: this function is unavailable under `no_ast`");

        let entry = self.custom_syntax.get_mut(&key).unwrap();
        entry.parse = Box::new(parse);
        entry.use_look_ahead = true;

        self
    }
    /// Register a custom syntax with the [`Engine`] with custom user-defined state,
    /// but with no look-ahead.  This enables the usage of `$raw$` to process raw script
    /// text character-by-character, by-passing the tokenizer.
    ///
    /// Not available under `no_custom_syntax` or `no_ast`.
    ///
    /// # WARNING - Low Level API
    ///
    /// This function is very low level.
    ///
    /// * `scope_may_be_changed` specifies variables have been added/removed by this custom syntax.
    /// * `parse` is the parsing function.
    /// * `func` is the implementation function.
    ///
    /// # Parsing Function Signature
    ///
    /// The parsing function has the following signature:
    ///
    /// `Fn(symbols: &[ImmutableString], state: &mut Dynamic) -> Result<Option<ImmutableString>, ParseError>`
    ///
    /// where:
    /// * `symbols`: a slice of symbols that have been parsed so far, possibly containing `$expr$` and/or `$block$`;
    ///   `$ident$` and other literal markers are replaced by the actual text
    /// * `state`: a [`Dynamic`] value that contains a user-defined state
    ///
    /// ## Return value
    ///
    /// * `Ok(None)`: parsing complete and there are no more symbols to match.
    /// * `Ok(Some(symbol))`: the next symbol to match, which can also be `$expr$`, `$ident$` or `$block$` etc.
    ///    `$raw$` can be returned such that the next character in the script text is returned without any processing.
    /// * `Err(ParseError)`: error that is reflected back to the [`Engine`], normally `ParseError(ParseErrorType::BadInput(LexError::ImproperSymbol(message)), Position::NONE)` to indicate a syntax error, but it can be any [`ParseError`][crate::ParseError].
    #[cfg(not(feature = "no_ast"))]
    pub fn register_custom_syntax_without_look_ahead_raw(
        &mut self,
        key: impl Into<Identifier>,
        parse: impl Fn(&[ImmutableString], &mut Dynamic) -> ParseResult<Option<ImmutableString>>
            + SendSync
            + 'static,
        scope_may_be_changed: bool,
        func: impl Fn(&mut EvalContext, &[Expression], &Dynamic) -> RhaiResult + SendSync + 'static,
    ) -> &mut Self {
        let key = key.into();

        self.register_custom_syntax_with_state_raw(
            key.clone(),
            move |symbols, _, state| parse(symbols, state),
            scope_may_be_changed,
            func,
        );

        self.custom_syntax.get_mut(&key).unwrap().use_look_ahead = false;
        self
    }
}