mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(parser): add ParserImpl::alloc method (#7063)
Pure refactor. Introduce `ParserImpl::alloc` method. Shorten `self.ast.alloc(...)` to `self.alloc(...)`. Also reduce `alloc` calls by using `AstBuilder` methods which already allocate where possible.
This commit is contained in:
parent
9d384ad6db
commit
9e85b104e2
10 changed files with 35 additions and 34 deletions
|
|
@ -58,7 +58,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
Ok(self.ast.binding_pattern_kind_object_pattern(
|
Ok(self.ast.binding_pattern_kind_object_pattern(
|
||||||
self.end_span(span),
|
self.end_span(span),
|
||||||
list,
|
list,
|
||||||
rest.map(|r| self.ast.alloc(r)),
|
rest.map(|r| self.alloc(r)),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,7 +75,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
Ok(self.ast.binding_pattern_kind_array_pattern(
|
Ok(self.ast.binding_pattern_kind_array_pattern(
|
||||||
self.end_span(span),
|
self.end_span(span),
|
||||||
list,
|
list,
|
||||||
rest.map(|r| self.ast.alloc(r)),
|
rest.map(|r| self.alloc(r)),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -376,7 +376,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
match self.cur_kind() {
|
match self.cur_kind() {
|
||||||
Kind::PrivateIdentifier => {
|
Kind::PrivateIdentifier => {
|
||||||
let private_ident = self.parse_private_identifier();
|
let private_ident = self.parse_private_identifier();
|
||||||
Ok((PropertyKey::PrivateIdentifier(self.ast.alloc(private_ident)), false))
|
Ok((PropertyKey::PrivateIdentifier(self.alloc(private_ident)), false))
|
||||||
}
|
}
|
||||||
_ => self.parse_property_name(),
|
_ => self.parse_property_name(),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
|
|
||||||
self.asi()?;
|
self.asi()?;
|
||||||
|
|
||||||
Ok(Statement::VariableDeclaration(self.ast.alloc(using_decl)))
|
Ok(Statement::VariableDeclaration(self.alloc(using_decl)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn parse_variable_declaration(
|
pub(crate) fn parse_variable_declaration(
|
||||||
|
|
|
||||||
|
|
@ -382,7 +382,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
)
|
)
|
||||||
.parse()
|
.parse()
|
||||||
{
|
{
|
||||||
Ok(regular_expression) => Some(self.ast.alloc(regular_expression)),
|
Ok(regular_expression) => Some(self.alloc(regular_expression)),
|
||||||
Err(diagnostic) => {
|
Err(diagnostic) => {
|
||||||
self.error(diagnostic);
|
self.error(diagnostic);
|
||||||
None
|
None
|
||||||
|
|
|
||||||
|
|
@ -15,13 +15,11 @@ impl<'a> CoverGrammar<'a, Expression<'a>> for AssignmentTarget<'a> {
|
||||||
match expr {
|
match expr {
|
||||||
Expression::ArrayExpression(array_expr) => {
|
Expression::ArrayExpression(array_expr) => {
|
||||||
ArrayAssignmentTarget::cover(array_expr.unbox(), p)
|
ArrayAssignmentTarget::cover(array_expr.unbox(), p)
|
||||||
.map(|pat| p.ast.alloc(pat))
|
.map(|pat| AssignmentTarget::ArrayAssignmentTarget(p.alloc(pat)))
|
||||||
.map(AssignmentTarget::ArrayAssignmentTarget)
|
|
||||||
}
|
}
|
||||||
Expression::ObjectExpression(object_expr) => {
|
Expression::ObjectExpression(object_expr) => {
|
||||||
ObjectAssignmentTarget::cover(object_expr.unbox(), p)
|
ObjectAssignmentTarget::cover(object_expr.unbox(), p)
|
||||||
.map(|pat| p.ast.alloc(pat))
|
.map(|pat| AssignmentTarget::ObjectAssignmentTarget(p.alloc(pat)))
|
||||||
.map(AssignmentTarget::ObjectAssignmentTarget)
|
|
||||||
}
|
}
|
||||||
_ => SimpleAssignmentTarget::cover(expr, p).map(AssignmentTarget::from),
|
_ => SimpleAssignmentTarget::cover(expr, p).map(AssignmentTarget::from),
|
||||||
}
|
}
|
||||||
|
|
@ -103,7 +101,7 @@ impl<'a> CoverGrammar<'a, Expression<'a>> for AssignmentTargetMaybeDefault<'a> {
|
||||||
match expr {
|
match expr {
|
||||||
Expression::AssignmentExpression(assignment_expr) => {
|
Expression::AssignmentExpression(assignment_expr) => {
|
||||||
let target = AssignmentTargetWithDefault::cover(assignment_expr.unbox(), p)?;
|
let target = AssignmentTargetWithDefault::cover(assignment_expr.unbox(), p)?;
|
||||||
Ok(AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(p.ast.alloc(target)))
|
Ok(AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(p.alloc(target)))
|
||||||
}
|
}
|
||||||
expr => {
|
expr => {
|
||||||
let target = AssignmentTarget::cover(expr, p)?;
|
let target = AssignmentTarget::cover(expr, p)?;
|
||||||
|
|
|
||||||
|
|
@ -136,11 +136,11 @@ impl<'a> ParserImpl<'a> {
|
||||||
let identifier = self.parse_identifier_reference()?;
|
let identifier = self.parse_identifier_reference()?;
|
||||||
let key = self.ast.alloc_identifier_name(identifier.span, identifier.name.clone());
|
let key = self.ast.alloc_identifier_name(identifier.span, identifier.name.clone());
|
||||||
// IdentifierReference ({ foo })
|
// IdentifierReference ({ foo })
|
||||||
let value = Expression::Identifier(self.ast.alloc(identifier.clone()));
|
let value = Expression::Identifier(self.alloc(identifier.clone()));
|
||||||
// CoverInitializedName ({ foo = bar })
|
// CoverInitializedName ({ foo = bar })
|
||||||
let init = if self.eat(Kind::Eq) {
|
let init = if self.eat(Kind::Eq) {
|
||||||
let right = self.parse_assignment_expression_or_higher()?;
|
let right = self.parse_assignment_expression_or_higher()?;
|
||||||
let left = AssignmentTarget::AssignmentTargetIdentifier(self.ast.alloc(identifier));
|
let left = AssignmentTarget::AssignmentTargetIdentifier(self.alloc(identifier));
|
||||||
Some(self.ast.expression_assignment(
|
Some(self.ast.expression_assignment(
|
||||||
self.end_span(span),
|
self.end_span(span),
|
||||||
AssignmentOperator::Assign,
|
AssignmentOperator::Assign,
|
||||||
|
|
@ -199,7 +199,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let ident = self.parse_identifier_name()?;
|
let ident = self.parse_identifier_name()?;
|
||||||
PropertyKey::StaticIdentifier(self.ast.alloc(ident))
|
PropertyKey::StaticIdentifier(self.alloc(ident))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok((key, computed))
|
Ok((key, computed))
|
||||||
|
|
|
||||||
|
|
@ -325,11 +325,11 @@ impl<'a> ParserImpl<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches!(self.cur_kind(), Kind::In | Kind::Of) {
|
if matches!(self.cur_kind(), Kind::In | Kind::Of) {
|
||||||
let init = ForStatementLeft::VariableDeclaration(self.ast.alloc(using_decl));
|
let init = ForStatementLeft::VariableDeclaration(self.alloc(using_decl));
|
||||||
return self.parse_for_in_or_of_loop(span, r#await, init);
|
return self.parse_for_in_or_of_loop(span, r#await, init);
|
||||||
}
|
}
|
||||||
|
|
||||||
let init = Some(ForStatementInit::VariableDeclaration(self.ast.alloc(using_decl)));
|
let init = Some(ForStatementInit::VariableDeclaration(self.alloc(using_decl)));
|
||||||
self.parse_for_loop(span, init, r#await)
|
self.parse_for_loop(span, init, r#await)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -168,14 +168,12 @@ impl<'a> ParserImpl<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let element_name = if is_reference {
|
let element_name = if is_reference {
|
||||||
let identifier = self.ast.identifier_reference(identifier.span, identifier.name);
|
let identifier = self.ast.alloc_identifier_reference(identifier.span, identifier.name);
|
||||||
JSXElementName::IdentifierReference(self.ast.alloc(identifier))
|
JSXElementName::IdentifierReference(identifier)
|
||||||
} else if name == "this" {
|
} else if name == "this" {
|
||||||
JSXElementName::ThisExpression(
|
JSXElementName::ThisExpression(self.ast.alloc_this_expression(identifier.span))
|
||||||
self.ast.alloc(self.ast.this_expression(identifier.span)),
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
JSXElementName::Identifier(self.ast.alloc(identifier))
|
JSXElementName::Identifier(self.alloc(identifier))
|
||||||
};
|
};
|
||||||
Ok(element_name)
|
Ok(element_name)
|
||||||
}
|
}
|
||||||
|
|
@ -189,11 +187,11 @@ impl<'a> ParserImpl<'a> {
|
||||||
object: JSXIdentifier<'a>,
|
object: JSXIdentifier<'a>,
|
||||||
) -> Result<Box<'a, JSXMemberExpression<'a>>> {
|
) -> Result<Box<'a, JSXMemberExpression<'a>>> {
|
||||||
let mut object = if object.name == "this" {
|
let mut object = if object.name == "this" {
|
||||||
let object = self.ast.this_expression(object.span);
|
let object = self.ast.alloc_this_expression(object.span);
|
||||||
JSXMemberExpressionObject::ThisExpression(self.ast.alloc(object))
|
JSXMemberExpressionObject::ThisExpression(object)
|
||||||
} else {
|
} else {
|
||||||
let object = self.ast.identifier_reference(object.span, object.name);
|
let object = self.ast.alloc_identifier_reference(object.span, object.name);
|
||||||
JSXMemberExpressionObject::IdentifierReference(self.ast.alloc(object))
|
JSXMemberExpressionObject::IdentifierReference(object)
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut span = span;
|
let mut span = span;
|
||||||
|
|
@ -381,14 +379,14 @@ impl<'a> ParserImpl<'a> {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(JSXAttributeName::Identifier(self.ast.alloc(identifier)))
|
Ok(JSXAttributeName::Identifier(self.alloc(identifier)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_jsx_attribute_value(&mut self) -> Result<JSXAttributeValue<'a>> {
|
fn parse_jsx_attribute_value(&mut self) -> Result<JSXAttributeValue<'a>> {
|
||||||
match self.cur_kind() {
|
match self.cur_kind() {
|
||||||
Kind::Str => self
|
Kind::Str => self
|
||||||
.parse_literal_string()
|
.parse_literal_string()
|
||||||
.map(|str_lit| JSXAttributeValue::StringLiteral(self.ast.alloc(str_lit))),
|
.map(|str_lit| JSXAttributeValue::StringLiteral(self.alloc(str_lit))),
|
||||||
Kind::LCurly => {
|
Kind::LCurly => {
|
||||||
let expr = self.parse_jsx_expression_container(/* is_jsx_child */ false)?;
|
let expr = self.parse_jsx_expression_container(/* is_jsx_child */ false)?;
|
||||||
Ok(JSXAttributeValue::ExpressionContainer(expr))
|
Ok(JSXAttributeValue::ExpressionContainer(expr))
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ mod lexer;
|
||||||
pub mod lexer;
|
pub mod lexer;
|
||||||
|
|
||||||
use context::{Context, StatementContext};
|
use context::{Context, StatementContext};
|
||||||
use oxc_allocator::Allocator;
|
use oxc_allocator::{Allocator, Box as ArenaBox};
|
||||||
use oxc_ast::{
|
use oxc_ast::{
|
||||||
ast::{Expression, Program},
|
ast::{Expression, Program},
|
||||||
AstBuilder,
|
AstBuilder,
|
||||||
|
|
@ -535,6 +535,11 @@ impl<'a> ParserImpl<'a> {
|
||||||
self.source_type = self.source_type.with_script(true);
|
self.source_type = self.source_type.with_script(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn alloc<T>(&self, value: T) -> ArenaBox<'a, T> {
|
||||||
|
self.ast.alloc(value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
|
|
@ -270,8 +270,8 @@ impl<'a> ParserImpl<'a> {
|
||||||
let constraint = self.try_parse(Self::try_parse_constraint_of_infer_type).unwrap_or(None);
|
let constraint = self.try_parse(Self::try_parse_constraint_of_infer_type).unwrap_or(None);
|
||||||
let span = self.end_span(span);
|
let span = self.end_span(span);
|
||||||
let ts_type_parameter =
|
let ts_type_parameter =
|
||||||
self.ast.ts_type_parameter(span, name, constraint, None, false, false, false);
|
self.ast.alloc_ts_type_parameter(span, name, constraint, None, false, false, false);
|
||||||
Ok(self.ast.alloc(ts_type_parameter))
|
Ok(ts_type_parameter)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_postfix_type_or_higher(&mut self) -> Result<TSType<'a>> {
|
fn parse_postfix_type_or_higher(&mut self) -> Result<TSType<'a>> {
|
||||||
|
|
@ -380,7 +380,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
if self.peek_at(Kind::Is) && !self.peek_token().is_on_new_line {
|
if self.peek_at(Kind::Is) && !self.peek_token().is_on_new_line {
|
||||||
return self.parse_this_type_predicate(this_type);
|
return self.parse_this_type_predicate(this_type);
|
||||||
}
|
}
|
||||||
Ok(TSType::TSThisType(self.ast.alloc(this_type)))
|
Ok(TSType::TSThisType(self.alloc(this_type)))
|
||||||
}
|
}
|
||||||
Kind::Typeof => {
|
Kind::Typeof => {
|
||||||
if self.peek_at(Kind::Import) {
|
if self.peek_at(Kind::Import) {
|
||||||
|
|
@ -583,7 +583,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
let name = self.parse_binding_identifier()?;
|
let name = self.parse_binding_identifier()?;
|
||||||
self.expect(Kind::In)?;
|
self.expect(Kind::In)?;
|
||||||
let constraint = self.parse_ts_type()?;
|
let constraint = self.parse_ts_type()?;
|
||||||
let type_parameter = self.ast.alloc(self.ast.ts_type_parameter(
|
let type_parameter = self.alloc(self.ast.ts_type_parameter(
|
||||||
self.end_span(type_parameter_span),
|
self.end_span(type_parameter_span),
|
||||||
name,
|
name,
|
||||||
Some(constraint),
|
Some(constraint),
|
||||||
|
|
@ -758,8 +758,8 @@ impl<'a> ParserImpl<'a> {
|
||||||
pub(crate) fn parse_ts_type_name(&mut self) -> Result<TSTypeName<'a>> {
|
pub(crate) fn parse_ts_type_name(&mut self) -> Result<TSTypeName<'a>> {
|
||||||
let span = self.start_span();
|
let span = self.start_span();
|
||||||
let ident = self.parse_identifier_name()?;
|
let ident = self.parse_identifier_name()?;
|
||||||
let ident = self.ast.identifier_reference(ident.span, ident.name);
|
let ident = self.ast.alloc_identifier_reference(ident.span, ident.name);
|
||||||
let mut left = TSTypeName::IdentifierReference(self.ast.alloc(ident));
|
let mut left = TSTypeName::IdentifierReference(ident);
|
||||||
while self.eat(Kind::Dot) {
|
while self.eat(Kind::Dot) {
|
||||||
let right = self.parse_identifier_name()?;
|
let right = self.parse_identifier_name()?;
|
||||||
left = self.ast.ts_type_name_qualified_name(self.end_span(span), left, right);
|
left = self.ast.ts_type_name_qualified_name(self.end_span(span), left, right);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue