@@ -179,6 +179,8 @@ pub struct Parser<'a> {
179
179
pub obsolete_set : HashSet < ObsoleteSyntax > ,
180
180
/// Used to determine the path to externally loaded source files
181
181
pub directory : Directory ,
182
+ /// Whether to parse sub-modules in other files.
183
+ pub recurse_into_file_modules : bool ,
182
184
/// Name of the root module this parser originated from. If `None`, then the
183
185
/// name is not known. This does not change while the parser is descending
184
186
/// into modules, and sub-parsers have new values for this name.
@@ -190,6 +192,7 @@ pub struct Parser<'a> {
190
192
pub cfg_mods : bool ,
191
193
}
192
194
195
+
193
196
struct TokenCursor {
194
197
frame : TokenCursorFrame ,
195
198
stack : Vec < TokenCursorFrame > ,
@@ -439,6 +442,7 @@ impl<'a> Parser<'a> {
439
442
pub fn new ( sess : & ' a ParseSess ,
440
443
tokens : TokenStream ,
441
444
directory : Option < Directory > ,
445
+ recurse_into_file_modules : bool ,
442
446
desugar_doc_comments : bool )
443
447
-> Self {
444
448
let mut parser = Parser {
@@ -450,6 +454,7 @@ impl<'a> Parser<'a> {
450
454
prev_token_kind : PrevTokenKind :: Other ,
451
455
restrictions : Restrictions :: empty ( ) ,
452
456
obsolete_set : HashSet :: new ( ) ,
457
+ recurse_into_file_modules : recurse_into_file_modules,
453
458
directory : Directory { path : PathBuf :: new ( ) , ownership : DirectoryOwnership :: Owned } ,
454
459
root_module_name : None ,
455
460
expected_tokens : Vec :: new ( ) ,
@@ -467,12 +472,14 @@ impl<'a> Parser<'a> {
467
472
let tok = parser. next_tok ( ) ;
468
473
parser. token = tok. tok ;
469
474
parser. span = tok. sp ;
475
+
470
476
if let Some ( directory) = directory {
471
477
parser. directory = directory;
472
478
} else if parser. span != syntax_pos:: DUMMY_SP {
473
479
parser. directory . path = PathBuf :: from ( sess. codemap ( ) . span_to_filename ( parser. span ) ) ;
474
480
parser. directory . path . pop ( ) ;
475
481
}
482
+
476
483
parser. process_potential_macro_variable ( ) ;
477
484
parser
478
485
}
@@ -3921,6 +3928,7 @@ impl<'a> Parser<'a> {
3921
3928
mem:: replace ( & mut self . directory . ownership , DirectoryOwnership :: UnownedViaBlock ) ;
3922
3929
let item = self . parse_item_ ( attrs. clone ( ) , false , true ) ?;
3923
3930
self . directory . ownership = old_directory_ownership;
3931
+
3924
3932
match item {
3925
3933
Some ( i) => Stmt {
3926
3934
id : ast:: DUMMY_NODE_ID ,
@@ -5254,7 +5262,7 @@ impl<'a> Parser<'a> {
5254
5262
let id = self . parse_ident ( ) ?;
5255
5263
if self . check ( & token:: Semi ) {
5256
5264
self . bump ( ) ;
5257
- if in_cfg {
5265
+ if in_cfg && self . recurse_into_file_modules {
5258
5266
// This mod is in an external file. Let's go get it!
5259
5267
let ModulePathSuccess { path, directory_ownership, warn } =
5260
5268
self . submod_path ( id, & outer_attrs, id_span) ?;
@@ -5281,10 +5289,12 @@ impl<'a> Parser<'a> {
5281
5289
} else {
5282
5290
let old_directory = self . directory . clone ( ) ;
5283
5291
self . push_directory ( id, & outer_attrs) ;
5292
+
5284
5293
self . expect ( & token:: OpenDelim ( token:: Brace ) ) ?;
5285
5294
let mod_inner_lo = self . span ;
5286
5295
let attrs = self . parse_inner_attributes ( ) ?;
5287
5296
let module = self . parse_mod_items ( & token:: CloseDelim ( token:: Brace ) , mod_inner_lo) ?;
5297
+
5288
5298
self . directory = old_directory;
5289
5299
Ok ( ( id, ItemKind :: Mod ( module) , Some ( attrs) ) )
5290
5300
}
@@ -5347,7 +5357,8 @@ impl<'a> Parser<'a> {
5347
5357
fn submod_path ( & mut self ,
5348
5358
id : ast:: Ident ,
5349
5359
outer_attrs : & [ ast:: Attribute ] ,
5350
- id_sp : Span ) -> PResult < ' a , ModulePathSuccess > {
5360
+ id_sp : Span )
5361
+ -> PResult < ' a , ModulePathSuccess > {
5351
5362
if let Some ( path) = Parser :: submod_path_from_attr ( outer_attrs, & self . directory . path ) {
5352
5363
return Ok ( ModulePathSuccess {
5353
5364
directory_ownership : match path. file_name ( ) . and_then ( |s| s. to_str ( ) ) {
0 commit comments