7 adt::{record_field_list, variant_list},
8 expressions::{match_arm_list, record_expr_field_list},
9 traits::assoc_item_list,
10 use_item::use_tree_list,
16 // macro_rules! foo {}
20 pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
21 attributes::inner_attrs(p);
22 while !p.at(EOF) && !(p.at(T!['}']) && stop_on_r_curly) {
23 item_or_macro(p, stop_on_r_curly);
27 pub(super) const ITEM_RECOVERY_SET: TokenSet = TokenSet::new(&[
44 pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) {
46 attributes::outer_attrs(p);
48 let m = match opt_item(p, m) {
52 "expected item, found `;`\n\
53 consider removing this semicolon",
61 if paths::is_use_path_start(p) {
63 BlockLike::Block => (),
64 BlockLike::NotBlock => {
68 m.complete(p, MACRO_CALL);
74 T!['{'] => error_block(p, "expected an item"),
75 T!['}'] if !stop_on_r_curly => {
77 p.error("unmatched `}`");
81 EOF | T!['}'] => p.error("expected an item"),
82 _ => p.err_and_bump("expected an item"),
86 /// Try to parse an item, completing `m` in case of success.
87 pub(super) fn opt_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
89 // fn foo() { pub 92; }
90 let has_visibility = opt_visibility(p, false);
92 let m = match opt_item_without_modifiers(p, m) {
93 Ok(()) => return Ok(()),
97 let mut has_mods = false;
98 let mut has_extern = false;
101 if p.at(T![const]) && p.nth(1) != T!['{'] {
106 // test_err async_without_semicolon
107 // fn foo() { let _ = async {} }
108 if p.at(T![async]) && !matches!(p.nth(1), T!['{'] | T![move] | T![|]) {
113 // test_err unsafe_block_in_mod
114 // fn foo(){} unsafe { } fn bar(){}
115 if p.at(T![unsafe]) && p.nth(1) != T!['{'] {
120 if p.at(T![extern]) {
125 if p.at_contextual_kw(T![auto]) && p.nth(1) == T![trait] {
126 p.bump_remap(T![auto]);
131 // default impl T for Foo {}
132 if p.at_contextual_kw(T![default]) {
134 T![fn] | T![type] | T![const] | T![impl] => {
135 p.bump_remap(T![default]);
138 // test default_unsafe_item
139 // default unsafe impl T for Foo {
140 // default unsafe fn foo() {}
142 T![unsafe] if matches!(p.nth(2), T![impl] | T![fn]) => {
143 p.bump_remap(T![default]);
147 // test default_async_fn
149 // default async fn foo() {}
152 let mut maybe_fn = p.nth(2);
153 let is_unsafe = if matches!(maybe_fn, T![unsafe]) {
154 // test default_async_unsafe_fn
156 // default async unsafe fn foo() {}
164 if matches!(maybe_fn, T![fn]) {
165 p.bump_remap(T![default]);
177 // test existential_type
178 // existential type Foo: Fn() -> usize;
179 if p.at_contextual_kw(T![existential]) && p.nth(1) == T![type] {
180 p.bump_remap(T![existential]);
188 T![const] if p.nth(1) != T!['{'] => consts::konst(p, m),
190 T![trait] => traits::trait_(p, m),
191 T![impl] => traits::impl_(p, m),
193 T![type] => type_alias(p, m),
196 // unsafe extern "C" {}
198 T!['{'] if has_extern => {
200 m.complete(p, EXTERN_BLOCK);
203 _ if has_visibility || has_mods => {
205 p.error("expected existential, fn, trait or impl");
207 p.error("expected an item");
209 m.complete(p, ERROR);
217 fn opt_item_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
220 T![extern] if la == T![crate] => extern_crate(p, m),
221 T![use] => use_item::use_(p, m),
222 T![mod] => mod_item(p, m),
224 T![type] => type_alias(p, m),
225 T![struct] => adt::strukt(p, m),
226 T![enum] => adt::enum_(p, m),
227 IDENT if p.at_contextual_kw(T![union]) && p.nth(1) == IDENT => adt::union(p, m),
229 T![macro] => macro_def(p, m),
230 IDENT if p.at_contextual_kw(T![macro_rules]) && p.nth(1) == BANG => macro_rules(p, m),
232 T![const] if (la == IDENT || la == T![_] || la == T![mut]) => consts::konst(p, m),
233 T![static] if (la == IDENT || la == T![_] || la == T![mut]) => consts::static_(p, m),
242 fn extern_crate(p: &mut Parser, m: Marker) {
247 // test extern_crate_self
248 // extern crate self;
251 m.complete(p, NAME_REF);
256 // test extern_crate_rename
257 // extern crate foo as bar;
260 m.complete(p, EXTERN_CRATE);
265 pub(crate) fn mod_item(p: &mut Parser, m: Marker) {
269 // test mod_item_curly
272 } else if !p.eat(T![;]) {
273 p.error("expected `;` or `{`");
275 m.complete(p, MODULE);
280 fn type_alias(p: &mut Parser, m: Marker) {
285 // test type_item_type_params
286 // type Result<T> = ();
287 generic_params::opt_generic_param_list(p);
290 generic_params::bounds(p);
293 // test type_item_where_clause_deprecated
294 // type Foo where Foo: Copy = ();
295 generic_params::opt_where_clause(p);
300 // test type_item_where_clause
301 // type Foo = () where Foo: Copy;
302 generic_params::opt_where_clause(p);
305 m.complete(p, TYPE_ALIAS);
308 pub(crate) fn item_list(p: &mut Parser) {
309 assert!(p.at(T!['{']));
312 mod_contents(p, true);
314 m.complete(p, ITEM_LIST);
317 pub(crate) fn extern_item_list(p: &mut Parser) {
318 assert!(p.at(T!['{']));
321 mod_contents(p, true);
323 m.complete(p, EXTERN_ITEM_LIST);
326 fn macro_rules(p: &mut Parser, m: Marker) {
327 assert!(p.at_contextual_kw(T![macro_rules]));
328 p.bump_remap(T![macro_rules]);
334 // Special-case `macro_rules! try`.
335 // This is a hack until we do proper edition support
337 // test try_macro_rules
338 // macro_rules! try { () => {} }
346 // test macro_rules_non_brace
347 // macro_rules! m ( ($i:ident) => {} );
348 // macro_rules! m [ ($i:ident) => {} ];
349 T!['['] | T!['('] => {
353 T!['{'] => token_tree(p),
354 _ => p.error("expected `{`, `[`, `(`"),
356 m.complete(p, MACRO_RULES);
360 // macro m($i:ident) {}
361 fn macro_def(p: &mut Parser, m: Marker) {
363 name_r(p, ITEM_RECOVERY_SET);
365 // test macro_def_curly
366 // macro m { ($i:ident) => {} }
368 } else if p.at(T!['(']) {
372 T!['{'] | T!['['] | T!['('] => token_tree(p),
373 _ => p.error("expected `{`, `[`, `(`"),
375 m.complete(p, TOKEN_TREE);
377 p.error("unmatched `(`");
380 m.complete(p, MACRO_DEF);
385 fn fn_(p: &mut Parser, m: Marker) {
388 name_r(p, ITEM_RECOVERY_SET);
389 // test function_type_params
390 // fn foo<T: Clone + Copy>(){}
391 generic_params::opt_generic_param_list(p);
394 params::param_list_fn_def(p);
396 p.error("expected function arguments");
398 // test function_ret_type
403 // test function_where_clause
404 // fn foo<T>() where T: Copy {}
405 generic_params::opt_where_clause(p);
409 // trait T { fn foo(); }
412 expressions::block_expr(p);
417 fn macro_call(p: &mut Parser) -> BlockLike {
418 assert!(paths::is_use_path_start(p));
420 macro_call_after_excl(p)
423 pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
431 T!['('] | T!['['] => {
436 p.error("expected `{`, `[`, `(`");
442 pub(crate) fn token_tree(p: &mut Parser) {
443 let closing_paren_kind = match p.current() {
451 while !p.at(EOF) && !p.at(closing_paren_kind) {
453 T!['{'] | T!['('] | T!['['] => token_tree(p),
455 p.error("unmatched `}`");
456 m.complete(p, TOKEN_TREE);
459 T![')'] | T![']'] => p.err_and_bump("unmatched brace"),
463 p.expect(closing_paren_kind);
464 m.complete(p, TOKEN_TREE);