From 3b20b40eb70e80ffa8c77e5bf3e42ec0c9a72360 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 19 Oct 2018 02:45:35 -0700 Subject: [PATCH] Proc macro generated code for parsing seems to work --- schala-lang/codegen/src/lib.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/schala-lang/codegen/src/lib.rs b/schala-lang/codegen/src/lib.rs index 3c1a597..33a9c24 100644 --- a/schala-lang/codegen/src/lib.rs +++ b/schala-lang/codegen/src/lib.rs @@ -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 }