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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;

mod aspect;
mod components;
mod services;
mod system;
mod systems;

#[proc_macro_derive(Aspect, attributes(aspect, components))]
pub fn derive_aspect(input: TokenStream) -> TokenStream {
    // Construct a string representation of the type definition
    let s = input.to_string();

    // Parse the string representation
    let ast = syn::parse_derive_input(&s).unwrap();

    // Build the impl
    let gen = aspect::impl_aspect(ast);

    // Return the generated impl
    let result = gen.parse().unwrap();

    //panic!("{}", result);

    result
}

#[proc_macro_derive(ComponentManager, attributes(hot, cold))]
pub fn derive_components(input: TokenStream) -> TokenStream {
    // Construct a string representation of the type definition
    let s = input.to_string();

    // Parse the string representation
    let ast = syn::parse_derive_input(&s).unwrap();

    // Build the impl
    let gen = components::impl_components(ast);

    // Return the generated impl
    gen.parse().unwrap()
}

#[proc_macro_derive(ServiceManager)]
pub fn derive_services(input: TokenStream) -> TokenStream {
    // Construct a string representation of the type definition
    let s = input.to_string();

    // Parse the string representation
    let ast = syn::parse_derive_input(&s).unwrap();

    // Build the impl
    let gen = services::impl_services(ast);

    // Return the generated impl
    gen.parse().unwrap()
}

#[proc_macro_derive(System,
                    attributes(data, system_type, process, aspect, aspect_a, aspect_b,
                                 interval, timed_interval, activated, reactivated, deactivated))]
pub fn derive_system(input: TokenStream) -> TokenStream {
    // Construct a string representation of the type definition
    let s = input.to_string();

    // Parse the string representation
    let ast = syn::parse_derive_input(&s).unwrap();

    // Build the impl
    let gen = system::impl_system(ast);

    // Return the generated impl
    gen.parse().unwrap()
}

#[proc_macro_derive(SystemManager, attributes(data, passive))]
pub fn derive_systems(input: TokenStream) -> TokenStream {
    // Construct a string representation of the type definition
    let s = input.to_string();

    // Parse the string representation
    let ast = syn::parse_derive_input(&s).unwrap();

    // Build the impl
    let gen = systems::impl_systems(ast);

    // Return the generated impl
    gen.parse().unwrap()
}

fn improper_attr_format(attr: &str, module: &str) -> ! {
    panic!(
        "{} was not in the correct format. Please refer to the {} \
         module documentation for more information.",
        attr,
        module
    )
}

fn read_path_item<F>(attr: &syn::MetaItem, fail: F) -> String
where
    F: FnOnce(),
{
    match attr {
        &syn::MetaItem::Word(ref word) => word.to_string(),
        &syn::MetaItem::List(_, ref items) => {
            if items.len() != 1 {
                fail();
                unreachable!();
            }

            match items[0] {
                syn::NestedMetaItem::Literal(syn::Lit::Str(ref value, _)) => value.clone(),
                syn::NestedMetaItem::MetaItem(syn::MetaItem::Word(ref word)) => word.to_string(),
                _ => {
                    fail();
                    unreachable!();
                }
            }
        }
        &syn::MetaItem::NameValue(_, syn::Lit::Str(ref value, _)) => value.clone(),
        _ => {
            fail();
            unreachable!();
        }
    }
}

fn quote_path(path: &str) -> quote::Tokens {
    let mut tokens = quote::Tokens::new();
    for (i, part) in path.split("::").enumerate() {
        if i != 0 {
            tokens.append("::");
        }
        tokens.append(part);
    }
    tokens
}