summaryrefslogtreecommitdiff
path: root/derive/src
diff options
context:
space:
mode:
Diffstat (limited to 'derive/src')
-rw-r--r--derive/src/lib.rs30
1 files changed, 14 insertions, 16 deletions
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 @@
1extern crate proc_macro; 1extern crate proc_macro;
2use proc_macro::{TokenStream,TokenTree,Ident,Delimiter}; 2use proc_macro::{TokenStream,TokenTree,Ident,Delimiter};
3 3
4use lookahead::LookAhead;
5
4#[proc_macro_derive(CrimtagMappable)] 6#[proc_macro_derive(CrimtagMappable)]
5pub fn derive_crimtag_mappable(s: TokenStream) -> TokenStream { 7pub fn derive_crimtag_mappable(s: TokenStream) -> TokenStream {
6 let mut i = s.into_iter(); 8 let mut i = LookAhead::<2,_,_>::new(s.into_iter());
7 while let Some(t) = i.next() { 9 while let Some(t) = i.next() {
8 if let TokenTree::Ident(ident) = t && 10 if let TokenTree::Ident(ident) = t &&
9 ident.to_string() == "struct" { 11 ident.to_string() == "struct" {
@@ -18,7 +20,7 @@ pub fn derive_crimtag_mappable(s: TokenStream) -> TokenStream {
18 20
19 // TODO: Expand this to include the generic parameters and the where? at 21 // TODO: Expand this to include the generic parameters and the where? at
20 // least the parameters. 22 // least the parameters.
21 println!("struct name: {:?}", name); 23 //println!("struct name: {:?}", name);
22 24
23 let mut newfunc = format!( 25 let mut newfunc = format!(
24r#"impl<'a> crimtag::MappedStructure<'a> for {} {{ 26r#"impl<'a> crimtag::MappedStructure<'a> for {} {{
@@ -34,24 +36,20 @@ r#"impl<'a> crimtag::MappedStructure<'a> for {} {{
34 if let TokenTree::Group(g) = &n && 36 if let TokenTree::Group(g) = &n &&
35 g.delimiter() == Delimiter::Brace { 37 g.delimiter() == Delimiter::Brace {
36 // Operate on the struct members. 38 // Operate on the struct members.
37 let mut gi = g.stream().into_iter(); 39 let mut gi = LookAhead::<2,_,_>::new(g.stream().into_iter());
38 let mut cur = 0usize;
39 let mut tok = [gi.next(), gi.next()];
40 loop { 40 loop {
41 if let Some(TokenTree::Punct(ch)) = &tok[(cur+1)%2] && 41 if let Some(TokenTree::Punct(ch)) = gi.peek(1) &&
42 ch.as_char() == ':' && 42 ch.as_char() == ':' &&
43 let Some(TokenTree::Ident(id)) = &tok[cur] { 43 let Some(TokenTree::Ident(id)) = gi.peek(0) {
44 println!("!!!!!!!!!! -> {}", id); 44 //println!("!!!!!!!!!! -> {}", id);
45 newfunc.push_str(&format!( 45 newfunc.push_str(&format!(
46r#" "{}" => Some(self.{}.into()), 46r#" "{}" => Some((&self.{}).into()),
47"#, id, id)); 47"#, id, id));
48 } 48 }
49 println!(" - {:?} {:?}", tok[cur], tok[(cur+1)%2] ); 49 //println!(" -> {:?} ", gi.peek(0));
50 //println!(" {:?}", gi.peek(1) );
50 // Update next. 51 // Update next.
51 if let Some(ni) = gi.next() { 52 if gi.next().is_none() {
52 tok[cur].replace( ni );
53 cur = (cur+1)%2;
54 } else {
55 break; 53 break;
56 } 54 }
57 } 55 }
@@ -63,13 +61,13 @@ r#" _ => None,
63 } 61 }
64 } 62 }
65}"#); 63}"#);
66 64/*
67 println!(); 65 println!();
68 println!(); 66 println!();
69 println!("{}", newfunc); 67 println!("{}", newfunc);
70 println!(); 68 println!();
71 println!(); 69 println!();
72 70*/
73 newfunc.parse().unwrap() 71 newfunc.parse().unwrap()
74} 72}
75 73