mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
refactor(parser): clean up ParserState (#3345)
This commit is contained in:
parent
e2c6fe0cb1
commit
1e802c71d5
6 changed files with 16 additions and 26 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
//! Code related to navigating `Token`s from the lexer
|
//! 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_diagnostics::Result;
|
||||||
use oxc_span::Span;
|
use oxc_span::Span;
|
||||||
|
|
||||||
|
|
@ -306,4 +306,9 @@ impl<'a> ParserImpl<'a> {
|
||||||
self.ctx = ctx;
|
self.ctx = ctx;
|
||||||
result
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
) -> Result<Box<'a, Class<'a>>> {
|
) -> Result<Box<'a, Class<'a>>> {
|
||||||
self.bump_any(); // advance `class`
|
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 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) {
|
let id = if self.cur_kind().is_binding_identifier() && !self.at(Kind::Implements) {
|
||||||
|
|
@ -360,7 +360,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
kind
|
kind
|
||||||
};
|
};
|
||||||
|
|
||||||
let decorators = self.state.consume_decorators();
|
let decorators = self.consume_decorators();
|
||||||
|
|
||||||
let value = self.parse_method(r#async, generator)?;
|
let value = self.parse_method(r#async, generator)?;
|
||||||
|
|
||||||
|
|
@ -444,7 +444,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
accessibility,
|
accessibility,
|
||||||
optional,
|
optional,
|
||||||
definite,
|
definite,
|
||||||
decorators: self.state.consume_decorators(),
|
decorators: self.consume_decorators(),
|
||||||
};
|
};
|
||||||
Ok(ClassElement::PropertyDefinition(self.ast.alloc(property_definition)))
|
Ok(ClassElement::PropertyDefinition(self.ast.alloc(property_definition)))
|
||||||
}
|
}
|
||||||
|
|
@ -474,6 +474,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
AccessorPropertyType::AccessorProperty
|
AccessorPropertyType::AccessorProperty
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let decorators = self.consume_decorators();
|
||||||
Ok(self.ast.accessor_property(
|
Ok(self.ast.accessor_property(
|
||||||
r#type,
|
r#type,
|
||||||
self.end_span(span),
|
self.end_span(span),
|
||||||
|
|
@ -481,7 +482,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
value,
|
value,
|
||||||
computed,
|
computed,
|
||||||
r#static,
|
r#static,
|
||||||
self.state.consume_decorators(),
|
decorators,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -264,7 +264,7 @@ impl<'a> SeparatedList<'a> for FormalParameterList<'a> {
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let pattern = p.parse_binding_pattern_with_initializer()?;
|
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(
|
let formal_parameter = p.ast.formal_parameter(
|
||||||
p.end_span(span),
|
p.end_span(span),
|
||||||
pattern,
|
pattern,
|
||||||
|
|
|
||||||
|
|
@ -287,7 +287,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
errors: vec![],
|
errors: vec![],
|
||||||
token: Token::default(),
|
token: Token::default(),
|
||||||
prev_token_end: 0,
|
prev_token_end: 0,
|
||||||
state: ParserState::new(allocator),
|
state: ParserState::default(),
|
||||||
ctx: Self::default_context(source_type, options),
|
ctx: Self::default_context(source_type, options),
|
||||||
ast: AstBuilder::new(allocator),
|
ast: AstBuilder::new(allocator),
|
||||||
preserve_parens: options.preserve_parens,
|
preserve_parens: options.preserve_parens,
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,10 @@
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
use oxc_allocator::{Allocator, Vec};
|
|
||||||
use oxc_ast::ast::Decorator;
|
use oxc_ast::ast::Decorator;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
pub struct ParserState<'a> {
|
pub struct ParserState<'a> {
|
||||||
allocator: &'a Allocator,
|
|
||||||
|
|
||||||
pub not_parenthesized_arrow: FxHashSet<u32>,
|
pub not_parenthesized_arrow: FxHashSet<u32>,
|
||||||
|
|
||||||
pub decorators: Vec<'a, Decorator<'a>>,
|
pub decorators: Vec<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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -418,7 +418,7 @@ impl<'a> ParserImpl<'a> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut decorators = self.ast.new_vec();
|
let mut decorators = vec![];
|
||||||
while self.at(Kind::At) {
|
while self.at(Kind::At) {
|
||||||
let decorator = self.parse_decorator()?;
|
let decorator = self.parse_decorator()?;
|
||||||
decorators.push(decorator);
|
decorators.push(decorator);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue