7

I have created a function-like procedural macro. I call it with a declarative macro as parameter. I was hoping to find a way that the declarative macro is expanded before processing the procedural macro (or expand it inside the procedural macro). Example (code is also available at https://github.com/Jasperav/rec_macro):

Call site:

macro_rules! t {
    ($e:expr) => {
        $e.to_string() + " I am a macro"
    }
}

fn main() {
    re!(t!("hi"));
}

Proc macro crate:

#[proc_macro_hack]
pub fn re(input: TokenStream) -> TokenStream {
    panic!("{:#?}", input) // I can see "hi" and something like t!, but not the result of the macro expansion
}

Inside the panic message, I see see that the declarative macro (t) is inside. I just want the raw value of hi I am a macro in this case (or some syn type). In my case, the declarative macro should always expand to a &str or String.

Is it possible to expand t before invoking re, or to expand t manually inside the proc macro? I want to see the result of the macro (in this case: "hi I am a macro") inside my proc functional like macro.

Thanks.

J. Doe
  • 12,159
  • 9
  • 60
  • 114
  • I'm not sure if you're having issues with the panic message only, or with the expansion -- there is a difference between debug-formatting a `TokenStream`, which stringizes it immediately, and returning it from the macro, which *may or may not* allow it to be further expanded -- I've never tried it myself, but macro invocations are usually expanded outside-in, recursively. Does your proc macro actually expand to something containing the `t!` macro invocation or are you just asking about the `panic!` message? – trent Apr 25 '20 at 21:57
  • @trentcl I updated the question and repo. I want to see the result of the macro expansion in my proc macro implementation, which is any input + " I am a macro", but I don't know if it is possible. – J. Doe Apr 26 '20 at 01:41
  • Have you tried recursive macro calls? as in having `t` call itself? – Teymour Apr 28 '20 at 08:41

0 Answers0