Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions crates/oxc_parser/src/lexer/byte_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,7 @@ ascii_byte_handler!(QOS(lexer) {
// #
ascii_byte_handler!(HAS(lexer) {
lexer.consume_char();
// HashbangComment ::
// `#!` SingleLineCommentChars?
if lexer.token.start() == 0 && lexer.next_ascii_byte_eq(b'!') {
lexer.read_hashbang_comment()
} else {
lexer.private_identifier()
}
lexer.private_identifier()
});

// `A..=Z`, `a..=z` (except special cases below), `_`, `$`
Expand Down
16 changes: 13 additions & 3 deletions crates/oxc_parser/src/lexer/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,25 @@ impl<'a> Lexer<'a> {
}
}

/// Section 12.5 Hashbang Comments
pub(super) fn read_hashbang_comment(&mut self) -> Kind {
/// Section 12.5 Hashbang Comments.
///
/// # SAFETY
/// Next 2 bytes must be `#!`.
pub(super) unsafe fn read_hashbang_comment(&mut self) -> Kind {
debug_assert!(self.peek_2_bytes() == Some([b'#', b'!']));

// SAFETY: Caller guarantees next 2 bytes are `#!`
unsafe {
self.source.next_byte_unchecked();
self.source.next_byte_unchecked();
}

while let Some(c) = self.peek_char() {
if is_line_terminator(c) {
break;
}
self.consume_char();
}
self.token.set_is_on_new_line(true);
Kind::HashbangComment
}
}
17 changes: 16 additions & 1 deletion crates/oxc_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,21 @@ impl<'a> Lexer<'a> {
self.context = context;
}

/// Main entry point
/// Read first token in file.
pub fn first_token(&mut self) -> Token {
// HashbangComment ::
// `#!` SingleLineCommentChars?
let kind = if let Some([b'#', b'!']) = self.peek_2_bytes() {
// SAFETY: Next 2 bytes are `#!`
unsafe { self.read_hashbang_comment() }
} else {
self.read_next_token()
};
self.finish_next(kind)
}

/// Read next token in file.
/// Use `first_token` for first token, and this method for all further tokens.
pub fn next_token(&mut self) -> Token {
let kind = self.read_next_token();
self.finish_next(kind)
Expand Down Expand Up @@ -273,6 +287,7 @@ impl<'a> Lexer<'a> {

/// Read each char and set the current token
/// Whitespace and line terminators are skipped
#[inline] // Make sure is inlined into `next_token`
fn read_next_token(&mut self) -> Kind {
self.trivia_builder.has_pure_comment = false;
self.trivia_builder.has_no_side_effects_comment = false;
Expand Down
5 changes: 3 additions & 2 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,9 @@ impl<'a> ParserImpl<'a> {

#[expect(clippy::cast_possible_truncation)]
fn parse_program(&mut self) -> Program<'a> {
// initialize cur_token and prev_token by moving onto the first token
self.bump_any();
// Initialize by moving onto the first token.
// Checks for hashbang comment.
self.token = self.lexer.first_token();

let hashbang = self.parse_hashbang();
let (directives, statements) =
Expand Down
4 changes: 3 additions & 1 deletion tasks/benchmark/benches/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ fn bench_lexer(criterion: &mut Criterion) {
let mut allocator = Allocator::default();
b.iter(|| {
let mut lexer = Lexer::new_for_benchmarks(&allocator, source_text, source_type);
while lexer.next_token().kind() != Kind::Eof {}
if lexer.first_token().kind() != Kind::Eof {
while lexer.next_token().kind() != Kind::Eof {}
}
allocator.reset();
});
});
Expand Down
Loading