title: How do I debug macros?
body: <p>So I've got the following macro code I'm trying to debug. I've taken it from the <a href="http://doc.rust-lang.org/book/macros.html" rel="noreferrer" title="The Rust Programming Language Book">Rust Book</a> under the section "The deep end". I renamed the variables within the macro to more closely follow <a href="http://esolangs.org/wiki/Bitwise_Cyclic_Tag" rel="noreferrer" title="Bitwise Cyclic Tag">this</a> post.</p>
  
  <p>My goal is to have the program print out each line of the BCT program. I'm well aware that this is very compiler heavy.</p>
  
  <p>The only error rustc is giving me is:</p>
  
  <pre><code>user@debian:~/rust/macros$ rustc --pretty expanded src/main.rs -Z unstable-options &gt; src/main.precomp.rs
  src/main.rs:151:34: 151:35 error: no rules expected the token `0`
  src/main.rs:151     bct!(0, 1, 1, 1, 0, 0, 0; 1, 0);
  </code></pre>
  
  <p>What steps can I take to figure out <strong><em>where</em></strong> in the macro the problem is coming from?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>fn main() {
  {
      // "Bitwise Cyclic Tag" automation through macros
      macro_rules! bct {
          // cmd 0:  0 ... =&gt; ...
          (0, $($program:tt),* ; $_head:tt)
              =&gt; (bct_p!($($program),*, 0 ; ));
          (0, $($program:tt),* ; $_head:tt, $($tail:tt),*)
              =&gt; (bct_p!($($program),*, 0 ; $($tail),*));
  
          // cmd 1x:  1 ... =&gt; 1 ... x
          (1, $x:tt, $($program:tt),* ; 1)
              =&gt; (bct_p!($($program),*, 1, $x ; 1, $x));
          (1, $x:tt, $($program:tt),* ; 1, $($tail:tt),*)
              =&gt; (bct_p!($($program),*, 1, $x ; 1, $($tail),*, $x));
  
          // cmd 1x:  0 ... =&gt; 0 ...
          (1, $x:tt, $($program:tt),* ; $($tail:tt),*)
              =&gt; (bct_p!($($program),*, 1, $x ; $($tail),*));
  
          // halt on empty data string
          ( $($program:tt),* ; )
              =&gt; (());
          }
  
      macro_rules! print_bct {
          ($x:tt ; )
              =&gt; (print!("{}", stringify!($x)));
          ( ; $d:tt)
              =&gt; (print!("{}", stringify!($d)));
          ($x:tt, $($program:tt),* ; )
              =&gt; {
                  print!("{}", stringify!($x));
                  print_bct!($program ;);
              };
          ($x:tt, $($program:tt),* ; $($data:tt),*)
              =&gt; {
                  print!("{}", stringify!($x));
                  print_bct!($program ; $data);
              };
          ( ; $d:tt, $($data:tt),*)
              =&gt; {
                  print!("{}", stringify!($d));
                  print_bct!( ; $data);
              };
      }
  
      macro_rules! bct_p {
          ($($program:tt),* ; )
              =&gt; {
                  print_bct!($($program:tt),* ; );
                  println!("");
                  bct!($($program),* ; );
              };
          ($($program:tt),* ; $(data:tt),*)
              =&gt; {
                  print_bct!($($program),* ; $($data),*);
                  println!("");
                  bct!($($program),* ; $($data),*);
              };
      }
  
      // the compiler is going to hate me...
      bct!(0, 1, 1, 1, 0, 0, 0; 1, 0);
  }            
  </code></pre>