summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/parser/src/grammar/items/adt.rs
blob: 17f41b8e13a40d1c3e88e35724eadce199e652e9 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::grammar::attributes::ATTRIBUTE_FIRST;

use super::*;

// test struct_item
// struct S {}
pub(super) fn strukt(p: &mut Parser<'_>, m: Marker) {
    p.bump(T![struct]);
    struct_or_union(p, m, true);
}

// test union_item
// struct U { i: i32, f: f32 }
pub(super) fn union(p: &mut Parser<'_>, m: Marker) {
    assert!(p.at_contextual_kw(T![union]));
    p.bump_remap(T![union]);
    struct_or_union(p, m, false);
}

fn struct_or_union(p: &mut Parser<'_>, m: Marker, is_struct: bool) {
    name_r(p, ITEM_RECOVERY_SET);
    generic_params::opt_generic_param_list(p);
    match p.current() {
        T![where] => {
            generic_params::opt_where_clause(p);
            match p.current() {
                T![;] => p.bump(T![;]),
                T!['{'] => record_field_list(p),
                _ => {
                    //FIXME: special case `(` error message
                    p.error("expected `;` or `{`");
                }
            }
        }
        T!['{'] => record_field_list(p),
        // test unit_struct
        // struct S;
        T![;] if is_struct => {
            p.bump(T![;]);
        }
        // test tuple_struct
        // struct S(String, usize);
        T!['('] if is_struct => {
            tuple_field_list(p);
            // test tuple_struct_where
            // struct S<T>(T) where T: Clone;
            generic_params::opt_where_clause(p);
            p.expect(T![;]);
        }
        _ => p.error(if is_struct { "expected `;`, `{`, or `(`" } else { "expected `{`" }),
    }
    m.complete(p, if is_struct { STRUCT } else { UNION });
}

pub(super) fn enum_(p: &mut Parser<'_>, m: Marker) {
    p.bump(T![enum]);
    name_r(p, ITEM_RECOVERY_SET);
    generic_params::opt_generic_param_list(p);
    generic_params::opt_where_clause(p);
    if p.at(T!['{']) {
        variant_list(p);
    } else {
        p.error("expected `{`");
    }
    m.complete(p, ENUM);
}

pub(crate) fn variant_list(p: &mut Parser<'_>) {
    assert!(p.at(T!['{']));
    let m = p.start();
    p.bump(T!['{']);
    while !p.at(EOF) && !p.at(T!['}']) {
        if p.at(T!['{']) {
            error_block(p, "expected enum variant");
            continue;
        }
        variant(p);
        if !p.at(T!['}']) {
            p.expect(T![,]);
        }
    }
    p.expect(T!['}']);
    m.complete(p, VARIANT_LIST);

    fn variant(p: &mut Parser<'_>) {
        let m = p.start();
        attributes::outer_attrs(p);
        if p.at(IDENT) {
            name(p);
            match p.current() {
                T!['{'] => record_field_list(p),
                T!['('] => tuple_field_list(p),
                _ => (),
            }

            // test variant_discriminant
            // enum E { X(i32) = 10 }
            if p.eat(T![=]) {
                expressions::expr(p);
            }
            m.complete(p, VARIANT);
        } else {
            m.abandon(p);
            p.err_and_bump("expected enum variant");
        }
    }
}

// test record_field_list
// struct S { a: i32, b: f32 }
pub(crate) fn record_field_list(p: &mut Parser<'_>) {
    assert!(p.at(T!['{']));
    let m = p.start();
    p.bump(T!['{']);
    while !p.at(T!['}']) && !p.at(EOF) {
        if p.at(T!['{']) {
            error_block(p, "expected field");
            continue;
        }
        record_field(p);
        if !p.at(T!['}']) {
            p.expect(T![,]);
        }
    }
    p.expect(T!['}']);
    m.complete(p, RECORD_FIELD_LIST);

    fn record_field(p: &mut Parser<'_>) {
        let m = p.start();
        // test record_field_attrs
        // struct S { #[attr] f: f32 }
        attributes::outer_attrs(p);
        opt_visibility(p, false);
        if p.at(IDENT) {
            name(p);
            p.expect(T![:]);
            types::type_(p);
            m.complete(p, RECORD_FIELD);
        } else {
            m.abandon(p);
            p.err_and_bump("expected field declaration");
        }
    }
}

const TUPLE_FIELD_FIRST: TokenSet =
    types::TYPE_FIRST.union(ATTRIBUTE_FIRST).union(VISIBILITY_FIRST);

fn tuple_field_list(p: &mut Parser<'_>) {
    assert!(p.at(T!['(']));
    let m = p.start();
    delimited(p, T!['('], T![')'], T![,], TUPLE_FIELD_FIRST, |p| {
        let m = p.start();
        // test tuple_field_attrs
        // struct S (#[attr] f32);
        attributes::outer_attrs(p);
        let has_vis = opt_visibility(p, true);
        if !p.at_ts(types::TYPE_FIRST) {
            p.error("expected a type");
            if has_vis {
                m.complete(p, ERROR);
            } else {
                m.abandon(p);
            }
            return false;
        }
        types::type_(p);
        m.complete(p, TUPLE_FIELD);
        true
    });

    m.complete(p, TUPLE_FIELD_LIST);
}