Skip to content

Commit 03712a8

Browse files
authored
Rollup merge of rust-lang#60995 - topecongiro:parser-from-stream-and-base-dir, r=michaelwoerister
Add stream_to_parser_with_base_dir This PR adds `stream_to_parser_with_base_dir`, which creates a parser from a token stream and a base directory. Context: I would like to parse `cfg_if!` macro and get a list of modules defined inside it from rustfmt so that rustfmt can format those modules (cc rust-lang/rustfmt#3253). To do so, I need to create a parser from `TokenStream` and set the directory of `Parser` to the same directory as the parent directory of a file which contains `cfg_if!` invocation. AFAIK there is no way to achieve this, and hence this PR. Alternatively, I could change the visibility of `Parser.directory` from `crate` to `pub` so that the value can be modified after initializing a parser. I don't have a preference over either approach (or others, as long as it works).
2 parents 643db3a + 1f1a917 commit 03712a8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libsyntax/parse/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,23 @@ pub fn stream_to_parser(sess: &ParseSess, stream: TokenStream) -> Parser<'_> {
332332
Parser::new(sess, stream, None, true, false)
333333
}
334334

335+
/// Given stream, the `ParseSess` and the base directory, produces a parser.
336+
///
337+
/// Use this function when you are creating a parser from the token stream
338+
/// and also care about the current working directory of the parser (e.g.,
339+
/// you are trying to resolve modules defined inside a macro invocation).
340+
///
341+
/// # Note
342+
///
343+
/// The main usage of this function is outside of rustc, for those who uses
344+
/// libsyntax as a library. Please do not remove this function while refactoring
345+
/// just because it is not used in rustc codebase!
346+
pub fn stream_to_parser_with_base_dir<'a>(sess: &'a ParseSess,
347+
stream: TokenStream,
348+
base_dir: Directory<'a>) -> Parser<'a> {
349+
Parser::new(sess, stream, Some(base_dir), true, false)
350+
}
351+
335352
/// A sequence separator.
336353
pub struct SeqSep {
337354
/// The seperator token.

0 commit comments

Comments
 (0)