aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src/grammar/items/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libsyntax2/src/grammar/items/mod.rs')
-rw-r--r--crates/libsyntax2/src/grammar/items/mod.rs393
1 files changed, 0 insertions, 393 deletions
diff --git a/crates/libsyntax2/src/grammar/items/mod.rs b/crates/libsyntax2/src/grammar/items/mod.rs
deleted file mode 100644
index 2567313ab..000000000
--- a/crates/libsyntax2/src/grammar/items/mod.rs
+++ /dev/null
@@ -1,393 +0,0 @@
1
2mod consts;
3mod nominal;
4mod traits;
5mod use_item;
6
7use super::*;
8pub(crate) use self::{
9 expressions::{named_field_list, match_arm_list},
10 nominal::{enum_variant_list, named_field_def_list},
11 traits::{trait_item_list, impl_item_list},
12 use_item::use_tree_list,
13};
14
15// test mod_contents
16// fn foo() {}
17// macro_rules! foo {}
18// foo::bar!();
19// super::baz! {}
20// struct S;
21pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
22 attributes::inner_attributes(p);
23 while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) {
24 item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod)
25 }
26}
27
28pub(super) enum ItemFlavor {
29 Mod, Trait
30}
31
32const ITEM_RECOVERY_SET: TokenSet =
33 token_set![FN_KW, STRUCT_KW, ENUM_KW, IMPL_KW, TRAIT_KW, CONST_KW, STATIC_KW, LET_KW,
34 MOD_KW, PUB_KW, CRATE_KW];
35
36pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) {
37 let m = p.start();
38 match maybe_item(p, flavor) {
39 MaybeItem::Item(kind) => {
40 m.complete(p, kind);
41 }
42 MaybeItem::None => {
43 if paths::is_path_start(p) {
44 match macro_call(p) {
45 BlockLike::Block => (),
46 BlockLike::NotBlock => {
47 p.expect(SEMI);
48 }
49 }
50 m.complete(p, MACRO_CALL);
51 } else {
52 m.abandon(p);
53 if p.at(L_CURLY) {
54 error_block(p, "expected an item");
55 } else if p.at(R_CURLY) && !stop_on_r_curly {
56 let e = p.start();
57 p.error("unmatched `}`");
58 p.bump();
59 e.complete(p, ERROR);
60 } else if !p.at(EOF) && !p.at(R_CURLY) {
61 p.err_and_bump("expected an item");
62 } else {
63 p.error("expected an item");
64 }
65 }
66 }
67 MaybeItem::Modifiers => {
68 p.error("expected fn, trait or impl");
69 m.complete(p, ERROR);
70 }
71 }
72}
73
74pub(super) enum MaybeItem {
75 None,
76 Item(SyntaxKind),
77 Modifiers,
78}
79
80pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem {
81 attributes::outer_attributes(p);
82 opt_visibility(p);
83 if let Some(kind) = items_without_modifiers(p) {
84 return MaybeItem::Item(kind);
85 }
86
87 let mut has_mods = false;
88 // modifiers
89 has_mods |= p.eat(CONST_KW);
90
91 // test unsafe_block_in_mod
92 // fn foo(){} unsafe { } fn bar(){}
93 if p.at(UNSAFE_KW) && p.nth(1) != L_CURLY {
94 p.eat(UNSAFE_KW);
95 has_mods = true;
96 }
97 if p.at(EXTERN_KW) {
98 has_mods = true;
99 abi(p);
100 }
101 if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == TRAIT_KW {
102 p.bump_remap(AUTO_KW);
103 has_mods = true;
104 }
105 if p.at(IDENT) && p.at_contextual_kw("default") && p.nth(1) == IMPL_KW {
106 p.bump_remap(DEFAULT_KW);
107 has_mods = true;
108 }
109
110 // items
111 let kind = match p.current() {
112 // test extern_fn
113 // extern fn foo() {}
114
115 // test const_fn
116 // const fn foo() {}
117
118 // test const_unsafe_fn
119 // const unsafe fn foo() {}
120
121 // test unsafe_extern_fn
122 // unsafe extern "C" fn foo() {}
123
124 // test unsafe_fn
125 // unsafe fn foo() {}
126 FN_KW => {
127 fn_def(p, flavor);
128 FN_DEF
129 }
130
131 // test unsafe_trait
132 // unsafe trait T {}
133
134 // test auto_trait
135 // auto trait T {}
136
137 // test unsafe_auto_trait
138 // unsafe auto trait T {}
139 TRAIT_KW => {
140 traits::trait_def(p);
141 TRAIT_DEF
142 }
143
144 // test unsafe_impl
145 // unsafe impl Foo {}
146
147 // test default_impl
148 // default impl Foo {}
149
150 // test unsafe_default_impl
151 // unsafe default impl Foo {}
152 IMPL_KW => {
153 traits::impl_item(p);
154 IMPL_ITEM
155 }
156 _ => return if has_mods {
157 MaybeItem::Modifiers
158 } else {
159 MaybeItem::None
160 }
161 };
162
163 MaybeItem::Item(kind)
164}
165
166fn items_without_modifiers(p: &mut Parser) -> Option<SyntaxKind> {
167 let la = p.nth(1);
168 let kind = match p.current() {
169 // test extern_crate
170 // extern crate foo;
171 EXTERN_KW if la == CRATE_KW => {
172 extern_crate_item(p);
173 EXTERN_CRATE_ITEM
174 }
175 TYPE_KW => {
176 type_def(p);
177 TYPE_DEF
178 }
179 MOD_KW => {
180 mod_item(p);
181 MODULE
182 }
183 STRUCT_KW => {
184 // test struct_items
185 // struct Foo;
186 // struct Foo {}
187 // struct Foo();
188 // struct Foo(String, usize);
189 // struct Foo {
190 // a: i32,
191 // b: f32,
192 // }
193 nominal::struct_def(p, STRUCT_KW);
194 if p.at(SEMI) {
195 p.err_and_bump(
196 "expected item, found `;`\n\
197 consider removing this semicolon"
198 );
199 }
200 STRUCT_DEF
201 }
202 IDENT if p.at_contextual_kw("union") => {
203 // test union_items
204 // union Foo {}
205 // union Foo {
206 // a: i32,
207 // b: f32,
208 // }
209 nominal::struct_def(p, UNION_KW);
210 STRUCT_DEF
211 }
212 ENUM_KW => {
213 nominal::enum_def(p);
214 ENUM_DEF
215 }
216 USE_KW => {
217 use_item::use_item(p);
218 USE_ITEM
219 }
220 CONST_KW if (la == IDENT || la == MUT_KW) => {
221 consts::const_def(p);
222 CONST_DEF
223 }
224 STATIC_KW => {
225 consts::static_def(p);
226 STATIC_DEF
227 }
228 // test extern_block
229 // extern {}
230 EXTERN_KW if la == L_CURLY || ((la == STRING || la == RAW_STRING) && p.nth(2) == L_CURLY) => {
231 abi(p);
232 extern_item_list(p);
233 EXTERN_BLOCK
234 }
235 _ => return None,
236 };
237 Some(kind)
238}
239
240fn extern_crate_item(p: &mut Parser) {
241 assert!(p.at(EXTERN_KW));
242 p.bump();
243 assert!(p.at(CRATE_KW));
244 p.bump();
245 name(p);
246 opt_alias(p);
247 p.expect(SEMI);
248}
249
250pub(crate) fn extern_item_list(p: &mut Parser) {
251 assert!(p.at(L_CURLY));
252 let m = p.start();
253 p.bump();
254 mod_contents(p, true);
255 p.expect(R_CURLY);
256 m.complete(p, EXTERN_ITEM_LIST);
257}
258
259fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
260 assert!(p.at(FN_KW));
261 p.bump();
262
263 name_r(p, ITEM_RECOVERY_SET);
264 // test function_type_params
265 // fn foo<T: Clone + Copy>(){}
266 type_params::opt_type_param_list(p);
267
268 if p.at(L_PAREN) {
269 match flavor {
270 ItemFlavor::Mod =>
271 params::param_list(p),
272 ItemFlavor::Trait =>
273 params::param_list_opt_patterns(p),
274 }
275 } else {
276 p.error("expected function arguments");
277 }
278 // test function_ret_type
279 // fn foo() {}
280 // fn bar() -> () {}
281 opt_fn_ret_type(p);
282
283 // test function_where_clause
284 // fn foo<T>() where T: Copy {}
285 type_params::opt_where_clause(p);
286
287 // test fn_decl
288 // trait T { fn foo(); }
289 if p.at(SEMI) {
290 p.bump();
291 } else {
292 expressions::block(p)
293 }
294}
295
296// test type_item
297// type Foo = Bar;
298fn type_def(p: &mut Parser) {
299 assert!(p.at(TYPE_KW));
300 p.bump();
301
302 name(p);
303
304 // test type_item_type_params
305 // type Result<T> = ();
306 type_params::opt_type_param_list(p);
307
308 if p.at(COLON) {
309 type_params::bounds(p);
310 }
311
312 // test type_item_where_clause
313 // type Foo where Foo: Copy = ();
314 type_params::opt_where_clause(p);
315
316 if p.eat(EQ) {
317 types::type_(p);
318 }
319 p.expect(SEMI);
320}
321
322pub(crate) fn mod_item(p: &mut Parser) {
323 assert!(p.at(MOD_KW));
324 p.bump();
325
326 name(p);
327 if p.at(L_CURLY) {
328 mod_item_list(p);
329 } else if !p.eat(SEMI) {
330 p.error("expected `;` or `{`");
331 }
332}
333
334pub(crate) fn mod_item_list(p: &mut Parser) {
335 assert!(p.at(L_CURLY));
336 let m = p.start();
337 p.bump();
338 mod_contents(p, true);
339 p.expect(R_CURLY);
340 m.complete(p, ITEM_LIST);
341}
342
343fn macro_call(p: &mut Parser) -> BlockLike {
344 assert!(paths::is_path_start(p));
345 paths::use_path(p);
346 macro_call_after_excl(p)
347}
348
349pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
350 p.expect(EXCL);
351 p.eat(IDENT);
352 let flavor = match p.current() {
353 L_CURLY => {
354 token_tree(p);
355 BlockLike::Block
356 }
357 L_PAREN | L_BRACK => {
358 token_tree(p);
359 BlockLike::NotBlock
360 }
361 _ => {
362 p.error("expected `{`, `[`, `(`");
363 BlockLike::NotBlock
364 },
365 };
366
367 flavor
368}
369
370pub(crate) fn token_tree(p: &mut Parser) {
371 let closing_paren_kind = match p.current() {
372 L_CURLY => R_CURLY,
373 L_PAREN => R_PAREN,
374 L_BRACK => R_BRACK,
375 _ => unreachable!(),
376 };
377 let m = p.start();
378 p.bump();
379 while !p.at(EOF) && !p.at(closing_paren_kind) {
380 match p.current() {
381 L_CURLY | L_PAREN | L_BRACK => token_tree(p),
382 R_CURLY => {
383 p.error("unmatched `}`");
384 m.complete(p, TOKEN_TREE);
385 return;
386 }
387 R_PAREN | R_BRACK => p.err_and_bump("unmatched brace"),
388 _ => p.bump()
389 }
390 };
391 p.expect(closing_paren_kind);
392 m.complete(p, TOKEN_TREE);
393}