diff options
Diffstat (limited to 'crates/ra_syntax/src/grammar/items/nominal.rs')
-rw-r--r-- | crates/ra_syntax/src/grammar/items/nominal.rs | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/grammar/items/nominal.rs b/crates/ra_syntax/src/grammar/items/nominal.rs new file mode 100644 index 000000000..8d02ad555 --- /dev/null +++ b/crates/ra_syntax/src/grammar/items/nominal.rs | |||
@@ -0,0 +1,154 @@ | |||
1 | use super::*; | ||
2 | |||
3 | pub(super) fn struct_def(p: &mut Parser, kind: SyntaxKind) { | ||
4 | assert!(p.at(STRUCT_KW) || p.at_contextual_kw("union")); | ||
5 | p.bump_remap(kind); | ||
6 | |||
7 | name_r(p, ITEM_RECOVERY_SET); | ||
8 | type_params::opt_type_param_list(p); | ||
9 | match p.current() { | ||
10 | WHERE_KW => { | ||
11 | type_params::opt_where_clause(p); | ||
12 | match p.current() { | ||
13 | SEMI => { | ||
14 | p.bump(); | ||
15 | return; | ||
16 | } | ||
17 | L_CURLY => named_field_def_list(p), | ||
18 | _ => { | ||
19 | //TODO: special case `(` error message | ||
20 | p.error("expected `;` or `{`"); | ||
21 | return; | ||
22 | } | ||
23 | } | ||
24 | } | ||
25 | SEMI if kind == STRUCT_KW => { | ||
26 | p.bump(); | ||
27 | return; | ||
28 | } | ||
29 | L_CURLY => named_field_def_list(p), | ||
30 | L_PAREN if kind == STRUCT_KW => { | ||
31 | pos_field_list(p); | ||
32 | p.expect(SEMI); | ||
33 | } | ||
34 | _ if kind == STRUCT_KW => { | ||
35 | p.error("expected `;`, `{`, or `(`"); | ||
36 | return; | ||
37 | } | ||
38 | _ => { | ||
39 | p.error("expected `{`"); | ||
40 | return; | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | |||
45 | pub(super) fn enum_def(p: &mut Parser) { | ||
46 | assert!(p.at(ENUM_KW)); | ||
47 | p.bump(); | ||
48 | name_r(p, ITEM_RECOVERY_SET); | ||
49 | type_params::opt_type_param_list(p); | ||
50 | type_params::opt_where_clause(p); | ||
51 | if p.at(L_CURLY) { | ||
52 | enum_variant_list(p); | ||
53 | } else { | ||
54 | p.error("expected `{`") | ||
55 | } | ||
56 | } | ||
57 | |||
58 | pub(crate) fn enum_variant_list(p: &mut Parser) { | ||
59 | assert!(p.at(L_CURLY)); | ||
60 | let m = p.start(); | ||
61 | p.bump(); | ||
62 | while !p.at(EOF) && !p.at(R_CURLY) { | ||
63 | if p.at(L_CURLY) { | ||
64 | error_block(p, "expected enum variant"); | ||
65 | continue; | ||
66 | } | ||
67 | let var = p.start(); | ||
68 | attributes::outer_attributes(p); | ||
69 | if p.at(IDENT) { | ||
70 | name(p); | ||
71 | match p.current() { | ||
72 | L_CURLY => named_field_def_list(p), | ||
73 | L_PAREN => pos_field_list(p), | ||
74 | EQ => { | ||
75 | p.bump(); | ||
76 | expressions::expr(p); | ||
77 | } | ||
78 | _ => (), | ||
79 | } | ||
80 | var.complete(p, ENUM_VARIANT); | ||
81 | } else { | ||
82 | var.abandon(p); | ||
83 | p.err_and_bump("expected enum variant"); | ||
84 | } | ||
85 | if !p.at(R_CURLY) { | ||
86 | p.expect(COMMA); | ||
87 | } | ||
88 | } | ||
89 | p.expect(R_CURLY); | ||
90 | m.complete(p, ENUM_VARIANT_LIST); | ||
91 | } | ||
92 | |||
93 | pub(crate) fn named_field_def_list(p: &mut Parser) { | ||
94 | assert!(p.at(L_CURLY)); | ||
95 | let m = p.start(); | ||
96 | p.bump(); | ||
97 | while !p.at(R_CURLY) && !p.at(EOF) { | ||
98 | if p.at(L_CURLY) { | ||
99 | error_block(p, "expected field"); | ||
100 | continue; | ||
101 | } | ||
102 | named_field_def(p); | ||
103 | if !p.at(R_CURLY) { | ||
104 | p.expect(COMMA); | ||
105 | } | ||
106 | } | ||
107 | p.expect(R_CURLY); | ||
108 | m.complete(p, NAMED_FIELD_DEF_LIST); | ||
109 | |||
110 | fn named_field_def(p: &mut Parser) { | ||
111 | let m = p.start(); | ||
112 | // test field_attrs | ||
113 | // struct S { | ||
114 | // #[serde(with = "url_serde")] | ||
115 | // pub uri: Uri, | ||
116 | // } | ||
117 | attributes::outer_attributes(p); | ||
118 | opt_visibility(p); | ||
119 | if p.at(IDENT) { | ||
120 | name(p); | ||
121 | p.expect(COLON); | ||
122 | types::type_(p); | ||
123 | m.complete(p, NAMED_FIELD_DEF); | ||
124 | } else { | ||
125 | m.abandon(p); | ||
126 | p.err_and_bump("expected field declaration"); | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | |||
131 | fn pos_field_list(p: &mut Parser) { | ||
132 | assert!(p.at(L_PAREN)); | ||
133 | let m = p.start(); | ||
134 | if !p.expect(L_PAREN) { | ||
135 | return; | ||
136 | } | ||
137 | while !p.at(R_PAREN) && !p.at(EOF) { | ||
138 | let m = p.start(); | ||
139 | opt_visibility(p); | ||
140 | if !p.at_ts(types::TYPE_FIRST) { | ||
141 | p.error("expected a type"); | ||
142 | m.complete(p, ERROR); | ||
143 | break; | ||
144 | } | ||
145 | types::type_(p); | ||
146 | m.complete(p, POS_FIELD); | ||
147 | |||
148 | if !p.at(R_PAREN) { | ||
149 | p.expect(COMMA); | ||
150 | } | ||
151 | } | ||
152 | p.expect(R_PAREN); | ||
153 | m.complete(p, POS_FIELD_LIST); | ||
154 | } | ||