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

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

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

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

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

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

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

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

            
79
183
    TokenStream::from(expanded)
80
183
}
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
}