refactor(parser): clean up ParserState (#3345)

This commit is contained in:
Boshen 2024-05-19 01:30:16 +08:00 committed by GitHub
parent e2c6fe0cb1
commit 1e802c71d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 16 additions and 26 deletions

View file

@ -1,6 +1,6 @@
//! Code related to navigating `Token`s from the lexer
use oxc_ast::ast::RegExpFlags;
use oxc_ast::ast::{Decorator, RegExpFlags};
use oxc_diagnostics::Result;
use oxc_span::Span;
@ -306,4 +306,9 @@ impl<'a> ParserImpl<'a> {
self.ctx = ctx;
result
}
pub(crate) fn consume_decorators(&mut self) -> oxc_allocator::Vec<'a, Decorator<'a>> {
let decorators = std::mem::take(&mut self.state.decorators);
self.ast.new_vec_from_iter(decorators)
}
}

View file

@ -57,7 +57,7 @@ impl<'a> ParserImpl<'a> {
) -> Result<Box<'a, Class<'a>>> {
self.bump_any(); // advance `class`
let decorators = self.state.consume_decorators();
let decorators = self.consume_decorators();
let start_span = decorators.iter().next().map_or(start_span, |d| d.span);
let id = if self.cur_kind().is_binding_identifier() && !self.at(Kind::Implements) {
@ -360,7 +360,7 @@ impl<'a> ParserImpl<'a> {
kind
};
let decorators = self.state.consume_decorators();
let decorators = self.consume_decorators();
let value = self.parse_method(r#async, generator)?;
@ -444,7 +444,7 @@ impl<'a> ParserImpl<'a> {
accessibility,
optional,
definite,
decorators: self.state.consume_decorators(),
decorators: self.consume_decorators(),
};
Ok(ClassElement::PropertyDefinition(self.ast.alloc(property_definition)))
}
@ -474,6 +474,7 @@ impl<'a> ParserImpl<'a> {
AccessorPropertyType::AccessorProperty
};
let decorators = self.consume_decorators();
Ok(self.ast.accessor_property(
r#type,
self.end_span(span),
@ -481,7 +482,7 @@ impl<'a> ParserImpl<'a> {
value,
computed,
r#static,
self.state.consume_decorators(),
decorators,
))
}
}

View file

@ -264,7 +264,7 @@ impl<'a> SeparatedList<'a> for FormalParameterList<'a> {
}
_ => {
let pattern = p.parse_binding_pattern_with_initializer()?;
let decorators = p.state.consume_decorators();
let decorators = p.consume_decorators();
let formal_parameter = p.ast.formal_parameter(
p.end_span(span),
pattern,

View file

@ -287,7 +287,7 @@ impl<'a> ParserImpl<'a> {
errors: vec![],
token: Token::default(),
prev_token_end: 0,
state: ParserState::new(allocator),
state: ParserState::default(),
ctx: Self::default_context(source_type, options),
ast: AstBuilder::new(allocator),
preserve_parens: options.preserve_parens,

View file

@ -1,26 +1,10 @@
use rustc_hash::FxHashSet;
use oxc_allocator::{Allocator, Vec};
use oxc_ast::ast::Decorator;
#[derive(Default)]
pub struct ParserState<'a> {
allocator: &'a Allocator,
pub not_parenthesized_arrow: FxHashSet<u32>,
pub decorators: Vec<'a, Decorator<'a>>,
}
impl<'a> ParserState<'a> {
pub fn new(allocator: &'a Allocator) -> Self {
Self {
allocator,
not_parenthesized_arrow: FxHashSet::default(),
decorators: Vec::new_in(allocator),
}
}
pub fn consume_decorators(&mut self) -> Vec<'a, Decorator<'a>> {
std::mem::replace(&mut self.decorators, Vec::new_in(self.allocator))
}
pub decorators: Vec<Decorator<'a>>,
}

View file

@ -418,7 +418,7 @@ impl<'a> ParserImpl<'a> {
return Ok(());
}
let mut decorators = self.ast.new_vec();
let mut decorators = vec![];
while self.at(Kind::At) {
let decorator = self.parse_decorator()?;
decorators.push(decorator);