I'd love to be able to use just macros, however so far my attempts met with
little success.
Here's what I'm trying to do (right now expansion just echoes the input):

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 );
)

fn main()
{
    trace_macros!(true);

    stmt_list!(
        let mut x = 0;
        while x < 10 {
            let y = x + 1;
            x = y
        }
    )
}

C:\rust\test>rustc --pretty expanded parse.rs
stmt_list! { let mut x = 0 ; while x < 10 { let y = x + 1 ; x = y } }
parse.rs:18:14: 18:15 error: No rules expected the token: x
parse.rs:18         while x < 10 {
                          ^

Am I totally off-base trying to do this with macros?   Do I need a syntax
extension?

Vadim


On Wed, Sep 18, 2013 at 8:06 PM, Paul Stansifer <[email protected]>wrote:

> What you describe is the status quo for syntax extensions and macros.
>
> The standard way for a syntax extension (built into the Rust compiler, not
> a macro) to operate is to use the macro parser (which in turn uses the Rust
> parser) to turn token trees into AST nodes. See libsyntax/ext/ for
> implementations of existing syntax extensions.
>
> If you want to write a macro, on the other hand, which is sufficient for
> most needs, then you don't need to do anything, because this is what macros
> already do -- as soon as they parse, say, `$s1:stmt`, `s1` is a statement
> AST node, ready for interpolation.
>
> Does this help?
>
> Paul
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to