Skip to content

Commit 8b66d5e

Browse files
authored
fix(es/parser): Revert #9141 (#9171)
**Description:** Reverts #9141 because it caused an issue for https://github.com/toeverything/AFFiNE.
1 parent a416cae commit 8b66d5e

File tree

8 files changed

+77
-110
lines changed

8 files changed

+77
-110
lines changed

crates/swc_ecma_parser/src/parser/class_and_fn.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ impl<I: Tokens> Parser<I> {
745745
}
746746

747747
trace_cur!(self, parse_class_member_with_is_static__normal_class_member);
748-
let mut key = if readonly.is_some() && is!(self, '!' | ':') {
748+
let mut key = if readonly.is_some() && is_one_of!(self, '!', ':') {
749749
Key::Public(PropName::Ident(Ident::new(
750750
"readonly".into(),
751751
readonly.unwrap(),
@@ -1155,8 +1155,8 @@ impl<I: Tokens> Parser<I> {
11551155
}
11561156

11571157
fn is_class_property(&mut self, asi: bool) -> bool {
1158-
(self.input.syntax().typescript() && is!(self, '!' | ':'))
1159-
|| is!(self, '=' | '}')
1158+
(self.input.syntax().typescript() && is_one_of!(self, '!', ':'))
1159+
|| is_one_of!(self, '=', '}')
11601160
|| if asi {
11611161
is!(self, ';')
11621162
} else {

crates/swc_ecma_parser/src/parser/expr.rs

+11-37
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<I: Tokens> Parser<I> {
8282
let start = self.input.cur_span();
8383

8484
if self.input.syntax().typescript()
85-
&& (is!(self, '<' | JSXTagStart))
85+
&& (is_one_of!(self, '<', JSXTagStart))
8686
&& (peeked_is!(self, IdentName) || peeked_is!(self, JSXName))
8787
{
8888
let ctx = Context {
@@ -395,7 +395,7 @@ impl<I: Tokens> Parser<I> {
395395
}
396396

397397
if is!(self, "let")
398-
|| (self.input.syntax().typescript() && is!(self, IdentRef | "await"))
398+
|| (self.input.syntax().typescript() && is_one_of!(self, IdentRef, "await"))
399399
|| is!(self, IdentRef)
400400
{
401401
let ctx = self.ctx();
@@ -1226,7 +1226,7 @@ impl<I: Tokens> Parser<I> {
12261226
)
12271227
.map(|expr| (Box::new(Expr::TaggedTpl(expr)), true))
12281228
.map(Some)
1229-
} else if is!(p, '=' | "as") {
1229+
} else if is_one_of!(p, '=', "as") {
12301230
Ok(Some((
12311231
Box::new(Expr::TsInstantiation(TsInstantiation {
12321232
span: span!(p, start),
@@ -1258,7 +1258,7 @@ impl<I: Tokens> Parser<I> {
12581258
None
12591259
};
12601260

1261-
if obj.is_import() && !is!(self, '.' | '(') {
1261+
if obj.is_import() && !is_one_of!(self, '.', '(') {
12621262
unexpected!(self, "`.` or `(`")
12631263
}
12641264

@@ -1597,7 +1597,7 @@ impl<I: Tokens> Parser<I> {
15971597
let callee = self.parse_new_expr()?;
15981598
return_if_arrow!(self, callee);
15991599

1600-
let type_args = if self.input.syntax().typescript() && is!(self, '<' | "<<") {
1600+
let type_args = if self.input.syntax().typescript() && is_one_of!(self, '<', "<<") {
16011601
self.try_parse_ts(|p| {
16021602
let type_args = p.parse_ts_type_args()?;
16031603
if is!(p, '(') {
@@ -2145,44 +2145,18 @@ impl<I: Tokens> Parser<I> {
21452145
}
21462146

21472147
fn is_start_of_left_hand_side_expr(&mut self) -> PResult<bool> {
2148-
Ok(is!(
2149-
self,
2150-
"this"
2151-
| "super"
2152-
| "null"
2153-
| "true"
2154-
| "false"
2155-
| Num
2156-
| BigInt
2157-
| Str
2158-
| '`'
2159-
| '('
2160-
| '['
2161-
| '{'
2162-
| "function"
2163-
| "class"
2164-
| "new"
2165-
| Regex
2166-
| IdentRef
2148+
Ok(is_one_of!(
2149+
self, "this", "super", "null", "true", "false", Num, BigInt, Str, '`', '(', '[', '{',
2150+
"function", "class", "new", Regex, IdentRef
21672151
) || (is!(self, "import")
21682152
&& (peeked_is!(self, '(') || peeked_is!(self, '<') || peeked_is!(self, '.'))))
21692153
}
21702154

21712155
pub(super) fn is_start_of_expr(&mut self) -> PResult<bool> {
21722156
Ok(self.is_start_of_left_hand_side_expr()?
2173-
|| is!(
2174-
self,
2175-
'+' | '-'
2176-
| '~'
2177-
| '!'
2178-
| "delete"
2179-
| "typeof"
2180-
| "void"
2181-
| "++"
2182-
| "--"
2183-
| '<'
2184-
| "await"
2185-
| "yield"
2157+
|| is_one_of!(
2158+
self, '+', '-', '~', '!', "delete", "typeof", "void", "++", "--", '<', "await",
2159+
"yield"
21862160
)
21872161
|| (is!(self, '#') && peeked_is!(self, IdentName)))
21882162
}

crates/swc_ecma_parser/src/parser/expr/ops.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl<I: Tokens> Parser<I> {
281281
}
282282

283283
// Parse unary expression
284-
if is!(self, "delete" | "void" | "typeof" | '+' | '-' | '~' | '!') {
284+
if is_one_of!(self, "delete", "void", "typeof", '+', '-', '~', '!') {
285285
let op = match bump!(self) {
286286
tok!("delete") => op!("delete"),
287287
tok!("void") => op!("void"),
@@ -341,7 +341,7 @@ impl<I: Tokens> Parser<I> {
341341
return Ok(expr);
342342
}
343343

344-
if is!(self, "++" | "--") {
344+
if is_one_of!(self, "++", "--") {
345345
self.check_assign_target(&expr, false);
346346

347347
let op = if bump!(self) == tok!("++") {
@@ -378,7 +378,7 @@ impl<I: Tokens> Parser<I> {
378378

379379
let span = span!(self, start);
380380

381-
if is!(self, ')' | ']' | ';' | ',') && !ctx.in_async {
381+
if is_one_of!(self, ')', ']', ';', ',') && !ctx.in_async {
382382
if ctx.module {
383383
self.emit_err(span, SyntaxError::InvalidIdentInAsync);
384384
}

crates/swc_ecma_parser/src/parser/macros.rs

+30-34
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ macro_rules! is {
1919
($p:expr, BindingIdent) => {{
2020
let ctx = $p.ctx();
2121
match $p.input.cur() {
22-
Some(&crate::token::Word(ref w)) => !ctx.is_reserved(w),
22+
Some(&Word(ref w)) => !ctx.is_reserved(w),
2323
_ => false,
2424
}
2525
}};
2626

2727
($p:expr, IdentRef) => {{
2828
let ctx = $p.ctx();
2929
match $p.input.cur() {
30-
Some(&crate::token::Word(ref w)) => !ctx.is_reserved(w),
30+
Some(&Word(ref w)) => !ctx.is_reserved(w),
3131
_ => false,
3232
}
3333
}};
3434

3535
($p:expr,IdentName) => {{
3636
match $p.input.cur() {
37-
Some(&crate::token::Word(..)) => true,
37+
Some(&Word(..)) => true,
3838
_ => false,
3939
}
4040
}};
@@ -77,33 +77,29 @@ macro_rules! is {
7777
($p:expr, $t:tt) => {
7878
is_exact!($p, $t)
7979
};
80-
81-
($p:expr, $t:tt | $($rest:tt)*) => {{
82-
is!($p, $t) || is!($p, $($rest)*)
83-
}};
8480
}
8581

8682
#[allow(unused)]
8783
macro_rules! peeked_is {
8884
($p:expr, BindingIdent) => {{
8985
let ctx = $p.ctx();
9086
match peek!($p) {
91-
Some(&crate::token::Word(ref w)) => !ctx.is_reserved(w),
87+
Some(&Word(ref w)) => !ctx.is_reserved(w),
9288
_ => false,
9389
}
9490
}};
9591

9692
($p:expr, IdentRef) => {{
9793
let ctx = $p.ctx();
9894
match peek!($p) {
99-
Some(&crate::token::Word(ref w)) => !ctx.is_reserved(w),
95+
Some(&Word(ref w)) => !ctx.is_reserved(w),
10096
_ => false,
10197
}
10298
}};
10399

104100
($p:expr,IdentName) => {{
105101
match peek!($p) {
106-
Some(&crate::token::Word(..)) => true,
102+
Some(&Word(..)) => true,
107103
_ => false,
108104
}
109105
}};
@@ -125,10 +121,6 @@ macro_rules! peeked_is {
125121
_ => false,
126122
}
127123
};
128-
129-
($p:expr, $t:tt | $($rest:tt)*) => {
130-
peeked_is!($p, $t) || peeked_is!($p, $($rest)*)
131-
};
132124
}
133125

134126
/// Returns true on eof.
@@ -138,6 +130,15 @@ macro_rules! eof {
138130
};
139131
}
140132

133+
macro_rules! is_one_of {
134+
($p:expr, $($t:tt),+) => {{
135+
false
136+
$(
137+
|| is!($p, $t)
138+
)*
139+
}};
140+
}
141+
141142
// This will panic if current != token
142143
macro_rules! assert_and_bump {
143144
($p:expr, $t:tt) => {{
@@ -160,6 +161,9 @@ macro_rules! assert_and_bump {
160161
/// if token has data like string.
161162
macro_rules! eat {
162163
($p:expr, ';') => {{
164+
if cfg!(feature = "debug") {
165+
tracing::trace!("eat(';'): cur={:?}", cur!($p, false));
166+
}
163167
match $p.input.cur() {
164168
Some(&Token::Semi) => {
165169
$p.input.bump();
@@ -206,11 +210,7 @@ macro_rules! expect {
206210
const TOKEN: &Token = &token_including_semi!($t);
207211
if !eat!($p, $t) {
208212
let cur = $p.input.dump_cur();
209-
syntax_error!(
210-
$p,
211-
$p.input.cur_span(),
212-
crate::error::SyntaxError::Expected(TOKEN, cur)
213-
)
213+
syntax_error!($p, $p.input.cur_span(), SyntaxError::Expected(TOKEN, cur))
214214
}
215215
}};
216216
}
@@ -220,11 +220,7 @@ macro_rules! expect_exact {
220220
const TOKEN: &Token = &token_including_semi!($t);
221221
if !eat_exact!($p, $t) {
222222
let cur = $p.input.dump_cur();
223-
syntax_error!(
224-
$p,
225-
$p.input.cur_span(),
226-
crate::error::SyntaxError::Expected(TOKEN, cur)
227-
)
223+
syntax_error!($p, $p.input.cur_span(), SyntaxError::Expected(TOKEN, cur))
228224
}
229225
}};
230226
}
@@ -295,9 +291,6 @@ macro_rules! bump {
295291
$p.input.knows_cur(),
296292
"parser should not call bump() without knowing current token"
297293
);
298-
if cfg!(feature = "debug") {
299-
tracing::info!("Bump: {:?}", $p.input.cur());
300-
}
301294
$p.input.bump()
302295
}};
303296
}
@@ -384,13 +377,16 @@ macro_rules! syntax_error {
384377
}
385378
}
386379
}
387-
tracing::error!(
388-
"Syntax error called from {}:{}:{}\nCurrent token = {:?}",
389-
file!(),
390-
line!(),
391-
column!(),
392-
$p.input.cur()
393-
);
380+
381+
if cfg!(feature = "debug") {
382+
tracing::error!(
383+
"Syntax error called from {}:{}:{}\nCurrent token = {:?}",
384+
file!(),
385+
line!(),
386+
column!(),
387+
$p.input.cur()
388+
);
389+
}
394390
return Err(err.into());
395391
}};
396392
}

crates/swc_ecma_parser/src/parser/object.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,7 @@ impl<I: Tokens> ParseObject<Box<Expr>> for Parser<I> {
183183
let key = self.parse_prop_name()?;
184184

185185
if self.input.syntax().typescript()
186-
&& !is!(
187-
self,
188-
'(' | '[' | ':' | ',' | '?' | '=' | '*' | IdentName | Str | Num
189-
)
186+
&& !is_one_of!(self, '(', '[', ':', ',', '?', '=', '*', IdentName, Str, Num)
190187
&& !(self.input.syntax().typescript() && is!(self, '<'))
191188
&& !(is!(self, '}') && matches!(key, PropName::Ident(..)))
192189
{
@@ -245,7 +242,7 @@ impl<I: Tokens> ParseObject<Box<Expr>> for Parser<I> {
245242

246243
// `ident` from parse_prop_name is parsed as 'IdentifierName'
247244
// It means we should check for invalid expressions like { for, }
248-
if is!(self, '=' | ',' | '}') {
245+
if is_one_of!(self, '=', ',', '}') {
249246
let is_reserved_word = { self.ctx().is_reserved_word(&ident.sym) };
250247
if is_reserved_word {
251248
self.emit_err(ident.span, SyntaxError::ReservedWordInObjShorthandOrPat);

crates/swc_ecma_parser/src/parser/stmt.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'a, I: Tokens> Parser<I> {
9696
let start = cur_pos!(self);
9797
let decorators = self.parse_decorators(true)?;
9898

99-
if is!(self, "import" | "export") {
99+
if is_one_of!(self, "import", "export") {
100100
return self.handle_import_export(top_level, decorators);
101101
}
102102

@@ -640,7 +640,7 @@ impl<'a, I: Tokens> Parser<I> {
640640
};
641641

642642
self.with_ctx(ctx).parse_with(|p| {
643-
while is!(p, "case" | "default") {
643+
while is_one_of!(p, "case", "default") {
644644
let mut cons = vec![];
645645
let is_case = is!(p, "case");
646646
let case_start = cur_pos!(p);
@@ -657,7 +657,7 @@ impl<'a, I: Tokens> Parser<I> {
657657
};
658658
expect!(p, ':');
659659

660-
while !eof!(p) && !is!(p, "case" | "default" | '}') {
660+
while !eof!(p) && !is_one_of!(p, "case", "default", '}') {
661661
cons.push(p.parse_stmt_list_item(false)?);
662662
}
663663

@@ -868,7 +868,7 @@ impl<'a, I: Tokens> Parser<I> {
868868
let should_include_in = kind != VarDeclKind::Var || !for_loop;
869869

870870
if self.syntax().typescript() && for_loop {
871-
let res = if is!(self, "in" | "of") {
871+
let res = if is_one_of!(self, "in", "of") {
872872
self.ts_look_ahead(|p| {
873873
//
874874
if !eat!(p, "of") && !eat!(p, "in") {
@@ -999,7 +999,7 @@ impl<'a, I: Tokens> Parser<I> {
999999
}
10001000

10011001
//FIXME: This is wrong. Should check in/of only on first loop.
1002-
let init = if !for_loop || !is!(self, "in" | "of") {
1002+
let init = if !for_loop || !is_one_of!(self, "in", "of") {
10031003
if eat!(self, '=') {
10041004
let expr = self.parse_assignment_expr()?;
10051005
let expr = self.verify_expr(expr)?;
@@ -1243,13 +1243,13 @@ impl<'a, I: Tokens> Parser<I> {
12431243
fn parse_for_head(&mut self) -> PResult<TempForHead> {
12441244
let strict = self.ctx().strict;
12451245

1246-
if is!(self, "const" | "var")
1246+
if is_one_of!(self, "const", "var")
12471247
|| (is!(self, "let")
12481248
&& peek!(self).map_or(false, |v| v.kind().follows_keyword_let(strict)))
12491249
{
12501250
let decl = self.parse_var_stmt(true)?;
12511251

1252-
if is!(self, "of" | "in") {
1252+
if is_one_of!(self, "of", "in") {
12531253
if decl.decls.len() != 1 {
12541254
for d in decl.decls.iter().skip(1) {
12551255
self.emit_err(d.name.span(), SyntaxError::TooManyVarInForInHead);
@@ -1342,7 +1342,7 @@ impl<'a, I: Tokens> Parser<I> {
13421342
}
13431343

13441344
// for (a of b)
1345-
if is!(self, "of" | "in") {
1345+
if is_one_of!(self, "of", "in") {
13461346
let is_in = is!(self, "in");
13471347

13481348
let pat = self.reparse_expr_as_pat(PatType::AssignPat, init)?;

0 commit comments

Comments
 (0)