1 //! FIXME: write short doc here
9 adt::{enum_variant_list, record_field_def_list},
10 expressions::{match_arm_list, record_field_list},
11 traits::{impl_item_list, trait_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_attributes(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 = token_set![
30 FN_KW, STRUCT_KW, ENUM_KW, IMPL_KW, TRAIT_KW, CONST_KW, STATIC_KW, LET_KW, MOD_KW, PUB_KW,
31 CRATE_KW, USE_KW, MACRO_KW
34 pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) {
36 attributes::outer_attributes(p);
37 let m = match maybe_item(p, m) {
41 "expected item, found `;`\n\
42 consider removing this semicolon",
49 if paths::is_use_path_start(p) {
51 BlockLike::Block => (),
52 BlockLike::NotBlock => {
56 m.complete(p, MACRO_CALL);
60 error_block(p, "expected an item");
61 } else if p.at(T!['}']) && !stop_on_r_curly {
63 p.error("unmatched `}`");
66 } else if !p.at(EOF) && !p.at(T!['}']) {
67 p.err_and_bump("expected an item");
69 p.error("expected an item");
74 pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
76 // fn foo() { pub 92; }
77 let has_visibility = opt_visibility(p);
79 let m = match items_without_modifiers(p, m) {
80 Ok(()) => return Ok(()),
84 let mut has_mods = false;
87 has_mods |= p.eat(T![const]);
89 // test_err async_without_semicolon
90 // fn foo() { let _ = async {} }
91 if p.at(T![async]) && p.nth(1) != T!['{'] && p.nth(1) != T![move] && p.nth(1) != T![|] {
96 // test_err unsafe_block_in_mod
97 // fn foo(){} unsafe { } fn bar(){}
98 if p.at(T![unsafe]) && p.nth(1) != T!['{'] {
103 if p.at(T![extern]) {
107 if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == T![trait] {
108 p.bump_remap(T![auto]);
113 // default impl T for Foo {}
114 if p.at(IDENT) && p.at_contextual_kw("default") {
116 T![fn] | T![type] | T![const] | T![impl] => {
117 p.bump_remap(T![default]);
121 // test default_unsafe_item
122 // default unsafe impl T for Foo {
123 // default unsafe fn foo() {}
125 if matches!(p.nth(2), T![impl] | T![fn]) {
126 p.bump_remap(T![default]);
135 // test existential_type
136 // existential type Foo: Fn() -> usize;
137 if p.at(IDENT) && p.at_contextual_kw("existential") && p.nth(1) == T![type] {
138 p.bump_remap(T![existential]);
154 traits::trait_def(p);
155 m.complete(p, TRAIT);
159 consts::const_def(p, m);
173 if !has_visibility && !has_mods {
177 p.error("expected existential, fn, trait or impl");
179 p.error("expected an item");
181 m.complete(p, ERROR);
188 fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
193 T![extern] if la == T![crate] => extern_crate_item(p, m),
197 T![mod] => mod_item(p, m),
203 // struct Foo(String, usize);
208 adt::struct_def(p, m);
210 // test pub_macro_def
211 // pub macro m($:ident) {}
215 IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => {
222 adt::union_def(p, m);
224 T![enum] => adt::enum_def(p, m),
225 T![use] => use_item::use_item(p, m),
226 T![const] if (la == IDENT || la == T![_] || la == T![mut]) => consts::const_def(p, m),
227 T![static] => consts::static_def(p, m),
231 if la == T!['{'] || ((la == STRING || la == RAW_STRING) && p.nth(2) == T!['{']) =>
235 m.complete(p, EXTERN_BLOCK);
242 fn extern_crate_item(p: &mut Parser, m: Marker) {
243 assert!(p.at(T![extern]));
245 assert!(p.at(T![crate]));
256 m.complete(p, EXTERN_CRATE);
259 pub(crate) fn extern_item_list(p: &mut Parser) {
260 assert!(p.at(T!['{']));
263 mod_contents(p, true);
265 m.complete(p, EXTERN_ITEM_LIST);
268 fn fn_def(p: &mut Parser) {
269 assert!(p.at(T![fn]));
272 name_r(p, ITEM_RECOVERY_SET);
273 // test function_type_params
274 // fn foo<T: Clone + Copy>(){}
275 type_params::opt_type_param_list(p);
278 params::param_list_fn_def(p);
280 p.error("expected function arguments");
282 // test function_ret_type
287 // test function_where_clause
288 // fn foo<T>() where T: Copy {}
289 type_params::opt_where_clause(p);
292 // trait T { fn foo(); }
296 expressions::block_expr(p)
302 fn type_def(p: &mut Parser, m: Marker) {
303 assert!(p.at(T![type]));
308 // test type_item_type_params
309 // type Result<T> = ();
310 type_params::opt_type_param_list(p);
313 type_params::bounds(p);
316 // test type_item_where_clause
317 // type Foo where Foo: Copy = ();
318 type_params::opt_where_clause(p);
323 m.complete(p, TYPE_ALIAS);
326 pub(crate) fn mod_item(p: &mut Parser, m: Marker) {
327 assert!(p.at(T![mod]));
333 } else if !p.eat(T![;]) {
334 p.error("expected `;` or `{`");
336 m.complete(p, MODULE);
339 pub(crate) fn mod_item_list(p: &mut Parser) {
340 assert!(p.at(T!['{']));
343 mod_contents(p, true);
345 m.complete(p, ITEM_LIST);
349 // macro m { ($i:ident) => {} }
350 // macro m($i:ident) {}
351 fn macro_def(p: &mut Parser, m: Marker) {
353 name_r(p, ITEM_RECOVERY_SET);
356 } else if !p.at(T!['(']) {
357 p.error("unmatched `(`");
362 T!['{'] | T!['['] | T!['('] => token_tree(p),
363 _ => p.error("expected `{`, `[`, `(`"),
365 m.complete(p, TOKEN_TREE);
368 m.complete(p, MACRO_DEF);
371 fn macro_call(p: &mut Parser) -> BlockLike {
372 assert!(paths::is_use_path_start(p));
374 macro_call_after_excl(p)
377 pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
382 // Special-case `macro_rules! try`.
383 // This is a hack until we do proper edition support
385 // test try_macro_rules
386 // macro_rules! try { () => {} }
398 T!['('] | T!['['] => {
403 p.error("expected `{`, `[`, `(`");
409 pub(crate) fn token_tree(p: &mut Parser) {
410 let closing_paren_kind = match p.current() {
418 while !p.at(EOF) && !p.at(closing_paren_kind) {
420 T!['{'] | T!['('] | T!['['] => token_tree(p),
422 p.error("unmatched `}`");
423 m.complete(p, TOKEN_TREE);
426 T![')'] | T![']'] => p.err_and_bump("unmatched brace"),
430 p.expect(closing_paren_kind);
431 m.complete(p, TOKEN_TREE);