@@ -31,14 +31,10 @@ impl<'a> Parser<'a> {
31
31
/// Parses a source module as a crate. This is the main entry point for the parser.
32
32
pub fn parse_crate_mod ( & mut self ) -> PResult < ' a , Crate > {
33
33
let lo = self . token . span ;
34
- let krate = Ok ( ast:: Crate {
35
- attrs : self . parse_inner_attributes ( ) ?,
36
- module : self . parse_mod_items ( & token:: Eof , lo) ?,
37
- span : lo. to ( self . token . span ) ,
38
- // Filled in by proc_macro_harness::inject()
39
- proc_macros : Vec :: new ( ) ,
40
- } ) ;
41
- krate
34
+ let ( module, attrs) = self . parse_mod ( & token:: Eof ) ?;
35
+ let span = lo. to ( self . token . span ) ;
36
+ let proc_macros = Vec :: new ( ) ; // Filled in by `proc_macro_harness::inject()`.
37
+ Ok ( ast:: Crate { attrs, module, span, proc_macros } )
42
38
}
43
39
44
40
/// Parses a `mod <foo> { ... }` or `mod <foo>;` item.
@@ -60,17 +56,23 @@ impl<'a> Parser<'a> {
60
56
self . push_directory ( id, & attrs) ;
61
57
62
58
self . expect ( & token:: OpenDelim ( token:: Brace ) ) ?;
63
- let mod_inner_lo = self . token . span ;
64
- let inner_attrs = self . parse_inner_attributes ( ) ?;
65
- let module = self . parse_mod_items ( & token:: CloseDelim ( token:: Brace ) , mod_inner_lo) ?;
59
+ let module = self . parse_mod ( & token:: CloseDelim ( token:: Brace ) ) ?;
66
60
67
61
self . directory = old_directory;
68
- ( module, inner_attrs )
62
+ module
69
63
} ;
70
64
attrs. append ( & mut inner_attrs) ;
71
65
Ok ( ( id, ItemKind :: Mod ( module) ) )
72
66
}
73
67
68
+ /// Parses the contents of a module (inner attributes followed by module items).
69
+ fn parse_mod ( & mut self , term : & TokenKind ) -> PResult < ' a , ( Mod , Vec < Attribute > ) > {
70
+ let lo = self . token . span ;
71
+ let attrs = self . parse_inner_attributes ( ) ?;
72
+ let module = self . parse_mod_items ( term, lo) ?;
73
+ Ok ( ( module, attrs) )
74
+ }
75
+
74
76
/// Given a termination token, parses all of the items in a module.
75
77
fn parse_mod_items ( & mut self , term : & TokenKind , inner_lo : Span ) -> PResult < ' a , Mod > {
76
78
let mut items = vec ! [ ] ;
@@ -271,12 +273,11 @@ impl<'a> Parser<'a> {
271
273
let mut p0 =
272
274
new_sub_parser_from_file ( self . sess , & path, directory_ownership, Some ( name) , id_sp) ;
273
275
p0. cfg_mods = self . cfg_mods ;
274
- let mod_inner_lo = p0. token . span ;
275
- let mod_attrs = p0. parse_inner_attributes ( ) ?;
276
- let mut m0 = p0. parse_mod_items ( & token:: Eof , mod_inner_lo) ?;
277
- m0. inline = false ;
276
+ let mut module = p0. parse_mod ( & token:: Eof ) ?;
277
+ module. 0 . inline = false ;
278
+
278
279
self . sess . included_mod_stack . borrow_mut ( ) . pop ( ) ;
279
- Ok ( ( m0 , mod_attrs ) )
280
+ Ok ( module )
280
281
}
281
282
282
283
fn push_directory ( & mut self , id : Ident , attrs : & [ Attribute ] ) {
0 commit comments