diff --git a/examples/app.rs b/examples/app.rs index c848e54255db9c7de5c55722ccf9f3c04bcd71b0..40eaccff4c4d6ebda223f7a947a7d00865c1aeb5 100644 --- a/examples/app.rs +++ b/examples/app.rs @@ -4,11 +4,37 @@ extern crate parsetest; use parsetest::app; app!{ - id1 , - id2 , - id2 , + device: ab::cc } fn main() { println!("here"); } + +// impl Synom for UnitType { +// named!(parse -> Self, do_parse!( +// which: syn!(StructOrEnum) >> +// name: syn!(Ident) >> +// item: switch!(value!(which), +// StructOrEnum::Struct(struct_token) => map!( +// punct!(;), +// |semi_token| UnitType::Struct { +// struct_token, +// name, +// semi_token, +// } +// ) +// | +// StructOrEnum::Enum(enum_token) => map!( +// braces!(syn!(Ident)), +// |(brace_token, variant)| UnitType::Enum { +// enum_token, +// name, +// brace_token, +// variant, +// } +// ) +// ) >> +// (item) +// )); +// } diff --git a/src/lib.rs b/src/lib.rs index 1e48cb17ee6d77285df370ace0ce14be0da277cc..ae80a87b3c80a2e25d84fe730aff9701324c70b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,8 +55,9 @@ pub fn lite(input: TokenStream) -> TokenStream { } } } +use syn::Path; -enum AppKeys { +enum AppKeyWords { Device, Resources, Interrupts, @@ -65,39 +66,73 @@ enum AppKeys { Init, } -fn to_app_keys(id: Ident) -> Option<AppKeys> { - match id.as_ref() { - "device" => Some(AppKeys::Device), - _ => None, - } +impl Synom for AppKeyWords { + named!(parse -> Self, do_parse!( + key: call!(Ident::parse) >> ( + match key.as_ref() { + "device" => AppKeyWords::Device, + "resources" => AppKeyWords::Resources, + _ => panic!("-- AppKeys") + } + ) + )); } -// struct KeyVal { +enum AppKeys { + Device(Path), + Resources(LitInt), + Interrupts, + Tasks, + Idle, + Init, +} -// } +impl Synom for AppKeys { + named!(parse -> Self, do_parse!( + which: syn!(AppKeyWords) >> + name: punct!(:) >> + item: switch!(value!(which), + AppKeyWords::Device => map!(syn!(Path), |path| AppKeys::Device(path)) + // | + // AppKeyWords::Resource => AppKeys::Tasks + | + _ => map!(syn!(Path), |path| AppKeys::Device(path)) + ) >> + (item) + )); +} -// impl Synom for KeyVal { -// named!(parse -> Self, do_parse!( -// val: syn!(Punctuadet) >> (KeyVal { }) +#[proc_macro] +pub fn app(input: TokenStream) -> TokenStream { + println!("-- app --"); + let k: AppKeys = syn::parse(input).unwrap(); -// field: syn!(Ident) >> -// _colon: punct!(:) >> -// + switch!(value!(field.as_ref()), -// "path" => map!(syn!(Path), |path| { -// + parsed_init.path = Some(path); -// + }) | -// + "resources" => map!(parse_resource_ident, |res| { -// + parsed_init.resources = Some(res); -// + }) | -// + _ => call!(|_| { -// + field.span.unstable().error(format!("Unknown field `{}`", field)).emit(); -// + panic!("Unknown field `{}`", field); -// + }) -// + ) >> -// + _comma: syn!(Token![,]) >> -// + (()) -// )); -// } + match k { + AppKeys::Device(dev) => println!("device {}", dev.into_tokens()), + AppKeys::Resources(res) => println!("resources {}", res.into_tokens()), + _ => println!("not matched"), + } + // for k in k.keyvals.into_iter() { + // println!("{:?}", k.key.as_ref()); + // println!("{:?}", k.val); + // } + + // { + // Ok(app) => { + // let tokens = quote!(); + // tokens.into() + // } + // Err(err) => { + // let desc = err.description(); + // let tokens = quote! { + // compile_error!(#desc) + // }; + // tokens.into() + // } + // } + let tokens = quote!(); + tokens.into() +} use syn::punctuated::Punctuated; @@ -125,7 +160,7 @@ impl Synom for KeyVals { } #[proc_macro] -pub fn app(input: TokenStream) -> TokenStream { +pub fn appa(input: TokenStream) -> TokenStream { println!("-- app --"); let k: KeyVals = syn::parse(input).unwrap(); for k in k.keyvals.into_iter() { @@ -149,6 +184,13 @@ pub fn app(input: TokenStream) -> TokenStream { tokens.into() } +// fn to_app_keys(id: Ident) -> Option<AppKeys> { +// match id.as_ref() { +// "device" => Some(AppKeys::Device), +// _ => None, +// } +// } +// https://github.com/japaric/rtfm-syntax/pull/3 // named!(parse -> LitInt, do_parse!( // val: syn!(LitInt) >> (val) // ));