4ecf63c54d
At the cost of breaking code
37 lines
971 B
Rust
37 lines
971 B
Rust
#![feature(box_patterns)]
|
|
extern crate proc_macro;
|
|
#[macro_use]
|
|
extern crate quote;
|
|
#[macro_use]
|
|
extern crate syn;
|
|
|
|
use self::proc_macro::TokenStream;
|
|
use self::syn::fold::Fold;
|
|
|
|
struct RecursiveDescentFn {
|
|
}
|
|
|
|
impl Fold for RecursiveDescentFn {
|
|
fn fold_item_fn(&mut self, mut i: syn::ItemFn) -> syn::ItemFn {
|
|
let box block = i.block;
|
|
let mut stmts = block.stmts;
|
|
let new_item: syn::Stmt = parse_quote! {
|
|
println!("FROM PROC MACRO");
|
|
};
|
|
let mut new_stmts = vec![new_item];
|
|
new_stmts.append(&mut stmts);
|
|
let new_block = syn::Block { brace_token: block.brace_token, stmts: new_stmts };
|
|
i.block = Box::new(new_block);
|
|
i
|
|
}
|
|
}
|
|
|
|
#[proc_macro_attribute]
|
|
pub fn recursive_descent_method(attr: TokenStream, item: TokenStream) -> TokenStream {
|
|
|
|
let input: syn::ItemFn = parse_macro_input!(item as syn::ItemFn);
|
|
let mut folder = RecursiveDescentFn {};
|
|
let output = folder.fold_item_fn(input);
|
|
TokenStream::from(quote!(#output))
|
|
}
|