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
44 changes: 26 additions & 18 deletions crates/oxc_parser/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ impl<'a> ParserImpl<'a> {
#[inline]
pub(crate) fn can_insert_semicolon(&self) -> bool {
let token = self.cur_token();
let kind = token.kind();
kind == Kind::Semicolon || kind == Kind::RCurly || kind.is_eof() || token.is_on_new_line()
matches!(token.kind(), Kind::Semicolon | Kind::RCurly | Kind::Eof) || token.is_on_new_line()
}

/// # Errors
Expand Down Expand Up @@ -323,23 +322,37 @@ impl<'a> ParserImpl<'a> {
}

pub(crate) fn parse_normal_list<F, T>(&mut self, open: Kind, close: Kind, f: F) -> Vec<'a, T>
where
F: Fn(&mut Self) -> T,
{
self.expect(open);
let mut list = self.ast.vec();
while !self.at(close) && !self.has_fatal_error() {
list.push(f(self));
}
self.expect(close);
list
}

pub(crate) fn parse_normal_list_breakable<F, T>(
&mut self,
open: Kind,
close: Kind,
f: F,
) -> Vec<'a, T>
where
F: Fn(&mut Self) -> Option<T>,
{
self.expect(open);
let mut list = self.ast.vec();
loop {
let kind = self.cur_kind();
if kind == close || self.has_fatal_error() {
if self.at(close) || self.has_fatal_error() {
break;
}
match f(self) {
Some(e) => {
list.push(e);
}
None => {
break;
}
if let Some(e) = f(self) {
list.push(e);
} else {
break;
}
}
self.expect(close);
Expand Down Expand Up @@ -387,11 +400,7 @@ impl<'a> ParserImpl<'a> {
let mut rest: Option<BindingRestElement<'a>> = None;
let mut first = true;
loop {
let kind = self.cur_kind();
if self.has_fatal_error() {
break;
}
if kind == close {
if self.at(close) || self.has_fatal_error() {
break;
}

Expand Down Expand Up @@ -423,8 +432,7 @@ impl<'a> ParserImpl<'a> {
}

if self.at(Kind::Dot3) {
let r = self.parse_rest_element();
rest.replace(r);
rest.replace(self.parse_rest_element());
} else {
list.push(parse_element(self));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/js/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl<'a> ParserImpl<'a> {

fn parse_class_body(&mut self) -> Box<'a, ClassBody<'a>> {
let span = self.start_span();
let class_elements = self.parse_normal_list(Kind::LCurly, Kind::RCurly, |p| {
let class_elements = self.parse_normal_list_breakable(Kind::LCurly, Kind::RCurly, |p| {
// Skip empty class element `;`
if p.eat(Kind::Semicolon) {
while p.eat(Kind::Semicolon) {}
Expand Down
57 changes: 16 additions & 41 deletions crates/oxc_parser/src/js/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ use oxc_ast::{NONE, ast::*};
use oxc_span::GetSpan;

use super::VariableDeclarationParent;
use crate::{
ParserImpl, StatementContext, diagnostics,
lexer::Kind,
modifiers::{ModifierFlags, Modifiers},
};
use crate::{ParserImpl, StatementContext, diagnostics, lexer::Kind};

impl<'a> ParserImpl<'a> {
pub(crate) fn parse_let(&mut self, stmt_ctx: StatementContext) -> Statement<'a> {
Expand Down Expand Up @@ -78,7 +74,7 @@ impl<'a> ParserImpl<'a> {
start_span: u32,
kind: VariableDeclarationKind,
decl_parent: VariableDeclarationParent,
modifiers: &Modifiers<'a>,
declare: bool,
) -> Box<'a, VariableDeclaration<'a>> {
let mut declarations = self.ast.vec();
loop {
Expand All @@ -92,19 +88,7 @@ impl<'a> ParserImpl<'a> {
if matches!(decl_parent, VariableDeclarationParent::Statement) {
self.asi();
}

self.verify_modifiers(
modifiers,
ModifierFlags::DECLARE,
diagnostics::modifier_cannot_be_used_here,
);

self.ast.alloc_variable_declaration(
self.end_span(start_span),
kind,
declarations,
modifiers.contains_declare(),
)
self.ast.alloc_variable_declaration(self.end_span(start_span), kind, declarations, declare)
}

fn parse_variable_declarator(
Expand Down Expand Up @@ -181,28 +165,24 @@ impl<'a> ParserImpl<'a> {
let span = self.start_span();

let is_await = self.eat(Kind::Await);
let kind = if is_await {
VariableDeclarationKind::AwaitUsing
} else {
VariableDeclarationKind::Using
};

self.expect(Kind::Using);

// BindingList[?In, ?Yield, ?Await, ~Pattern]
let mut declarations: oxc_allocator::Vec<'_, VariableDeclarator<'_>> = self.ast.vec();
let mut declarations = self.ast.vec();
loop {
let declaration = self.parse_variable_declarator(
VariableDeclarationParent::Statement,
if is_await {
VariableDeclarationKind::AwaitUsing
} else {
VariableDeclarationKind::Using
},
);

match declaration.id.kind {
BindingPatternKind::BindingIdentifier(_) => {}
_ => {
self.error(diagnostics::invalid_identifier_in_using_declaration(
declaration.id.span(),
));
}
let declaration =
self.parse_variable_declarator(VariableDeclarationParent::Statement, kind);

if !matches!(declaration.id.kind, BindingPatternKind::BindingIdentifier(_)) {
self.error(diagnostics::invalid_identifier_in_using_declaration(
declaration.id.span(),
));
}

// Excluding `for` loops, an initializer is required in a UsingDeclaration.
Expand All @@ -218,11 +198,6 @@ impl<'a> ParserImpl<'a> {
}
}

let kind = if is_await {
VariableDeclarationKind::AwaitUsing
} else {
VariableDeclarationKind::Using
};
self.ast.variable_declaration(self.end_span(span), kind, declarations, false)
}
}
104 changes: 52 additions & 52 deletions crates/oxc_parser/src/js/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ impl<'a> ParserImpl<'a> {
Kind::Do => self.parse_do_while_statement(),
Kind::While => self.parse_while_statement(),
Kind::For => self.parse_for_statement(),
Kind::Break | Kind::Continue => self.parse_break_or_continue_statement(),
Kind::Continue => self.parse_continue_statement(),
Kind::Break => self.parse_break_statement(),
Kind::With => self.parse_with_statement(),
Kind::Switch => self.parse_switch_statement(),
Kind::Throw => self.parse_throw_statement(),
Expand Down Expand Up @@ -206,13 +207,9 @@ impl<'a> ParserImpl<'a> {
/// Section 14.2 Block Statement
pub(crate) fn parse_block(&mut self) -> Box<'a, BlockStatement<'a>> {
let span = self.start_span();
self.expect(Kind::LCurly);
let mut body = self.ast.vec();
while !self.at(Kind::RCurly) && !self.has_fatal_error() {
let stmt = self.parse_statement_list_item(StatementContext::StatementList);
body.push(stmt);
}
self.expect(Kind::RCurly);
let body = self.parse_normal_list(Kind::LCurly, Kind::RCurly, |p| {
p.parse_statement_list_item(StatementContext::StatementList)
});
self.ast.alloc_block_statement(self.end_span(span), body)
}

Expand All @@ -232,7 +229,7 @@ impl<'a> ParserImpl<'a> {
start_span,
kind,
VariableDeclarationParent::Statement,
&Modifiers::empty(),
false,
);

if stmt_ctx.is_single_statement() && decl.kind.is_lexical() {
Expand Down Expand Up @@ -315,10 +312,25 @@ impl<'a> ParserImpl<'a> {

// `for (const` | `for (var`
match self.cur_kind() {
Kind::Const | Kind::Var => {
Kind::Const => {
let start_span = self.start_span();
self.bump_any();
return self.parse_variable_declaration_for_statement(
span,
start_span,
VariableDeclarationKind::Const,
r#await,
);
}
Kind::Var => {
let start_span = self.start_span();
return self
.parse_variable_declaration_for_statement(span, start_span, None, r#await);
self.bump_any();
return self.parse_variable_declaration_for_statement(
span,
start_span,
VariableDeclarationKind::Var,
r#await,
);
}
Kind::Let => {
// `for (let`
Expand All @@ -330,7 +342,7 @@ impl<'a> ParserImpl<'a> {
return self.parse_variable_declaration_for_statement(
span,
start_span,
Some(VariableDeclarationKind::Let),
VariableDeclarationKind::Let,
r#await,
);
}
Expand Down Expand Up @@ -403,40 +415,25 @@ impl<'a> ParserImpl<'a> {
&mut self,
span: u32,
start_span: u32,
decl_kind: Option<VariableDeclarationKind>,
decl_kind: VariableDeclarationKind,
r#await: bool,
) -> Statement<'a> {
let init_declaration = self.context(Context::empty(), Context::In, |p| {
let decl_ctx = VariableDeclarationParent::For;
let kind = if let Some(kind) = decl_kind {
kind
} else {
let kind = p.get_variable_declaration_kind();
p.bump_any();
kind
};
p.parse_variable_declaration(start_span, kind, decl_ctx, &Modifiers::empty())
p.parse_variable_declaration(
start_span,
decl_kind,
VariableDeclarationParent::For,
false,
)
});

self.parse_any_for_loop(span, init_declaration, r#await)
}

fn is_using_declaration(&mut self) -> bool {
self.lookahead(|p| {
p.is_next_token_binding_identifier_or_start_of_object_destructuring_on_same_line(false)
})
}

fn is_next_token_binding_identifier_or_start_of_object_destructuring_on_same_line(
&mut self,
disallow_of: bool,
) -> bool {
self.bump_any();
if disallow_of && self.at(Kind::Of) {
return false;
}
(self.cur_kind().is_binding_identifier() || self.at(Kind::LCurly))
&& !self.cur_token().is_on_new_line()
let token = self.lexer.peek_token();
let kind = token.kind();
(kind.is_binding_identifier() || kind == Kind::LCurly) && !token.is_on_new_line()
}

fn parse_using_declaration_for_statement(&mut self, span: u32, r#await: bool) -> Statement<'a> {
Expand Down Expand Up @@ -495,10 +492,10 @@ impl<'a> ParserImpl<'a> {
self.check_missing_initializer(d);
}
}
let test = if !self.at(Kind::Semicolon) && !self.at(Kind::RParen) {
Some(self.context(Context::In, Context::empty(), ParserImpl::parse_expr))
} else {
let test = if matches!(self.cur_kind(), Kind::Semicolon | Kind::RParen) {
None
} else {
Some(self.context(Context::In, Context::empty(), ParserImpl::parse_expr))
};
self.expect(Kind::Semicolon);
let update = if self.at(Kind::RParen) {
Expand Down Expand Up @@ -549,20 +546,23 @@ impl<'a> ParserImpl<'a> {
}

/// Section 14.8 Continue Statement
fn parse_continue_statement(&mut self) -> Statement<'a> {
let span = self.start_span();
self.bump_any(); // bump `continue`
let label =
if self.can_insert_semicolon() { None } else { Some(self.parse_label_identifier()) };
self.asi();
self.ast.statement_continue(self.end_span(span), label)
}

/// Section 14.9 Break Statement
fn parse_break_or_continue_statement(&mut self) -> Statement<'a> {
fn parse_break_statement(&mut self) -> Statement<'a> {
let span = self.start_span();
let kind = self.cur_kind();
self.bump_any(); // bump `break` or `continue`
self.bump_any(); // bump `break`
let label =
if self.can_insert_semicolon() { None } else { Some(self.parse_label_identifier()) };
self.asi();
let end_span = self.end_span(span);
match kind {
Kind::Break => self.ast.statement_break(end_span, label),
Kind::Continue => self.ast.statement_continue(end_span, label),
_ => unreachable!(),
}
self.ast.statement_break(self.end_span(span), label)
}

/// Section 14.10 Return Statement
Expand Down Expand Up @@ -604,7 +604,7 @@ impl<'a> ParserImpl<'a> {
self.ast.statement_switch(self.end_span(span), discriminant, cases)
}

pub(crate) fn parse_switch_case(&mut self) -> Option<SwitchCase<'a>> {
pub(crate) fn parse_switch_case(&mut self) -> SwitchCase<'a> {
let span = self.start_span();
let test = match self.cur_kind() {
Kind::Default => {
Expand All @@ -626,7 +626,7 @@ impl<'a> ParserImpl<'a> {
let stmt = self.parse_statement_list_item(StatementContext::StatementList);
consequent.push(stmt);
}
Some(self.ast.switch_case(self.end_span(span), test, consequent))
self.ast.switch_case(self.end_span(span), test, consequent)
}

/// Section 14.14 Throw Statement
Expand Down
Loading
Loading