Skip to content

Commit c792c7d

Browse files
authored
feat(engine-js): lazy compile extremely long patterns in precompiled grammars (#916)
1 parent 79d83ff commit c792c7d

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

docs/references/engine-js-compat.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
Compatibility reference of all built-in grammars with the [JavaScript RegExp engine](/guide/regex-engines#javascript-regexp-engine).
44

5-
> Generated on Tuesday, January 21, 2025
5+
> Generated on Monday, February 3, 2025
66
>
7-
> Version `2.0.3`
7+
> Version `2.2.0`
88
>
9-
> Runtime: Node.js v22.11.0
9+
> Runtime: Node.js v22.13.1
1010
1111
## Report Summary
1212

1313
| | Count |
1414
| :-------------- | --------------------------: |
1515
| Total Languages | 219 |
16-
| Supported | [214](#supported-languages) |
16+
| Supported | [215](#supported-languages) |
1717
| Mismatched | [0](#mismatched-languages) |
18-
| Unsupported | [5](#unsupported-languages) |
18+
| Unsupported | [4](#unsupported-languages) |
1919

2020
## Supported Languages
2121

@@ -56,6 +56,7 @@ In some edge cases, it's not guaranteed that the highlighting will be 100% the s
5656
| cmake | ✅ OK | 23 | - | |
5757
| cobol | ✅ OK | 868 | - | |
5858
| codeowners | ✅ OK | 4 | - | |
59+
| codeql | ✅ OK | 151 | - | |
5960
| coffee | ✅ OK | 471 | - | |
6061
| common-lisp | ✅ OK | 60 | - | |
6162
| coq | ✅ OK | 26 | - | |
@@ -259,7 +260,6 @@ Languages that throw with the JavaScript RegExp engine, either because they cont
259260

260261
| Language | Highlight Match | Patterns Parsable | Patterns Failed | Diff |
261262
| ---------- | :-------------- | ----------------: | --------------: | ---: |
262-
| codeql | ✅ OK | 150 | 1 | |
263263
| csharp | ❌ Error | 312 | 1 | 137 |
264264
| purescript | ❌ Error | 72 | 1 | |
265265
| razor | ❌ Error | 961 | 1 | |

packages/engine-javascript/src/engine-compile.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RegexEngine } from '@shikijs/types'
2-
import type { OnigurumaToEsOptions } from 'oniguruma-to-es'
2+
import type { ToRegExpOptions } from 'oniguruma-to-es'
33
import type { JavaScriptRegexScannerOptions } from './scanner'
44
import { toRegExp } from 'oniguruma-to-es'
55
import { JavaScriptScanner } from './scanner'
@@ -25,12 +25,15 @@ export interface JavaScriptRegexEngineOptions extends JavaScriptRegexScannerOpti
2525
/**
2626
* The default regex constructor for the JavaScript RegExp engine.
2727
*/
28-
export function defaultJavaScriptRegexConstructor(pattern: string, options?: OnigurumaToEsOptions): RegExp {
28+
export function defaultJavaScriptRegexConstructor(pattern: string, options?: ToRegExpOptions): RegExp {
2929
return toRegExp(
3030
pattern,
3131
{
3232
global: true,
3333
hasIndices: true,
34+
// This has no benefit for the standard JS engine, but it avoids a perf penalty for
35+
// precompiled grammars when constructing extremely long patterns that aren't always used
36+
lazyCompileLength: 3000,
3437
rules: {
3538
// Needed since TextMate grammars merge backrefs across patterns
3639
allowOrphanBackrefs: true,

packages/langs-precompiled/scripts/__snapshots__/precompile.test.ts.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ exports[`precompile 1`] = `
9292
},
9393
},
9494
end: /*@__PURE__*/ new EmulatedRegExp("^(?=\\\\P{space})|(?!^)", "dgv", {
95-
strategy: "search_start_clip",
95+
strategy: "clip_search",
9696
}),
9797
patterns: [
9898
{
@@ -113,7 +113,7 @@ exports[`precompile 1`] = `
113113
"1": { name: "punctuation.whitespace.comment.leading.yaml" },
114114
},
115115
end: /*@__PURE__*/ new EmulatedRegExp("(?!^)", "dgv", {
116-
strategy: "search_start_clip",
116+
strategy: "clip_search",
117117
}),
118118
patterns: [
119119
{
@@ -378,7 +378,7 @@ exports[`precompile 1`] = `
378378
property: {
379379
begin: /(?=!|&)/dgv,
380380
end: /*@__PURE__*/ new EmulatedRegExp("(?!^)", "dgv", {
381-
strategy: "search_start_clip",
381+
strategy: "clip_search",
382382
}),
383383
name: "meta.property.yaml",
384384
patterns: [

packages/langs-precompiled/scripts/langs.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export function toJsLiteral(value: any, seen = new Set()): string {
126126
}
127127

128128
if (value instanceof EmulatedRegExp) {
129-
return `/*@__PURE__*/ new EmulatedRegExp(${JSON.stringify(value.rawArgs.pattern)},${JSON.stringify(value.rawArgs.flags)},${JSON.stringify(value.rawArgs.options)})`
129+
return `/*@__PURE__*/ new EmulatedRegExp(${JSON.stringify(value.source)},"${value.flags}",${JSON.stringify(value.rawOptions)})`
130130
}
131131

132132
// RegExp

pnpm-lock.yaml

+15-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ catalog:
5757
monaco-editor-core: ^0.52.2
5858
ofetch: ^1.4.1
5959
ohash: ^1.1.4
60-
oniguruma-to-es: ^2.3.0
60+
oniguruma-to-es: ^3.1.0
6161
picocolors: ^1.1.1
6262
pinia: ^2.3.1
6363
pnpm: ^9.15.4

0 commit comments

Comments
 (0)