I'm working on a DSL implemented as a proc macro, and would like to be able to get to the original source, as a string, of the contents of the proc macro.
So for instance, if my proc macro is invoked like so:
my_macro! {
one
two
three
}
I would like to get access to this string inside the proc macro implementation:
let source: String = get_source(input) // this is what I want to be able to do
println!("{}", source);
// prints: "one\ntwo\nthree"
I can't figure out how to do this. When I try to convert the token stream to a string, the whitespace information is lost.
So for instance if I try to convert the token stream directly to a string, each token is simply separated by a space:
#[proc_macro]
pub fn my_macro(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
println!("{}", input);
// prints "one two three"
...
How can I get the input source?