rustre_parser/parser/
static_rules.rs

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
131
132
133
134
135
136
137
138
139
use super::*;

pub fn parse_static_params<'slice, 'src>(input: Input<'slice, 'src>) -> IResult<'slice, 'src> {
    opt(node(
        StaticParamsNode,
        many_delimited(
            t(OpenStaticPar),
            parse_static_param,
            t(Semicolon),
            t(CloseStaticPar),
        ),
    ))(input)
}

pub fn parse_static_param<'slice, 'src>(input: Input<'slice, 'src>) -> IResult<'slice, 'src> {
    node(
        StaticParamNode,
        alt((
            join((
                t(Type),
                expect(ident::parse_id_any, "expected id after `type`"),
            )),
            join((
                t(Const),
                expect(ident::parse_id_any, "expected id after `const`"),
                expect(
                    join((t(Colon), expect(parse_type, "expected type after colon"))),
                    "const static param must specify a type",
                ),
            )),
            join((
                nodes::parse_node_type,
                expect(
                    join((
                        ident::parse_id_any,
                        expect(
                            nodes::parse_params_and_returns,
                            "signature must include params and return values",
                        ),
                    )),
                    "unterminated node signature",
                ),
            )),
        )),
    )(input)
}

pub fn parse_effective_node<'slice, 'src>(input: Input<'slice, 'src>) -> IResult<'slice, 'src> {
    node(
        EffectiveNodeNode,
        join((ident::parse_lv6_id_ref, opt(parse_static_args))),
    )(input)
}

pub fn parse_static_args<'slice, 'src>(input: Input<'slice, 'src>) -> IResult<'slice, 'src> {
    node(
        StaticArgsNode,
        many_delimited(
            t(OpenStaticPar),
            parse_static_arg,
            alt((t(Comma), t(Semicolon))),
            t(CloseStaticPar),
        ),
    )(input)
}

pub fn parse_static_arg<'slice, 'src>(input: Input<'slice, 'src>) -> IResult<'slice, 'src> {
    node(
        StaticArgNode,
        alt((
            join((t(Type), expect(parse_type, "expected type"))),
            join((
                t(Const),
                expect(expression::parse_expression, "expected expression"),
            )),
            join((
                alt((t(Node), t(Function))),
                expect(parse_effective_node, "expected node"),
            )),
            join((
                alt((ident::parse_lv6_id_ref, parse_predef_op)),
                peek(alt((t(Comma), t(Semicolon)))),
            )),
            parse_surely_node,
            parse_surely_type,
            // TODO: check if that works, normally it's a SimpleExp but I guess we can verify the
            //       simpleness of the expression at post-parsing time
            expression::parse_expression,
        )),
    )(input)
}

pub fn parse_named_static_arg<'slice, 'src>(input: Input<'slice, 'src>) -> IResult<'slice, 'src> {
    node(
        NamedStaticArgNode,
        alt((
            join((t(Type), ident::parse_id_any, t(Equal), parse_type)),
            join((
                t(Const),
                ident::parse_id_any,
                t(Equal),
                expression::parse_expression,
            )),
            join((
                // TODO: unexpect `unsafe`
                alt((t(Function), t(Node))),
                ident::parse_id_any,
                t(Equal),
                parse_effective_node,
            )),
            join((
                ident::parse_id_any,
                t(Equal),
                alt((
                    parse_predef_op,
                    parse_surely_node,
                    parse_surely_type,
                    // TODO: check if that works, normally it's a SimpleExp but I guess we can verify the
                    //       simpleness of the expression at post-parsing time
                    expression::parse_expression,
                )),
            )),
        )),
    )(input)
}

pub fn parse_surely_node<'slice, 'src>(input: Input<'slice, 'src>) -> IResult<'slice, 'src> {
    node(
        EffectiveNodeNode,
        join((ident::parse_id_any, parse_static_args)),
    )(input)
}

pub fn parse_surely_type<'slice, 'src>(input: Input<'slice, 'src>) -> IResult<'slice, 'src> {
    node(
        TypeNode,
        join((alt((t(Bool), t(Int), t(Real))), parse_type_hat)),
    )(input)
}