2018-10-19 02:36:23 -07:00
|
|
|
#![feature(box_patterns)]
|
2018-10-19 02:45:35 -07:00
|
|
|
#![recursion_limit="128"]
|
2018-10-17 15:29:32 -07:00
|
|
|
extern crate proc_macro;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate quote;
|
2018-10-19 02:36:23 -07:00
|
|
|
#[macro_use]
|
2018-10-17 15:29:32 -07:00
|
|
|
extern crate syn;
|
|
|
|
|
|
|
|
use self::proc_macro::TokenStream;
|
2018-10-19 02:36:23 -07:00
|
|
|
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;
|
2021-10-19 22:29:41 -07:00
|
|
|
let ident = &i.ident;
|
2018-10-19 02:45:35 -07:00
|
|
|
|
|
|
|
let new_block: syn::Block = parse_quote! {
|
|
|
|
{
|
2019-01-05 18:38:18 -08:00
|
|
|
let next_token_before_parse = self.token_handler.peek();
|
2018-10-19 02:45:35 -07:00
|
|
|
let record = ParseRecord {
|
|
|
|
production_name: stringify!(#ident).to_string(),
|
2018-11-20 03:03:08 -08:00
|
|
|
next_token: format!("{}", next_token_before_parse.to_string_with_metadata()),
|
2018-10-19 02:45:35 -07:00
|
|
|
level: self.parse_level,
|
|
|
|
};
|
|
|
|
self.parse_level += 1;
|
|
|
|
self.parse_record.push(record);
|
|
|
|
let result = { #block };
|
|
|
|
|
|
|
|
if self.parse_level != 0 {
|
|
|
|
self.parse_level -= 1;
|
|
|
|
}
|
2019-10-23 00:48:59 -07:00
|
|
|
|
2019-10-01 21:40:30 -07:00
|
|
|
result.map_err(|mut parse_error: ParseError| {
|
|
|
|
parse_error.production_name = Some(stringify!(#ident).to_string());
|
|
|
|
parse_error
|
|
|
|
})
|
2018-10-19 02:45:35 -07:00
|
|
|
}
|
2018-10-19 02:36:23 -07:00
|
|
|
};
|
|
|
|
i.block = Box::new(new_block);
|
|
|
|
i
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 15:29:32 -07:00
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
2018-10-20 00:55:37 -07:00
|
|
|
pub fn recursive_descent_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
2018-10-19 02:36:23 -07:00
|
|
|
|
|
|
|
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))
|
2018-10-17 15:29:32 -07:00
|
|
|
}
|