procmacro2 example
parent
55a1fa90b1
commit
eba689e28f
@ -0,0 +1,9 @@
|
||||
#![feature(proc_macro_non_items)]
|
||||
#![feature(use_extern_macros)]
|
||||
extern crate procmacro2;
|
||||
|
||||
fn main() {
|
||||
procmacro2::misc_syntax!(
|
||||
where while abcd : u64 >> 1 + 2 * 3; where T: 'x + A<B='y+C+D>;[M];A::f
|
||||
);
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
Cargo.lock
|
||||
target
|
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "procmacro2"
|
||||
version = "1.0.0"
|
||||
|
||||
[dependencies]
|
||||
syn = "0.12"
|
||||
quote = "0.4"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
@ -0,0 +1,48 @@
|
||||
#![feature(proc_macro)]
|
||||
#![crate_type = "proc-macro"]
|
||||
extern crate proc_macro;
|
||||
#[macro_use] extern crate syn;
|
||||
#[macro_use] extern crate quote;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use syn::{Ident, Type, Expr, WhereClause, TypeSlice, Path};
|
||||
use syn::synom::Synom;
|
||||
|
||||
struct MiscSyntax {
|
||||
id: Ident,
|
||||
ty: Type,
|
||||
expr: Expr,
|
||||
where_clause: WhereClause,
|
||||
type_slice: TypeSlice,
|
||||
path: Path
|
||||
}
|
||||
|
||||
impl Synom for MiscSyntax {
|
||||
named!(parse -> Self, do_parse!(
|
||||
keyword!(where) >>
|
||||
keyword!(while) >>
|
||||
id: syn!(Ident) >>
|
||||
punct!(:) >>
|
||||
ty: syn!(Type) >>
|
||||
punct!(>>) >>
|
||||
expr: syn!(Expr) >>
|
||||
punct!(;) >>
|
||||
where_clause: syn!(WhereClause) >>
|
||||
punct!(;) >>
|
||||
type_slice: syn!(TypeSlice) >>
|
||||
punct!(;) >>
|
||||
path: syn!(Path) >>
|
||||
(MiscSyntax { id, ty, expr, where_clause, type_slice, path })
|
||||
));
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn misc_syntax(input: TokenStream) -> TokenStream {
|
||||
let m: MiscSyntax = syn::parse(input).expect("expected Miscellaneous Syntax");
|
||||
let MiscSyntax { id, ty, expr, where_clause, type_slice, path } = m;
|
||||
|
||||
(quote! {
|
||||
let #id: #ty = #expr;
|
||||
println!("variable = {}", #id);
|
||||
}).into()
|
||||
}
|
Loading…
Reference in New Issue