Struct rhai::AST [−][src]
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
impl AST
[src]
pub fn new(
statements: impl IntoIterator<Item = Stmt>,
functions: impl Into<Shared<Module>>
) -> Self
[src]
statements: impl IntoIterator<Item = Stmt>,
functions: impl Into<Shared<Module>>
) -> Self
Create a new AST
.
pub fn new_with_source(
statements: impl IntoIterator<Item = Stmt>,
functions: impl Into<Shared<Module>>,
source: impl Into<Identifier>
) -> Self
[src]
statements: impl IntoIterator<Item = Stmt>,
functions: impl Into<Shared<Module>>,
source: impl Into<Identifier>
) -> Self
Create a new AST
with a source name.
pub fn source(&self) -> Option<&str>
[src]
Get the source, if any.
pub fn set_source(&mut self, source: impl Into<Identifier>) -> &mut SelfⓘNotable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
Notable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
Set the source.
pub fn clear_source(&mut self) -> &mut SelfⓘNotable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
Notable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
Clear the source.
pub fn statements(&self) -> &[Stmt]ⓘ
[src]
this method is volatile and may change
(INTERNALS) Get the statements.
Exported under the internals
feature only.
pub fn shared_lib(&self) -> Shared<Module>
[src]
this method is volatile and may change
(INTERNALS) Get the internal shared Module
containing all script-defined functions.
Exported under the internals
feature only.
Not available under no_function
.
pub fn lib(&self) -> &Module
[src]
this method is volatile and may change
(INTERNALS) Get the internal Module
containing all script-defined functions.
Exported under the internals
feature only.
Not available under no_function
.
pub fn resolver(&self) -> Option<Shared<StaticModuleResolver>>
[src]
(INTERNALS) Get the embedded module resolver.
Exported under the internals
feature only.
pub fn clone_functions_only(&self) -> Self
[src]
Clone the AST
’s functions into a new AST
.
No statements are cloned.
Not available under no_function
.
This operation is cheap because functions are shared.
pub fn clone_functions_only_filtered(
&self,
filter: impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool
) -> Self
[src]
&self,
filter: impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool
) -> Self
Clone the AST
’s functions into a new AST
based on a filter predicate.
No statements are cloned.
Not available under no_function
.
This operation is cheap because functions are shared.
pub fn clone_statements_only(&self) -> Self
[src]
pub fn merge(&self, other: &Self) -> Self
[src]
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(r#" 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!");
pub fn combine(&mut self, other: Self) -> &mut SelfⓘNotable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
Notable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
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(r#" 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!");
pub fn merge_filtered(
&self,
other: &Self,
filter: impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool
) -> Self
[src]
&self,
other: &Self,
filter: impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool
) -> Self
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
are first selected based on a filter
predicate, then 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(r#" fn foo(x) { 42 + x } foo(1) "#)?; let ast2 = engine.compile(r#" fn foo(n) { "hello" + n } fn error() { 0 } foo("!") "#)?; // Merge 'ast2', picking only 'error()' but not 'foo(_)', into 'ast1' let ast = ast1.merge_filtered(&ast2, |_, _, script, name, params| script && name == "error" && params == 0); // 'ast' is essentially: // // fn foo(n) { 42 + n } // <- definition of 'ast1::foo' is not overwritten // // because 'ast2::foo' is filtered away // foo(1) // <- notice this will be 43 instead of "hello1", // // but it is no longer the return value // fn error() { 0 } // <- this function passes the filter and is merged // foo("!") // <- returns "42!" // Evaluate it assert_eq!(engine.eval_ast::<String>(&ast)?, "42!");
pub fn combine_filtered(
&mut self,
other: Self,
filter: impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool
) -> &mut SelfⓘNotable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
&mut self,
other: Self,
filter: impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool
) -> &mut Selfⓘ
Notable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
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
are first selected based on a filter
predicate, then 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(r#" fn foo(x) { 42 + x } foo(1) "#)?; let ast2 = engine.compile(r#" fn foo(n) { "hello" + n } fn error() { 0 } foo("!") "#)?; // Combine 'ast2', picking only 'error()' but not 'foo(_)', into 'ast1' ast1.combine_filtered(ast2, |_, _, script, name, params| script && name == "error" && params == 0); // 'ast1' is essentially: // // fn foo(n) { 42 + n } // <- definition of 'ast1::foo' is not overwritten // // because 'ast2::foo' is filtered away // foo(1) // <- notice this will be 43 instead of "hello1", // // but it is no longer the return value // fn error() { 0 } // <- this function passes the filter and is merged // foo("!") // <- returns "42!" // Evaluate it assert_eq!(engine.eval_ast::<String>(&ast1)?, "42!");
pub fn retain_functions(
&mut self,
filter: impl Fn(FnNamespace, FnAccess, &str, usize) -> bool
) -> &mut SelfⓘNotable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
&mut self,
filter: impl Fn(FnNamespace, FnAccess, &str, usize) -> bool
) -> &mut Selfⓘ
Notable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
Filter out the functions, retaining only some based on a filter predicate.
Not available under no_function
.
Example
use rhai::Engine; let engine = Engine::new(); let mut ast = engine.compile(r#" fn foo(n) { n + 1 } fn bar() { print("hello"); } "#)?; // Remove all functions except 'foo(_)' ast.retain_functions(|_, _, name, params| name == "foo" && params == 1);
pub fn iter_functions<'a>(
&'a self
) -> impl Iterator<Item = ScriptFnMetadata<'_>> + 'a
[src]
&'a self
) -> impl Iterator<Item = ScriptFnMetadata<'_>> + 'a
Iterate through all function definitions.
Not available under no_function
.
pub fn clear_functions(&mut self)
[src]
Clear all function definitions in the AST
.
Not available under no_function
.
pub fn clear_statements(&mut self)
[src]
Clear all statements in the AST
, leaving only function definitions.
pub fn walk(&self, on_node: &mut impl FnMut(&[ASTNode<'_>]) -> bool) -> bool
[src]
(INTERNALS) Recursively walk the AST
, including function bodies (if any).
Return false
from the callback to terminate the walk.
Exported under the internals
feature only.
Trait Implementations
impl<A: AsRef<AST>> Add<A> for &AST
[src]
type Output = AST
The resulting type after applying the +
operator.
fn add(self, rhs: A) -> Self::Output
[src]
impl<A: Into<AST>> AddAssign<A> for AST
[src]
fn add_assign(&mut self, rhs: A)
[src]
impl AsRef<[Stmt]> for AST
[src]
impl AsRef<Module> for AST
[src]
impl Clone for AST
[src]
impl Debug for AST
[src]
impl Default for AST
[src]
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
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow(&self) -> &TⓘNotable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
Notable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut TⓘNotable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
Notable traits for &'_ mut R
impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,