Proc macro generated code for parsing seems to work

This commit is contained in:
greg 2018-10-19 02:45:35 -07:00
parent 4ecf63c54d
commit 3b20b40eb7
1 changed files with 24 additions and 6 deletions

View File

@ -1,4 +1,5 @@
#![feature(box_patterns)]
#![recursion_limit="128"]
extern crate proc_macro;
#[macro_use]
extern crate quote;
@ -14,13 +15,30 @@ 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 ref ident = i.ident;
let new_block: syn::Block = parse_quote! {
{
let next_token = self.peek_with_token_offset();
let record = ParseRecord {
production_name: stringify!(#ident).to_string(),
next_token: format!("{}", next_token.to_string_with_metadata()),
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;
}
match result {
Err(ParseError { token: None, msg }) =>
Err(ParseError { token: Some(next_token), msg }),
_ => result
}
}
};
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
}