Skip to content

Commit

Permalink
refactor(parser): move code around for parsing Modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jun 23, 2024
1 parent 2725b71 commit e953dcd
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 478 deletions.
129 changes: 0 additions & 129 deletions crates/oxc_ast/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,132 +183,3 @@ mod ts;
use macros::inherit_variants;

pub use self::{js::*, jsx::*, literal::*, ts::*};

#[cfg(feature = "serialize")]
use serde::Serialize;
#[cfg(feature = "serialize")]
use tsify::Tsify;

use oxc_allocator::Vec;
use oxc_span::Span;

#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type", rename_all = "camelCase"))]
pub struct Modifier {
#[cfg_attr(feature = "serialize", serde(flatten))]
pub span: Span,
pub kind: ModifierKind,
}

#[derive(Debug, Default, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(transparent))]
pub struct Modifiers<'a>(Option<Vec<'a, Modifier>>);

impl<'a> Modifiers<'a> {
pub fn new(modifiers: Vec<'a, Modifier>) -> Self {
Self(Some(modifiers))
}

pub fn empty() -> Self {
Self(None)
}

pub fn is_none(&self) -> bool {
self.0.is_none()
}

pub fn contains(&self, target: ModifierKind) -> bool {
self.0
.as_ref()
.map_or(false, |modifiers| modifiers.iter().any(|modifier| modifier.kind == target))
}

pub fn iter(&self) -> impl Iterator<Item = &Modifier> + '_ {
self.0.as_ref().into_iter().flat_map(|modifiers| modifiers.iter())
}

/// Find a modifier by kind
pub fn find(&self, kind: ModifierKind) -> Option<&Modifier> {
self.find_where(|modifier| modifier.kind == kind)
}

pub fn find_where<F>(&self, f: F) -> Option<&Modifier>
where
F: Fn(&Modifier) -> bool,
{
self.0.as_ref().and_then(|modifiers| modifiers.iter().find(|modifier| f(modifier)))
}

pub fn is_contains_const(&self) -> bool {
self.contains(ModifierKind::Const)
}

pub fn is_contains_declare(&self) -> bool {
self.contains(ModifierKind::Declare)
}

pub fn is_contains_abstract(&self) -> bool {
self.contains(ModifierKind::Abstract)
}

pub fn remove_type_modifiers(&mut self) {
if let Some(list) = &mut self.0 {
list.retain(|m| !m.kind.is_typescript_syntax());
}
}

pub fn add_modifier(&mut self, modifier: Modifier) {
if let Some(list) = self.0.as_mut() {
list.push(modifier);
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
pub enum ModifierKind {
Abstract,
Accessor,
Async,
Const,
Declare,
Default,
Export,
In,
Public,
Private,
Protected,
Readonly,
Static,
Out,
Override,
}

impl ModifierKind {
pub fn is_typescript_syntax(&self) -> bool {
!matches!(self, Self::Async | Self::Default | Self::Export | Self::Static)
}

pub fn as_str(self) -> &'static str {
match self {
Self::Abstract => "abstract",
Self::Accessor => "accessor",
Self::Async => "async",
Self::Const => "const",
Self::Declare => "declare",
Self::Default => "default",
Self::Export => "export",
Self::In => "in",
Self::Public => "public",
Self::Private => "private",
Self::Protected => "protected",
Self::Readonly => "readonly",
Self::Static => "static",
Self::Out => "out",
Self::Override => "override",
}
}
}
8 changes: 7 additions & 1 deletion crates/oxc_parser/src/js/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ use oxc_diagnostics::Result;
use oxc_span::{GetSpan, Span};

use super::list::ClassElements;
use crate::{diagnostics, lexer::Kind, list::NormalList, Context, ParserImpl, StatementContext};
use crate::{
diagnostics,
lexer::Kind,
list::NormalList,
modifiers::{ModifierKind, Modifiers},
Context, ParserImpl, StatementContext,
};

type Extends<'a> =
Vec<'a, (Expression<'a>, Option<Box<'a, TSTypeParameterInstantiation<'a>>>, Span)>;
Expand Down
7 changes: 6 additions & 1 deletion crates/oxc_parser/src/js/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use oxc_diagnostics::Result;
use oxc_span::{GetSpan, Span};

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

impl<'a> ParserImpl<'a> {
pub(crate) fn parse_let(&mut self, stmt_ctx: StatementContext) -> Result<Statement<'a>> {
Expand Down
8 changes: 7 additions & 1 deletion crates/oxc_parser/src/js/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ use oxc_diagnostics::Result;
use oxc_span::Span;

use super::{list::FormalParameterList, FunctionKind};
use crate::{diagnostics, lexer::Kind, list::SeparatedList, Context, ParserImpl, StatementContext};
use crate::{
diagnostics,
lexer::Kind,
list::SeparatedList,
modifiers::{ModifierKind, Modifiers},
Context, ParserImpl, StatementContext,
};

impl FunctionKind {
pub(crate) fn is_id_required(self) -> bool {
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_parser/src/js/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use super::{
list::{AssertEntries, ExportNamedSpecifiers, ImportSpecifierList},
FunctionKind,
};
use crate::{diagnostics, lexer::Kind, list::SeparatedList, Context, ParserImpl};
use crate::{
diagnostics, lexer::Kind, list::SeparatedList, modifiers::Modifiers, Context, ParserImpl,
};

impl<'a> ParserImpl<'a> {
/// [Import Call](https://tc39.es/ecma262/#sec-import-calls)
Expand Down
5 changes: 4 additions & 1 deletion crates/oxc_parser/src/js/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use oxc_span::{Atom, GetSpan, Span};
use super::{
grammar::CoverGrammar, list::SwitchCases, VariableDeclarationContext, VariableDeclarationParent,
};
use crate::{diagnostics, lexer::Kind, list::NormalList, Context, ParserImpl, StatementContext};
use crate::{
diagnostics, lexer::Kind, list::NormalList, modifiers::Modifiers, Context, ParserImpl,
StatementContext,
};

impl<'a> ParserImpl<'a> {
// Section 12
Expand Down
14 changes: 1 addition & 13 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
mod context;
mod cursor;
mod list;
mod modifiers;
mod state;

mod js;
Expand Down Expand Up @@ -313,19 +314,6 @@ impl<'a> ParserImpl<'a> {
}
}

/// Backdoor to create a `ParserImpl` without holding a `UniquePromise`, for unit tests.
/// This function must NOT be exposed in public API as it breaks safety invariants.
#[cfg(test)]
fn new_for_tests(
allocator: &'a Allocator,
source_text: &'a str,
source_type: SourceType,
options: ParserOptions,
) -> Self {
let unique = UniquePromise::new_for_tests();
Self::new(allocator, source_text, source_type, options, unique)
}

/// Main entry point
///
/// Returns an empty `Program` on unrecoverable error,
Expand Down
Loading

0 comments on commit e953dcd

Please sign in to comment.