Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ oxc_isolated_declarations = { version = "0.60.0", path = "crates/oxc_isolated_de
oxc_mangler = { version = "0.60.0", path = "crates/oxc_mangler" }
oxc_minifier = { version = "0.60.0", path = "crates/oxc_minifier" }
oxc_napi = { version = "0.60.0", path = "crates/oxc_napi" }
oxc_parser = { version = "0.60.0", path = "crates/oxc_parser" }
oxc_parser = { version = "0.60.0", path = "crates/oxc_parser", features = ["regular_expression"] }
oxc_parser_napi = { version = "0.60.0", path = "napi/parser" }
oxc_regular_expression = { version = "0.60.0", path = "crates/oxc_regular_expression" }
oxc_semantic = { version = "0.60.0", path = "crates/oxc_semantic" }
Expand Down
8 changes: 6 additions & 2 deletions crates/oxc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ required-features = ["full"]
[dependencies]
oxc_allocator = { workspace = true }
oxc_ast = { workspace = true }
oxc_parser = { workspace = true }
oxc_regular_expression = { workspace = true }
oxc_parser = { workspace = true, features = [] }
oxc_regular_expression = { workspace = true, optional = true }

oxc_ast_visit = { workspace = true, optional = true }
oxc_cfg = { workspace = true, optional = true }
Expand All @@ -44,6 +44,8 @@ oxc_syntax = { workspace = true }
oxc_transformer = { workspace = true, optional = true }

[features]
default = ["regular_expression"]

full = [
"codegen",
"mangler",
Expand All @@ -53,6 +55,7 @@ full = [
"isolated_declarations",
"ast_visit",
"cfg",
"regular_expression",
]

semantic = ["oxc_semantic"]
Expand All @@ -63,6 +66,7 @@ mangler = ["oxc_mangler"]
cfg = ["oxc_cfg"]
isolated_declarations = ["oxc_isolated_declarations"]
ast_visit = ["oxc_ast_visit"]
regular_expression = ["oxc_regular_expression", "oxc_parser/regular_expression"]

serialize = [
"oxc_allocator/from_raw_parts",
Expand Down
1 change: 1 addition & 0 deletions crates/oxc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod parser {
pub use oxc_parser::*;
}

#[cfg(feature = "regular_expression")]
pub mod regular_expression {
#[doc(inline)]
pub use oxc_regular_expression::*;
Expand Down
5 changes: 4 additions & 1 deletion crates/oxc_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ oxc_allocator = { workspace = true }
oxc_ast = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_ecmascript = { workspace = true }
oxc_regular_expression = { workspace = true }
oxc_regular_expression = { workspace = true, optional = true }
oxc_span = { workspace = true }
oxc_syntax = { workspace = true }

Expand All @@ -43,5 +43,8 @@ oxc_ast_visit = { workspace = true, features = ["serialize"] }
pico-args = { workspace = true }

[features]
default = ["regular_expression"]
# Parse regex
regular_expression = ["oxc_regular_expression"]
# Expose Lexer for benchmarks
benchmarking = []
37 changes: 26 additions & 11 deletions crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use cow_utils::CowUtils;
use oxc_allocator::Box;
use oxc_ast::ast::*;
use oxc_diagnostics::Result;
#[cfg(feature = "regular_expression")]
use oxc_regular_expression::ast::Pattern;
use oxc_span::{Atom, GetSpan, Span};
use oxc_syntax::{
Expand Down Expand Up @@ -342,25 +343,39 @@ impl<'a> ParserImpl<'a> {
let flags_text = &self.source_text[flags_start as usize..self.cur_token().end as usize];
let raw = self.cur_src();
self.bump_any();

// Parse pattern if options is enabled and also flags are valid
let pattern = (self.options.parse_regular_expression && !flags_error)
.then_some(())
.map(|()| {
self.parse_regex_pattern(pattern_start, pattern_text, flags_start, flags_text)
})
.map_or_else(
|| RegExpPattern::Raw(pattern_text),
|pat| {
pat.map_or_else(|| RegExpPattern::Invalid(pattern_text), RegExpPattern::Pattern)
},
);
#[cfg(feature = "regular_expression")]
let pattern = {
(self.options.parse_regular_expression && !flags_error)
.then_some(())
.map(|()| {
self.parse_regex_pattern(pattern_start, pattern_text, flags_start, flags_text)
})
.map_or_else(
|| RegExpPattern::Raw(pattern_text),
|pat| {
pat.map_or_else(
|| RegExpPattern::Invalid(pattern_text),
RegExpPattern::Pattern,
)
},
)
};
#[cfg(not(feature = "regular_expression"))]
let pattern = {
let _ = (flags_start, flags_text, flags_error);
RegExpPattern::Raw(pattern_text)
};

Ok(self.ast.reg_exp_literal(
self.end_span(span),
RegExp { pattern, flags },
Some(Atom::from(raw)),
))
}

#[cfg(feature = "regular_expression")]
fn parse_regex_pattern(
&mut self,
pattern_span_offset: u32,
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ pub struct ParseOptions {
/// Whether to parse regular expressions or not.
///
/// Default: `false`
#[cfg(feature = "regular_expression")]
pub parse_regular_expression: bool,

/// Allow [`return`] statements outside of functions.
Expand Down Expand Up @@ -223,6 +224,7 @@ pub struct ParseOptions {
impl Default for ParseOptions {
fn default() -> Self {
Self {
#[cfg(feature = "regular_expression")]
parse_regular_expression: false,
allow_return_outside_function: false,
preserve_parens: true,
Expand Down
2 changes: 1 addition & 1 deletion napi/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test = false
doctest = false

[dependencies]
oxc = { workspace = true, features = ["ast_visit", "semantic", "serialize"] }
oxc = { workspace = true, features = ["ast_visit", "regular_expression", "semantic", "serialize"] }
oxc_ast_macros = { workspace = true }
oxc_estree = { workspace = true }
oxc_napi = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion napi/playground/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test = false
doctest = false

[dependencies]
oxc = { workspace = true, features = ["ast_visit", "codegen", "minifier", "semantic", "serialize", "transformer", "isolated_declarations"] }
oxc = { workspace = true, features = ["ast_visit", "codegen", "minifier", "semantic", "serialize", "transformer", "isolated_declarations", "regular_expression"] }
oxc_index = { workspace = true }
oxc_linter = { workspace = true }
oxc_napi = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion tasks/benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ oxc_isolated_declarations = { workspace = true, optional = true }
oxc_linter = { workspace = true, optional = true }
oxc_mangler = { workspace = true, optional = true }
oxc_minifier = { workspace = true, optional = true }
oxc_parser = { workspace = true, features = ["benchmarking"], optional = true }
oxc_parser = { workspace = true, features = ["benchmarking", "regular_expression"], optional = true }
oxc_prettier = { workspace = true, optional = true }
oxc_semantic = { workspace = true, optional = true }
oxc_span = { workspace = true, optional = true, features = ["schemars", "serialize"] }
Expand Down
Loading