@shikijs/engine-javascript 1.25.0 → 1.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/engine-compile.d.mts +4 -0
- package/dist/engine-compile.d.ts +4 -0
- package/dist/engine-compile.mjs +46 -0
- package/dist/engine-raw.d.mts +12 -0
- package/dist/engine-raw.d.ts +12 -0
- package/dist/engine-raw.mjs +22 -0
- package/dist/index.d.mts +5 -60
- package/dist/index.d.ts +5 -60
- package/dist/index.mjs +4 -127
- package/dist/shared/engine-javascript.DCvMc5Xx.d.mts +63 -0
- package/dist/shared/engine-javascript.DCvMc5Xx.d.ts +63 -0
- package/dist/shared/engine-javascript.hzpS1_41.mjs +93 -0
- package/package.json +7 -3
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { toRegExp } from 'oniguruma-to-es';
|
|
2
|
+
import { J as JavaScriptScanner } from './shared/engine-javascript.hzpS1_41.mjs';
|
|
3
|
+
|
|
4
|
+
function defaultJavaScriptRegexConstructor(pattern, options) {
|
|
5
|
+
return toRegExp(
|
|
6
|
+
pattern,
|
|
7
|
+
{
|
|
8
|
+
global: true,
|
|
9
|
+
hasIndices: true,
|
|
10
|
+
rules: {
|
|
11
|
+
// Needed since TextMate grammars merge backrefs across patterns
|
|
12
|
+
allowOrphanBackrefs: true,
|
|
13
|
+
// Improves search performance for generated regexes
|
|
14
|
+
asciiWordBoundaries: true,
|
|
15
|
+
// Follow `vscode-oniguruma` which enables this Oniguruma option by default
|
|
16
|
+
captureGroup: true,
|
|
17
|
+
// Removing `\G` anchors in cases when they're not supported for emulation allows
|
|
18
|
+
// supporting more grammars, but also allows some mismatches
|
|
19
|
+
ignoreUnsupportedGAnchors: true
|
|
20
|
+
},
|
|
21
|
+
...options
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
function createJavaScriptRegexEngine(options = {}) {
|
|
26
|
+
const _options = Object.assign(
|
|
27
|
+
{
|
|
28
|
+
target: "auto",
|
|
29
|
+
cache: /* @__PURE__ */ new Map()
|
|
30
|
+
},
|
|
31
|
+
options
|
|
32
|
+
);
|
|
33
|
+
_options.regexConstructor ||= (pattern) => defaultJavaScriptRegexConstructor(pattern, { target: _options.target });
|
|
34
|
+
return {
|
|
35
|
+
createScanner(patterns) {
|
|
36
|
+
return new JavaScriptScanner(patterns, _options);
|
|
37
|
+
},
|
|
38
|
+
createString(s) {
|
|
39
|
+
return {
|
|
40
|
+
content: s
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { RegexEngine } from '@shikijs/types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Raw JavaScript regex engine that only supports precompiled grammars.
|
|
5
|
+
*
|
|
6
|
+
* This further simplifies the engine by excluding the regex compilation step.
|
|
7
|
+
*
|
|
8
|
+
* Zero dependencies.
|
|
9
|
+
*/
|
|
10
|
+
declare function createJavaScriptRawEngine(): RegexEngine;
|
|
11
|
+
|
|
12
|
+
export { createJavaScriptRawEngine };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { RegexEngine } from '@shikijs/types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Raw JavaScript regex engine that only supports precompiled grammars.
|
|
5
|
+
*
|
|
6
|
+
* This further simplifies the engine by excluding the regex compilation step.
|
|
7
|
+
*
|
|
8
|
+
* Zero dependencies.
|
|
9
|
+
*/
|
|
10
|
+
declare function createJavaScriptRawEngine(): RegexEngine;
|
|
11
|
+
|
|
12
|
+
export { createJavaScriptRawEngine };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { J as JavaScriptScanner } from './shared/engine-javascript.hzpS1_41.mjs';
|
|
2
|
+
|
|
3
|
+
function createJavaScriptRawEngine() {
|
|
4
|
+
const options = {
|
|
5
|
+
cache: /* @__PURE__ */ new Map(),
|
|
6
|
+
regexConstructor: () => {
|
|
7
|
+
throw new Error("JavaScriptRawEngine: only support precompiled grammar");
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
return {
|
|
11
|
+
createScanner(patterns) {
|
|
12
|
+
return new JavaScriptScanner(patterns, options);
|
|
13
|
+
},
|
|
14
|
+
createString(s) {
|
|
15
|
+
return {
|
|
16
|
+
content: s
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { createJavaScriptRawEngine };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,60 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Whether to allow invalid regex patterns.
|
|
8
|
-
*
|
|
9
|
-
* @default false
|
|
10
|
-
*/
|
|
11
|
-
forgiving?: boolean;
|
|
12
|
-
/**
|
|
13
|
-
* The target ECMAScript version.
|
|
14
|
-
*
|
|
15
|
-
* Oniguruma-To-ES uses RegExp features from later versions of ECMAScript to provide improved
|
|
16
|
-
* accuracy and add support for more grammars. If using target `ES2024` or later, the RegExp `v`
|
|
17
|
-
* flag is used which requires Node.js 20+ or Chrome 112+.
|
|
18
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets
|
|
19
|
-
*
|
|
20
|
-
* For maximum compatibility, you can set it to `ES2018` which uses the RegExp `u` flag but
|
|
21
|
-
* supports a few less grammars.
|
|
22
|
-
*
|
|
23
|
-
* Set to `auto` to automatically detect the latest version supported by the environment.
|
|
24
|
-
*
|
|
25
|
-
* @default 'auto'
|
|
26
|
-
*/
|
|
27
|
-
target?: 'auto' | 'ES2025' | 'ES2024' | 'ES2018';
|
|
28
|
-
/**
|
|
29
|
-
* Cache for regex patterns.
|
|
30
|
-
*/
|
|
31
|
-
cache?: Map<string, RegExp | Error> | null;
|
|
32
|
-
/**
|
|
33
|
-
* Custom pattern to RegExp constructor.
|
|
34
|
-
*
|
|
35
|
-
* By default `oniguruma-to-es` is used.
|
|
36
|
-
*/
|
|
37
|
-
regexConstructor?: (pattern: string) => RegExp;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* The default RegExp constructor for JavaScript regex engine.
|
|
41
|
-
*/
|
|
42
|
-
declare function defaultJavaScriptRegexConstructor(pattern: string, options?: OnigurumaToEsOptions): RegExp;
|
|
43
|
-
declare class JavaScriptScanner implements PatternScanner {
|
|
44
|
-
patterns: string[];
|
|
45
|
-
options: JavaScriptRegexEngineOptions;
|
|
46
|
-
regexps: (RegExp | null)[];
|
|
47
|
-
constructor(patterns: string[], options?: JavaScriptRegexEngineOptions);
|
|
48
|
-
findNextMatchSync(string: string | RegexEngineString, startPosition: number, _options: number): IOnigMatch | null;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Use the modern JavaScript RegExp engine to implement the OnigScanner.
|
|
52
|
-
*
|
|
53
|
-
* As Oniguruma supports some features that can't be emulated using native JavaScript regexes, some
|
|
54
|
-
* patterns are not supported. Errors will be thrown when parsing TextMate grammars with
|
|
55
|
-
* unsupported patterns, and when the grammar includes patterns that use invalid Oniguruma syntax.
|
|
56
|
-
* Set `forgiving` to `true` to ignore these errors and skip any unsupported or invalid patterns.
|
|
57
|
-
*/
|
|
58
|
-
declare function createJavaScriptRegexEngine(options?: JavaScriptRegexEngineOptions): RegexEngine;
|
|
59
|
-
|
|
60
|
-
export { type JavaScriptRegexEngineOptions, JavaScriptScanner, createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor };
|
|
1
|
+
export { J as JavaScriptRegexEngineOptions, a as JavaScriptRegexScannerOptions, b as JavaScriptScanner, c as createJavaScriptRegexEngine, d as defaultJavaScriptRegexConstructor } from './shared/engine-javascript.DCvMc5Xx.mjs';
|
|
2
|
+
export { createJavaScriptRawEngine } from './engine-raw.mjs';
|
|
3
|
+
import '@shikijs/types';
|
|
4
|
+
import 'oniguruma-to-es';
|
|
5
|
+
import '@shikijs/vscode-textmate';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,60 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Whether to allow invalid regex patterns.
|
|
8
|
-
*
|
|
9
|
-
* @default false
|
|
10
|
-
*/
|
|
11
|
-
forgiving?: boolean;
|
|
12
|
-
/**
|
|
13
|
-
* The target ECMAScript version.
|
|
14
|
-
*
|
|
15
|
-
* Oniguruma-To-ES uses RegExp features from later versions of ECMAScript to provide improved
|
|
16
|
-
* accuracy and add support for more grammars. If using target `ES2024` or later, the RegExp `v`
|
|
17
|
-
* flag is used which requires Node.js 20+ or Chrome 112+.
|
|
18
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets
|
|
19
|
-
*
|
|
20
|
-
* For maximum compatibility, you can set it to `ES2018` which uses the RegExp `u` flag but
|
|
21
|
-
* supports a few less grammars.
|
|
22
|
-
*
|
|
23
|
-
* Set to `auto` to automatically detect the latest version supported by the environment.
|
|
24
|
-
*
|
|
25
|
-
* @default 'auto'
|
|
26
|
-
*/
|
|
27
|
-
target?: 'auto' | 'ES2025' | 'ES2024' | 'ES2018';
|
|
28
|
-
/**
|
|
29
|
-
* Cache for regex patterns.
|
|
30
|
-
*/
|
|
31
|
-
cache?: Map<string, RegExp | Error> | null;
|
|
32
|
-
/**
|
|
33
|
-
* Custom pattern to RegExp constructor.
|
|
34
|
-
*
|
|
35
|
-
* By default `oniguruma-to-es` is used.
|
|
36
|
-
*/
|
|
37
|
-
regexConstructor?: (pattern: string) => RegExp;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* The default RegExp constructor for JavaScript regex engine.
|
|
41
|
-
*/
|
|
42
|
-
declare function defaultJavaScriptRegexConstructor(pattern: string, options?: OnigurumaToEsOptions): RegExp;
|
|
43
|
-
declare class JavaScriptScanner implements PatternScanner {
|
|
44
|
-
patterns: string[];
|
|
45
|
-
options: JavaScriptRegexEngineOptions;
|
|
46
|
-
regexps: (RegExp | null)[];
|
|
47
|
-
constructor(patterns: string[], options?: JavaScriptRegexEngineOptions);
|
|
48
|
-
findNextMatchSync(string: string | RegexEngineString, startPosition: number, _options: number): IOnigMatch | null;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Use the modern JavaScript RegExp engine to implement the OnigScanner.
|
|
52
|
-
*
|
|
53
|
-
* As Oniguruma supports some features that can't be emulated using native JavaScript regexes, some
|
|
54
|
-
* patterns are not supported. Errors will be thrown when parsing TextMate grammars with
|
|
55
|
-
* unsupported patterns, and when the grammar includes patterns that use invalid Oniguruma syntax.
|
|
56
|
-
* Set `forgiving` to `true` to ignore these errors and skip any unsupported or invalid patterns.
|
|
57
|
-
*/
|
|
58
|
-
declare function createJavaScriptRegexEngine(options?: JavaScriptRegexEngineOptions): RegexEngine;
|
|
59
|
-
|
|
60
|
-
export { type JavaScriptRegexEngineOptions, JavaScriptScanner, createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor };
|
|
1
|
+
export { J as JavaScriptRegexEngineOptions, a as JavaScriptRegexScannerOptions, b as JavaScriptScanner, c as createJavaScriptRegexEngine, d as defaultJavaScriptRegexConstructor } from './shared/engine-javascript.DCvMc5Xx.js';
|
|
2
|
+
export { createJavaScriptRawEngine } from './engine-raw.js';
|
|
3
|
+
import '@shikijs/types';
|
|
4
|
+
import 'oniguruma-to-es';
|
|
5
|
+
import '@shikijs/vscode-textmate';
|
package/dist/index.mjs
CHANGED
|
@@ -1,127 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return toRegExp(
|
|
6
|
-
pattern,
|
|
7
|
-
{
|
|
8
|
-
global: true,
|
|
9
|
-
hasIndices: true,
|
|
10
|
-
rules: {
|
|
11
|
-
// Needed since TextMate grammars merge backrefs across patterns
|
|
12
|
-
allowOrphanBackrefs: true,
|
|
13
|
-
// Improves search performance for generated regexes
|
|
14
|
-
asciiWordBoundaries: true,
|
|
15
|
-
// Follow `vscode-oniguruma` which enables this Oniguruma option by default
|
|
16
|
-
captureGroup: true,
|
|
17
|
-
// Removing `\G` anchors in cases when they're not supported for emulation allows
|
|
18
|
-
// supporting more grammars, but also allows some mismatches
|
|
19
|
-
ignoreUnsupportedGAnchors: true
|
|
20
|
-
},
|
|
21
|
-
...options
|
|
22
|
-
}
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
class JavaScriptScanner {
|
|
26
|
-
constructor(patterns, options = {}) {
|
|
27
|
-
this.patterns = patterns;
|
|
28
|
-
this.options = options;
|
|
29
|
-
const {
|
|
30
|
-
forgiving = false,
|
|
31
|
-
cache,
|
|
32
|
-
target = "auto",
|
|
33
|
-
regexConstructor = (pattern) => defaultJavaScriptRegexConstructor(pattern, { target })
|
|
34
|
-
} = options;
|
|
35
|
-
this.regexps = patterns.map((p) => {
|
|
36
|
-
const cached = cache?.get(p);
|
|
37
|
-
if (cached) {
|
|
38
|
-
if (cached instanceof RegExp) {
|
|
39
|
-
return cached;
|
|
40
|
-
}
|
|
41
|
-
if (forgiving)
|
|
42
|
-
return null;
|
|
43
|
-
throw cached;
|
|
44
|
-
}
|
|
45
|
-
try {
|
|
46
|
-
const regex = regexConstructor(p);
|
|
47
|
-
cache?.set(p, regex);
|
|
48
|
-
return regex;
|
|
49
|
-
} catch (e) {
|
|
50
|
-
cache?.set(p, e);
|
|
51
|
-
if (forgiving)
|
|
52
|
-
return null;
|
|
53
|
-
throw e;
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
regexps;
|
|
58
|
-
findNextMatchSync(string, startPosition, _options) {
|
|
59
|
-
const str = typeof string === "string" ? string : string.content;
|
|
60
|
-
const pending = [];
|
|
61
|
-
function toResult(index, match, offset = 0) {
|
|
62
|
-
return {
|
|
63
|
-
index,
|
|
64
|
-
captureIndices: match.indices.map((indice) => {
|
|
65
|
-
if (indice == null) {
|
|
66
|
-
return {
|
|
67
|
-
start: MAX,
|
|
68
|
-
end: MAX,
|
|
69
|
-
length: 0
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
return {
|
|
73
|
-
start: indice[0] + offset,
|
|
74
|
-
end: indice[1] + offset,
|
|
75
|
-
length: indice[1] - indice[0]
|
|
76
|
-
};
|
|
77
|
-
})
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
for (let i = 0; i < this.regexps.length; i++) {
|
|
81
|
-
const regexp = this.regexps[i];
|
|
82
|
-
if (!regexp)
|
|
83
|
-
continue;
|
|
84
|
-
try {
|
|
85
|
-
regexp.lastIndex = startPosition;
|
|
86
|
-
const match = regexp.exec(str);
|
|
87
|
-
if (!match)
|
|
88
|
-
continue;
|
|
89
|
-
if (match.index === startPosition) {
|
|
90
|
-
return toResult(i, match, 0);
|
|
91
|
-
}
|
|
92
|
-
pending.push([i, match, 0]);
|
|
93
|
-
} catch (e) {
|
|
94
|
-
if (this.options.forgiving)
|
|
95
|
-
continue;
|
|
96
|
-
throw e;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
if (pending.length) {
|
|
100
|
-
const minIndex = Math.min(...pending.map((m) => m[1].index));
|
|
101
|
-
for (const [i, match, offset] of pending) {
|
|
102
|
-
if (match.index === minIndex) {
|
|
103
|
-
return toResult(i, match, offset);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
function createJavaScriptRegexEngine(options = {}) {
|
|
111
|
-
const _options = {
|
|
112
|
-
cache: /* @__PURE__ */ new Map(),
|
|
113
|
-
...options
|
|
114
|
-
};
|
|
115
|
-
return {
|
|
116
|
-
createScanner(patterns) {
|
|
117
|
-
return new JavaScriptScanner(patterns, _options);
|
|
118
|
-
},
|
|
119
|
-
createString(s) {
|
|
120
|
-
return {
|
|
121
|
-
content: s
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export { JavaScriptScanner, createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor };
|
|
1
|
+
export { createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor } from './engine-compile.mjs';
|
|
2
|
+
export { createJavaScriptRawEngine } from './engine-raw.mjs';
|
|
3
|
+
export { J as JavaScriptScanner } from './shared/engine-javascript.hzpS1_41.mjs';
|
|
4
|
+
import 'oniguruma-to-es';
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { PatternScanner, RegexEngineString, RegexEngine } from '@shikijs/types';
|
|
2
|
+
import { OnigurumaToEsOptions } from 'oniguruma-to-es';
|
|
3
|
+
import { IOnigMatch } from '@shikijs/vscode-textmate';
|
|
4
|
+
|
|
5
|
+
interface JavaScriptRegexScannerOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Whether to allow invalid regex patterns.
|
|
8
|
+
*
|
|
9
|
+
* @default false
|
|
10
|
+
*/
|
|
11
|
+
forgiving?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Cache for regex patterns.
|
|
14
|
+
*/
|
|
15
|
+
cache?: Map<string, RegExp | Error> | null;
|
|
16
|
+
/**
|
|
17
|
+
* Custom pattern to RegExp constructor.
|
|
18
|
+
*
|
|
19
|
+
* By default `oniguruma-to-es` is used.
|
|
20
|
+
*/
|
|
21
|
+
regexConstructor?: (pattern: string) => RegExp;
|
|
22
|
+
}
|
|
23
|
+
declare class JavaScriptScanner implements PatternScanner {
|
|
24
|
+
patterns: (string | RegExp)[];
|
|
25
|
+
options: JavaScriptRegexScannerOptions;
|
|
26
|
+
regexps: (RegExp | null)[];
|
|
27
|
+
constructor(patterns: (string | RegExp)[], options?: JavaScriptRegexScannerOptions);
|
|
28
|
+
findNextMatchSync(string: string | RegexEngineString, startPosition: number, _options: number): IOnigMatch | null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface JavaScriptRegexEngineOptions extends JavaScriptRegexScannerOptions {
|
|
32
|
+
/**
|
|
33
|
+
* The target ECMAScript version.
|
|
34
|
+
*
|
|
35
|
+
* Oniguruma-To-ES uses RegExp features from later versions of ECMAScript to provide improved
|
|
36
|
+
* accuracy and add support for more grammars. If using target `ES2024` or later, the RegExp `v`
|
|
37
|
+
* flag is used which requires Node.js 20+ or Chrome 112+.
|
|
38
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets
|
|
39
|
+
*
|
|
40
|
+
* For maximum compatibility, you can set it to `ES2018` which uses the RegExp `u` flag but
|
|
41
|
+
* supports a few less grammars.
|
|
42
|
+
*
|
|
43
|
+
* Set to `auto` to automatically detect the latest version supported by the environment.
|
|
44
|
+
*
|
|
45
|
+
* @default 'auto'
|
|
46
|
+
*/
|
|
47
|
+
target?: 'auto' | 'ES2025' | 'ES2024' | 'ES2018';
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The default RegExp constructor for JavaScript regex engine.
|
|
51
|
+
*/
|
|
52
|
+
declare function defaultJavaScriptRegexConstructor(pattern: string, options?: OnigurumaToEsOptions): RegExp;
|
|
53
|
+
/**
|
|
54
|
+
* Use the modern JavaScript RegExp engine to implement the OnigScanner.
|
|
55
|
+
*
|
|
56
|
+
* As Oniguruma supports some features that can't be emulated using native JavaScript regexes, some
|
|
57
|
+
* patterns are not supported. Errors will be thrown when parsing TextMate grammars with
|
|
58
|
+
* unsupported patterns, and when the grammar includes patterns that use invalid Oniguruma syntax.
|
|
59
|
+
* Set `forgiving` to `true` to ignore these errors and skip any unsupported or invalid patterns.
|
|
60
|
+
*/
|
|
61
|
+
declare function createJavaScriptRegexEngine(options?: JavaScriptRegexEngineOptions): RegexEngine;
|
|
62
|
+
|
|
63
|
+
export { type JavaScriptRegexEngineOptions as J, type JavaScriptRegexScannerOptions as a, JavaScriptScanner as b, createJavaScriptRegexEngine as c, defaultJavaScriptRegexConstructor as d };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { PatternScanner, RegexEngineString, RegexEngine } from '@shikijs/types';
|
|
2
|
+
import { OnigurumaToEsOptions } from 'oniguruma-to-es';
|
|
3
|
+
import { IOnigMatch } from '@shikijs/vscode-textmate';
|
|
4
|
+
|
|
5
|
+
interface JavaScriptRegexScannerOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Whether to allow invalid regex patterns.
|
|
8
|
+
*
|
|
9
|
+
* @default false
|
|
10
|
+
*/
|
|
11
|
+
forgiving?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Cache for regex patterns.
|
|
14
|
+
*/
|
|
15
|
+
cache?: Map<string, RegExp | Error> | null;
|
|
16
|
+
/**
|
|
17
|
+
* Custom pattern to RegExp constructor.
|
|
18
|
+
*
|
|
19
|
+
* By default `oniguruma-to-es` is used.
|
|
20
|
+
*/
|
|
21
|
+
regexConstructor?: (pattern: string) => RegExp;
|
|
22
|
+
}
|
|
23
|
+
declare class JavaScriptScanner implements PatternScanner {
|
|
24
|
+
patterns: (string | RegExp)[];
|
|
25
|
+
options: JavaScriptRegexScannerOptions;
|
|
26
|
+
regexps: (RegExp | null)[];
|
|
27
|
+
constructor(patterns: (string | RegExp)[], options?: JavaScriptRegexScannerOptions);
|
|
28
|
+
findNextMatchSync(string: string | RegexEngineString, startPosition: number, _options: number): IOnigMatch | null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface JavaScriptRegexEngineOptions extends JavaScriptRegexScannerOptions {
|
|
32
|
+
/**
|
|
33
|
+
* The target ECMAScript version.
|
|
34
|
+
*
|
|
35
|
+
* Oniguruma-To-ES uses RegExp features from later versions of ECMAScript to provide improved
|
|
36
|
+
* accuracy and add support for more grammars. If using target `ES2024` or later, the RegExp `v`
|
|
37
|
+
* flag is used which requires Node.js 20+ or Chrome 112+.
|
|
38
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets
|
|
39
|
+
*
|
|
40
|
+
* For maximum compatibility, you can set it to `ES2018` which uses the RegExp `u` flag but
|
|
41
|
+
* supports a few less grammars.
|
|
42
|
+
*
|
|
43
|
+
* Set to `auto` to automatically detect the latest version supported by the environment.
|
|
44
|
+
*
|
|
45
|
+
* @default 'auto'
|
|
46
|
+
*/
|
|
47
|
+
target?: 'auto' | 'ES2025' | 'ES2024' | 'ES2018';
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The default RegExp constructor for JavaScript regex engine.
|
|
51
|
+
*/
|
|
52
|
+
declare function defaultJavaScriptRegexConstructor(pattern: string, options?: OnigurumaToEsOptions): RegExp;
|
|
53
|
+
/**
|
|
54
|
+
* Use the modern JavaScript RegExp engine to implement the OnigScanner.
|
|
55
|
+
*
|
|
56
|
+
* As Oniguruma supports some features that can't be emulated using native JavaScript regexes, some
|
|
57
|
+
* patterns are not supported. Errors will be thrown when parsing TextMate grammars with
|
|
58
|
+
* unsupported patterns, and when the grammar includes patterns that use invalid Oniguruma syntax.
|
|
59
|
+
* Set `forgiving` to `true` to ignore these errors and skip any unsupported or invalid patterns.
|
|
60
|
+
*/
|
|
61
|
+
declare function createJavaScriptRegexEngine(options?: JavaScriptRegexEngineOptions): RegexEngine;
|
|
62
|
+
|
|
63
|
+
export { type JavaScriptRegexEngineOptions as J, type JavaScriptRegexScannerOptions as a, JavaScriptScanner as b, createJavaScriptRegexEngine as c, defaultJavaScriptRegexConstructor as d };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const MAX = 4294967295;
|
|
2
|
+
class JavaScriptScanner {
|
|
3
|
+
constructor(patterns, options = {}) {
|
|
4
|
+
this.patterns = patterns;
|
|
5
|
+
this.options = options;
|
|
6
|
+
const {
|
|
7
|
+
forgiving = false,
|
|
8
|
+
cache,
|
|
9
|
+
regexConstructor
|
|
10
|
+
} = options;
|
|
11
|
+
if (!regexConstructor) {
|
|
12
|
+
throw new Error("Option `regexConstructor` is not provided");
|
|
13
|
+
}
|
|
14
|
+
this.regexps = patterns.map((p) => {
|
|
15
|
+
if (typeof p !== "string") {
|
|
16
|
+
return p;
|
|
17
|
+
}
|
|
18
|
+
const cached = cache?.get(p);
|
|
19
|
+
if (cached) {
|
|
20
|
+
if (cached instanceof RegExp) {
|
|
21
|
+
return cached;
|
|
22
|
+
}
|
|
23
|
+
if (forgiving)
|
|
24
|
+
return null;
|
|
25
|
+
throw cached;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
const regex = regexConstructor(p);
|
|
29
|
+
cache?.set(p, regex);
|
|
30
|
+
return regex;
|
|
31
|
+
} catch (e) {
|
|
32
|
+
cache?.set(p, e);
|
|
33
|
+
if (forgiving)
|
|
34
|
+
return null;
|
|
35
|
+
throw e;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
regexps;
|
|
40
|
+
findNextMatchSync(string, startPosition, _options) {
|
|
41
|
+
const str = typeof string === "string" ? string : string.content;
|
|
42
|
+
const pending = [];
|
|
43
|
+
function toResult(index, match, offset = 0) {
|
|
44
|
+
return {
|
|
45
|
+
index,
|
|
46
|
+
captureIndices: match.indices.map((indice) => {
|
|
47
|
+
if (indice == null) {
|
|
48
|
+
return {
|
|
49
|
+
start: MAX,
|
|
50
|
+
end: MAX,
|
|
51
|
+
length: 0
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
start: indice[0] + offset,
|
|
56
|
+
end: indice[1] + offset,
|
|
57
|
+
length: indice[1] - indice[0]
|
|
58
|
+
};
|
|
59
|
+
})
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
for (let i = 0; i < this.regexps.length; i++) {
|
|
63
|
+
const regexp = this.regexps[i];
|
|
64
|
+
if (!regexp)
|
|
65
|
+
continue;
|
|
66
|
+
try {
|
|
67
|
+
regexp.lastIndex = startPosition;
|
|
68
|
+
const match = regexp.exec(str);
|
|
69
|
+
if (!match)
|
|
70
|
+
continue;
|
|
71
|
+
if (match.index === startPosition) {
|
|
72
|
+
return toResult(i, match, 0);
|
|
73
|
+
}
|
|
74
|
+
pending.push([i, match, 0]);
|
|
75
|
+
} catch (e) {
|
|
76
|
+
if (this.options.forgiving)
|
|
77
|
+
continue;
|
|
78
|
+
throw e;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (pending.length) {
|
|
82
|
+
const minIndex = Math.min(...pending.map((m) => m[1].index));
|
|
83
|
+
for (const [i, match, offset] of pending) {
|
|
84
|
+
if (match.index === minIndex) {
|
|
85
|
+
return toResult(i, match, offset);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export { JavaScriptScanner as J };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shikijs/engine-javascript",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.26.0",
|
|
5
5
|
"description": "Engine for Shiki using JavaScript's native RegExp",
|
|
6
6
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -21,6 +21,10 @@
|
|
|
21
21
|
".": {
|
|
22
22
|
"types": "./dist/index.d.mts",
|
|
23
23
|
"default": "./dist/index.mjs"
|
|
24
|
+
},
|
|
25
|
+
"./raw": {
|
|
26
|
+
"types": "./dist/engine-raw.d.mts",
|
|
27
|
+
"default": "./dist/engine-raw.mjs"
|
|
24
28
|
}
|
|
25
29
|
},
|
|
26
30
|
"main": "./dist/index.mjs",
|
|
@@ -30,9 +34,9 @@
|
|
|
30
34
|
"dist"
|
|
31
35
|
],
|
|
32
36
|
"dependencies": {
|
|
33
|
-
"@shikijs/vscode-textmate": "^
|
|
37
|
+
"@shikijs/vscode-textmate": "^10.0.1",
|
|
34
38
|
"oniguruma-to-es": "0.10.0",
|
|
35
|
-
"@shikijs/types": "1.
|
|
39
|
+
"@shikijs/types": "1.26.0"
|
|
36
40
|
},
|
|
37
41
|
"scripts": {
|
|
38
42
|
"build": "unbuild",
|