@@ -44,7 +44,8 @@ pub(super) const ITEM_RECOVERY_SET: TokenSet = TokenSet::new(&[
4444pub ( super ) fn item_or_macro ( p : & mut Parser , stop_on_r_curly : bool ) {
4545 let m = p. start ( ) ;
4646 attributes:: outer_attrs ( p) ;
47- let m = match maybe_item ( p, m) {
47+
48+ let m = match opt_item ( p, m) {
4849 Ok ( ( ) ) => {
4950 if p. at ( T ! [ ; ] ) {
5051 p. err_and_bump (
@@ -56,6 +57,7 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) {
5657 }
5758 Err ( m) => m,
5859 } ;
60+
5961 if paths:: is_use_path_start ( p) {
6062 match macro_call ( p) {
6163 BlockLike :: Block => ( ) ,
@@ -64,30 +66,30 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) {
6466 }
6567 }
6668 m. complete ( p, MACRO_CALL ) ;
67- } else {
68- m. abandon ( p) ;
69- if p. at ( T ! [ '{' ] ) {
70- error_block ( p, "expected an item" ) ;
71- } else if p. at ( T ! [ '}' ] ) && !stop_on_r_curly {
69+ return ;
70+ }
71+
72+ m. abandon ( p) ;
73+ match p. current ( ) {
74+ T ! [ '{' ] => error_block ( p, "expected an item" ) ,
75+ T ! [ '}' ] if !stop_on_r_curly => {
7276 let e = p. start ( ) ;
7377 p. error ( "unmatched `}`" ) ;
7478 p. bump ( T ! [ '}' ] ) ;
7579 e. complete ( p, ERROR ) ;
76- } else if !p. at ( EOF ) && !p. at ( T ! [ '}' ] ) {
77- p. err_and_bump ( "expected an item" ) ;
78- } else {
79- p. error ( "expected an item" ) ;
8080 }
81+ EOF | T ! [ '}' ] => p. error ( "expected an item" ) ,
82+ _ => p. err_and_bump ( "expected an item" ) ,
8183 }
8284}
8385
8486/// Try to parse an item, completing `m` in case of success.
85- pub ( super ) fn maybe_item ( p : & mut Parser , m : Marker ) -> Result < ( ) , Marker > {
87+ pub ( super ) fn opt_item ( p : & mut Parser , m : Marker ) -> Result < ( ) , Marker > {
8688 // test_err pub_expr
8789 // fn foo() { pub 92; }
8890 let has_visibility = opt_visibility ( p) ;
8991
90- let m = match items_without_modifiers ( p, m) {
92+ let m = match opt_item_without_modifiers ( p, m) {
9193 Ok ( ( ) ) => return Ok ( ( ) ) ,
9294 Err ( m) => m,
9395 } ;
@@ -235,48 +237,20 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
235237 Ok ( ( ) )
236238}
237239
238- fn items_without_modifiers ( p : & mut Parser , m : Marker ) -> Result < ( ) , Marker > {
240+ fn opt_item_without_modifiers ( p : & mut Parser , m : Marker ) -> Result < ( ) , Marker > {
239241 let la = p. nth ( 1 ) ;
240242 match p. current ( ) {
241- // test extern_crate
242- // extern crate foo;
243243 T ! [ extern] if la == T ! [ crate ] => extern_crate ( p, m) ,
244244 T ! [ use ] => use_item:: use_ ( p, m) ,
245245 T ! [ mod ] => mod_item ( p, m) ,
246246
247247 T ! [ type ] => type_alias ( p, m) ,
248-
249- T ! [ struct ] => {
250- // test struct_items
251- // struct Foo;
252- // struct Foo {}
253- // struct Foo();
254- // struct Foo(String, usize);
255- // struct Foo {
256- // a: i32,
257- // b: f32,
258- // }
259- adt:: strukt ( p, m) ;
260- }
248+ T ! [ struct ] => adt:: strukt ( p, m) ,
261249 T ! [ enum ] => adt:: enum_ ( p, m) ,
262- IDENT if p. at_contextual_kw ( "union" ) && p. nth ( 1 ) == IDENT => {
263- // test union_items
264- // union Foo {}
265- // union Foo {
266- // a: i32,
267- // b: f32,
268- // }
269- adt:: union ( p, m) ;
270- }
250+ IDENT if p. at_contextual_kw ( "union" ) && p. nth ( 1 ) == IDENT => adt:: union ( p, m) ,
271251
272- // test pub_macro_def
273- // pub macro m($:ident) {}
274- T ! [ macro] => {
275- macro_def ( p, m) ;
276- }
277- IDENT if p. at_contextual_kw ( "macro_rules" ) && p. nth ( 1 ) == BANG => {
278- macro_rules ( p, m) ;
279- }
252+ T ! [ macro] => macro_def ( p, m) ,
253+ IDENT if p. at_contextual_kw ( "macro_rules" ) && p. nth ( 1 ) == BANG => macro_rules ( p, m) ,
280254
281255 T ! [ const ] if ( la == IDENT || la == T ! [ _] || la == T ! [ mut ] ) => consts:: konst ( p, m) ,
282256 T ! [ static ] => consts:: static_ ( p, m) ,
@@ -286,26 +260,78 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
286260 Ok ( ( ) )
287261}
288262
263+ // test extern_crate
264+ // extern crate foo;
289265fn extern_crate ( p : & mut Parser , m : Marker ) {
290- assert ! ( p. at( T ![ extern] ) ) ;
291266 p. bump ( T ! [ extern] ) ;
292-
293- assert ! ( p. at( T ![ crate ] ) ) ;
294267 p. bump ( T ! [ crate ] ) ;
295268
296269 if p. at ( T ! [ self ] ) {
270+ // test extern_crate_self
271+ // extern crate self;
297272 let m = p. start ( ) ;
298273 p. bump ( T ! [ self ] ) ;
299274 m. complete ( p, NAME_REF ) ;
300275 } else {
301276 name_ref ( p) ;
302277 }
303278
279+ // test extern_crate_rename
280+ // extern crate foo as bar;
304281 opt_rename ( p) ;
305282 p. expect ( T ! [ ; ] ) ;
306283 m. complete ( p, EXTERN_CRATE ) ;
307284}
308285
286+ // test mod_item
287+ // mod a;
288+ pub ( crate ) fn mod_item ( p : & mut Parser , m : Marker ) {
289+ p. bump ( T ! [ mod ] ) ;
290+ name ( p) ;
291+ if p. at ( T ! [ '{' ] ) {
292+ // test mod_item_curly
293+ // mod b { }
294+ item_list ( p) ;
295+ } else if !p. eat ( T ! [ ; ] ) {
296+ p. error ( "expected `;` or `{`" ) ;
297+ }
298+ m. complete ( p, MODULE ) ;
299+ }
300+
301+ // test type_alias
302+ // type Foo = Bar;
303+ fn type_alias ( p : & mut Parser , m : Marker ) {
304+ p. bump ( T ! [ type ] ) ;
305+
306+ name ( p) ;
307+
308+ // test type_item_type_params
309+ // type Result<T> = ();
310+ type_params:: opt_generic_param_list ( p) ;
311+
312+ if p. at ( T ! [ : ] ) {
313+ type_params:: bounds ( p) ;
314+ }
315+
316+ // test type_item_where_clause
317+ // type Foo where Foo: Copy = ();
318+ type_params:: opt_where_clause ( p) ;
319+ if p. eat ( T ! [ =] ) {
320+ types:: type_ ( p) ;
321+ }
322+ p. expect ( T ! [ ; ] ) ;
323+ m. complete ( p, TYPE_ALIAS ) ;
324+ }
325+
326+ pub ( crate ) fn item_list ( p : & mut Parser ) {
327+ assert ! ( p. at( T ![ '{' ] ) ) ;
328+ let m = p. start ( ) ;
329+ p. bump ( T ! [ '{' ] ) ;
330+ mod_contents ( p, true ) ;
331+ p. expect ( T ! [ '}' ] ) ;
332+ m. complete ( p, ITEM_LIST ) ;
333+ }
334+
309335pub ( crate ) fn extern_item_list ( p : & mut Parser ) {
310336 assert ! ( p. at( T ![ '{' ] ) ) ;
311337 let m = p. start ( ) ;
@@ -347,54 +373,6 @@ fn fn_(p: &mut Parser) {
347373 }
348374}
349375
350- // test type_item
351- // type Foo = Bar;
352- fn type_alias ( p : & mut Parser , m : Marker ) {
353- assert ! ( p. at( T ![ type ] ) ) ;
354- p. bump ( T ! [ type ] ) ;
355-
356- name ( p) ;
357-
358- // test type_item_type_params
359- // type Result<T> = ();
360- type_params:: opt_generic_param_list ( p) ;
361-
362- if p. at ( T ! [ : ] ) {
363- type_params:: bounds ( p) ;
364- }
365-
366- // test type_item_where_clause
367- // type Foo where Foo: Copy = ();
368- type_params:: opt_where_clause ( p) ;
369- if p. eat ( T ! [ =] ) {
370- types:: type_ ( p) ;
371- }
372- p. expect ( T ! [ ; ] ) ;
373- m. complete ( p, TYPE_ALIAS ) ;
374- }
375-
376- pub ( crate ) fn mod_item ( p : & mut Parser , m : Marker ) {
377- assert ! ( p. at( T ![ mod ] ) ) ;
378- p. bump ( T ! [ mod ] ) ;
379-
380- name ( p) ;
381- if p. at ( T ! [ '{' ] ) {
382- item_list ( p) ;
383- } else if !p. eat ( T ! [ ; ] ) {
384- p. error ( "expected `;` or `{`" ) ;
385- }
386- m. complete ( p, MODULE ) ;
387- }
388-
389- pub ( crate ) fn item_list ( p : & mut Parser ) {
390- assert ! ( p. at( T ![ '{' ] ) ) ;
391- let m = p. start ( ) ;
392- p. bump ( T ! [ '{' ] ) ;
393- mod_contents ( p, true ) ;
394- p. expect ( T ! [ '}' ] ) ;
395- m. complete ( p, ITEM_LIST ) ;
396- }
397-
398376fn macro_rules ( p : & mut Parser , m : Marker ) {
399377 assert ! ( p. at_contextual_kw( "macro_rules" ) ) ;
400378 p. bump_remap ( T ! [ macro_rules] ) ;
@@ -429,23 +407,24 @@ fn macro_rules(p: &mut Parser, m: Marker) {
429407}
430408
431409// test macro_def
432- // macro m { ($i:ident) => {} }
433410// macro m($i:ident) {}
434411fn macro_def ( p : & mut Parser , m : Marker ) {
435412 p. expect ( T ! [ macro] ) ;
436413 name_r ( p, ITEM_RECOVERY_SET ) ;
437414 if p. at ( T ! [ '{' ] ) {
415+ // test macro_def_curly
416+ // macro m { ($i:ident) => {} }
438417 token_tree ( p) ;
439- } else if !p. at ( T ! [ '(' ] ) {
440- p. error ( "unmatched `(`" ) ;
441- } else {
418+ } else if p. at ( T ! [ '(' ] ) {
442419 let m = p. start ( ) ;
443420 token_tree ( p) ;
444421 match p. current ( ) {
445422 T ! [ '{' ] | T ! [ '[' ] | T ! [ '(' ] => token_tree ( p) ,
446423 _ => p. error ( "expected `{`, `[`, `(`" ) ,
447424 }
448425 m. complete ( p, TOKEN_TREE ) ;
426+ } else {
427+ p. error ( "unmatched `(`" ) ;
449428 }
450429
451430 m. complete ( p, MACRO_DEF ) ;
0 commit comments