-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an option to the parser to avoid parsing out of line modules #42071
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,6 +179,8 @@ pub struct Parser<'a> { | |
pub obsolete_set: HashSet<ObsoleteSyntax>, | ||
/// Used to determine the path to externally loaded source files | ||
pub directory: Directory, | ||
/// Whether to parse sub-modules in other files. | ||
pub recurse_into_file_modules: bool, | ||
/// Name of the root module this parser originated from. If `None`, then the | ||
/// name is not known. This does not change while the parser is descending | ||
/// into modules, and sub-parsers have new values for this name. | ||
|
@@ -190,6 +192,7 @@ pub struct Parser<'a> { | |
pub cfg_mods: bool, | ||
} | ||
|
||
|
||
struct TokenCursor { | ||
frame: TokenCursorFrame, | ||
stack: Vec<TokenCursorFrame>, | ||
|
@@ -439,6 +442,7 @@ impl<'a> Parser<'a> { | |
pub fn new(sess: &'a ParseSess, | ||
tokens: TokenStream, | ||
directory: Option<Directory>, | ||
recurse_into_file_modules: bool, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really care, but adding the new argument "next-to-last" seems a bit error-prone, as I imagine people may be inclined add the new argument at the end of the list. More generally, having a lot of booleans is a crappy API, it'd be sort of nice to make this a builder-style thing... but I guess that's for another PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put it here to be next to the |
||
desugar_doc_comments: bool) | ||
-> Self { | ||
let mut parser = Parser { | ||
|
@@ -450,6 +454,7 @@ impl<'a> Parser<'a> { | |
prev_token_kind: PrevTokenKind::Other, | ||
restrictions: Restrictions::empty(), | ||
obsolete_set: HashSet::new(), | ||
recurse_into_file_modules: recurse_into_file_modules, | ||
directory: Directory { path: PathBuf::new(), ownership: DirectoryOwnership::Owned }, | ||
root_module_name: None, | ||
expected_tokens: Vec::new(), | ||
|
@@ -467,12 +472,14 @@ impl<'a> Parser<'a> { | |
let tok = parser.next_tok(); | ||
parser.token = tok.tok; | ||
parser.span = tok.sp; | ||
|
||
if let Some(directory) = directory { | ||
parser.directory = directory; | ||
} else if parser.span != syntax_pos::DUMMY_SP { | ||
parser.directory.path = PathBuf::from(sess.codemap().span_to_filename(parser.span)); | ||
parser.directory.path.pop(); | ||
} | ||
|
||
parser.process_potential_macro_variable(); | ||
parser | ||
} | ||
|
@@ -3921,6 +3928,7 @@ impl<'a> Parser<'a> { | |
mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock); | ||
let item = self.parse_item_(attrs.clone(), false, true)?; | ||
self.directory.ownership = old_directory_ownership; | ||
|
||
match item { | ||
Some(i) => Stmt { | ||
id: ast::DUMMY_NODE_ID, | ||
|
@@ -5254,7 +5262,7 @@ impl<'a> Parser<'a> { | |
let id = self.parse_ident()?; | ||
if self.check(&token::Semi) { | ||
self.bump(); | ||
if in_cfg { | ||
if in_cfg && self.recurse_into_file_modules { | ||
// This mod is in an external file. Let's go get it! | ||
let ModulePathSuccess { path, directory_ownership, warn } = | ||
self.submod_path(id, &outer_attrs, id_span)?; | ||
|
@@ -5281,10 +5289,12 @@ impl<'a> Parser<'a> { | |
} else { | ||
let old_directory = self.directory.clone(); | ||
self.push_directory(id, &outer_attrs); | ||
|
||
self.expect(&token::OpenDelim(token::Brace))?; | ||
let mod_inner_lo = self.span; | ||
let attrs = self.parse_inner_attributes()?; | ||
let module = self.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo)?; | ||
|
||
self.directory = old_directory; | ||
Ok((id, ItemKind::Mod(module), Some(attrs))) | ||
} | ||
|
@@ -5347,7 +5357,8 @@ impl<'a> Parser<'a> { | |
fn submod_path(&mut self, | ||
id: ast::Ident, | ||
outer_attrs: &[ast::Attribute], | ||
id_sp: Span) -> PResult<'a, ModulePathSuccess> { | ||
id_sp: Span) | ||
-> PResult<'a, ModulePathSuccess> { | ||
if let Some(path) = Parser::submod_path_from_attr(outer_attrs, &self.directory.path) { | ||
return Ok(ModulePathSuccess { | ||
directory_ownership: match path.file_name().and_then(|s| s.to_str()) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you just using
false
here because we don't expect inline modules in attributes? In other words, we don't expect this option to matter here one way or the other, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct