Skip to content

Commit

Permalink
refactor(regular_expression): Misc fixes (#6234)
Browse files Browse the repository at this point in the history
Preparation for #6141

- Keep `enum` size + add size asserts tests
- Arrange AST related directories
- Renaming
  • Loading branch information
leaysgur committed Oct 2, 2024
1 parent 1c31932 commit acab777
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 105 deletions.
12 changes: 6 additions & 6 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ const _: () = {
assert!(offset_of!(Alternative, span) == 0usize);
assert!(offset_of!(Alternative, body) == 8usize);

assert!(size_of::<Term>() == 24usize);
assert!(size_of::<Term>() == 16usize);
assert!(align_of::<Term>() == 8usize);

assert!(size_of::<BoundaryAssertion>() == 12usize);
Expand All @@ -1465,7 +1465,7 @@ const _: () = {
assert!(size_of::<LookAroundAssertionKind>() == 1usize);
assert!(align_of::<LookAroundAssertionKind>() == 1usize);

assert!(size_of::<Quantifier>() == 64usize);
assert!(size_of::<Quantifier>() == 56usize);
assert!(align_of::<Quantifier>() == 8usize);
assert!(offset_of!(Quantifier, span) == 0usize);
assert!(offset_of!(Quantifier, min) == 8usize);
Expand Down Expand Up @@ -1513,7 +1513,7 @@ const _: () = {
assert!(size_of::<CharacterClassContentsKind>() == 1usize);
assert!(align_of::<CharacterClassContentsKind>() == 1usize);

assert!(size_of::<CharacterClassContents>() == 24usize);
assert!(size_of::<CharacterClassContents>() == 16usize);
assert!(align_of::<CharacterClassContents>() == 8usize);

assert!(size_of::<CharacterClassRange>() == 40usize);
Expand Down Expand Up @@ -3000,7 +3000,7 @@ const _: () = {
assert!(offset_of!(Alternative, span) == 0usize);
assert!(offset_of!(Alternative, body) == 8usize);

assert!(size_of::<Term>() == 20usize);
assert!(size_of::<Term>() == 12usize);
assert!(align_of::<Term>() == 4usize);

assert!(size_of::<BoundaryAssertion>() == 12usize);
Expand All @@ -3020,7 +3020,7 @@ const _: () = {
assert!(size_of::<LookAroundAssertionKind>() == 1usize);
assert!(align_of::<LookAroundAssertionKind>() == 1usize);

assert!(size_of::<Quantifier>() == 56usize);
assert!(size_of::<Quantifier>() == 48usize);
assert!(align_of::<Quantifier>() == 8usize);
assert!(offset_of!(Quantifier, span) == 0usize);
assert!(offset_of!(Quantifier, min) == 8usize);
Expand Down Expand Up @@ -3068,7 +3068,7 @@ const _: () = {
assert!(size_of::<CharacterClassContentsKind>() == 1usize);
assert!(align_of::<CharacterClassContentsKind>() == 1usize);

assert!(size_of::<CharacterClassContents>() == 20usize);
assert!(size_of::<CharacterClassContents>() == 8usize);
assert!(align_of::<CharacterClassContents>() == 4usize);

assert!(size_of::<CharacterClassRange>() == 40usize);
Expand Down
26 changes: 16 additions & 10 deletions crates/oxc_regular_expression/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// NB: `#[span]`, `#[scope(...)]`,`#[visit(...)]` and `#[generate_derive(...)]` do NOT do anything to the code.
// They are purely markers for codegen used in `tasks/ast_tools` and `crates/oxc_traverse/scripts`. See docs in those crates.
// Read [`macro@oxc_ast_macros::ast`] for more information.

// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
#![allow(non_snake_case)]

Expand Down Expand Up @@ -76,19 +72,19 @@ pub struct Alternative<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum Term<'a> {
// Assertion, QuantifiableAssertion
BoundaryAssertion(BoundaryAssertion) = 0,
BoundaryAssertion(Box<'a, BoundaryAssertion>) = 0,
LookAroundAssertion(Box<'a, LookAroundAssertion<'a>>) = 1,
// Quantifier
Quantifier(Box<'a, Quantifier<'a>>) = 2,
// Atom, ExtendedAtom
Character(Character) = 3,
Character(Box<'a, Character>) = 3,
Dot(Dot) = 4,
CharacterClassEscape(CharacterClassEscape) = 5,
CharacterClassEscape(Box<'a, CharacterClassEscape>) = 5,
UnicodePropertyEscape(Box<'a, UnicodePropertyEscape<'a>>) = 6,
CharacterClass(Box<'a, CharacterClass<'a>>) = 7,
CapturingGroup(Box<'a, CapturingGroup<'a>>) = 8,
IgnoreGroup(Box<'a, IgnoreGroup<'a>>) = 9,
IndexedReference(IndexedReference) = 10,
IndexedReference(Box<'a, IndexedReference>) = 10,
NamedReference(Box<'a, NamedReference<'a>>) = 11,
}

Expand Down Expand Up @@ -286,9 +282,9 @@ pub enum CharacterClassContentsKind {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum CharacterClassContents<'a> {
CharacterClassRange(Box<'a, CharacterClassRange>) = 0,
CharacterClassEscape(CharacterClassEscape) = 1,
CharacterClassEscape(Box<'a, CharacterClassEscape>) = 1,
UnicodePropertyEscape(Box<'a, UnicodePropertyEscape<'a>>) = 2,
Character(Character) = 3,
Character(Box<'a, Character>) = 3,
/// `UnicodeSetsMode` only
NestedCharacterClass(Box<'a, CharacterClass<'a>>) = 4,
/// `UnicodeSetsMode` only
Expand Down Expand Up @@ -404,3 +400,13 @@ pub struct NamedReference<'a> {
pub span: Span,
pub name: Atom<'a>,
}

// See `oxc_ast/src/lib.rs` for the details
#[cfg(target_pointer_width = "64")]
#[test]
fn size_asserts() {
use std::mem::size_of;

assert!(size_of::<Term>() == 16);
assert!(size_of::<CharacterClassContents>() == 16);
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ impl<'a> Display for Alternative<'a> {
impl<'a> Display for Term<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BoundaryAssertion(it) => write!(f, "{it}"),
Self::BoundaryAssertion(it) => write!(f, "{}", it.as_ref()),
Self::LookAroundAssertion(it) => write!(f, "{}", it.as_ref()),
Self::Quantifier(it) => write!(f, "{}", it.as_ref()),
Self::Character(it) => write!(f, "{it}"),
Self::Character(it) => write!(f, "{}", it.as_ref()),
Self::Dot(it) => write!(f, "{it}"),
Self::CharacterClassEscape(it) => write!(f, "{it}"),
Self::CharacterClassEscape(it) => write!(f, "{}", it.as_ref()),
Self::UnicodePropertyEscape(it) => write!(f, "{}", it.as_ref()),
Self::CharacterClass(it) => write!(f, "{}", it.as_ref()),
Self::CapturingGroup(it) => write!(f, "{}", it.as_ref()),
Self::IgnoreGroup(it) => write!(f, "{}", it.as_ref()),
Self::IndexedReference(it) => write!(f, "{it}"),
Self::IndexedReference(it) => write!(f, "{}", it.as_ref()),
Self::NamedReference(it) => write!(f, "{}", it.as_ref()),
}
}
Expand Down Expand Up @@ -246,9 +246,9 @@ impl<'a> Display for CharacterClassContents<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CharacterClassRange(it) => write!(f, "{}", it.as_ref()),
Self::CharacterClassEscape(it) => write!(f, "{it}"),
Self::CharacterClassEscape(it) => write!(f, "{}", it.as_ref()),
Self::UnicodePropertyEscape(it) => write!(f, "{}", it.as_ref()),
Self::Character(it) => write!(f, "{it}"),
Self::Character(it) => write!(f, "{}", it.as_ref()),
Self::NestedCharacterClass(it) => write!(f, "{}", it.as_ref()),
Self::ClassStringDisjunction(it) => write!(f, "{}", it.as_ref()),
}
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_regular_expression/src/ast_impl/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod display;
pub mod visit;
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// NOTE: For now, this file is implemented by hand for convenience.
// But like `oxc_ast`, this should be generated by `tasks/ast_tools` in the future.

#![allow(unused_variables, clippy::wildcard_imports)]
use oxc_span::{GetSpan, Span};
use walk::walk_pattern;
Expand Down
Loading

0 comments on commit acab777

Please sign in to comment.