@@ -8,13 +8,22 @@ import { onigurumaToRegexp } from 'oniguruma-to-js'
88export interface JavaScriptRegexEngineOptions {
99 /**
1010 * Whether to allow invalid regex patterns.
11+ *
12+ * @default false
1113 */
1214 forgiving ?: boolean
1315
16+ /**
17+ * Use JavaScript to simulate some unsupported regex features.
18+ *
19+ * @default true
20+ */
21+ simulation ?: boolean
22+
1423 /**
1524 * Cache for regex patterns.
1625 */
17- cache ?: Map < string , RegExp | Error >
26+ cache ?: Map < string , RegExp | Error > | null
1827
1928 /**
2029 * Custom pattern to RegExp constructor.
@@ -45,13 +54,18 @@ export class JavaScriptScanner implements PatternScanner {
4554
4655 constructor (
4756 public patterns : string [ ] ,
48- public cache : Map < string , RegExp | Error > ,
49- public forgiving : boolean ,
50- public regexConstructor : ( pattern : string ) => RegExp = defaultJavaScriptRegexConstructor ,
57+ public options : JavaScriptRegexEngineOptions = { } ,
5158 ) {
59+ const {
60+ forgiving = false ,
61+ cache,
62+ simulation = true ,
63+ regexConstructor = defaultJavaScriptRegexConstructor ,
64+ } = options
65+
5266 this . contiguousAnchorSimulation = Array . from ( { length : patterns . length } , ( ) => false )
5367 this . regexps = patterns . map ( ( p , idx ) => {
54- if ( p . startsWith ( '(^|\\G)' ) || p . startsWith ( '(\\G|^)' ) )
68+ if ( simulation && ( p . startsWith ( '(^|\\G)' ) || p . startsWith ( '(\\G|^)' ) ) )
5569 this . contiguousAnchorSimulation [ idx ] = true
5670 const cached = cache ?. get ( p )
5771 if ( cached ) {
@@ -129,7 +143,7 @@ export class JavaScriptScanner implements PatternScanner {
129143 pending . push ( [ i , match , offset ] )
130144 }
131145 catch ( e ) {
132- if ( this . forgiving )
146+ if ( this . options . forgiving )
133147 continue
134148 throw e
135149 }
@@ -159,14 +173,14 @@ export class JavaScriptScanner implements PatternScanner {
159173 * @experimental
160174 */
161175export function createJavaScriptRegexEngine ( options : JavaScriptRegexEngineOptions = { } ) : RegexEngine {
162- const {
163- forgiving = false ,
164- cache = new Map ( ) ,
165- } = options
176+ const _options = {
177+ cache : new Map ( ) ,
178+ ... options ,
179+ }
166180
167181 return {
168182 createScanner ( patterns : string [ ] ) {
169- return new JavaScriptScanner ( patterns , cache , forgiving , options . regexConstructor )
183+ return new JavaScriptScanner ( patterns , _options )
170184 } ,
171185 createString ( s : string ) {
172186 return {
0 commit comments