#![no_std]
#[allow(unused_imports)]
#[macro_use]
extern crate proc_macro_hack_impl;
#[doc(hidden)]
pub use proc_macro_hack_impl::*;
#[macro_export]
macro_rules! proc_macro_expr_decl {
(#[$attr:meta] $($rest:tt)+) => {
proc_macro_expr_decl_helper!((#[$attr]) $($rest)+);
};
($name:ident ! => $name_impl:ident) => {
proc_macro_expr_decl_helper!(() $name ! => $name_impl);
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! proc_macro_expr_decl_helper {
(($($attrs:tt)*) #[$first:meta] $($rest:tt)+) => {
proc_macro_expr_decl_helper!(($($attrs)* #[$first]) $($rest)+);
};
(($($attrs:tt)*) $name:ident ! => $name_impl:ident) => {
#[derive(ProcMacroHackExpr)]
#[allow(unused, non_camel_case_types)]
$($attrs)*
enum $name {
$name_impl
}
};
(($($attrs:tt)*) $name:ident ! => $name_impl:ident #[$first:meta] $($rest:tt)+) => {
proc_macro_expr_decl_helper!(($($attrs)*) $name ! => $name_impl);
proc_macro_expr_decl_helper!((#[$first]) $($rest)+);
};
}
#[macro_export]
macro_rules! proc_macro_item_decl {
(#[$attr:meta] $($rest:tt)+) => {
proc_macro_item_decl_helper!((#[$attr]) $($rest)+);
};
($name:ident ! => $name_impl:ident) => {
proc_macro_item_decl_helper!(() $name ! => $name_impl);
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! proc_macro_item_decl_helper {
(($($attrs:tt)*) #[$first:meta] $($rest:tt)+) => {
proc_macro_item_decl_helper!(($($attrs)* #[$first]) $($rest)+);
};
(($($attrs:tt)*) $name:ident ! => $name_impl:ident) => {
#[derive(ProcMacroHackItem)]
#[allow(unused, non_camel_case_types)]
$($attrs)*
enum $name {
$name_impl
}
};
(($($attrs:tt)*) $name:ident ! => $name_impl:ident #[$first:meta] $($rest:tt)+) => {
proc_macro_item_decl_helper!(($($attrs)*) $name ! => $name_impl);
proc_macro_item_decl_helper!((#[$first]) $($rest)+);
};
}
#[macro_export]
macro_rules! proc_macro_expr_impl {
($(
$( #[$attr:meta] )*
pub fn $func:ident($input:ident: &str) -> String $body:block
)+) => {
$(
mod $func {
extern crate proc_macro;
pub use self::proc_macro::TokenStream;
}
// Parses an input that looks like:
//
// ```
// #[allow(unused)]
$( #[$attr] )*
#[proc_macro_derive($func)]
pub fn $func(input: $func::TokenStream) -> $func::TokenStream {
let source = input.to_string();
let mut tokens = source.trim();
for &prefix in &[
"#",
"[",
"allow",
"(",
"unused",
")",
"]",
"enum",
"ProcMacroHack",
"{",
"Input",
"=",
"(",
"stringify",
"!",
"(",
] {
assert!(tokens.starts_with(prefix));
tokens = &tokens[prefix.len()..].trim();
}
for &suffix in &[
"}",
",",
"1",
".",
")",
"0",
",",
")",
] {
if suffix == "," && !tokens.ends_with(suffix) {
continue;
}
assert!(tokens.ends_with(suffix));
tokens = &tokens[..tokens.len() - suffix.len()].trim();
}
fn func($input: &str) -> String $body
format!("
macro_rules! proc_macro_call {{
() => {{
{}
}}
}}
", func(tokens)).parse().unwrap()
}
)+
};
}
#[macro_export]
macro_rules! proc_macro_item_impl {
($(
$( #[$attr:meta] )*
pub fn $func:ident($input:ident: &str) -> String $body:block
)+) => {
$(
mod $func {
extern crate proc_macro;
pub use self::proc_macro::TokenStream;
}
// Parses an input that looks like:
//
// ```
// #[allow(unused)]
$( #[$attr] )*
#[proc_macro_derive($func)]
pub fn $func(input: $func::TokenStream) -> $func::TokenStream {
let source = input.to_string();
let mut tokens = source.trim();
for &prefix in &[
"#",
"[",
"allow",
"(",
"unused",
")",
"]",
"enum",
"ProcMacroHack",
"{",
"Input",
"=",
"(",
"stringify",
"!",
"(",
] {
assert!(tokens.starts_with(prefix));
tokens = &tokens[prefix.len()..].trim();
}
for &suffix in &[
"}",
",",
"1",
".",
")",
"0",
",",
")",
] {
if suffix == "," && !tokens.ends_with(suffix) {
continue;
}
assert!(tokens.ends_with(suffix));
tokens = &tokens[..tokens.len() - suffix.len()].trim();
}
fn func($input: &str) -> String $body
func(tokens).parse().unwrap()
}
)+
};
}