diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-09-21 15:18:36 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-09-26 13:19:14 +1000 |
| commit | 9640d1c02354dc3167f775e56629aaf8974e78f7 (patch) | |
| tree | 29109dd5a8ddd6f225a702f38a7c4665f92d9309 /compiler/rustc_parse/src | |
| parent | 14281e614759f6e761a01acaf9dccbd0da4ff1ef (diff) | |
| download | rust-9640d1c02354dc3167f775e56629aaf8974e78f7.tar.gz rust-9640d1c02354dc3167f775e56629aaf8974e78f7.zip | |
Move `#!` checking.
Currently does the "is this a `#!` at the start of the file?" check for every single token(!) This commit moves it so it only happens once.
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/lexer/mod.rs | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/compiler/rustc_parse/src/lexer/mod.rs b/compiler/rustc_parse/src/lexer/mod.rs index 394a5b86480..0e8a739fb62 100644 --- a/compiler/rustc_parse/src/lexer/mod.rs +++ b/compiler/rustc_parse/src/lexer/mod.rs @@ -38,10 +38,16 @@ pub struct UnmatchedBrace { pub(crate) fn parse_token_trees<'a>( sess: &'a ParseSess, - src: &'a str, - start_pos: BytePos, + mut src: &'a str, + mut start_pos: BytePos, override_span: Option<Span>, ) -> (PResult<'a, TokenStream>, Vec<UnmatchedBrace>) { + // Skip `#!`, if present. + if let Some(shebang_len) = rustc_lexer::strip_shebang(src) { + src = &src[shebang_len..]; + start_pos = start_pos + BytePos::from_usize(shebang_len); + } + StringReader { sess, start_pos, pos: start_pos, src, override_span }.into_token_trees() } @@ -65,13 +71,6 @@ impl<'a> StringReader<'a> { fn next_token(&mut self) -> (Spacing, Token) { let mut spacing = Spacing::Joint; - // Skip `#!` at the start of the file - if self.pos == self.start_pos - && let Some(shebang_len) = rustc_lexer::strip_shebang(self.src) - { - self.pos = self.pos + BytePos::from_usize(shebang_len); - } - // Skip trivial (whitespace & comments) tokens loop { let start_src_index = self.src_index(self.pos); |
