aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/grammar/items/nominal.rs
blob: 495462ca7ac894dcba40bea0d0a1f677e9eee702 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use super::*;

pub(super) fn struct_def(p: &mut Parser, kind: SyntaxKind) {
    assert!(p.at(STRUCT_KW) || p.at_contextual_kw("union"));
    p.bump_remap(kind);

    name_r(p, ITEM_RECOVERY_SET);
    type_params::opt_type_param_list(p);
    match p.current() {
        WHERE_KW => {
            type_params::opt_where_clause(p);
            match p.current() {
                SEMI => {
                    p.bump();
                    return;
                }
                L_CURLY => named_field_def_list(p),
                _ => {
                    //TODO: special case `(` error message
                    p.error("expected `;` or `{`");
                    return;
                }
            }
        }
        SEMI if kind == STRUCT_KW => {
            p.bump();
            return;
        }
        L_CURLY => named_field_def_list(p),
        L_PAREN if kind == STRUCT_KW => {
            pos_field_list(p);
            // test tuple_struct_where
            // struct Test<T>(T) where T: Clone;
            // struct Test<T>(T);
            type_params::opt_where_clause(p);
            p.expect(SEMI);
        }
        _ if kind == STRUCT_KW => {
            p.error("expected `;`, `{`, or `(`");
            return;
        }
        _ => {
            p.error("expected `{`");
            return;
        }
    }
}

pub(super) fn enum_def(p: &mut Parser) {
    assert!(p.at(ENUM_KW));
    p.bump();
    name_r(p, ITEM_RECOVERY_SET);
    type_params::opt_type_param_list(p);
    type_params::opt_where_clause(p);
    if p.at(L_CURLY) {
        enum_variant_list(p);
    } else {
        p.error("expected `{`")
    }
}

pub(crate) fn enum_variant_list(p: &mut Parser) {
    assert!(p.at(L_CURLY));
    let m = p.start();
    p.bump();
    while !p.at(EOF) && !p.at(R_CURLY) {
        if p.at(L_CURLY) {
            error_block(p, "expected enum variant");
            continue;
        }
        let var = p.start();
        attributes::outer_attributes(p);
        if p.at(IDENT) {
            name(p);
            match p.current() {
                L_CURLY => named_field_def_list(p),
                L_PAREN => pos_field_list(p),
                EQ => {
                    p.bump();
                    expressions::expr(p);
                }
                _ => (),
            }
            var.complete(p, ENUM_VARIANT);
        } else {
            var.abandon(p);
            p.err_and_bump("expected enum variant");
        }
        if !p.at(R_CURLY) {
            p.expect(COMMA);
        }
    }
    p.expect(R_CURLY);
    m.complete(p, ENUM_VARIANT_LIST);
}

pub(crate) fn named_field_def_list(p: &mut Parser) {
    assert!(p.at(L_CURLY));
    let m = p.start();
    p.bump();
    while !p.at(R_CURLY) && !p.at(EOF) {
        if p.at(L_CURLY) {
            error_block(p, "expected field");
            continue;
        }
        named_field_def(p);
        if !p.at(R_CURLY) {
            p.expect(COMMA);
        }
    }
    p.expect(R_CURLY);
    m.complete(p, NAMED_FIELD_DEF_LIST);

    fn named_field_def(p: &mut Parser) {
        let m = p.start();
        // test field_attrs
        // struct S {
        //     #[serde(with = "url_serde")]
        //     pub uri: Uri,
        // }
        attributes::outer_attributes(p);
        opt_visibility(p);
        if p.at(IDENT) {
            name(p);
            p.expect(COLON);
            types::type_(p);
            m.complete(p, NAMED_FIELD_DEF);
        } else {
            m.abandon(p);
            p.err_and_bump("expected field declaration");
        }
    }
}

fn pos_field_list(p: &mut Parser) {
    assert!(p.at(L_PAREN));
    let m = p.start();
    if !p.expect(L_PAREN) {
        return;
    }
    while !p.at(R_PAREN) && !p.at(EOF) {
        let m = p.start();
        opt_visibility(p);
        if !p.at_ts(types::TYPE_FIRST) {
            p.error("expected a type");
            m.complete(p, ERROR);
            break;
        }
        types::type_(p);
        m.complete(p, POS_FIELD);

        if !p.at(R_PAREN) {
            p.expect(COMMA);
        }
    }
    p.expect(R_PAREN);
    m.complete(p, POS_FIELD_LIST);
}