On Thu, Sep 19, 2013 at 12:05 PM, Paul Stansifer
<[email protected]>wrote:
> (note that you should wrap your second RHS in `{}` in order to avoid
> hitting the following bugs: https://github.com/mozilla/rust/issues/8012
> https://github.com/mozilla/rust/issues/4375)
>
> I had misunderstood your original question: now I see that, by "operate
> on", you meant that you wanted to run the macro parser on code that has
> already been parsed from tokens into AST.
>
Originally I *did* want to operate on AST, because I couldn't get macro
system to parse more than the first statement when I was writing my rules
like this:
( $head:stmt ; $($rest:*stmt*);+ ) => ( $head ; stmt_list!( $($rest);+
) );
Apparently interpolated statements cannot be re-parsed again?
So I figured that maybe I could let Rust parser build an AST for me, and
then tweak that. Hence my question about syntax extensions.
However, later I'd discovered your lambda calculus
interpreter<https://github.com/mozilla/rust/issues/3201>,
and decided to give built-in macros another go, this time using "tt" to
capture tails of statement lists.
> There isn't a mechanism to do this in the Rust macro system. However, you
> have discovered a potential workaround: capture the tokens as a token tree
> and pass them as an argument to a macro (which, after all, accepts token
> trees as arguments).
>
> The parse error is caused by the fact that (perhaps unintuitively), `tt`
> only matches a single token if it doesn't see a `(`, `[`, or `{`. I have
> not tested the following code (I fear it may run afoul of macro parser
> limitations), but this may work:
>
Is tt specifier documented somewhere? I am guessing it stands for "token
tree", but what is a token tree? How is it different from AST?
> macro_rules! stmt_list(
> ( while $cond:expr { $($body:tt)+ } ) =>
> ( while $cond { stmt_list!( $($body)+ ) } );
> ( $head:stmt ; $($rest:tt)+ ) =>
> ( $head ; stmt_list!( $($rest)+ ) );
> ( $head:stmt ) =>
> ( $head );
> )
>
That doesn't seem to work :-(
stmt_list! { let mut x = 0 ; while x < 10 { let y = x + 1 ; x = y } }
parse.rs:17:11: 17:15 error: unexpected token: `an interpolated statement`
parse.rs:17 ( $head ; stmt_list!( $($rest)+ ) );
^~~~
Vadim
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev