Skip to content

Commit

Permalink
feat(ast, parser): add oxc_regular_expression types to the parser a…
Browse files Browse the repository at this point in the history
…nd AST. (#5256)

closes #5060
  • Loading branch information
rzvxa committed Sep 3, 2024
1 parent 6730088 commit 9c630dc
Show file tree
Hide file tree
Showing 25 changed files with 1,013 additions and 165 deletions.
2 changes: 2 additions & 0 deletions .github/.generated_ast_watch_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ src:
- 'crates/oxc_syntax/src/operator.rs'
- 'crates/oxc_span/src/span/types.rs'
- 'crates/oxc_span/src/source_type/types.rs'
- 'crates/oxc_regular_expression/src/ast.rs'
- 'crates/oxc_ast/src/generated/assert_layouts.rs'
- 'crates/oxc_ast/src/generated/ast_kind.rs'
- 'crates/oxc_ast/src/generated/ast_builder.rs'
- 'crates/oxc_ast/src/generated/visit.rs'
- 'crates/oxc_ast/src/generated/visit_mut.rs'
- 'crates/oxc_ast/src/generated/derive_clone_in.rs'
- 'crates/oxc_regular_expression/src/generated/derive_clone_in.rs'
- 'crates/oxc_syntax/src/generated/derive_clone_in.rs'
- 'crates/oxc_ast/src/generated/derive_get_span.rs'
- 'crates/oxc_ast/src/generated/derive_get_span_mut.rs'
Expand Down
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions crates/oxc_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ workspace = true
doctest = false

[dependencies]
oxc_allocator = { workspace = true }
oxc_ast_macros = { workspace = true }
oxc_span = { workspace = true }
oxc_syntax = { workspace = true }
oxc_allocator = { workspace = true }
oxc_ast_macros = { workspace = true }
oxc_span = { workspace = true }
oxc_syntax = { workspace = true }
oxc_regular_expression = { workspace = true }

bitflags = { workspace = true }
num-bigint = { workspace = true }
Expand All @@ -44,4 +45,5 @@ serialize = [
"oxc_span/serialize",
"oxc_syntax/serialize",
"oxc_syntax/to_js_string",
"oxc_regular_expression/serialize"
]
25 changes: 21 additions & 4 deletions crates/oxc_ast/src/ast/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
use std::hash::Hash;

use bitflags::bitflags;
use oxc_allocator::CloneIn;
use oxc_allocator::{Box, CloneIn};
use oxc_ast_macros::ast;
use oxc_regular_expression::ast::Pattern;
use oxc_span::{Atom, GetSpan, GetSpanMut, Span};
use oxc_syntax::number::{BigintBase, NumberBase};
#[cfg(feature = "serialize")]
Expand Down Expand Up @@ -86,7 +87,7 @@ pub struct BigIntLiteral<'a> {
///
/// <https://tc39.es/ecma262/#sec-literals-regular-expression-literals>
#[ast(visit)]
#[derive(Debug, Clone, Hash)]
#[derive(Debug, Hash)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
Expand All @@ -103,16 +104,32 @@ pub struct RegExpLiteral<'a> {
///
/// <https://tc39.es/ecma262/multipage/text-processing.html#sec-regexp-regular-expression-objects>
#[ast]
#[derive(Debug, Clone, Hash)]
#[derive(Debug, Hash)]
#[generate_derive(CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub struct RegExp<'a> {
/// The regex pattern between the slashes
pub pattern: Atom<'a>,
pub pattern: RegExpPattern<'a>,
/// Regex flags after the closing slash
pub flags: RegExpFlags,
}

/// A regular expression pattern
///
/// This pattern may or may not be parsed.
#[ast]
#[derive(Debug, Hash)]
#[generate_derive(CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum RegExpPattern<'a> {
/// Unparsed pattern, Contains a string slice of the pattern.
Raw(&'a str) = 0,
/// An invalid pattern, Contains a string slice of the pattern.
Invalid(/* raw */ &'a str) = 1,
/// A parsed pattern, Read [Pattern] for more details.
Pattern(Box<'a, Pattern<'a>>) = 2,
}

#[ast]
#[derive(Debug, Clone, Hash)]
#[generate_derive(CloneIn)]
Expand Down
29 changes: 29 additions & 0 deletions crates/oxc_ast/src/ast_impl/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,35 @@ impl<'a> fmt::Display for RegExp<'a> {
}
}

impl<'a> RegExpPattern<'a> {
pub fn len(&self) -> usize {
match self {
Self::Raw(it) | Self::Invalid(it) => it.len(),
Self::Pattern(it) => it.span.size() as usize,
}
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}

pub fn source_text(&self, source_text: &'a str) -> &'a str {
match self {
Self::Raw(raw) | Self::Invalid(raw) => raw,
Self::Pattern(pat) => pat.span.source_text(source_text),
}
}
}

impl<'a> fmt::Display for RegExpPattern<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Raw(it) | Self::Invalid(it) => write!(f, "{it}"),
Self::Pattern(it) => it.fmt(f),
}
}
}

impl TryFrom<char> for RegExpFlags {
type Error = char;

Expand Down
Loading

0 comments on commit 9c630dc

Please sign in to comment.