Skip to content

Commit adf99f2

Browse files
committedSep 12, 2024··
feat(engine-js): introduce simulation option
1 parent 43ecce7 commit adf99f2

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed
 

‎packages/engine-javascript/src/index.ts

+25-11
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@ import { onigurumaToRegexp } from 'oniguruma-to-js'
88
export 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
*/
161175
export 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 {

‎packages/engine-javascript/test/verify.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ for (const file of files) {
2828
for (const instance of instances) {
2929
i += 1
3030
describe(`instances ${i}`, () => {
31-
const scanner = new JavaScriptScanner(instance.constractor[0], cache, false)
31+
const scanner = new JavaScriptScanner(instance.constractor[0], { cache })
3232
let j = 0
3333
for (const execution of instance.executions) {
3434
j += 1

0 commit comments

Comments
 (0)
Please sign in to comment.