macro_rules! tt_call { { macro = [{ $($m:ident)::* }] $( $input:ident = [{ $($tokens:tt)* }] )* } => { ... }; { macro = [{ $($m:ident)::* }] $( $input:ident = [{ $($tokens:tt)* }] )* ~~> $($return:ident)::* } => { ... }; { macro = [{ $($m:ident)::* }] $( $input:ident = [{ $($tokens:tt)* }] )* ~~> $($return:ident)::* ! { $( $name:ident = [{ $($state:tt)* }] )* } } => { ... }; { macro = [{ $($m:ident)::* }] $( $input:ident = [{ $($tokens:tt)* }] )* ~~> $($return:ident)::* ! { $caller:tt $( $name:ident = [{ $($state:tt)* }] )* } } => { ... }; }
Expand description
Evaluate a tt-call macro and return its output to a given return destination.
§Input
The input must start with an argument called macro
which provides the name
of the macro for tt_call!
to invoke.
macro = [{
name of macro to call}]
After that there may be any number of key-value pairs to be passed as arguments to the macro being called.
$(
arbitrary key= [{
arbitrary tokens}]
)*
Finally a specification of the macro invocation to which this call should return its output.
~~>
name of return destination macro! {
arbitrary tokens
}
§Examples
use tt_call::{tt_call, tt_is_ident};
macro_rules! print_is_ident {
{
token = [{ $token:tt }]
is_ident = [{ true }]
} => {
println!("turns out `{}` is an ident", stringify!($token));
};
{
token = [{ $token:tt }]
is_ident = [{ false }]
} => {
println!("nope, `{}` is not an ident", stringify!($token));
};
}
fn main() {
tt_call! {
macro = [{ tt_is_ident }]
input = [{ foo }]
~~> print_is_ident! {
token = [{ foo }]
}
}
}
If the invoked macro provides the entirety of the input to the return
destination macro, then the !
and argument list may be omitted.
use tt_call::{tt_call, tt_is_ident};
macro_rules! print_is_ident {
{
is_ident = [{ true }]
} => {
println!("that token is an ident");
};
{
is_ident = [{ false }]
} => {
println!("nope, not an ident");
};
}
fn main() {
tt_call! {
macro = [{ tt_is_ident }]
input = [{ foo }]
~~> print_is_ident
}
}
And if the invoked macro produces exactly one output value and we just want to expand to that output value, the destination macro may be omitted entirely.
use tt_call::{tt_call, tt_is_ident};
fn main() {
let is_ident = tt_call! {
macro = [{ tt_is_ident }]
input = [{ foo }]
};
println!("{}", is_ident); // prints true or false
}