2018-11-24 07:34:13 -06:00
|
|
|
// force-host
|
2017-03-17 18:41:09 -05:00
|
|
|
// no-prefer-dynamic
|
2016-08-04 14:20:01 -05:00
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
#![crate_type = "proc-macro"]
|
2018-07-05 20:09:35 -05:00
|
|
|
#![feature(proc_macro_hygiene)]
|
2016-08-04 14:20:01 -05:00
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
extern crate proc_macro;
|
2016-08-04 14:20:01 -05:00
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
use proc_macro::*;
|
2016-08-04 14:20:01 -05:00
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
#[proc_macro]
|
|
|
|
pub fn cond(input: TokenStream) -> TokenStream {
|
2017-01-17 21:27:09 -06:00
|
|
|
let mut conds = Vec::new();
|
2017-03-17 18:41:09 -05:00
|
|
|
let mut input = input.into_iter().peekable();
|
2017-01-17 21:27:09 -06:00
|
|
|
while let Some(tree) = input.next() {
|
2018-04-02 10:19:32 -05:00
|
|
|
let cond = match tree {
|
|
|
|
TokenTree::Group(tt) => tt.stream(),
|
2017-01-17 21:27:09 -06:00
|
|
|
_ => panic!("Invalid input"),
|
|
|
|
};
|
2017-03-17 18:41:09 -05:00
|
|
|
let mut cond_trees = cond.clone().into_iter();
|
|
|
|
let test = cond_trees.next().expect("Unexpected empty condition in `cond!`");
|
|
|
|
let rhs = cond_trees.collect::<TokenStream>();
|
2017-01-17 21:27:09 -06:00
|
|
|
if rhs.is_empty() {
|
|
|
|
panic!("Invalid macro usage in cond: {}", cond);
|
|
|
|
}
|
2018-04-02 10:19:32 -05:00
|
|
|
let is_else = match test {
|
2018-05-13 10:46:38 -05:00
|
|
|
TokenTree::Ident(ref word) => &*word.to_string() == "else",
|
2017-01-17 21:27:09 -06:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
conds.push(if is_else || input.peek().is_none() {
|
2017-03-14 17:04:46 -05:00
|
|
|
quote!({ $rhs })
|
2017-01-17 21:27:09 -06:00
|
|
|
} else {
|
2017-03-14 17:04:46 -05:00
|
|
|
quote!(if $test { $rhs } else)
|
2017-01-17 21:27:09 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
conds.into_iter().flat_map(|x| x.into_iter()).collect()
|
2016-08-04 14:20:01 -05:00
|
|
|
}
|