Skip to content
Snippets Groups Projects
Commit 3a52eec7 authored by Per's avatar Per
Browse files

keyval (not the right way yet)

parent 504d378a
No related branches found
No related tags found
No related merge requests found
...@@ -50,6 +50,18 @@ ...@@ -50,6 +50,18 @@
"kind": "build", "kind": "build",
"isDefault": true "isDefault": true
} }
},
{
"type": "shell",
"label": "cargo run --example app",
"command": "cargo run --example app",
"problemMatcher": [
"$rustc"
],
"group": {
"kind": "build",
"isDefault": true
} }
},
] ]
} }
\ No newline at end of file
#![feature(proc_macro)]
extern crate parsetest;
use parsetest::app;
app!{
id1, id2
}
fn main() {
println!("here");
}
...@@ -9,7 +9,7 @@ extern crate syn; ...@@ -9,7 +9,7 @@ extern crate syn;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use syn::spanned::Spanned; use syn::spanned::Spanned;
use syn::synom::Synom; use syn::synom::Synom;
use syn::LitInt; use syn::{Ident, LitInt};
use quote::ToTokens; use quote::ToTokens;
use std::convert::From; use std::convert::From;
...@@ -24,7 +24,10 @@ pub fn lit(input: TokenStream) -> TokenStream { ...@@ -24,7 +24,10 @@ pub fn lit(input: TokenStream) -> TokenStream {
if !(10 <= value && value < 100) { if !(10 <= value && value < 100) {
v.span() v.span()
.unstable() .unstable()
.error(format!("expected literal 10 <= x < 100, got {}", value,)) .error(format!(
"expected literal 10 <= x < 100, got {}",
value,
))
.emit(); .emit();
} }
From::from(v.into_tokens()) From::from(v.into_tokens())
...@@ -40,7 +43,10 @@ pub fn lite(input: TokenStream) -> TokenStream { ...@@ -40,7 +43,10 @@ pub fn lite(input: TokenStream) -> TokenStream {
if !(10 <= value && value < 100) { if !(10 <= value && value < 100) {
v.span() v.span()
.unstable() .unstable()
.error(format!("expected literal 10 <= x < 100, got {}", value,)) .error(format!(
"expected literal 10 <= x < 100, got {}",
value,
))
.emit(); .emit();
} }
From::from(v.into_tokens()) From::from(v.into_tokens())
...@@ -55,6 +61,89 @@ pub fn lite(input: TokenStream) -> TokenStream { ...@@ -55,6 +61,89 @@ pub fn lite(input: TokenStream) -> TokenStream {
} }
} }
use syn::Path;
enum AppKeys {
Device,
Resources,
Interrupts,
Tasks,
Idle,
Init,
}
fn to_app_keys(id: Ident) -> Option<AppKeys> {
match id.as_ref() {
"device" => Some(AppKeys::Device),
_ => None,
}
}
// struct KeyVal {
// }
// impl Synom for KeyVal {
// named!(parse -> Self, do_parse!(
// val: syn!(Punctuadet) >> (KeyVal { })
// 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![,]) >>
// + (())
// ));
// }
use syn::punctuated::Punctuated;
struct AppK {}
struct KeyVal {
kv: Punctuated<Ident, Token![,]>,
}
impl Synom for KeyVal {
named!(parse -> Self, do_parse!(
kv: call!(Punctuated::parse_terminated_nonempty) >>
(KeyVal { kv })
));
}
#[proc_macro]
pub fn app(input: TokenStream) -> TokenStream {
println!("-- app --");
let k: KeyVal = syn::parse(input).unwrap();
for k in k.kv.into_iter() {
println!("{:?}", k.as_ref());
}
// {
// 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()
}
// named!(parse -> LitInt, do_parse!( // named!(parse -> LitInt, do_parse!(
// val: syn!(LitInt) >> (val) // val: syn!(LitInt) >> (val)
// )); // ));
...@@ -85,30 +174,33 @@ pub fn lite(input: TokenStream) -> TokenStream { ...@@ -85,30 +174,33 @@ pub fn lite(input: TokenStream) -> TokenStream {
// } // }
// } // }
/// MyLit // /// MyLit
struct MyLit { // struct MyLit {
val: LitInt, // val: LitInt,
} // }
impl Synom for MyLit { // impl Synom for MyLit {
named!(parse -> Self, do_parse!( // named!(parse -> Self, do_parse!(
val: syn!(LitInt) >> (MyLit { val }) // val: syn!(LitInt) >> (MyLit { val })
)); // ));
} // }
#[proc_macro] // #[proc_macro]
pub fn mylit(input: TokenStream) -> TokenStream { // pub fn mylit(input: TokenStream) -> TokenStream {
let v: MyLit = syn::parse(input).unwrap(); // let v: MyLit = syn::parse(input).unwrap();
let value = v.val.value(); // let value = v.val.value();
if !(10 <= value && value < 100) { // if !(10 <= value && value < 100) {
v.val // v.val
.span() // .span()
.unstable() // .unstable()
.error(format!("expected literal 10 <= x < 100, got {}", value,)) // .error(format!(
.emit(); // "expected literal 10 <= x < 100, got {}",
} // value,
From::from(v.val.into_tokens()) // ))
} // .emit();
// }
// From::from(v.val.into_tokens())
// }
// /// MyLitP // /// MyLitP
// struct MyLitP { // struct MyLitP {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment