Skip to content
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

fix(es/parser): Revert lexer fix for << #7807

Merged
merged 1 commit into from
Aug 14, 2023
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
4 changes: 0 additions & 4 deletions crates/swc_ecma_parser/src/lexer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,6 @@ impl Tokens for Lexer<'_> {
fn take_errors(&mut self) -> Vec<Error> {
take(&mut self.errors.borrow_mut())
}

fn reset_to(&mut self, to: BytePos) {
self.input.reset_to(to);
}
}

impl<'a> Iterator for Lexer<'a> {
Expand Down
3 changes: 0 additions & 3 deletions crates/swc_ecma_parser/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ macro_rules! tok {
('>') => {
crate::token::Token::BinOp(crate::token::BinOpToken::Gt)
};
("<<") => {
crate::token::Token::BinOp(crate::token::BinOpToken::LShift)
};
(">>") => {
crate::token::Token::BinOp(crate::token::BinOpToken::RShift)
};
Expand Down
23 changes: 2 additions & 21 deletions crates/swc_ecma_parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,11 @@ impl<I: Tokens> Parser<I> {
let obj = self.parse_primary_expr()?;
return_if_arrow!(self, obj);

let type_args = if self.syntax().typescript() {
let type_args = if self.syntax().typescript() && is!(self, '<') {
self.try_parse_ts_type_args()
} else {
None
};

let obj = if let Some(type_args) = type_args {
trace_cur!(self, parse_member_expr_or_new_expr__with_type_args);
Box::new(Expr::TsInstantiation(TsInstantiation {
Expand Down Expand Up @@ -1621,27 +1620,9 @@ impl<I: Tokens> Parser<I> {
let callee = self.parse_new_expr()?;
return_if_arrow!(self, callee);

let type_args = if self.input.syntax().typescript() && is_one_of!(self, '<', "<<") {
let type_args_start = self.input.cur_pos();
let type_args = if self.input.syntax().typescript() && is!(self, '<') {
self.try_parse_ts(|p| {
trace_cur!(p, parse_lhs_expr__type_args);

if is!(p, "<<") {
let ctx = Context {
should_not_lex_lt_or_gt_as_type: false,
in_type: true,
..p.ctx()
};
p.input.reset_to(type_args_start);
p.input.set_ctx(ctx);
}

trace_cur!(p, parse_lhs_expr__before_type_args);

let type_args = p.parse_ts_type_args()?;

trace_cur!(p, parse_lhs_expr__after_type_args);

if is!(p, '(') {
Ok(Some(type_args))
} else {
Expand Down
20 changes: 3 additions & 17 deletions crates/swc_ecma_parser/src/parser/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ pub trait Tokens: Clone + Iterator<Item = TokenAndSpan> {
fn add_module_mode_error(&self, error: Error);

fn take_errors(&mut self) -> Vec<Error>;

fn reset_to(&mut self, to: BytePos);
}

#[derive(Clone)]
Expand Down Expand Up @@ -143,8 +141,6 @@ impl Tokens for TokensInput {
fn take_errors(&mut self) -> Vec<Error> {
take(&mut self.errors.borrow_mut())
}

fn reset_to(&mut self, _: BytePos) {}
}

/// Note: Lexer need access to parser's context to lex correctly.
Expand Down Expand Up @@ -257,10 +253,6 @@ impl<I: Tokens> Tokens for Capturing<I> {
fn take_errors(&mut self) -> Vec<Error> {
self.inner.take_errors()
}

fn reset_to(&mut self, to: BytePos) {
self.inner.reset_to(to);
}
}

/// This struct is responsible for managing current token and peeked token.
Expand Down Expand Up @@ -326,9 +318,9 @@ impl<I: Tokens> Buffer<I> {

#[cold]
#[inline(never)]
pub fn dump_cur(&self) -> String {
match &self.cur {
Some(v) => format!("{:?}", v.token),
pub fn dump_cur(&mut self) -> String {
match self.cur() {
Some(v) => format!("{:?}", v),
None => "<eof>".to_string(),
}
}
Expand Down Expand Up @@ -502,10 +494,4 @@ impl<I: Tokens> Buffer<I> {
pub(crate) fn set_token_context(&mut self, c: lexer::TokenContexts) {
self.iter.set_token_context(c)
}

#[inline]
pub(crate) fn reset_to(&mut self, to: BytePos) {
self.cur = None;
self.iter.reset_to(to)
}
}
29 changes: 2 additions & 27 deletions crates/swc_ecma_parser/src/parser/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,7 @@ impl<I: Tokens> Parser<I> {
permit_in_out: bool,
permit_const: bool,
) -> PResult<Box<TsTypeParamDecl>> {
trace_cur!(self, parse_ts_type_params);

let ret = self.in_type().parse_with(|p| {
self.in_type().parse_with(|p| {
p.ts_in_no_context(|p| {
let start = cur_pos!(p);

Expand All @@ -485,11 +483,7 @@ impl<I: Tokens> Parser<I> {
params,
}))
})
})?;

trace_cur!(self, parse_ts_type_params__end);

Ok(ret)
})
}

/// `tsParseTypeOrTypePredicateAnnotation`
Expand Down Expand Up @@ -585,25 +579,11 @@ impl<I: Tokens> Parser<I> {

#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
pub(super) fn try_parse_ts_type_args(&mut self) -> Option<Box<TsTypeParamInstantiation>> {
let start = self.input.cur_pos();

trace_cur!(self, try_parse_ts_type_args);
debug_assert!(self.input.syntax().typescript());

self.try_parse_ts(|p| {
let nested = is!(p, "<<");
if nested {
let ctx = Context {
should_not_lex_lt_or_gt_as_type: false,
in_type: true,
..p.ctx()
};
p.input.reset_to(start);
p.input.set_ctx(ctx);
}

let type_args = p.parse_ts_type_args()?;
trace_cur!(p, try_parse_ts_type_args__after_type_args);

if is_one_of!(
p, '<', // invalid syntax
Expand Down Expand Up @@ -1865,12 +1845,9 @@ impl<I: Tokens> Parser<I> {

// ----- inlined `self.tsFillSignature(tt.arrow, node)`
let type_params = self.try_parse_ts_type_params(false, true)?;
trace_cur!(self, parse_ts_fn_or_constructor_type__after_type_params);
expect!(self, '(');
let params = self.parse_ts_binding_list_for_signature()?;
trace_cur!(self, parse_ts_fn_or_constructor_type__after_params);
let type_ann = self.parse_ts_type_or_type_predicate_ann(&tok!("=>"))?;
trace_cur!(self, parse_ts_fn_or_constructor_type__after_type_ann);
// ----- end

Ok(if is_fn_type {
Expand Down Expand Up @@ -2038,8 +2015,6 @@ impl<I: Tokens> Parser<I> {
permit_in_out: bool,
permit_const: bool,
) -> PResult<Option<Box<TsTypeParamDecl>>> {
trace_cur!(self, try_parse_ts_type_params);

if !cfg!(feature = "typescript") {
return Ok(None);
}
Expand Down

This file was deleted.

169 changes: 0 additions & 169 deletions crates/swc_ecma_parser/tests/typescript/issue-7187/1/input.tsx.json

This file was deleted.

Loading