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
191
    fn parse(input: ParseStream) -> Result<Self> {
20
191
        let content;
21
191
        parenthesized!(content in input);
22
191
        let rule_set: LitStr = content.parse()?;
23
191
        let _: Comma = content.parse()?;
24
191
        let priority: LitInt = content.parse()?;
25
191
        Ok(RuleSetAndPriority { rule_set, priority })
26
191
    }
27
}
28

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

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

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

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

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

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

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

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

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

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

            
96
39
    Ok(paths)
97
39
}
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
29
    fn parse(input: ParseStream) -> Result<Self> {
108
29
        let name = input.parse()?;
109
29
        input.parse::<Comma>()?;
110
29
        let priority = input.parse()?;
111

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

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

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

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

            
136
10
        Ok(Self {
137
10
            name,
138
10
            priority,
139
10
            dependencies,
140
10
            solver_families,
141
10
        })
142
29
    }
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
29
pub fn register_rule_set(args: TokenStream) -> TokenStream {
156
    let RuleSetArgs {
157
29
        name,
158
29
        priority,
159
29
        dependencies,
160
29
        solver_families,
161
29
    } = parse_macro_input!(args as RuleSetArgs);
162

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

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

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

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

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