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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
136
8
        Ok(Self {
137
8
            name,
138
8
            priority,
139
8
            dependencies,
140
8
            solver_families,
141
8
        })
142
24
    }
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
 * register_rule_set!("MyRuleSet", 10, ("DependencyRuleSet", "AnotherRuleSet"));
152
1
* ```
153
1
 */
154
#[proc_macro]
155
24
pub fn register_rule_set(args: TokenStream) -> TokenStream {
156
    let RuleSetArgs {
157
24
        name,
158
24
        priority,
159
24
        dependencies,
160
24
        solver_families,
161
24
    } = parse_macro_input!(args as RuleSetArgs);
162

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

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

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

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

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