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
#![recursion_limit = "256"]

extern crate proc_macro;
extern crate proc_macro2;
extern crate quote;
extern crate syn;

use proc_macro::TokenStream;
use quote::ToTokens;
use syn::parse_macro_input;

mod ast;
mod expand;
mod parse;

#[proc_macro]
pub fn rules(input: TokenStream) -> TokenStream {
    parse_macro_input!(input as ast::Rules)
        .into_token_stream()
        .into()
}

fn verify_rule(_rule: &ast::SubRule) {
    // FIXME(#11) pattern rule verification
}

// FIXME(#12) we could save some computation by using intermediate results from the SubRules.
fn collect_vars(rule: &ast::SubRule, vars: &mut Vec<ast::MetaVar>) {
    for m in &rule.matchers {
        match m {
            ast::Fragment::Var(id, ty) => vars.push(ast::MetaVar {
                name: id.clone(),
                ty: ast::MetaVarType::T(*ty),
            }),
            ast::Fragment::Repeat(sub_rule, rkind, _) => {
                let mut sub = vec![];
                collect_vars(sub_rule, &mut sub);
                for s in sub {
                    vars.push(match rkind {
                        ast::RepeatKind::ZeroOrMore | ast::RepeatKind::OneOrMore => ast::MetaVar {
                            name: s.name,
                            ty: ast::MetaVarType::Vec(Box::new(s.ty)),
                        },
                        ast::RepeatKind::ZeroOrOne => ast::MetaVar {
                            name: s.name,
                            ty: ast::MetaVarType::Option(Box::new(s.ty)),
                        },
                    })
                }
            }
            ast::Fragment::Group(sub_rule, _) => {
                collect_vars(sub_rule, vars);
            }
            _ => {}
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::ast::*;
    use proc_macro2::Span;
    use syn::Ident;

    #[test]
    fn test_collect_vars() {
        fn run_test(rule: ast::SubRule, expected: Vec<MetaVar>) {
            let mut result = vec![];
            collect_vars(&rule, &mut result);
            assert_eq!(result, expected);
        }

        // ``
        let ast = ast::SubRule { matchers: vec![] };
        run_test(ast, vec![]);

        // `$foo:vis`
        let ast = ast::SubRule {
            matchers: vec![Fragment::Var(
                Ident::new("foo", Span::call_site()),
                Type::Vis,
            )],
        };
        run_test(
            ast,
            vec![MetaVar {
                name: Ident::new("foo", Span::call_site()),
                ty: MetaVarType::T(Type::Vis),
            }],
        );

        // `foo`
        let ast = ast::SubRule {
            matchers: vec![Fragment::Ident(Ident::new("foo", Span::call_site()))],
        };
        run_test(ast, vec![]);

        // `foo $bar:Tt $($foo:expr)*`
        let ast = ast::SubRule {
            matchers: vec![
                Fragment::Ident(Ident::new("foo", Span::call_site())),
                Fragment::Var(Ident::new("bar", Span::call_site()), Type::Tt),
                Fragment::Repeat(
                    SubRule {
                        matchers: vec![Fragment::Var(
                            Ident::new("foo", Span::call_site()),
                            Type::Expr,
                        )],
                    },
                    RepeatKind::OneOrMore,
                    None,
                ),
            ],
        };
        run_test(
            ast,
            vec![
                MetaVar {
                    name: Ident::new("bar", Span::call_site()),
                    ty: MetaVarType::T(Type::Tt),
                },
                MetaVar {
                    name: Ident::new("foo", Span::call_site()),
                    ty: MetaVarType::Vec(Box::new(MetaVarType::T(Type::Expr))),
                },
            ],
        );
    }
}