Skip to content

Commit

Permalink
feat(engine-js): introduce simulation option
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 12, 2024
1 parent 43ecce7 commit adf99f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
36 changes: 25 additions & 11 deletions packages/engine-javascript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ import { onigurumaToRegexp } from 'oniguruma-to-js'
export interface JavaScriptRegexEngineOptions {
/**
* Whether to allow invalid regex patterns.
*
* @default false
*/
forgiving?: boolean

/**
* Use JavaScript to simulate some unsupported regex features.
*
* @default true
*/
simulation?: boolean

/**
* Cache for regex patterns.
*/
cache?: Map<string, RegExp | Error>
cache?: Map<string, RegExp | Error> | null

/**
* Custom pattern to RegExp constructor.
Expand Down Expand Up @@ -45,13 +54,18 @@ export class JavaScriptScanner implements PatternScanner {

constructor(
public patterns: string[],
public cache: Map<string, RegExp | Error>,
public forgiving: boolean,
public regexConstructor: (pattern: string) => RegExp = defaultJavaScriptRegexConstructor,
public options: JavaScriptRegexEngineOptions = {},
) {
const {
forgiving = false,
cache,
simulation = true,
regexConstructor = defaultJavaScriptRegexConstructor,
} = options

this.contiguousAnchorSimulation = Array.from({ length: patterns.length }, () => false)
this.regexps = patterns.map((p, idx) => {
if (p.startsWith('(^|\\G)') || p.startsWith('(\\G|^)'))
if (simulation && (p.startsWith('(^|\\G)') || p.startsWith('(\\G|^)')))
this.contiguousAnchorSimulation[idx] = true
const cached = cache?.get(p)
if (cached) {
Expand Down Expand Up @@ -129,7 +143,7 @@ export class JavaScriptScanner implements PatternScanner {
pending.push([i, match, offset])
}
catch (e) {
if (this.forgiving)
if (this.options.forgiving)
continue
throw e
}
Expand Down Expand Up @@ -159,14 +173,14 @@ export class JavaScriptScanner implements PatternScanner {
* @experimental
*/
export function createJavaScriptRegexEngine(options: JavaScriptRegexEngineOptions = {}): RegexEngine {
const {
forgiving = false,
cache = new Map(),
} = options
const _options = {
cache: new Map(),
...options,
}

return {
createScanner(patterns: string[]) {
return new JavaScriptScanner(patterns, cache, forgiving, options.regexConstructor)
return new JavaScriptScanner(patterns, _options)
},
createString(s: string) {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/engine-javascript/test/verify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ for (const file of files) {
for (const instance of instances) {
i += 1
describe(`instances ${i}`, () => {
const scanner = new JavaScriptScanner(instance.constractor[0], cache, false)
const scanner = new JavaScriptScanner(instance.constractor[0], { cache })
let j = 0
for (const execution of instance.executions) {
j += 1
Expand Down

0 comments on commit adf99f2

Please sign in to comment.