@@ -3,13 +3,31 @@ import type { JavaScriptRegexEngineOptions, PatternScanner, RegexEngine, RegexEn
33
44const MAX = 4294967295
55
6+ /**
7+ * The default RegExp constructor for JavaScript regex engine.
8+ */
9+ export function defaultJavaScriptRegexConstructor ( pattern : string ) : RegExp {
10+ return onigurumaToRegexp (
11+ pattern
12+ . replace ( / \| \\ G ( \| | \) ) / g, '$1' )
13+ . replace ( / ( \( | \| ) \\ G \| / g, '$1' )
14+ // YAML specific handling; TODO: move to tm-grammars
15+ . replaceAll ( '[^\\s[-?:,\\[\\]{}#&*!|>\'"%@`]]' , '[^\\s\\-?:,\\[\\]{}#&*!|>\'"%@`]' ) ,
16+ {
17+ flags : 'dgm' ,
18+ ignoreContiguousAnchors : true ,
19+ } ,
20+ )
21+ }
22+
623export class JavaScriptScanner implements PatternScanner {
724 regexps : ( RegExp | null ) [ ]
825
926 constructor (
1027 public patterns : string [ ] ,
1128 public cache : Map < string , RegExp | Error > ,
1229 public forgiving : boolean ,
30+ public regexConstructor : ( pattern : string ) => RegExp = defaultJavaScriptRegexConstructor ,
1331 ) {
1432 this . regexps = patterns . map ( ( p ) => {
1533 const cached = cache ?. get ( p )
@@ -22,17 +40,7 @@ export class JavaScriptScanner implements PatternScanner {
2240 throw cached
2341 }
2442 try {
25- const regex = onigurumaToRegexp (
26- p
27- . replace ( / \| \\ G ( \| | \) ) / g, '$1' )
28- . replace ( / ( \( | \| ) \\ G \| / g, '$1' )
29- // YAML specific handling; TODO: move to tm-grammars
30- . replaceAll ( '[^\\s[-?:,\\[\\]{}#&*!|>\'"%@`]]' , '[^\\s\\-?:,\\[\\]{}#&*!|>\'"%@`]' ) ,
31- {
32- flags : 'dgm' ,
33- ignoreContiguousAnchors : true ,
34- } ,
35- )
43+ const regex = regexConstructor ( p )
3644 cache ?. set ( p , regex )
3745 return regex
3846 }
@@ -126,7 +134,7 @@ export function createJavaScriptRegexEngine(options: JavaScriptRegexEngineOption
126134
127135 return {
128136 createScanner ( patterns : string [ ] ) {
129- return new JavaScriptScanner ( patterns , cache , forgiving )
137+ return new JavaScriptScanner ( patterns , cache , forgiving , options . regexConstructor )
130138 } ,
131139 createString ( s : string ) {
132140 return {
0 commit comments