From 1553b785cbc75bfc47066096b81d0066017a91ad Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 30 Jun 2026 13:21:38 -0700 Subject: Added LookAhead, fixed up the proc-macro. LookAhead should make the rest of the parser and lexer stuff way easier, probably spin that out into it's own lib later on. --- derive/src/lib.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'derive/src') diff --git a/derive/src/lib.rs b/derive/src/lib.rs index f9c9180..bb299d0 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -1,9 +1,11 @@ extern crate proc_macro; use proc_macro::{TokenStream,TokenTree,Ident,Delimiter}; +use lookahead::LookAhead; + #[proc_macro_derive(CrimtagMappable)] pub fn derive_crimtag_mappable(s: TokenStream) -> TokenStream { - let mut i = s.into_iter(); + let mut i = LookAhead::<2,_,_>::new(s.into_iter()); while let Some(t) = i.next() { if let TokenTree::Ident(ident) = t && ident.to_string() == "struct" { @@ -18,7 +20,7 @@ pub fn derive_crimtag_mappable(s: TokenStream) -> TokenStream { // TODO: Expand this to include the generic parameters and the where? at // least the parameters. - println!("struct name: {:?}", name); + //println!("struct name: {:?}", name); let mut newfunc = format!( r#"impl<'a> crimtag::MappedStructure<'a> for {} {{ @@ -34,24 +36,20 @@ r#"impl<'a> crimtag::MappedStructure<'a> for {} {{ if let TokenTree::Group(g) = &n && g.delimiter() == Delimiter::Brace { // Operate on the struct members. - let mut gi = g.stream().into_iter(); - let mut cur = 0usize; - let mut tok = [gi.next(), gi.next()]; + let mut gi = LookAhead::<2,_,_>::new(g.stream().into_iter()); loop { - if let Some(TokenTree::Punct(ch)) = &tok[(cur+1)%2] && + if let Some(TokenTree::Punct(ch)) = gi.peek(1) && ch.as_char() == ':' && - let Some(TokenTree::Ident(id)) = &tok[cur] { - println!("!!!!!!!!!! -> {}", id); + let Some(TokenTree::Ident(id)) = gi.peek(0) { + //println!("!!!!!!!!!! -> {}", id); newfunc.push_str(&format!( -r#" "{}" => Some(self.{}.into()), +r#" "{}" => Some((&self.{}).into()), "#, id, id)); } - println!(" - {:?} {:?}", tok[cur], tok[(cur+1)%2] ); + //println!(" -> {:?} ", gi.peek(0)); + //println!(" {:?}", gi.peek(1) ); // Update next. - if let Some(ni) = gi.next() { - tok[cur].replace( ni ); - cur = (cur+1)%2; - } else { + if gi.next().is_none() { break; } } @@ -63,13 +61,13 @@ r#" _ => None, } } }"#); - +/* println!(); println!(); println!("{}", newfunc); println!(); println!(); - +*/ newfunc.parse().unwrap() } -- cgit v1.2.3