Solomon Ucko
Solomon Ucko

Reputation: 6109

Rust macro_rules nested repetition

In a macro_rules! transcriber, nested repetitions are not handled properly. This works fine:

macro_rules! demo_macro {
    ($op:tt; $($arg:tt),*) {
        ($($op * $arg),*)
    }
}

fn main() {
    println!("{?:}", demo_macro!(2; 1,2,3));
}

and outputs (2, 4, 6), but this does not compile:

macro_rules! demo_macro {
    ([$($op:tt)*] $($arg:tt),*) {
        ($($($op)* $arg),*)
    }
}

fn main() {
    println!("{?:}", demo_macro!([2*] 1,2,3));
}

and results in this error message:

error: meta-variable `op` repeats 2 times, but `arg` repeats 3 times
 --> src/main.rs:3:11
  |
3 |         ($($($op)* $arg),*)
  |           ^^^^^^^^^^^^^^

If I change 1,2,3 to 1,2 so the repetition counts match up, I get this error message:

error: attempted to repeat an expression containing no syntax variables matched as repeating at this depth
 --> src/main.rs:3:13
  |
3 |         ($($($op)* $arg),*)
  |             ^^^^^

Any idea how to make demo_macro!([2*] 1,2,3) result in (2, 4, 6)?

Upvotes: 6

Views: 3483

Answers (1)

Solomon Ucko
Solomon Ucko

Reputation: 6109

Yay, this works!

macro_rules! unbracket {
    (_ [$($tt1:tt)*]) => { $($tt1)* };
    (() [$($tt1:tt)*]) => { ($($tt1)*) };
    ([] [$($tt1:tt)*]) => { [$($tt1)*] };
    ({} [$($tt1:tt)*]) => { {$($tt1)*} };
    ($tt0:tt [$($tt1:tt)*] @unbracket ($($tt2:tt)*) $($tt3:tt)*) => { unbracket!{ $tt0 [$($tt1)* $($tt2)*] $($tt3)*} };
    ($tt0:tt [$($tt1:tt)*] @unbracket [$($tt2:tt)*] $($tt3:tt)*) => { unbracket!{ $tt0 [$($tt1)* $($tt2)*] $($tt3)*} };
    ($tt0:tt [$($tt1:tt)*] @unbracket {$($tt2:tt)*} $($tt3:tt)*) => { unbracket!{ $tt0 [$($tt1)* $($tt2)*] $($tt3)*} };
    ($tt0:tt [$($tt1:tt)*] $tt2:tt $($tt3:tt)*) => { unbracket!{$tt0 [$($tt1)* $tt2] $($tt3)*} };
}

macro_rules! demo_macro {
    ($op:tt $($arg:tt),*) => {
        unbracket!(() [] $(@unbracket $op $arg),*)
    }
}

fn main() {
    println!("{:?}", demo_macro!([2*] 1,2,3));
}

Upvotes: 6

Related Questions