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
6 changes: 6 additions & 0 deletions .changeset/fluffy-cobras-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_parser: patch
---

perf(es/parser): optimize `skip_space`
49 changes: 5 additions & 44 deletions crates/swc_ecma_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl<'a> Lexer<'a> {
if self.state.had_line_break && C == b'-' && self.eat(b'>') {
self.emit_module_mode_error(start, SyntaxError::LegacyCommentInModule);
self.skip_line_comment(0);
self.skip_space::<true>();
self.skip_space();
return self.read_token();
}

Expand Down Expand Up @@ -303,7 +303,7 @@ impl<'a> Lexer<'a> {
if had_line_break_before_last && self.is_str("====") {
self.emit_error_span(fixed_len_span(start, 7), SyntaxError::TS1185);
self.skip_line_comment(4);
self.skip_space::<true>();
self.skip_space();
return self.read_token();
}

Expand Down Expand Up @@ -347,7 +347,7 @@ impl Lexer<'_> {
if C == b'<' && self.is(b'!') && self.peek() == Some('-') && self.peek_ahead() == Some('-')
{
self.skip_line_comment(3);
self.skip_space::<true>();
self.skip_space();
self.emit_module_mode_error(start, SyntaxError::LegacyCommentInModule);

return self.read_token();
Expand Down Expand Up @@ -399,7 +399,7 @@ impl Lexer<'_> {
{
self.emit_error_span(fixed_len_span(start, 7), SyntaxError::TS1185);
self.skip_line_comment(5);
self.skip_space::<true>();
self.skip_space();
return self.read_token();
}

Expand Down Expand Up @@ -871,45 +871,6 @@ impl<'a> Lexer<'a> {
}
}

/// Skip comments or whitespaces.
///
/// See https://tc39.github.io/ecma262/#sec-white-space
#[inline(never)]
fn skip_space<const LEX_COMMENTS: bool>(&mut self) {
loop {
let (offset, newline) = {
let mut skip = self::whitespace::SkipWhitespace {
input: self.input().as_str(),
newline: false,
offset: 0,
};

skip.scan();

(skip.offset, skip.newline)
};

self.input_mut().bump_bytes(offset as usize);
if newline {
self.state_mut().mark_had_line_break();
}

if LEX_COMMENTS && self.input().is_byte(b'/') {
if let Some(c) = self.peek() {
if c == '/' {
self.skip_line_comment(2);
continue;
} else if c == '*' {
self.skip_block_comment();
continue;
}
}
}

break;
}
}

/// Ensure that ident cannot directly follow numbers.
fn ensure_not_ident(&mut self) -> LexResult<()> {
match self.cur() {
Expand Down Expand Up @@ -2178,7 +2139,7 @@ impl<'a> Lexer<'a> {
let span = fixed_len_span(start, 7);
self.emit_error_span(span, SyntaxError::TS1185);
self.skip_line_comment(5);
self.skip_space::<true>();
self.skip_space();
return self.error_span(span, SyntaxError::TS1185);
}

Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_parser/src/lexer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl crate::input::Tokens for Lexer<'_> {
}

fn scan_jsx_open_el_terminal_token(&mut self) -> TokenAndSpan {
self.skip_space::<true>();
self.skip_space();
let start = self.input.cur_pos();
let res = match self.scan_jsx_attrs_terminal_token() {
Ok(res) => Ok(res),
Expand Down Expand Up @@ -382,7 +382,7 @@ impl Lexer<'_> {
self.state.had_line_break = self.state.is_first;
self.state.is_first = false;

self.skip_space::<true>();
self.skip_space();
*start = self.input.cur_pos();

if self.input.last_pos() == self.input.end_pos() {
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_parser/src/lexer/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ use crate::{
pub(super) type ByteHandler = fn(&mut Lexer<'_>) -> LexResult<Token>;

/// Lookup table mapping any incoming byte to a handler function defined below.
#[rustfmt::skip]
pub(super) static BYTE_HANDLERS: [ByteHandler; 256] = [
// 0 1 2 3 4 5 6 7 8 9 A B C D E F //
// 0 1 2 3 4 5 6 7 8 9 A B C D E F //
ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, // 0
ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, // 1
ERR, EXL, QOT, HSH, IDN, PRC, AMP, QOT, PNO, PNC, ATR, PLS, COM, MIN, PRD, SLH, // 2
Expand Down
Loading
Loading