Struct casbin::rhai::AST

source ·
pub struct AST { /* private fields */ }
Expand description

Compiled AST (abstract syntax tree) of a Rhai script.

§Thread Safety

Currently, AST is neither Send nor Sync. Turn on the sync feature to make it Send + Sync.

Implementations§

source§

impl AST

source

pub fn empty() -> AST

Create an empty AST.

source

pub fn source(&self) -> Option<&str>

Get the source, if any.

source

pub fn set_source(&mut self, source: impl Into<ImmutableString>) -> &mut AST

Set the source.

source

pub fn clear_source(&mut self) -> &mut AST

Clear the source.

source

pub fn clone_statements_only(&self) -> AST

Clone the AST’s script statements into a new AST. No functions are cloned.

source

pub fn merge(&self, other: &AST) -> AST

Merge two AST into one. Both AST’s are untouched and a new, merged, version is returned.

Statements in the second AST are simply appended to the end of the first without any processing. Thus, the return value of the first AST (if using expression-statement syntax) is buried. Of course, if the first AST uses a return statement at the end, then the second AST will essentially be dead code.

All script-defined functions in the second AST overwrite similarly-named functions in the first AST with the same number of parameters.

§Example
use rhai::Engine;

let engine = Engine::new();

let ast1 = engine.compile("
    fn foo(x) { 42 + x }
    foo(1)
")?;

let ast2 = engine.compile(r#"
    fn foo(n) { `hello${n}` }
    foo("!")
"#)?;

let ast = ast1.merge(&ast2);    // Merge 'ast2' into 'ast1'

// Notice that using the '+' operator also works:
// let ast = &ast1 + &ast2;

// 'ast' is essentially:
//
//    fn foo(n) { `hello${n}` } // <- definition of first 'foo' is overwritten
//    foo(1)                    // <- notice this will be "hello1" instead of 43,
//                              //    but it is no longer the return value
//    foo("!")                  // returns "hello!"

// Evaluate it
assert_eq!(engine.eval_ast::<String>(&ast)?, "hello!");
source

pub fn combine(&mut self, other: AST) -> &mut AST

Combine one AST with another. The second AST is consumed.

Statements in the second AST are simply appended to the end of the first without any processing. Thus, the return value of the first AST (if using expression-statement syntax) is buried. Of course, if the first AST uses a return statement at the end, then the second AST will essentially be dead code.

All script-defined functions in the second AST overwrite similarly-named functions in the first AST with the same number of parameters.

§Example
use rhai::Engine;

let engine = Engine::new();

let mut ast1 = engine.compile("
    fn foo(x) { 42 + x }
    foo(1)
")?;

let ast2 = engine.compile(r#"
    fn foo(n) { `hello${n}` }
    foo("!")
"#)?;

ast1.combine(ast2);    // Combine 'ast2' into 'ast1'

// Notice that using the '+=' operator also works:
// ast1 += ast2;

// 'ast1' is essentially:
//
//    fn foo(n) { `hello${n}` } // <- definition of first 'foo' is overwritten
//    foo(1)                    // <- notice this will be "hello1" instead of 43,
//                              //    but it is no longer the return value
//    foo("!")                  // returns "hello!"

// Evaluate it
assert_eq!(engine.eval_ast::<String>(&ast1)?, "hello!");
source

pub fn clear_statements(&mut self) -> &mut AST

Clear all statements in the AST, leaving only function definitions.

source

pub fn iter_literal_variables( &self, include_constants: bool, include_variables: bool ) -> impl Iterator<Item = (&str, bool, Dynamic)>

Extract all top-level literal constant and/or variable definitions. This is useful for extracting all global constants from a script without actually running it.

A literal constant/variable definition takes the form of: const VAR = value; and let VAR = value; where value is a literal expression or will be optimized into a literal.

§Example
use rhai::{Engine, Scope};

let engine = Engine::new();

let ast = engine.compile(
"
    const A = 40 + 2;   // constant that optimizes into a literal
    let b = 123;        // literal variable
    const B = b * A;    // non-literal constant
    const C = 999;      // literal constant
    b = A + C;          // expression

    {                   // <- new block scope
        const Z = 0;    // <- literal constant not at top-level

        print(Z);       // make sure the block is not optimized away
    }
")?;

let mut iter = ast.iter_literal_variables(true, false)
                  .map(|(name, is_const, value)| (name, is_const, value.as_int().unwrap()));

assert_eq!(iter.next(), Some(("A", true, 42)));
assert_eq!(iter.next(), Some(("C", true, 999)));
assert_eq!(iter.next(), None);

let mut iter = ast.iter_literal_variables(false, true)
                  .map(|(name, is_const, value)| (name, is_const, value.as_int().unwrap()));

assert_eq!(iter.next(), Some(("b", false, 123)));
assert_eq!(iter.next(), None);

let mut iter = ast.iter_literal_variables(true, true)
                  .map(|(name, is_const, value)| (name, is_const, value.as_int().unwrap()));

assert_eq!(iter.next(), Some(("A", true, 42)));
assert_eq!(iter.next(), Some(("b", false, 123)));
assert_eq!(iter.next(), Some(("C", true, 999)));
assert_eq!(iter.next(), None);

let scope: Scope = ast.iter_literal_variables(true, false).collect();

assert_eq!(scope.len(), 2);

Ok(())

Trait Implementations§

source§

impl<A> Add<A> for &AST
where A: AsRef<AST>,

§

type Output = AST

The resulting type after applying the + operator.
source§

fn add(self, rhs: A) -> <&AST as Add<A>>::Output

Performs the + operation. Read more
source§

impl<A> AddAssign<A> for AST
where A: Into<AST>,

source§

fn add_assign(&mut self, rhs: A)

Performs the += operation. Read more
source§

impl AsRef<[Stmt]> for AST

source§

fn as_ref(&self) -> &[Stmt]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Borrow<[Stmt]> for AST

source§

fn borrow(&self) -> &[Stmt]

Immutably borrows from an owned value. Read more
source§

impl Clone for AST

source§

fn clone(&self) -> AST

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for AST

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for AST

source§

fn default() -> AST

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for AST

§

impl Send for AST

§

impl Sync for AST

§

impl Unpin for AST

§

impl !UnwindSafe for AST

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.