8000 refactor(es/parser): Remove `cur!(false)` macro by bvanjoi · Pull Request #10583 · swc-project/swc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor(es/parser): Remove cur!(false) macro #10583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
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
16 changes: 8 additions & 8 deletions crates/swc_ecma_lexer/src/common/parser/expr.rs
8000
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ fn parse_subscript<'a, P: Parser<'a>>(
no_computed_member: bool,
) -> PResult<(Box<Expr>, bool)> {
trace_cur!(p, parse_subscript);
let _ = cur!(p, false);
p.input_mut().cur();

if p.input().syntax().typescript() {
if !p.input_mut().had_line_break_before_cur() && p.input_mut().is(&P::Token::BANG) {
Expand Down Expand Up @@ -1312,7 +1312,7 @@ fn parse_bin_op_recursively_inner<'a, P: Parser<'a>>(
let expr = left;
let node = if peek!(p).is_some_and(|cur| cur.is_const()) {
p.bump(); // as
let _ = cur!(p, false);
p.input_mut().cur();
p.bump(); // const
TsConstAssertion {
span: p.span(start),
Expand Down Expand Up @@ -1352,9 +1352,9 @@ fn parse_bin_op_recursively_inner<'a, P: Parser<'a>>(

let ctx = p.ctx();
// Return left on eof
let word = match cur!(p, false) {
Ok(cur) => cur,
Err(..) => return Ok((left, None)),
let word = match p.input_mut().cur() {
Some(cur) => cur,
None => return Ok((left, None)),
};
let op = if word.is_in() && ctx.contains(Context::IncludeInExpr) {
op!("in")
Expand Down Expand Up @@ -1742,7 +1742,7 @@ pub fn parse_lhs_expr<'a, P: Parser<'a>>(p: &mut P) -> PResult<Box<Expr>> {
expect!(p, &P::Token::LPAREN);
}
debug_assert!(
!cur!(p, false).is_ok_and(|cur| cur.is_lparen()),
!p.input_mut().cur().is_some_and(|cur| cur.is_lparen()),
"parse_new_expr() should eat paren if it exists"
);
return Ok(NewExpr { type_args, ..ne }.into());
Expand Down Expand Up @@ -1857,7 +1857,7 @@ fn parse_args_or_pats_inner<'a, P: Parser<'a>>(
} else {
let mut expr = parse_bin_expr(p)?;

if cur!(p, false).is_ok_and(|t| t.is_assign_op()) {
if p.input_mut().cur().is_some_and(|t| t.is_assign_op()) {
expr = finish_assignment_expr(p, start, expr)?
}

Expand All @@ -1876,7 +1876,7 @@ fn parse_args_or_pats_inner<'a, P: Parser<'a>>(
peek.is_comma() || peek.is_equal() || peek.is_rparen() || peek.is_colon()
}) {
p.assert_and_bump(&P::Token::QUESTION)?;
let _ = cur!(p, false);
p.input_mut().cur();
if arg.spread.is_some() {
p.emit_err(p.input().prev_span(), SyntaxError::TS1047);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_lexer/src/common/parser/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{

// https://tc39.es/ecma262/#prod-ModuleExportName
pub fn parse_module_export_name<'a, P: Parser<'a>>(p: &mut P) -> PResult<ModuleExportName> {
let Ok(cur) = cur!(p, false) else {
let Some(cur) = p.input_mut().cur() else {
unexpected!(p, "identifier or st 8000 ring");
};
let module_export_name = if cur.is_str() {
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_lexer/src/common/parser/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn parse_jsx_empty_expr<'a>(p: &mut impl Parser<'a>) -> JSXEmptyExpr {

pub fn parse_jsx_text<'a>(p: &mut impl Parser<'a>) -> JSXText {
debug_assert!(p.input().syntax().jsx());
debug_assert!(cur!(p, false).is_ok_and(|t| t.is_jsx_text()));
debug_assert!(p.input_mut().cur().is_some_and(|t| t.is_jsx_text()));
let token = p.bump();
let span = p.input().prev_span();
let (value, raw) = token.take_jsx_text(p.input_mut());
Expand Down Expand Up @@ -246,7 +246,7 @@ fn parse_jsx_opening_element_at<'a, P: Parser<'a>>(
pub fn parse_jsx_attrs<'a, P: Parser<'a>>(p: &mut P) -> PResult<Vec<JSXAttrOrSpread>> {
let mut attrs = Vec::with_capacity(8);

while cur!(p, false).is_ok() {
while p.input_mut().cur().is_some() {
trace_cur!(p, parse_jsx_opening__attrs_loop);

if p.input_mut()
Expand Down
17 changes: 2 additions & 15 deletions crates/swc_ecma_lexer/src/common/parser/macros.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/// cur!($parser, required:bool)
macro_rules! cur {
($p:expr, false) => {{
match $p.input_mut().cur() {
Some(c) => Ok(c),
None => {
let pos = $p.input().end_pos();
let last = Span::new(pos, pos);
Err(crate::error::Error::new(
last,
crate::error::SyntaxError::Eof,
))
}
}
}};
($p:expr, true) => {{
match $p.input_mut().cur() {
Some(c) => {
Expand Down Expand Up @@ -90,7 +77,7 @@ macro_rules! peek {
$p.input().knows_cur(),
"parser should not call peek() without knowing current token.
Current token is {:?}",
cur!($p, false),
$p.input_mut().cur(),
);
$p.input_mut().peek()
}};
Expand Down Expand Up @@ -121,7 +108,7 @@ macro_rules! debug_tracing {
/// Returns true on eof.
macro_rules! eof {
($p:expr) => {
cur!($p, false).is_err()
$p.input_mut().cur().is_none()
};
}

Expand Down
15 changes: 12 additions & 3 deletions crates/swc_ecma_lexer/src/common/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub trait Parser<'a>: Sized + Clone {

fn eat_general_semi(&mut self) -> bool {
if cfg!(feature = "debug") {
tracing::trace!("eat(';'): cur={:?}", cur!(self, false));
tracing::trace!("eat(';'): cur={:?}", self.input_mut().cur());
}
let Some(cur) = self.input_mut().cur() else {
return true;
Expand Down Expand Up @@ -472,11 +472,20 @@ pub trait Parser<'a>: Sized + Clone {
}

pub fn parse_shebang<'a>(p: &mut impl Parser<'a>) -> PResult<Option<Atom>> {
let cur = cur!(p, false);
Ok(if cur.is_ok_and(|t| t.is_shebang()) {
Ok(if p.input_mut().cur().is_some_and(|t| t.is_shebang()) {
let t = p.bump();
Some(t.take_shebang(p.input_mut()))
} else {
None
})
}

pub fn eof_error<'a, P: Parser<'a>>(p: &mut P) -> crate::error::Error {
debug_assert!(
p.input_mut().cur().is_none(),
"Parser should not call throw_eof_error() without knowing current token"
);
let pos = p.input().end_pos();
let last = Span::new(pos, pos);
crate::error::Error::new(last, crate::error::SyntaxError::Eof)
}
9 changes: 5 additions & 4 deletions crates/swc_ecma_lexer/src/common/parser/module_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn parse_from_clause_and_semi<'a, P: Parser<'a>>(
} else {
unexpected!(p, "a string literal")
};
let _ = cur!(p, false);
p.input_mut().cur();
let with = if p.input().syntax().import_attributes()
&& !p.input_mut().had_line_break_before_cur()
&& (p.input_mut().eat(&P::Token::ASSERT) || p.input_mut().eat(&P::Token::WITH))
Expand Down Expand Up @@ -758,9 +758,10 @@ fn parse_import<'a, P: Parser<'a>>(p: &mut P) -> PResult<ModuleItem> {
expect!(p, &P::Token::IMPORT);

// Handle import 'mod.js'
if cur!(p, false).is_ok_and(|cur| cur.is_str()) {
if p.input_mut().cur().is_some_and(|cur| cur.is_str()) {
let src = Box::new(parse_str_lit(p));
let _ = cur!(p, false);
debug_assert!(p.input_mut().get_cur().is_none());
p.input_mut().cur();
let with = if p.input().syntax().import_attributes()
&& !p.input_mut().had_line_break_before_cur()
&& (p.input_mut().eat(&P::Token::ASSERT) || p.input_mut().eat(&P::Token::WITH))
Expand Down Expand Up @@ -886,7 +887,7 @@ fn parse_import<'a, P: Parser<'a>>(p: &mut P) -> PResult<ModuleItem> {
}
};

let _ = cur!(p, false);
p.input_mut().cur();
let with = if p.input().syntax().import_attributes()
&& !p.input_mut().had_line_break_before_cur()
&& (p.input_mut().eat(&P::Token::ASSERT) || p.input_mut().eat(&P::Token::WITH))
Expand Down
11 changes: 7 additions & 4 deletions crates/swc_ecma_lexer/src/common/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
lexer::token::TokenFactory,
parser::{
class_and_fn::{parse_async_fn_decl, parse_class_decl, parse_decorators},
eof_error,
expr::{parse_await_expr, parse_bin_op_recursively, parse_for_head_prefix},
ident::{parse_binding_ident, parse_label_ident},
pat::reparse_expr_as_pat,
Expand Down Expand Up @@ -323,7 +324,7 @@ pub fn parse_using_decl<'a, P: Parser<'a>>(
// reader = init()

// is two statements
let _ = cur!(p, false);
p.input_mut().cur();
if p.input_mut().has_linebreak_between_cur_and_peeked() {
return Ok(None);
}
Expand Down Expand Up @@ -1331,7 +1332,10 @@ fn parse_stmt_internal<'a, P: Parser<'a>>(
}
.into())
} else {
if cur!(p, false)?.is_bin_op() {
let Some(cur) = p.input_mut().cur() else {
return Err(eof_error(p));
};
if cur.is_bin_op() {
p.emit_err(p.input().cur_span(), SyntaxError::TS1005);
let expr = parse_bin_op_recursively(p, expr, 0)?;
return Ok(ExprStmt {
Expand Down Expand Up @@ -1376,8 +1380,7 @@ pub(super) fn parse_block_body<'a, P: Parser<'a>, Type: IsDirective + From<Stmt>
);
false
} else {
let c = cur!(p, false).ok();
c != end
p.input_mut().cur() != end
}
} {
let stmt = parse_stmt_like(p, true, &handle_import_export)?;
Expand Down
20 changes: 11 additions & 9 deletions crates/swc_ecma_lexer/src/common/parser/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use swc_ecma_ast::*;

use super::{
class_and_fn::{parse_class_decl, parse_fn_block_or_expr_body, parse_fn_decl},
eof_error,
expr::{is_start_of_left_hand_side_expr, parse_new_expr},
ident::parse_maybe_private_name,
is_simple_param_list::IsSimpleParameterList,
Expand Down Expand Up @@ -129,8 +130,7 @@ where

if kind == ParsingContext::EnumMembers {
let expect = P::Token::COMMA;
let cur = cur!(p, false);
let cur = match cur.ok() {
let cur = match p.input_mut().cur() {
Some(tok) => tok.clone().to_string(p.input()),
None => "EOF".to_string(),
};
Expand Down Expand Up @@ -324,7 +324,9 @@ where
pub fn eat_any_ts_modifier<'a>(p: &mut impl Parser<'a>) -> PResult<bool> {
if p.syntax().typescript()
&& {
let cur = cur!(p, false)?;
let Some(cur) = p.input_mut().cur() else {
return Err(eof_error(p));
};
cur.is_public() || cur.is_protected() || cur.is_private() || cur.is_readonly()
}
&& peek!(p).is_some_and(|t| t.is_word() || t.is_lbrace() || t.is_lbracket())
Expand Down Expand Up @@ -612,7 +614,7 @@ fn expect_then_parse_ts_type<'a, P: Parser<'a>>(

p.in_type().parse_with(|p| {
if !p.input_mut().eat(token) {
let got = format!("{:?}", cur!(p, false).ok());
let got = format!("{:?}", p.input_mut().cur());
syntax_error!(
p,
p.input().cur_span(),
Expand Down Expand Up @@ -784,7 +786,7 @@ pub fn parse_ts_type_or_type_predicate_ann<'a, P: Parser<'a>>(
p.in_type().parse_with(|p| {
let return_token_start = p.input_mut().cur_pos();
if !p.input_mut().eat(return_token) {
let cur = format!("{:?}", cur!(p, false).ok());
let cur = format!("{:?}", p.input_mut().cur());
let span = p.input_mut().cur_span();
syntax_error!(
p,
Expand All @@ -806,7 +808,9 @@ pub fn parse_ts_type_or_type_predicate_ann<'a, P: Parser<'a>>(
};
if has_type_pred_asserts {
p.assert_and_bump(&P::Token::ASSERTS)?;
cur!(p, false)?;
if p.input_mut().cur().is_none() {
return Err(eof_error(p));
}
}

let has_type_pred_is = p.is_ident_ref()
Expand Down Expand Up @@ -893,7 +897,7 @@ pub(super) fn try_parse_ts_type_args<'a, P: Parser<'a>>(
}) {
Ok(None)
} else if p.input_mut().had_line_break_before_cur()
|| cur!(p, false).is_ok_and(|t| t.is_bin_op())
|| p.input_mut().cur().is_some_and(|t| t.is_bin_op())
|| !is_start_of_expr(p)
{
Ok(Some(type_args))
Expand Down Expand Up @@ -2211,8 +2215,6 @@ fn parse_ts_import_type<'a, P: Parser<'a>>(p: &mut P) -> PResult<TsImportType> {

expect!(p, &P::Token::LPAREN);

let _ = cur!(p, false);

let cur = cur!(p, true);
let arg = if cur.is_str() {
parse_str_lit(p)
Expand Down
Loading
0