diff --git a/Chapter09/Cargo.toml b/Chapter09/Cargo.toml index 1c59afa..9ac979d 100644 --- a/Chapter09/Cargo.toml +++ b/Chapter09/Cargo.toml @@ -7,6 +7,7 @@ flame = "0.2" rand = "0.4.2" requests = "0.0.30" rusty-machine = "0.5.4" +procmacro = { path = "procmacro" } [[bin]] name = "performance_release_mode" @@ -91,3 +92,7 @@ path = "metaprogramming_grammar.rs" [[bin]] name = "metaprogramming_ebnf" path = "metaprogramming_ebnf.rs" + +[[bin]] +name = "metaprogramming_procmacro" +path = "metaprogramming_procmacro.rs" diff --git a/Chapter09/metaprogramming_procmacro.rs b/Chapter09/metaprogramming_procmacro.rs new file mode 100644 index 0000000..f5454ed --- /dev/null +++ b/Chapter09/metaprogramming_procmacro.rs @@ -0,0 +1,7 @@ +#![feature(proc_macro_non_items)] +#![feature(use_extern_macros)] +extern crate procmacro; + +fn main() { + let _ = procmacro::f!(); +} diff --git a/Chapter09/procmacro/.gitignore b/Chapter09/procmacro/.gitignore new file mode 100644 index 0000000..fa8d85a --- /dev/null +++ b/Chapter09/procmacro/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +target diff --git a/Chapter09/procmacro/Cargo.toml b/Chapter09/procmacro/Cargo.toml new file mode 100644 index 0000000..486370b --- /dev/null +++ b/Chapter09/procmacro/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "procmacro" +version = "1.0.0" + +[dependencies] +syn = "0.12" +quote = "0.4" + +[lib] +proc-macro = true diff --git a/Chapter09/procmacro/src/lib.rs b/Chapter09/procmacro/src/lib.rs new file mode 100644 index 0000000..50798af --- /dev/null +++ b/Chapter09/procmacro/src/lib.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro)] +#![crate_type = "proc-macro"] +extern crate proc_macro; +extern crate syn; +#[macro_use] extern crate quote; +use proc_macro::TokenStream; + +#[proc_macro] +pub fn f(input: TokenStream) -> TokenStream { + assert!(input.is_empty()); + + (quote! { + 1 + 2 + }).into() +}