1 //! FIXME: write short doc here
9 adt::{record_field_list, variant_list},
10 expressions::{match_arm_list, record_expr_field_list},
11 traits::assoc_item_list,
12 use_item::use_tree_list,
18 // macro_rules! foo {}
22 pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
23 attributes::inner_attrs(p);
24 while !(stop_on_r_curly && p.at(T!['}']) || p.at(EOF)) {
25 item_or_macro(p, stop_on_r_curly)
29 pub(super) const ITEM_RECOVERY_SET: TokenSet = TokenSet::new(&[
46 pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) {
48 attributes::outer_attrs(p);
49 let m = match maybe_item(p, m) {
53 "expected item, found `;`\n\
54 consider removing this semicolon",
61 if paths::is_use_path_start(p) {
63 BlockLike::Block => (),
64 BlockLike::NotBlock => {
68 m.complete(p, MACRO_CALL);
72 error_block(p, "expected an item");
73 } else if p.at(T!['}']) && !stop_on_r_curly {
75 p.error("unmatched `}`");
78 } else if !p.at(EOF) && !p.at(T!['}']) {
79 p.err_and_bump("expected an item");
81 p.error("expected an item");
86 pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
88 // fn foo() { pub 92; }
89 let has_visibility = opt_visibility(p);
91 let m = match items_without_modifiers(p, m) {
92 Ok(()) => return Ok(()),
96 let mut has_mods = false;
99 has_mods |= p.eat(T![const]);
101 // test_err async_without_semicolon
102 // fn foo() { let _ = async {} }
103 if p.at(T![async]) && p.nth(1) != T!['{'] && p.nth(1) != T![move] && p.nth(1) != T![|] {
108 // test_err unsafe_block_in_mod
109 // fn foo(){} unsafe { } fn bar(){}
110 if p.at(T![unsafe]) && p.nth(1) != T!['{'] {
115 if p.at(T![extern]) && p.nth(1) != T!['{'] && (p.nth(1) != STRING || p.nth(2) != T!['{']) {
119 if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == T![trait] {
120 p.bump_remap(T![auto]);
125 // default impl T for Foo {}
126 if p.at(IDENT) && p.at_contextual_kw("default") {
128 T![fn] | T![type] | T![const] | T![impl] => {
129 p.bump_remap(T![default]);
133 // test default_unsafe_item
134 // default unsafe impl T for Foo {
135 // default unsafe fn foo() {}
137 if matches!(p.nth(2), T![impl] | T![fn]) {
138 p.bump_remap(T![default]);
147 // test existential_type
148 // existential type Foo: Fn() -> usize;
149 if p.at(IDENT) && p.at_contextual_kw("existential") && p.nth(1) == T![type] {
150 p.bump_remap(T![existential]);
167 m.complete(p, TRAIT);
185 // unsafe extern "C" {}
189 m.complete(p, EXTERN_BLOCK);
193 if !has_visibility && !has_mods {
197 p.error("expected existential, fn, trait or impl");
199 p.error("expected an item");
201 m.complete(p, ERROR);
208 fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
213 T![extern] if la == T![crate] => extern_crate(p, m),
217 T![mod] => mod_item(p, m),
223 // struct Foo(String, usize);
230 // test pub_macro_def
231 // pub macro m($:ident) {}
235 IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => {
244 T![enum] => adt::enum_(p, m),
245 T![use] => use_item::use_(p, m),
246 T![const] if (la == IDENT || la == T![_] || la == T![mut]) => consts::konst(p, m),
247 T![static] => consts::static_(p, m),
250 T![extern] if la == T!['{'] || (la == STRING && p.nth(2) == T!['{']) => {
253 m.complete(p, EXTERN_BLOCK);
260 fn extern_crate(p: &mut Parser, m: Marker) {
261 assert!(p.at(T![extern]));
263 assert!(p.at(T![crate]));
274 m.complete(p, EXTERN_CRATE);
277 pub(crate) fn extern_item_list(p: &mut Parser) {
278 assert!(p.at(T!['{']));
281 mod_contents(p, true);
283 m.complete(p, EXTERN_ITEM_LIST);
286 fn fn_(p: &mut Parser) {
287 assert!(p.at(T![fn]));
290 name_r(p, ITEM_RECOVERY_SET);
291 // test function_type_params
292 // fn foo<T: Clone + Copy>(){}
293 type_params::opt_generic_param_list(p);
296 params::param_list_fn_def(p);
298 p.error("expected function arguments");
300 // test function_ret_type
305 // test function_where_clause
306 // fn foo<T>() where T: Copy {}
307 type_params::opt_where_clause(p);
310 // trait T { fn foo(); }
314 expressions::block_expr(p)
320 fn type_alias(p: &mut Parser, m: Marker) {
321 assert!(p.at(T![type]));
326 // test type_item_type_params
327 // type Result<T> = ();
328 type_params::opt_generic_param_list(p);
331 type_params::bounds(p);
334 // test type_item_where_clause
335 // type Foo where Foo: Copy = ();
336 type_params::opt_where_clause(p);
341 m.complete(p, TYPE_ALIAS);
344 pub(crate) fn mod_item(p: &mut Parser, m: Marker) {
345 assert!(p.at(T![mod]));
351 } else if !p.eat(T![;]) {
352 p.error("expected `;` or `{`");
354 m.complete(p, MODULE);
357 pub(crate) fn item_list(p: &mut Parser) {
358 assert!(p.at(T!['{']));
361 mod_contents(p, true);
363 m.complete(p, ITEM_LIST);
367 // macro m { ($i:ident) => {} }
368 // macro m($i:ident) {}
369 fn macro_def(p: &mut Parser, m: Marker) {
371 name_r(p, ITEM_RECOVERY_SET);
374 } else if !p.at(T!['(']) {
375 p.error("unmatched `(`");
380 T!['{'] | T!['['] | T!['('] => token_tree(p),
381 _ => p.error("expected `{`, `[`, `(`"),
383 m.complete(p, TOKEN_TREE);
386 m.complete(p, MACRO_DEF);
389 fn macro_call(p: &mut Parser) -> BlockLike {
390 assert!(paths::is_use_path_start(p));
392 macro_call_after_excl(p)
395 pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
400 // Special-case `macro_rules! try`.
401 // This is a hack until we do proper edition support
403 // test try_macro_rules
404 // macro_rules! try { () => {} }
416 T!['('] | T!['['] => {
421 p.error("expected `{`, `[`, `(`");
427 pub(crate) fn token_tree(p: &mut Parser) {
428 let closing_paren_kind = match p.current() {
436 while !p.at(EOF) && !p.at(closing_paren_kind) {
438 T!['{'] | T!['('] | T!['['] => token_tree(p),
440 p.error("unmatched `}`");
441 m.complete(p, TOKEN_TREE);
444 T![')'] | T![']'] => p.err_and_bump("unmatched brace"),
448 p.expect(closing_paren_kind);
449 m.complete(p, TOKEN_TREE);