Skip to content

Commit a10dada

Browse files
authored
perf(es/parser): Remove EOF check (#10976)
This PR removes certain EOF checks during parsing and modifies some error outputs. And I think these changes are considered acceptable.
1 parent e7dd33b commit a10dada

File tree

54 files changed

+99
-165
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+99
-165
lines changed

.changeset/famous-socks-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
swc_ecma_lexer: major
3+
---
4+
5+
refactor(es/parser): rm eof check

crates/swc_ecma_lexer/src/common/parser/buffer.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ pub trait Buffer<'a> {
6767
fn expect_jsx_text_token_and_bump(&mut self) -> (Atom, Atom);
6868
fn expect_shebang_token_and_bump(&mut self) -> Atom;
6969

70-
#[inline]
71-
fn knows_cur(&self) -> bool {
72-
!self.cur().is_eof()
73-
}
74-
7570
fn had_line_break_before_cur(&self) -> bool {
7671
self.get_cur().had_line_break()
7772
}

crates/swc_ecma_lexer/src/common/parser/class_and_fn.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -849,9 +849,11 @@ fn parse_class_member_with_is_static<'a, P: Parser<'a>>(
849849
let mut readonly = None;
850850
let mut modifier_span = None;
851851
let declare = declare_token.is_some();
852-
while let Some(modifier) =
852+
while let Some(modifier) = if p.input().syntax().typescript() {
853853
parse_ts_modifier(p, &["abstract", "readonly", "override", "static"], true)?
854-
{
854+
} else {
855+
None
856+
} {
855857
modifier_span = Some(p.input().prev_span());
856858
match modifier {
857859
"abstract" => {
@@ -1159,7 +1161,8 @@ fn parse_class_member_with_is_static<'a, P: Parser<'a>>(
11591161
{
11601162
// handle async foo(){}
11611163

1162-
if parse_ts_modifier(p, &["override"], false)?.is_some() {
1164+
if p.input().syntax().typescript() && parse_ts_modifier(p, &["override"], false)?.is_some()
1165+
{
11631166
is_override = true;
11641167
p.emit_err(
11651168
p.input().prev_span(),
@@ -1499,7 +1502,7 @@ fn parse_class_member<'a, P: Parser<'a>>(p: &mut P) -> PResult<ClassMember> {
14991502
fn parse_class_body<'a, P: Parser<'a>>(p: &mut P) -> PResult<Vec<ClassMember>> {
15001503
let mut elems = Vec::with_capacity(32);
15011504
let mut has_constructor_with_body = false;
1502-
while !eof!(p) && !p.input().is(&P::Token::RBRACE) {
1505+
while !p.input().is(&P::Token::RBRACE) {
15031506
if p.input_mut().eat(&P::Token::SEMI) {
15041507
let span = p.input().prev_span();
15051508
debug_assert!(span.lo <= span.hi);

crates/swc_ecma_lexer/src/common/parser/expr.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn parse_array_lit<'a, P: Parser<'a>>(p: &mut P) -> PResult<Box<Expr>> {
7373

7474
let mut elems = Vec::with_capacity(8);
7575

76-
while !eof!(p) && !p.input().is(&P::Token::RBRACKET) {
76+
while !p.input().is(&P::Token::RBRACKET) {
7777
if p.input().is(&P::Token::COMMA) {
7878
expect!(p, &P::Token::COMMA);
7979
elems.push(None);
@@ -289,7 +289,7 @@ pub fn parse_args<'a, P: Parser<'a>>(
289289
let mut first = true;
290290
let mut expr_or_spreads = Vec::with_capacity(2);
291291

292-
while !eof!(p) && !p.input().is(&P::Token::RPAREN) {
292+
while !p.input().is(&P::Token::RPAREN) {
293293
if first {
294294
first = false;
295295
} else {
@@ -1242,8 +1242,6 @@ pub fn parse_bin_expr<'a, P: Parser<'a>>(p: &mut P) -> PResult<Box<Expr>> {
12421242
{
12431243
p.emit_err(p.input().cur_span(), SyntaxError::TS1109);
12441244
Invalid { span: err.span() }.into()
1245-
} else if cur.is_eof() {
1246-
return Err(eof_error(p));
12471245
} else {
12481246
return Err(err);
12491247
}
@@ -1805,7 +1803,7 @@ fn parse_args_or_pats_inner<'a, P: Parser<'a>>(
18051803

18061804
// TODO(kdy1): optimize (once we parsed a pattern, we can parse everything else
18071805
// as a pattern instead of reparsing)
1808-
while !eof!(p) && !p.input().is(&P::Token::RPAREN) {
1806+
while !p.input().is(&P::Token::RPAREN) {
18091807
// https://github.com/swc-project/swc/issues/410
18101808
let is_async = p.input().is(&P::Token::ASYNC)
18111809
&& peek!(p).is_some_and(|t| t.is_lparen() || t.is_word() || t.is_function());

crates/swc_ecma_lexer/src/common/parser/macros.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ macro_rules! syntax_error {
5151
macro_rules! peek {
5252
($p:expr) => {{
5353
debug_assert!(
54-
$p.input().knows_cur(),
54+
!$p.input().cur().is_eof(),
5555
"parser should not call peek() without knowing current token.
5656
Current token is {:?}",
5757
$p.input().cur(),
@@ -82,13 +82,6 @@ macro_rules! debug_tracing {
8282
}};
8383
}
8484

85-
/// Returns true on eof.
86-
macro_rules! eof {
87-
($p:expr) => {
88-
$p.input().cur().is_eof()
89-
};
90-
}
91-
9285
macro_rules! return_if_arrow {
9386
($p:expr, $expr:expr) => {{
9487
// FIXME:

crates/swc_ecma_lexer/src/common/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub trait Parser<'a>: Sized + Clone {
253253
#[inline(always)]
254254
fn bump(&mut self) {
255255
debug_assert!(
256-
self.input().knows_cur(),
256+
!self.input().cur().is_eof(),
257257
"parser should not call bump() without knowing current token"
258258
);
259259
self.input_mut().bump()

crates/swc_ecma_lexer/src/common/parser/module_item.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,6 @@ fn parse_export<'a, P: Parser<'a>>(
458458
if cur.is_error() {
459459
let err = p.input_mut().expect_error_token_and_bump();
460460
return Err(err);
461-
} else if cur.is_eof() {
462-
return Err(eof_error(p));
463461
}
464462

465463
return parse_default_class(p, start, class_start, decorators, true)
@@ -660,7 +658,7 @@ fn parse_export<'a, P: Parser<'a>>(
660658

661659
expect!(p, &P::Token::LBRACE);
662660

663-
while !eof!(p) && !p.input().is(&P::Token::RBRACE) {
661+
while !p.input().is(&P::Token::RBRACE) {
664662
let specifier = parse_named_export_specifier(p, type_only)?;
665663
specifiers.push(ExportSpecifier::Named(specifier));
666664

@@ -871,7 +869,7 @@ fn parse_import<'a, P: Parser<'a>>(p: &mut P) -> PResult<ModuleItem> {
871869
}));
872870
// Named imports are only allowed in evaluation phase.
873871
} else if phase == ImportPhase::Evaluation && p.input_mut().eat(&P::Token::LBRACE) {
874-
while !eof!(p) && !p.input().is(&P::Token::RBRACE) {
872+
while !p.input().is(&P::Token::RBRACE) {
875873
specifiers.push(parse_import_specifier(p, type_only)?);
876874

877875
if p.input().is(&P::Token::RBRACE) {

crates/swc_ecma_lexer/src/common/parser/pat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ pub fn parse_array_binding_pat<'a, P: Parser<'a>>(p: &mut P) -> PResult<Pat> {
416416

417417
let mut rest_span = Span::default();
418418

419-
while !eof!(p) && !p.input().is(&P::Token::RBRACKET) {
419+
while !p.input().is(&P::Token::RBRACKET) {
420420
if p.input_mut().eat(&P::Token::COMMA) {
421421
elems.push(None);
422422
continue;
@@ -622,7 +622,7 @@ pub fn parse_constructor_params<'a, P: Parser<'a>>(p: &mut P) -> PResult<Vec<Par
622622
let mut params = Vec::new();
623623
let mut rest_span = Span::default();
624624

625-
while !eof!(p) && !p.input().is(&P::Token::RPAREN) {
625+
while !p.input().is(&P::Token::RPAREN) {
626626
if !rest_span.is_dummy() {
627627
p.emit_err(rest_span, SyntaxError::TS1014);
628628
}
@@ -676,7 +676,7 @@ pub fn parse_formal_params<'a, P: Parser<'a>>(p: &mut P) -> PResult<Vec<Param>>
676676
let mut params = Vec::new();
677677
let mut rest_span = Span::default();
678678

679-
while !eof!(p) && !p.input().is(&P::Token::RPAREN) {
679+
while !p.input().is(&P::Token::RPAREN) {
680680
if !rest_span.is_dummy() {
681681
p.emit_err(rest_span, SyntaxError::TS1014);
682682
}

crates/swc_ecma_lexer/src/common/parser/stmt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ pub fn parse_var_stmt<'a, P: Parser<'a>>(p: &mut P, for_loop: bool) -> PResult<B
255255
// var a,;
256256
//
257257
// NewLine is ok
258-
if p.input().is(&P::Token::SEMI) || eof!(p) {
258+
if p.input().is(&P::Token::SEMI) {
259259
let prev_span = p.input().prev_span();
260260
let span = if prev_span == var_span {
261261
Span::new_with_checked(prev_span.hi, prev_span.hi)
@@ -329,7 +329,7 @@ pub fn parse_using_decl<'a, P: Parser<'a>>(
329329
// var a,;
330330
//
331331
// NewLine is ok
332-
if p.input().is(&P::Token::SEMI) || eof!(p) {
332+
if p.input().is(&P::Token::SEMI) {
333333
let span = p.input().prev_span();
334334
p.emit_err(span, SyntaxError::TS1009);
335335
break;
@@ -975,7 +975,7 @@ fn parse_switch_stmt<'a, P: Parser<'a>>(p: &mut P) -> PResult<Stmt> {
975975
};
976976
expect!(p, &P::Token::COLON);
977977

978-
while !eof!(p) && {
978+
while {
979979
let cur = p.input().cur();
980980
!(cur.is_case() || cur.is_default() || cur.is_rbrace())
981981
} {

crates/swc_ecma_lexer/src/common/parser/typescript.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,6 @@ pub fn eat_any_ts_modifier<'a>(p: &mut impl Parser<'a>) -> PResult<bool> {
315315
if p.syntax().typescript()
316316
&& {
317317
let cur = p.input().cur();
318-
if cur.is_eof() {
319-
return Err(eof_error(p));
320-
}
321318
cur.is_public() || cur.is_protected() || cur.is_private() || cur.is_readonly()
322319
}
323320
&& peek!(p).is_some_and(|t| t.is_word() || t.is_lbrace() || t.is_lbracket())
@@ -337,9 +334,7 @@ pub fn parse_ts_modifier<'a, P: Parser<'a>>(
337334
allowed_modifiers: &[&'static str],
338335
stop_on_start_of_class_static_blocks: bool,
339336
) -> PResult<Option<&'static str>> {
340-
if !p.input().syntax().typescript() {
341-
return Ok(None);
342-
}
337+
debug_assert!(p.input().syntax().typescript());
343338
let pos = {
344339
let cur = p.input().cur();
345340
let modifier = if cur.is_unknown_ident() {
@@ -431,7 +426,7 @@ pub fn parse_ts_entity_name<'a, P: Parser<'a>>(
431426
while p.input_mut().eat(&P::Token::DOT) {
432427
let dot_start = p.input().cur_pos();
433428
let cur = p.input().cur();
434-
if cur.is_eof() || (!cur.is_hash() && !cur.is_word()) {
429+
if !cur.is_hash() && !cur.is_word() {
435430
p.emit_err(
436431
Span::new_with_checked(dot_start, dot_start),
437432
SyntaxError::TS1003,
@@ -2128,7 +2123,7 @@ pub fn parse_ts_interface_decl<'a, P: Parser<'a>>(
21282123
if p.input().is(&P::Token::EXTENDS) {
21292124
p.emit_err(p.input().cur_span(), SyntaxError::TS1172);
21302125

2131-
while !eof!(p) && !p.input().is(&P::Token::LBRACE) {
2126+
while !p.input().cur().is_eof() && !p.input().is(&P::Token::LBRACE) {
21322127
p.bump();
21332128
}
21342129
}

0 commit comments

Comments
 (0)