Escape special character of a macro?

Is that possible to escape special character of a macro to been able to put a macro in a macro?

macro_rules! closure
{
  () =>
  {
    macro_rules! macro1
    {
      ( $( $Arg : tt )* ) => { };
    }
  }
}

Currently it gives error:

error: attempted to repeat an expression containing no syntax variables matched as repeating at this depth
 --> src/bin/problem.rs:7:10
  |
7 |       ( $( $Arg : tt )* ) => { };
  |     

Playground

Not yet, but with #![feature(macro_metavar_expr)] you can use $$ to escape $, and that functionality might be stabilizing soon.

On stable, you can approximate the desired behavior by passing a $ to the macro and capturing/using it through a $dollar:tt binder.

4 Likes

Aha! Thanks. Could you please elaborate? Any sample?

E.g.

macro_rules! closure
{
  () => {
      closure!{#internal $}
  };
  (#internal $dollar:tt) =>
  {
    macro_rules! macro1
    {
      ( $dollar( $dollar Arg : tt )* ) => { };
    }
  };
}

closure!();

macro1!(x y z);

fn main() {}

or on nightly,

#![feature(macro_metavar_expr)]

macro_rules! closure
{
  () =>
  {
    macro_rules! macro1
    {
      ( $$( $$Arg : tt )* ) => { };
    }
  }
}

closure!();

macro1!(x y z);

fn main() {}
3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.