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 if p.at(T![const]) && p.nth(1) != T!['{'] {
104 // test_err async_without_semicolon
105 // fn foo() { let _ = async {} }
106 if p.at(T![async]) && p.nth(1) != T!['{'] && p.nth(1) != T![move] && p.nth(1) != T![|] {
111 // test_err unsafe_block_in_mod
112 // fn foo(){} unsafe { } fn bar(){}
113 if p.at(T![unsafe]) && p.nth(1) != T!['{'] {
118 if p.at(T![extern]) && p.nth(1) != T!['{'] && (p.nth(1) != STRING || p.nth(2) != T!['{']) {
122 if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == T![trait] {
123 p.bump_remap(T![auto]);
128 // default impl T for Foo {}
129 if p.at(IDENT) && p.at_contextual_kw("default") {
131 T![fn] | T![type] | T![const] | T![impl] => {
132 p.bump_remap(T![default]);
136 // test default_unsafe_item
137 // default unsafe impl T for Foo {
138 // default unsafe fn foo() {}
140 if matches!(p.nth(2), T![impl] | T![fn]) {
141 p.bump_remap(T![default]);
150 // test existential_type
151 // existential type Foo: Fn() -> usize;
152 if p.at(IDENT) && p.at_contextual_kw("existential") && p.nth(1) == T![type] {
153 p.bump_remap(T![existential]);
170 m.complete(p, TRAIT);
173 T![const] if p.nth(1) != T!['{'] => {
188 // unsafe extern "C" {}
192 m.complete(p, EXTERN_BLOCK);
196 if !has_visibility && !has_mods {
200 p.error("expected existential, fn, trait or impl");
202 p.error("expected an item");
204 m.complete(p, ERROR);
211 fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
216 T![extern] if la == T![crate] => extern_crate(p, m),
220 T![mod] => mod_item(p, m),
226 // struct Foo(String, usize);
233 // test pub_macro_def
234 // pub macro m($:ident) {}
238 IDENT if p.at_contextual_kw("macro_rules") && p.nth(1) == BANG => {
241 IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => {
250 T![enum] => adt::enum_(p, m),
251 T![use] => use_item::use_(p, m),
252 T![const] if (la == IDENT || la == T![_] || la == T![mut]) => consts::konst(p, m),
253 T![static] => consts::static_(p, m),
256 T![extern] if la == T!['{'] || (la == STRING && p.nth(2) == T!['{']) => {
259 m.complete(p, EXTERN_BLOCK);
266 fn extern_crate(p: &mut Parser, m: Marker) {
267 assert!(p.at(T![extern]));
269 assert!(p.at(T![crate]));
280 m.complete(p, EXTERN_CRATE);
283 pub(crate) fn extern_item_list(p: &mut Parser) {
284 assert!(p.at(T!['{']));
287 mod_contents(p, true);
289 m.complete(p, EXTERN_ITEM_LIST);
292 fn fn_(p: &mut Parser) {
293 assert!(p.at(T![fn]));
296 name_r(p, ITEM_RECOVERY_SET);
297 // test function_type_params
298 // fn foo<T: Clone + Copy>(){}
299 type_params::opt_generic_param_list(p);
302 params::param_list_fn_def(p);
304 p.error("expected function arguments");
306 // test function_ret_type
311 // test function_where_clause
312 // fn foo<T>() where T: Copy {}
313 type_params::opt_where_clause(p);
316 // trait T { fn foo(); }
320 expressions::block_expr(p)
326 fn type_alias(p: &mut Parser, m: Marker) {
327 assert!(p.at(T![type]));
332 // test type_item_type_params
333 // type Result<T> = ();
334 type_params::opt_generic_param_list(p);
337 type_params::bounds(p);
340 // test type_item_where_clause
341 // type Foo where Foo: Copy = ();
342 type_params::opt_where_clause(p);
347 m.complete(p, TYPE_ALIAS);
350 pub(crate) fn mod_item(p: &mut Parser, m: Marker) {
351 assert!(p.at(T![mod]));
357 } else if !p.eat(T![;]) {
358 p.error("expected `;` or `{`");
360 m.complete(p, MODULE);
363 pub(crate) fn item_list(p: &mut Parser) {
364 assert!(p.at(T!['{']));
367 mod_contents(p, true);
369 m.complete(p, ITEM_LIST);
372 fn macro_rules(p: &mut Parser, m: Marker) {
373 assert!(p.at_contextual_kw("macro_rules"));
374 p.bump_remap(T![macro_rules]);
380 // Special-case `macro_rules! try`.
381 // This is a hack until we do proper edition support
383 // test try_macro_rules
384 // macro_rules! try { () => {} }
392 // test macro_rules_non_brace
393 // macro_rules! m ( ($i:ident) => {} );
394 // macro_rules! m [ ($i:ident) => {} ];
395 T!['['] | T!['('] => {
399 T!['{'] => token_tree(p),
400 _ => p.error("expected `{`, `[`, `(`"),
402 m.complete(p, MACRO_RULES);
406 // macro m { ($i:ident) => {} }
407 // macro m($i:ident) {}
408 fn macro_def(p: &mut Parser, m: Marker) {
410 name_r(p, ITEM_RECOVERY_SET);
413 } else if !p.at(T!['(']) {
414 p.error("unmatched `(`");
419 T!['{'] | T!['['] | T!['('] => token_tree(p),
420 _ => p.error("expected `{`, `[`, `(`"),
422 m.complete(p, TOKEN_TREE);
425 m.complete(p, MACRO_DEF);
428 fn macro_call(p: &mut Parser) -> BlockLike {
429 assert!(paths::is_use_path_start(p));
431 macro_call_after_excl(p)
434 pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
442 T!['('] | T!['['] => {
447 p.error("expected `{`, `[`, `(`");
453 pub(crate) fn token_tree(p: &mut Parser) {
454 let closing_paren_kind = match p.current() {
462 while !p.at(EOF) && !p.at(closing_paren_kind) {
464 T!['{'] | T!['('] | T!['['] => token_tree(p),
466 p.error("unmatched `}`");
467 m.complete(p, TOKEN_TREE);
470 T![')'] | T![']'] => p.err_and_bump("unmatched brace"),
474 p.expect(closing_paren_kind);
475 m.complete(p, TOKEN_TREE);