macro_rules! foo {
    ( $( $( $tok:tt )* ,)* ) => {
        [ bar!( $( $tok )* ,)* ]
    }
}
Produces error: local ambiguity when calling macro 'foo'. Because the comma ',' is of type :tt. I would need a way to match all token trees excluding the coma token.
foo!(
    add t0 zero zero,
    add t1 t0 a0,
    lb t1 0(t1),
)
Should produce:
[
    bar!(add t0 zero zero),
    bar!(add t1 t0 a0),
    bar!(lb t1 0(t1)),
]
                TT munching to the rescue!
macro_rules! foo {
    (@accumulate [ $($accumulated:tt)* ] [ ]) => { [ $($accumulated)* ] };
    (@accumulate [ $($accumulated:tt)* ] [ $($this_line:tt)* ] , $($rest:tt)* ) => {
        foo! { @accumulate [
            $($accumulated)*
            bar!( $($this_line)* ),
        ] [] $($rest)* }
    };
    (@accumulate [ $($accumulated:tt)* ] [ $($this_line:tt)* ] $current:tt $($rest:tt)* ) => {
        foo! { @accumulate [ $($accumulated)* ] [ $($this_line)* $current ] $($rest)* }
    };
    ( $($t:tt)* ) => { foo! { @accumulate [] [] $($t)* } }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With