-
-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(engine-js): improve js engine by replacing hard-coded recursive …
…reference
- Loading branch information
Showing
8 changed files
with
131 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import fs from 'node:fs/promises' | ||
import { expandRecursiveBackReference } from './utils' | ||
|
||
interface ReplacementRecursiveBackReference { | ||
type: 'recursive-back-reference' | ||
regex: string | ||
groupName: string | ||
fallback: string | ||
recursive?: number | ||
} | ||
|
||
interface ReplacementStatic { | ||
type: 'static' | ||
regex: string | ||
replacement: string | ||
} | ||
|
||
type Replacement = ReplacementRecursiveBackReference | ReplacementStatic | ||
|
||
const replacements: Replacement[] = [ | ||
{ | ||
// Subroutine recursive reference are not supported in JavaScript regex engine. | ||
// We expand a few levels of recursion to literals to simulate the behavior (incomplete) | ||
type: 'recursive-back-reference', | ||
regex: '(?<square>[^\\[\\]\\\\]|\\\\.|\\[\\g<square>*+\\])', | ||
groupName: 'square', | ||
fallback: '(?:[^\\[\\]\\\\])', | ||
}, | ||
{ | ||
type: 'recursive-back-reference', | ||
regex: '(?<url>(?>[^\\s()]+)|\\(\\g<url>*\\))', | ||
groupName: 'url', | ||
fallback: '[^\\s\\(\\)]', | ||
}, | ||
] | ||
|
||
const result = replacements.map((r) => { | ||
switch (r.type) { | ||
case 'recursive-back-reference': | ||
return [r.regex, expandRecursiveBackReference(r.regex, r.groupName, r.fallback, r.recursive ?? 2)] | ||
case 'static': | ||
return [r.regex, r.replacement] | ||
default: | ||
throw new Error(`Unknown replacement type: ${(r as any).type}`) | ||
} | ||
}) | ||
|
||
fs.writeFile(new URL('../src/replacements.ts', import.meta.url), `// Generated by script\n\nexport const replacements = ${JSON.stringify(result, null, 2)} as [string, string][]\n`, 'utf-8') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export function expandRecursiveBackReference( | ||
regex: string, | ||
name: string, | ||
fallback: string, | ||
recursive = 2, | ||
) { | ||
const refMarker = new RegExp(`\\\\g<${name}>`, 'g') | ||
const groupMaker = new RegExp(`\\(\\?<${name}>`, 'g') | ||
const normalized = regex.replace(groupMaker, '(?:') | ||
|
||
let out = regex | ||
for (let i = 0; i < recursive; i++) { | ||
out = out.replace(refMarker, normalized) | ||
} | ||
|
||
out = out | ||
.replace(refMarker, fallback) | ||
.replace(groupMaker, '(?:') | ||
|
||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Generated by script | ||
|
||
export const replacements = [ | ||
[ | ||
'(?<square>[^\\[\\]\\\\]|\\\\.|\\[\\g<square>*+\\])', | ||
'(?:[^\\[\\]\\\\]|\\\\.|\\[(?:[^\\[\\]\\\\]|\\\\.|\\[(?:[^\\[\\]\\\\]|\\\\.|\\[(?:[^\\[\\]\\\\])*+\\])*+\\])*+\\])', | ||
], | ||
[ | ||
'(?<url>(?>[^\\s()]+)|\\(\\g<url>*\\))', | ||
'(?:(?>[^\\s()]+)|\\((?:(?>[^\\s()]+)|\\((?:(?>[^\\s()]+)|\\([^\\s\\(\\)]*\\))*\\))*\\))', | ||
], | ||
] as [string, string][] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { expandRecursiveBackReference } from '../scripts/utils' | ||
|
||
describe('expandRecursiveBackReference', () => { | ||
it('case 1', () => { | ||
const name = 'square' | ||
const regex = '(?<square>[^\\[\\]\\\\]|\\\\.|\\[\\g<square>*\\])' | ||
const fallback = '(?:[^\\[\\]\\\\])' | ||
|
||
expect(expandRecursiveBackReference(regex, name, fallback, 0)) | ||
.toMatchInlineSnapshot(`"(?:[^\\[\\]\\\\]|\\\\.|\\[(?:[^\\[\\]\\\\])*\\])"`) | ||
|
||
expect(expandRecursiveBackReference(regex, name, fallback, 1)) | ||
.toMatchInlineSnapshot(`"(?:[^\\[\\]\\\\]|\\\\.|\\[(?:[^\\[\\]\\\\]|\\\\.|\\[(?:[^\\[\\]\\\\])*\\])*\\])"`) | ||
|
||
expect(expandRecursiveBackReference(regex, name, fallback, 2)) | ||
.toMatchInlineSnapshot(`"(?:[^\\[\\]\\\\]|\\\\.|\\[(?:[^\\[\\]\\\\]|\\\\.|\\[(?:[^\\[\\]\\\\]|\\\\.|\\[(?:[^\\[\\]\\\\])*\\])*\\])*\\])"`) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters