1
use proc_macro::TokenStream;
2

            
3
use proc_macro2::Span;
4
use quote::quote;
5
use syn::punctuated::Punctuated;
6
use syn::token::Comma;
7
use syn::{
8
    parenthesized, parse::Parse, parse::ParseStream, parse_macro_input, Ident, ItemFn, LitInt,
9
    LitStr, Path, Result,
10
};
11

            
12
#[derive(Debug)]
13
struct RuleSetAndPriority {
14
    rule_set: LitStr,
15
    priority: LitInt,
16
}
17

            
18
impl Parse for RuleSetAndPriority {
19
229
    fn parse(input: ParseStream) -> Result<Self> {
20
229
        let content;
21
229
        parenthesized!(content in input);
22
229
        let rule_set: LitStr = content.parse()?;
23
229
        let _: Comma = content.parse()?;
24
229
        let priority: LitInt = content.parse()?;
25
229
        Ok(RuleSetAndPriority { rule_set, priority })
26
229
    }
27
}
28

            
29
#[derive(Debug)]
30
struct RegisterRuleArgs {
31
    pub rule_sets: Vec<RuleSetAndPriority>,
32
}
33

            
34
impl Parse for RegisterRuleArgs {
35
231
    fn parse(input: ParseStream) -> Result<Self> {
36
231
        let rule_sets = Punctuated::<RuleSetAndPriority, Comma>::parse_terminated(input)?;
37
231
        Ok(RegisterRuleArgs {
38
231
            rule_sets: rule_sets.into_iter().collect(),
39
231
        })
40
231
    }
41
}
42

            
43
/**
44
 * Register a rule with the given rule sets and priorities.
45
 */
46
#[proc_macro_attribute]
47
231
pub fn register_rule(arg_tokens: TokenStream, item: TokenStream) -> TokenStream {
48
231
    let func = parse_macro_input!(item as ItemFn);
49
231
    let rule_ident = &func.sig.ident;
50
231
    let static_name = format!("CONJURE_GEN_RULE_{}", rule_ident).to_uppercase();
51
231
    let static_ident = Ident::new(&static_name, rule_ident.span());
52

            
53
231
    let args = parse_macro_input!(arg_tokens as RegisterRuleArgs);
54
231
    let rule_sets = args
55
231
        .rule_sets
56
231
        .iter()
57
231
        .map(|rule_set| {
58
229
            let rule_set_name = &rule_set.rule_set;
59
229
            let priority = &rule_set.priority;
60
229
            quote! {
61
229
                (#rule_set_name, #priority as u16)
62
229
            }
63
231
        })
64
231
        .collect::<Vec<_>>();
65

            
66
231
    let expanded = quote! {
67
231
        #func
68
231

            
69
231
        use ::conjure_core::rule_engine::_dependencies::*; // ToDo idk if we need to explicitly do that?
70
231

            
71
231
        #[::conjure_core::rule_engine::_dependencies::distributed_slice(::conjure_core::rule_engine::RULES_DISTRIBUTED_SLICE)]
72
231
        pub static #static_ident: ::conjure_core::rule_engine::Rule<'static> = ::conjure_core::rule_engine::Rule {
73
231
            name: stringify!(#rule_ident),
74
231
            application: #rule_ident,
75
231
            rule_sets: &[#(#rule_sets),*],
76
231
        };
77
231
    };
78

            
79
231
    TokenStream::from(expanded)
80
231
}
81

            
82
46
fn parse_parenthesized<T: Parse>(input: ParseStream) -> Result<Vec<T>> {
83
46
    let content;
84
46
    parenthesized!(content in input);
85

            
86
46
    let mut paths = Vec::new();
87
48
    while !content.is_empty() {
88
35
        let path = content.parse()?;
89
35
        paths.push(path);
90
35
        if content.is_empty() {
91
33
            break;
92
2
        }
93
2
        content.parse::<Comma>()?;
94
    }
95

            
96
46
    Ok(paths)
97
46
}
98

            
99
struct RuleSetArgs {
100
    name: LitStr,
101
    priority: LitInt,
102
    dependencies: Vec<LitStr>,
103
    solver_families: Vec<Path>,
104
}
105

            
106
impl Parse for RuleSetArgs {
107
34
    fn parse(input: ParseStream) -> Result<Self> {
108
34
        let name = input.parse()?;
109
34
        input.parse::<Comma>()?;
110
34
        let priority = input.parse()?;
111

            
112
34
        if input.is_empty() {
113
            return Ok(Self {
114
                name,
115
                priority,
116
                dependencies: Vec::new(),
117
                solver_families: Vec::new(),
118
            });
119
34
        }
120
34

            
121
34
        input.parse::<Comma>()?;
122
34
        let dependencies = parse_parenthesized::<LitStr>(input)?;
123

            
124
34
        if input.is_empty() {
125
22
            return Ok(Self {
126
22
                name,
127
22
                priority,
128
22
                dependencies,
129
22
                solver_families: Vec::new(),
130
22
            });
131
12
        }
132
12

            
133
12
        input.parse::<Comma>()?;
134
12
        let solver_families = parse_parenthesized::<Path>(input)?;
135

            
136
12
        Ok(Self {
137
12
            name,
138
12
            priority,
139
12
            dependencies,
140
12
            solver_families,
141
12
        })
142
34
    }
143
}
144

            
145
/**
146
* Register a rule set with the given name, priority, and dependencies.
147
*
148
1
* # Example
149
1
* ```rust
150
1
 * use conjure_macros::register_rule_set;
151
1
 * register_rule_set!("MyRuleSet", 10, ("DependencyRuleSet", "AnotherRuleSet"));
152
1
* ```
153
1
 */
154
#[proc_macro]
155
34
pub fn register_rule_set(args: TokenStream) -> TokenStream {
156
    let RuleSetArgs {
157
34
        name,
158
34
        priority,
159
34
        dependencies,
160
34
        solver_families,
161
34
    } = parse_macro_input!(args as RuleSetArgs);
162

            
163
34
    let static_name = format!("CONJURE_GEN_RULE_SET_{}", name.value()).to_uppercase();
164
34
    let static_ident = Ident::new(&static_name, Span::call_site());
165

            
166
34
    let dependencies = quote! {
167
34
        #(#dependencies),*
168
34
    };
169

            
170
34
    let solver_families = quote! {
171
34
        #(#solver_families),*
172
34
    };
173

            
174
34
    let expanded = quote! {
175
34
        use ::conjure_core::rule_engine::_dependencies::*; // ToDo idk if we need to explicitly do that?
176
34
        #[::conjure_core::rule_engine::_dependencies::distributed_slice(::conjure_core::rule_engine::RULE_SETS_DISTRIBUTED_SLICE)]
177
34
        pub static #static_ident: ::conjure_core::rule_engine::RuleSet<'static> = ::conjure_core::rule_engine::RuleSet::new(#name, #priority, &[#dependencies], &[#solver_families]);
178
34
    };
179
34

            
180
34
    TokenStream::from(expanded)
181
34
}