1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_derive(ProcMacroHackExpr)]
pub fn hack_expr(input: TokenStream) -> TokenStream {
    let (name, name_impl) = names(input);

    let rules = format!("
        #[macro_export]
        macro_rules! {} {{
            ($($tt:tt)*) => {{{{
                #[derive({})]
                #[allow(unused)]
                enum ProcMacroHack {{
                    Input = (stringify!($($tt)*), 0).1
                }}

                proc_macro_call!()
            }}}}
        }}
    ", name, name_impl);

    rules.parse().unwrap()
}

#[proc_macro_derive(ProcMacroHackItem)]
pub fn hack_item(input: TokenStream) -> TokenStream {
    let (name, name_impl) = names(input);

    let rules = format!("
        #[macro_export]
        macro_rules! {} {{
            ($($tt:tt)*) => {{
                #[derive({})]
                #[allow(unused)]
                enum ProcMacroHack {{
                    Input = (stringify!($($tt)*), 0).1
                }}
            }}
        }}
    ", name, name_impl);

    rules.parse().unwrap()
}

fn names(input: TokenStream) -> (String, String) {
    let source = input.to_string();

    let mut words = source.split_whitespace();
    assert_eq!(Some("#[allow(unused,"), words.next());
    assert_eq!(Some("non_camel_case_types)]"), words.next());
    assert_eq!(Some("enum"), words.next());
    let name = words.next().unwrap();
    assert_eq!(Some("{"), words.next());
    let name_impl = words.next().unwrap();
    assert_eq!(Some("}"), words.next());
    assert_eq!(None, words.next());

    (name.to_owned(), name_impl.to_owned())
}