@wolfstar/rust-regex 0.0.0-stage → 0.1.2
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/LICENSE +201 -0
- package/README.md +96 -2
- package/dist/esm/index.d.ts +82 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +1233 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +60 -3
|
@@ -0,0 +1,1233 @@
|
|
|
1
|
+
// \0@oxc-project+runtime@0.160.0/helpers/esm/typeof.js
|
|
2
|
+
function _typeof(o) {
|
|
3
|
+
"@babel/helpers - typeof";
|
|
4
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
5
|
+
return typeof o;
|
|
6
|
+
} : function(o) {
|
|
7
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
8
|
+
}, _typeof(o);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// \0@oxc-project+runtime@0.160.0/helpers/esm/toPrimitive.js
|
|
12
|
+
function toPrimitive(t, e) {
|
|
13
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
14
|
+
var r;
|
|
15
|
+
if ("undefined" != typeof Symbol && void 0 !== (r = t[Symbol.toPrimitive])) {
|
|
16
|
+
var i = r.call(t, e || "default");
|
|
17
|
+
if ("object" != _typeof(i)) return i;
|
|
18
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
19
|
+
}
|
|
20
|
+
return ("string" === e ? String : Number)(t);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// \0@oxc-project+runtime@0.160.0/helpers/esm/toPropertyKey.js
|
|
24
|
+
function toPropertyKey(t) {
|
|
25
|
+
var i = toPrimitive(t, "string");
|
|
26
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// \0@oxc-project+runtime@0.160.0/helpers/esm/defineProperty.js
|
|
30
|
+
function _defineProperty(e, r, t) {
|
|
31
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
32
|
+
value: t,
|
|
33
|
+
enumerable: !0,
|
|
34
|
+
configurable: !0,
|
|
35
|
+
writable: !0
|
|
36
|
+
}) : e[r] = t, e;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/lib/errors.ts
|
|
40
|
+
/**
|
|
41
|
+
* Thrown when a Rust regular expression cannot be transpiled.
|
|
42
|
+
*/
|
|
43
|
+
var RustRegexError = class extends SyntaxError {
|
|
44
|
+
constructor(code, message, pattern, position) {
|
|
45
|
+
super(position === void 0 ? `${message} (in /${pattern}/)` : `${message} (at index ${position} of /${pattern}/)`);
|
|
46
|
+
_defineProperty(this, "code", void 0);
|
|
47
|
+
_defineProperty(this, "pattern", void 0);
|
|
48
|
+
_defineProperty(this, "position", void 0);
|
|
49
|
+
this.name = "RustRegexError";
|
|
50
|
+
this.code = code;
|
|
51
|
+
this.pattern = pattern;
|
|
52
|
+
this.position = position;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// src/lib/emitter.ts
|
|
57
|
+
/**
|
|
58
|
+
* Unicode `\w` as Rust defines it (UTS #18): letters, marks, decimal digits, connector punctuation and joiners.
|
|
59
|
+
* JavaScript's own `\w` is ASCII only, so it cannot be used, even with the `u` or `v` flag.
|
|
60
|
+
*/
|
|
61
|
+
const WORD_CONTENT = String.raw`\p{Alphabetic}\p{M}\p{Nd}\p{Pc}\p{Join_Control}`;
|
|
62
|
+
const WORD = `[${WORD_CONTENT}]`;
|
|
63
|
+
const ASSERTIONS = {
|
|
64
|
+
"text-start": "^",
|
|
65
|
+
"text-end": "$",
|
|
66
|
+
"line-start": String.raw`(?<![^\n])`,
|
|
67
|
+
"line-end": String.raw`(?![^\n])`,
|
|
68
|
+
"word-boundary": `(?:(?<=${WORD})(?!${WORD})|(?<!${WORD})(?=${WORD}))`,
|
|
69
|
+
"not-word-boundary": `(?:(?<=${WORD})(?=${WORD})|(?<!${WORD})(?!${WORD}))`
|
|
70
|
+
};
|
|
71
|
+
/** Characters that are syntax outside of a class. */
|
|
72
|
+
const SYNTAX_CHARACTERS = /* @__PURE__ */ new Set("^$\\.*+?()[]{}|/");
|
|
73
|
+
/**
|
|
74
|
+
* Characters that need a backslash inside a class under the `v` flag: the class syntax characters and the reserved
|
|
75
|
+
* punctuators, which also covers the doubled ones (`&&`, `!!`, `##`, ...) the flag refuses to see unescaped.
|
|
76
|
+
*/
|
|
77
|
+
const CLASS_ESCAPES = /* @__PURE__ */ new Set("()[]{}/-\\|&!#%,:;<=>@`~$*+.?^");
|
|
78
|
+
/**
|
|
79
|
+
* The longest source, in UTF-16 units, that one `~~` may produce. Each `~~` writes both of its sides twice, so a short
|
|
80
|
+
* chain such as `[a~~b~~b~~b...]` doubles its output at every step and would otherwise run out of memory.
|
|
81
|
+
*/
|
|
82
|
+
const MAX_SYMMETRIC_DIFFERENCE_LENGTH = 1e6;
|
|
83
|
+
/** Thrown by the class writers, which know no pattern, and turned into a {@link RustRegexError} by {@link Emitter.emit}. */
|
|
84
|
+
const OUTPUT_TOO_LARGE = Symbol("output too large");
|
|
85
|
+
function isPrintableAscii(cp) {
|
|
86
|
+
return cp >= 32 && cp <= 126;
|
|
87
|
+
}
|
|
88
|
+
function hex(cp) {
|
|
89
|
+
return String.raw`\u{${cp.toString(16)}}`;
|
|
90
|
+
}
|
|
91
|
+
function literal(cp) {
|
|
92
|
+
if (!isPrintableAscii(cp)) return hex(cp);
|
|
93
|
+
const ch = String.fromCharCode(cp);
|
|
94
|
+
return SYNTAX_CHARACTERS.has(ch) ? `\\${ch}` : ch;
|
|
95
|
+
}
|
|
96
|
+
function classChar(cp) {
|
|
97
|
+
if (!isPrintableAscii(cp)) return hex(cp);
|
|
98
|
+
const ch = String.fromCharCode(cp);
|
|
99
|
+
return CLASS_ESCAPES.has(ch) ? `\\${ch}` : ch;
|
|
100
|
+
}
|
|
101
|
+
function property(expr, negated) {
|
|
102
|
+
return `\\${negated ? "P" : "p"}{${expr}}`;
|
|
103
|
+
}
|
|
104
|
+
function perl(cls, negated) {
|
|
105
|
+
switch (cls) {
|
|
106
|
+
case "d": return property("Nd", negated);
|
|
107
|
+
case "s": return property("White_Space", negated);
|
|
108
|
+
case "w": return `[${negated ? "^" : ""}${WORD_CONTENT}]`;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/** What a perl class or property adds to a bracketed class, which is not always a bracket of its own. */
|
|
112
|
+
function perlInClass(cls, negated) {
|
|
113
|
+
return cls === "w" && !negated ? WORD_CONTENT : perl(cls, negated);
|
|
114
|
+
}
|
|
115
|
+
function item(entry) {
|
|
116
|
+
switch (entry.kind) {
|
|
117
|
+
case "char": return classChar(entry.cp);
|
|
118
|
+
case "range": return `${classChar(entry.lo)}-${classChar(entry.hi)}`;
|
|
119
|
+
case "perl": return perlInClass(entry.cls, entry.negated);
|
|
120
|
+
case "property": return property(entry.expr, entry.negated);
|
|
121
|
+
case "class": return bracket(entry.set);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Under the `v` flag the operands of `&&` and `--` cannot be ranges or runs of characters, only a single character or
|
|
126
|
+
* a nested class, so each side is always wrapped in a class of its own.
|
|
127
|
+
*/
|
|
128
|
+
function operand(expr) {
|
|
129
|
+
return `[${body(expr)}]`;
|
|
130
|
+
}
|
|
131
|
+
function body(expr) {
|
|
132
|
+
if (expr.kind === "union") return expr.items.map(item).join("");
|
|
133
|
+
if (expr.op === "~~") {
|
|
134
|
+
const left = operand(expr.left);
|
|
135
|
+
const right = operand(expr.right);
|
|
136
|
+
if (left.length + right.length > MAX_SYMMETRIC_DIFFERENCE_LENGTH / 2) throw OUTPUT_TOO_LARGE;
|
|
137
|
+
return `[${left}--${right}][${right}--${left}]`;
|
|
138
|
+
}
|
|
139
|
+
return `${operand(expr.left)}${expr.op}${operand(expr.right)}`;
|
|
140
|
+
}
|
|
141
|
+
function bracket(set) {
|
|
142
|
+
return `[${set.negated ? "^" : ""}${body(set.expr)}]`;
|
|
143
|
+
}
|
|
144
|
+
function classNode(set) {
|
|
145
|
+
const { expr } = set;
|
|
146
|
+
if (!set.negated && expr.kind === "union" && expr.items.length === 1) {
|
|
147
|
+
const only = expr.items[0];
|
|
148
|
+
if (only?.kind === "perl") return perl(only.cls, only.negated);
|
|
149
|
+
if (only?.kind === "property") return property(only.expr, only.negated);
|
|
150
|
+
}
|
|
151
|
+
return bracket(set);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Turns the parsed pattern into the source of a JavaScript `RegExp` that is meant to run with the `v` flag, and with
|
|
155
|
+
* the `i` flag when `initialI` is set.
|
|
156
|
+
*
|
|
157
|
+
* Only case-insensitivity has to be a flag: everything else (`s`, `m`, `U`, `x`) was settled while parsing and is
|
|
158
|
+
* written out explicitly, so the one thing that can change inside the pattern is `i`. That is expressed with the
|
|
159
|
+
* ES2025 modifiers `(?i:...)` and `(?-i:...)`, and only when the pattern actually switches it.
|
|
160
|
+
*/
|
|
161
|
+
var Emitter = class {
|
|
162
|
+
constructor(pattern) {
|
|
163
|
+
this.pattern = pattern;
|
|
164
|
+
_defineProperty(this, "usesModifiers", false);
|
|
165
|
+
}
|
|
166
|
+
emit(alt, initialI) {
|
|
167
|
+
try {
|
|
168
|
+
const source = this.alt(alt, { rustI: initialI }, initialI);
|
|
169
|
+
return {
|
|
170
|
+
source: source === "" ? "(?:)" : source,
|
|
171
|
+
usesModifiers: this.usesModifiers
|
|
172
|
+
};
|
|
173
|
+
} catch (error) {
|
|
174
|
+
if (error !== OUTPUT_TOO_LARGE) throw error;
|
|
175
|
+
throw new RustRegexError("unsupported_feature", "the symmetric difference \"~~\" nests too deeply to be written as a JavaScript RegExp", this.pattern);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** `jsI` is whether the JavaScript engine is matching case-insensitively at this point of the output. */
|
|
179
|
+
alt(branches, state, jsI) {
|
|
180
|
+
return branches.map((branch) => this.seq(branch, state, jsI)).join("|");
|
|
181
|
+
}
|
|
182
|
+
seq(nodes, state, jsI) {
|
|
183
|
+
let out = "";
|
|
184
|
+
let closers = 0;
|
|
185
|
+
let current = jsI;
|
|
186
|
+
for (const node of nodes) {
|
|
187
|
+
if (node.type === "flags") {
|
|
188
|
+
state.rustI = node.i;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (state.rustI !== current) {
|
|
192
|
+
out += state.rustI ? "(?i:" : "(?-i:";
|
|
193
|
+
closers++;
|
|
194
|
+
current = state.rustI;
|
|
195
|
+
this.usesModifiers = true;
|
|
196
|
+
}
|
|
197
|
+
out += this.node(node, current);
|
|
198
|
+
}
|
|
199
|
+
return out + ")".repeat(closers);
|
|
200
|
+
}
|
|
201
|
+
node(node, jsI) {
|
|
202
|
+
switch (node.type) {
|
|
203
|
+
case "literal": return literal(node.cp);
|
|
204
|
+
case "dot": return node.dotAll ? String.raw`[\s\S]` : String.raw`[^\n]`;
|
|
205
|
+
case "assertion": return ASSERTIONS[node.kind];
|
|
206
|
+
case "class": return classNode(node.set);
|
|
207
|
+
case "group": return this.group(node, jsI);
|
|
208
|
+
case "repeat": return this.repeat(node, jsI);
|
|
209
|
+
case "flags": return "";
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
group(node, jsI) {
|
|
213
|
+
const inner = { rustI: node.iStart };
|
|
214
|
+
if (node.capture) return `${node.name === void 0 ? "(" : `(?<${node.name}>`}${this.alt(node.alt, inner, jsI)})`;
|
|
215
|
+
if (node.iStart !== jsI) {
|
|
216
|
+
this.usesModifiers = true;
|
|
217
|
+
return `${node.iStart ? "(?i:" : "(?-i:"}${this.alt(node.alt, inner, node.iStart)})`;
|
|
218
|
+
}
|
|
219
|
+
return `(?:${this.alt(node.alt, inner, jsI)})`;
|
|
220
|
+
}
|
|
221
|
+
repeat(node, jsI) {
|
|
222
|
+
const { min, max } = node;
|
|
223
|
+
const quantifier = min === 0 && max === null ? "*" : min === 1 && max === null ? "+" : min === 0 && max === 1 ? "?" : max === null ? `{${min},}` : min === max ? `{${min}}` : `{${min},${max}}`;
|
|
224
|
+
const child = this.node(node.node, jsI);
|
|
225
|
+
return `${node.node.type === "assertion" || node.node.type === "repeat" ? `(?:${child})` : child}${quantifier}${node.lazy ? "?" : ""}`;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
// src/lib/properties.ts
|
|
230
|
+
function normalize(name) {
|
|
231
|
+
return name.toLowerCase().replace(/[\s_-]/g, "");
|
|
232
|
+
}
|
|
233
|
+
function buildMap(entries) {
|
|
234
|
+
const map = /* @__PURE__ */ new Map();
|
|
235
|
+
for (const [canonical, ...aliases] of entries) {
|
|
236
|
+
if (canonical === void 0) continue;
|
|
237
|
+
map.set(normalize(canonical), canonical);
|
|
238
|
+
for (const alias of aliases) map.set(normalize(alias), canonical);
|
|
239
|
+
}
|
|
240
|
+
return map;
|
|
241
|
+
}
|
|
242
|
+
/** The first entry is what is emitted, the rest are spellings Rust accepts. */
|
|
243
|
+
const GENERAL_CATEGORIES = buildMap([
|
|
244
|
+
["L", "Letter"],
|
|
245
|
+
["Lu", "Uppercase_Letter"],
|
|
246
|
+
["Ll", "Lowercase_Letter"],
|
|
247
|
+
["Lt", "Titlecase_Letter"],
|
|
248
|
+
["LC", "Cased_Letter"],
|
|
249
|
+
["Lm", "Modifier_Letter"],
|
|
250
|
+
["Lo", "Other_Letter"],
|
|
251
|
+
[
|
|
252
|
+
"M",
|
|
253
|
+
"Mark",
|
|
254
|
+
"Combining_Mark"
|
|
255
|
+
],
|
|
256
|
+
["Mn", "Nonspacing_Mark"],
|
|
257
|
+
["Mc", "Spacing_Mark"],
|
|
258
|
+
["Me", "Enclosing_Mark"],
|
|
259
|
+
["N", "Number"],
|
|
260
|
+
[
|
|
261
|
+
"Nd",
|
|
262
|
+
"Decimal_Number",
|
|
263
|
+
"digit"
|
|
264
|
+
],
|
|
265
|
+
["Nl", "Letter_Number"],
|
|
266
|
+
["No", "Other_Number"],
|
|
267
|
+
[
|
|
268
|
+
"P",
|
|
269
|
+
"Punctuation",
|
|
270
|
+
"punct"
|
|
271
|
+
],
|
|
272
|
+
["Pc", "Connector_Punctuation"],
|
|
273
|
+
["Pd", "Dash_Punctuation"],
|
|
274
|
+
["Ps", "Open_Punctuation"],
|
|
275
|
+
["Pe", "Close_Punctuation"],
|
|
276
|
+
["Pi", "Initial_Punctuation"],
|
|
277
|
+
["Pf", "Final_Punctuation"],
|
|
278
|
+
["Po", "Other_Punctuation"],
|
|
279
|
+
["S", "Symbol"],
|
|
280
|
+
["Sm", "Math_Symbol"],
|
|
281
|
+
["Sc", "Currency_Symbol"],
|
|
282
|
+
["Sk", "Modifier_Symbol"],
|
|
283
|
+
["So", "Other_Symbol"],
|
|
284
|
+
["Z", "Separator"],
|
|
285
|
+
["Zs", "Space_Separator"],
|
|
286
|
+
["Zl", "Line_Separator"],
|
|
287
|
+
["Zp", "Paragraph_Separator"],
|
|
288
|
+
["C", "Other"],
|
|
289
|
+
[
|
|
290
|
+
"Cc",
|
|
291
|
+
"Control",
|
|
292
|
+
"cntrl"
|
|
293
|
+
],
|
|
294
|
+
["Cf", "Format"],
|
|
295
|
+
["Cs", "Surrogate"],
|
|
296
|
+
["Co", "Private_Use"],
|
|
297
|
+
["Cn", "Unassigned"]
|
|
298
|
+
]);
|
|
299
|
+
const BINARY_PROPERTIES = buildMap([
|
|
300
|
+
["ASCII"],
|
|
301
|
+
["ASCII_Hex_Digit", "AHex"],
|
|
302
|
+
["Alphabetic", "Alpha"],
|
|
303
|
+
["Any"],
|
|
304
|
+
["Assigned"],
|
|
305
|
+
["Bidi_Control", "Bidi_C"],
|
|
306
|
+
["Bidi_Mirrored", "Bidi_M"],
|
|
307
|
+
["Case_Ignorable", "CI"],
|
|
308
|
+
["Cased"],
|
|
309
|
+
["Changes_When_Casefolded", "CWCF"],
|
|
310
|
+
["Changes_When_Casemapped", "CWCM"],
|
|
311
|
+
["Changes_When_Lowercased", "CWL"],
|
|
312
|
+
["Changes_When_NFKC_Casefolded", "CWKCF"],
|
|
313
|
+
["Changes_When_Titlecased", "CWT"],
|
|
314
|
+
["Changes_When_Uppercased", "CWU"],
|
|
315
|
+
["Dash"],
|
|
316
|
+
["Default_Ignorable_Code_Point", "DI"],
|
|
317
|
+
["Deprecated", "Dep"],
|
|
318
|
+
["Diacritic", "Dia"],
|
|
319
|
+
["Emoji"],
|
|
320
|
+
["Emoji_Component", "EComp"],
|
|
321
|
+
["Emoji_Modifier", "EMod"],
|
|
322
|
+
["Emoji_Modifier_Base", "EBase"],
|
|
323
|
+
["Emoji_Presentation", "EPres"],
|
|
324
|
+
["Extended_Pictographic", "ExtPict"],
|
|
325
|
+
["Extender", "Ext"],
|
|
326
|
+
["Grapheme_Base", "Gr_Base"],
|
|
327
|
+
["Grapheme_Extend", "Gr_Ext"],
|
|
328
|
+
["Hex_Digit", "Hex"],
|
|
329
|
+
["IDS_Binary_Operator", "IDSB"],
|
|
330
|
+
["IDS_Trinary_Operator", "IDST"],
|
|
331
|
+
["ID_Continue", "IDC"],
|
|
332
|
+
["ID_Start", "IDS"],
|
|
333
|
+
["Ideographic", "Ideo"],
|
|
334
|
+
["Join_Control", "Join_C"],
|
|
335
|
+
["Logical_Order_Exception", "LOE"],
|
|
336
|
+
["Lowercase", "Lower"],
|
|
337
|
+
["Math"],
|
|
338
|
+
["Noncharacter_Code_Point", "NChar"],
|
|
339
|
+
["Pattern_Syntax", "Pat_Syn"],
|
|
340
|
+
["Pattern_White_Space", "Pat_WS"],
|
|
341
|
+
["Quotation_Mark", "QMark"],
|
|
342
|
+
["Radical"],
|
|
343
|
+
["Regional_Indicator", "RI"],
|
|
344
|
+
["Sentence_Terminal", "STerm"],
|
|
345
|
+
["Soft_Dotted", "SD"],
|
|
346
|
+
["Terminal_Punctuation", "Term"],
|
|
347
|
+
["Unified_Ideograph", "UIdeo"],
|
|
348
|
+
["Uppercase", "Upper"],
|
|
349
|
+
["Variation_Selector", "VS"],
|
|
350
|
+
["White_Space", "space"],
|
|
351
|
+
["XID_Continue", "XIDC"],
|
|
352
|
+
["XID_Start", "XIDS"]
|
|
353
|
+
]);
|
|
354
|
+
/**
|
|
355
|
+
* JavaScript wants `Old_Italic`, Rust also takes `olditalic`. Without the full script table the best that can be done
|
|
356
|
+
* is to title-case every word, which is right for every script name except the ones listed here. A name that is still
|
|
357
|
+
* wrong is rejected by the JavaScript engine, which `transpile` reports as an `invalid_output` error.
|
|
358
|
+
*/
|
|
359
|
+
const SCRIPT_EXCEPTIONS = /* @__PURE__ */ new Map([["signwriting", "SignWriting"]]);
|
|
360
|
+
function scriptName(value) {
|
|
361
|
+
const exception = SCRIPT_EXCEPTIONS.get(normalize(value));
|
|
362
|
+
if (exception !== void 0) return exception;
|
|
363
|
+
return value.trim().split(/[\s_-]+/).map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join("_");
|
|
364
|
+
}
|
|
365
|
+
const SINGLE_LETTER_CATEGORIES = /* @__PURE__ */ new Set([
|
|
366
|
+
"L",
|
|
367
|
+
"M",
|
|
368
|
+
"N",
|
|
369
|
+
"P",
|
|
370
|
+
"S",
|
|
371
|
+
"Z",
|
|
372
|
+
"C"
|
|
373
|
+
]);
|
|
374
|
+
const KEY_VALUE = /^\s*([A-Za-z_ -]+?)\s*(!=|=|:)\s*(.+?)\s*$/;
|
|
375
|
+
/**
|
|
376
|
+
* @param spec What is between the braces of `\p{...}`, without a leading `^`, or the letter of `\pL`.
|
|
377
|
+
* @param singleLetter Whether `spec` came from the `\pL` form, which only accepts the one-letter categories.
|
|
378
|
+
*/
|
|
379
|
+
function resolveProperty(spec, singleLetter) {
|
|
380
|
+
if (singleLetter) return SINGLE_LETTER_CATEGORIES.has(spec) ? {
|
|
381
|
+
expr: spec,
|
|
382
|
+
negated: false
|
|
383
|
+
} : {
|
|
384
|
+
error: "invalid",
|
|
385
|
+
message: `unknown Unicode class \\p${spec}`
|
|
386
|
+
};
|
|
387
|
+
const keyValue = KEY_VALUE.exec(spec);
|
|
388
|
+
if (keyValue !== null) {
|
|
389
|
+
const [, key = "", operator = "=", value = ""] = keyValue;
|
|
390
|
+
const negated = operator === "!=";
|
|
391
|
+
switch (normalize(key)) {
|
|
392
|
+
case "gc":
|
|
393
|
+
case "generalcategory":
|
|
394
|
+
case "category": {
|
|
395
|
+
const category = GENERAL_CATEGORIES.get(normalize(value));
|
|
396
|
+
return category === void 0 ? {
|
|
397
|
+
error: "invalid",
|
|
398
|
+
message: `unknown general category "${value}"`
|
|
399
|
+
} : {
|
|
400
|
+
expr: category,
|
|
401
|
+
negated
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
case "sc":
|
|
405
|
+
case "script": return {
|
|
406
|
+
expr: `Script=${scriptName(value)}`,
|
|
407
|
+
negated
|
|
408
|
+
};
|
|
409
|
+
case "scx":
|
|
410
|
+
case "scriptextensions": return {
|
|
411
|
+
expr: `Script_Extensions=${scriptName(value)}`,
|
|
412
|
+
negated
|
|
413
|
+
};
|
|
414
|
+
default: return {
|
|
415
|
+
error: "unsupported",
|
|
416
|
+
message: `the Unicode property "${key}" cannot be expressed in JavaScript`
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
const name = normalize(spec);
|
|
421
|
+
const binary = BINARY_PROPERTIES.get(name);
|
|
422
|
+
if (binary !== void 0) return {
|
|
423
|
+
expr: binary,
|
|
424
|
+
negated: false
|
|
425
|
+
};
|
|
426
|
+
const category = GENERAL_CATEGORIES.get(name);
|
|
427
|
+
if (category !== void 0) return {
|
|
428
|
+
expr: category,
|
|
429
|
+
negated: false
|
|
430
|
+
};
|
|
431
|
+
if (!/^[A-Za-z][A-Za-z _-]*$/.test(spec)) return {
|
|
432
|
+
error: "invalid",
|
|
433
|
+
message: `unknown Unicode class "${spec}"`
|
|
434
|
+
};
|
|
435
|
+
return {
|
|
436
|
+
expr: `Script=${scriptName(spec)}`,
|
|
437
|
+
negated: false
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// src/lib/parser.ts
|
|
442
|
+
/** The nesting limit of the Rust `regex` crate. */
|
|
443
|
+
const NEST_LIMIT = 250;
|
|
444
|
+
const MAX_CODE_POINT = 1114111;
|
|
445
|
+
const ASCII_CLASSES = {
|
|
446
|
+
alnum: [
|
|
447
|
+
[48, 57],
|
|
448
|
+
[65, 90],
|
|
449
|
+
[97, 122]
|
|
450
|
+
],
|
|
451
|
+
alpha: [[65, 90], [97, 122]],
|
|
452
|
+
ascii: [[0, 127]],
|
|
453
|
+
blank: [[32, 32], [9, 9]],
|
|
454
|
+
cntrl: [[0, 31], [127, 127]],
|
|
455
|
+
digit: [[48, 57]],
|
|
456
|
+
graph: [[33, 126]],
|
|
457
|
+
lower: [[97, 122]],
|
|
458
|
+
print: [[32, 126]],
|
|
459
|
+
punct: [
|
|
460
|
+
[33, 47],
|
|
461
|
+
[58, 64],
|
|
462
|
+
[91, 96],
|
|
463
|
+
[123, 126]
|
|
464
|
+
],
|
|
465
|
+
space: [[9, 13], [32, 32]],
|
|
466
|
+
upper: [[65, 90]],
|
|
467
|
+
word: [
|
|
468
|
+
[48, 57],
|
|
469
|
+
[65, 90],
|
|
470
|
+
[95, 95],
|
|
471
|
+
[97, 122]
|
|
472
|
+
],
|
|
473
|
+
xdigit: [
|
|
474
|
+
[48, 57],
|
|
475
|
+
[65, 70],
|
|
476
|
+
[97, 102]
|
|
477
|
+
]
|
|
478
|
+
};
|
|
479
|
+
const ASCII_CLASS = /\[:(\^?)([a-z]+):\]/y;
|
|
480
|
+
const WHITESPACE = /^\p{White_Space}$/u;
|
|
481
|
+
/** What the Rust `regex` crate accepts as a capture group name: a letter or `_`, then letters, numbers, `_`, `.`, `[` and `]`. */
|
|
482
|
+
const RUST_GROUP_NAME = /^[_\p{Alphabetic}][_.[\]\p{Alphabetic}\p{N}]*$/u;
|
|
483
|
+
/** What JavaScript accepts as a capture group name: an identifier. */
|
|
484
|
+
const JS_GROUP_NAME = /^[_\p{ID_Start}]\p{ID_Continue}*$/u;
|
|
485
|
+
function isAsciiAlphanumeric(ch) {
|
|
486
|
+
return ch >= "0" && ch <= "9" || ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z";
|
|
487
|
+
}
|
|
488
|
+
function union(items) {
|
|
489
|
+
return {
|
|
490
|
+
kind: "union",
|
|
491
|
+
items
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
var Parser = class {
|
|
495
|
+
constructor(src) {
|
|
496
|
+
this.src = src;
|
|
497
|
+
_defineProperty(this, "pos", 0);
|
|
498
|
+
_defineProperty(this, "depth", 0);
|
|
499
|
+
_defineProperty(this, "names", /* @__PURE__ */ new Set());
|
|
500
|
+
_defineProperty(this, "heights", /* @__PURE__ */ new WeakMap());
|
|
501
|
+
}
|
|
502
|
+
parse(flags) {
|
|
503
|
+
const alt = this.parseAlternation(flags);
|
|
504
|
+
if (this.pos < this.src.length) throw this.error("invalid_syntax", "unopened group: found an unmatched \")\"");
|
|
505
|
+
return alt;
|
|
506
|
+
}
|
|
507
|
+
error(code, message, position = this.pos) {
|
|
508
|
+
return new RustRegexError(code, message, this.src, position);
|
|
509
|
+
}
|
|
510
|
+
enter() {
|
|
511
|
+
if (++this.depth > NEST_LIMIT) throw this.nestLimit();
|
|
512
|
+
}
|
|
513
|
+
nestLimit(position = this.pos) {
|
|
514
|
+
return this.error("invalid_syntax", `exceeded the nest limit of ${NEST_LIMIT}`, position);
|
|
515
|
+
}
|
|
516
|
+
height(value) {
|
|
517
|
+
return this.heights.get(value) ?? 0;
|
|
518
|
+
}
|
|
519
|
+
withHeight(value, height, start) {
|
|
520
|
+
if (height > NEST_LIMIT) throw this.nestLimit(start);
|
|
521
|
+
this.heights.set(value, height);
|
|
522
|
+
return value;
|
|
523
|
+
}
|
|
524
|
+
altHeight(alt) {
|
|
525
|
+
let tallest = 0;
|
|
526
|
+
for (const branch of alt) for (const node of branch) tallest = Math.max(tallest, this.height(node));
|
|
527
|
+
return tallest;
|
|
528
|
+
}
|
|
529
|
+
/** A union is a level of its own only with two items or more: Rust folds a union of one item into that item. */
|
|
530
|
+
unionHeight(items) {
|
|
531
|
+
let tallest = 0;
|
|
532
|
+
for (const item of items) if (item.kind === "class") tallest = Math.max(tallest, this.height(item.set));
|
|
533
|
+
return (items.length > 1 ? 1 : 0) + tallest;
|
|
534
|
+
}
|
|
535
|
+
leave() {
|
|
536
|
+
this.depth--;
|
|
537
|
+
}
|
|
538
|
+
readCodePoint() {
|
|
539
|
+
const cp = this.src.codePointAt(this.pos);
|
|
540
|
+
if (cp === void 0) throw this.error("invalid_syntax", "unexpected end of the pattern");
|
|
541
|
+
this.pos += cp > 65535 ? 2 : 1;
|
|
542
|
+
return cp;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* In verbose mode (`(?x)`) whitespace and `#` comments are not part of the pattern. That holds inside a class and
|
|
546
|
+
* a counted repetition too, where an escaped `\#` is still a literal.
|
|
547
|
+
*/
|
|
548
|
+
skipVerbose(flags) {
|
|
549
|
+
if (!flags.x) return;
|
|
550
|
+
for (;;) {
|
|
551
|
+
const ch = this.src[this.pos];
|
|
552
|
+
if (ch === void 0) return;
|
|
553
|
+
if (ch === "#") while (this.pos < this.src.length && this.src[this.pos] !== "\n") this.pos++;
|
|
554
|
+
else if (WHITESPACE.test(ch)) this.pos++;
|
|
555
|
+
else return;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
parseAlternation(flags) {
|
|
559
|
+
const branches = [];
|
|
560
|
+
for (;;) {
|
|
561
|
+
branches.push(this.parseConcat(flags));
|
|
562
|
+
if (this.src[this.pos] !== "|") return branches;
|
|
563
|
+
this.pos++;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
parseConcat(flags) {
|
|
567
|
+
const nodes = [];
|
|
568
|
+
for (;;) {
|
|
569
|
+
this.skipVerbose(flags);
|
|
570
|
+
const ch = this.src[this.pos];
|
|
571
|
+
if (ch === void 0 || ch === "|" || ch === ")") return nodes;
|
|
572
|
+
let { node, repeatable } = this.parseAtom(flags);
|
|
573
|
+
for (;;) {
|
|
574
|
+
this.skipVerbose(flags);
|
|
575
|
+
const next = this.src[this.pos];
|
|
576
|
+
if (next !== "*" && next !== "+" && next !== "?" && next !== "{") break;
|
|
577
|
+
if (!repeatable) throw this.error("invalid_syntax", "repetition operator missing expression");
|
|
578
|
+
const operand = node;
|
|
579
|
+
const operatorAt = this.pos;
|
|
580
|
+
node = this.withHeight(this.parseRepetition(flags, node), this.height(operand) + 1, operatorAt);
|
|
581
|
+
repeatable = true;
|
|
582
|
+
}
|
|
583
|
+
nodes.push(node);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
parseRepetition(flags, node) {
|
|
587
|
+
let min;
|
|
588
|
+
let max;
|
|
589
|
+
switch (this.src[this.pos]) {
|
|
590
|
+
case "*":
|
|
591
|
+
this.pos++;
|
|
592
|
+
min = 0;
|
|
593
|
+
max = null;
|
|
594
|
+
break;
|
|
595
|
+
case "+":
|
|
596
|
+
this.pos++;
|
|
597
|
+
min = 1;
|
|
598
|
+
max = null;
|
|
599
|
+
break;
|
|
600
|
+
case "?":
|
|
601
|
+
this.pos++;
|
|
602
|
+
min = 0;
|
|
603
|
+
max = 1;
|
|
604
|
+
break;
|
|
605
|
+
default: [min, max] = this.parseCounted(flags);
|
|
606
|
+
}
|
|
607
|
+
let lazy = false;
|
|
608
|
+
if (this.src[this.pos] === "?") {
|
|
609
|
+
this.pos++;
|
|
610
|
+
lazy = true;
|
|
611
|
+
}
|
|
612
|
+
return {
|
|
613
|
+
type: "repeat",
|
|
614
|
+
min,
|
|
615
|
+
max,
|
|
616
|
+
lazy: lazy !== flags.U,
|
|
617
|
+
node
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
parseCounted(flags) {
|
|
621
|
+
const start = this.pos;
|
|
622
|
+
this.pos++;
|
|
623
|
+
this.skipVerbose(flags);
|
|
624
|
+
const min = this.readDecimal();
|
|
625
|
+
this.skipVerbose(flags);
|
|
626
|
+
let max = min;
|
|
627
|
+
if (this.src[this.pos] === ",") {
|
|
628
|
+
this.pos++;
|
|
629
|
+
this.skipVerbose(flags);
|
|
630
|
+
max = this.src[this.pos] === "}" ? null : this.readDecimal();
|
|
631
|
+
this.skipVerbose(flags);
|
|
632
|
+
}
|
|
633
|
+
if (this.src[this.pos] !== "}") throw this.error("invalid_syntax", "unclosed counted repetition", start);
|
|
634
|
+
this.pos++;
|
|
635
|
+
if (max !== null && min > max) throw this.error("invalid_syntax", "the minimum of a counted repetition is greater than its maximum", start);
|
|
636
|
+
return [min, max];
|
|
637
|
+
}
|
|
638
|
+
readDecimal() {
|
|
639
|
+
const start = this.pos;
|
|
640
|
+
while (this.src[this.pos] !== void 0 && this.src[this.pos] >= "0" && this.src[this.pos] <= "9") this.pos++;
|
|
641
|
+
if (this.pos === start) throw this.error("invalid_syntax", "a counted repetition expects a decimal number");
|
|
642
|
+
const value = Number(this.src.slice(start, this.pos));
|
|
643
|
+
if (value > 0xffffffff) throw this.error("invalid_syntax", "the number in a counted repetition is too big", start);
|
|
644
|
+
return value;
|
|
645
|
+
}
|
|
646
|
+
parseAtom(flags) {
|
|
647
|
+
switch (this.src[this.pos]) {
|
|
648
|
+
case "(": return this.parseGroup(flags);
|
|
649
|
+
case "[": {
|
|
650
|
+
const start = this.pos;
|
|
651
|
+
const set = this.parseBracket(flags);
|
|
652
|
+
const node = {
|
|
653
|
+
type: "class",
|
|
654
|
+
set
|
|
655
|
+
};
|
|
656
|
+
return {
|
|
657
|
+
node: this.withHeight(node, this.height(set), start),
|
|
658
|
+
repeatable: true
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
case ".":
|
|
662
|
+
this.pos++;
|
|
663
|
+
return {
|
|
664
|
+
node: {
|
|
665
|
+
type: "dot",
|
|
666
|
+
dotAll: flags.s
|
|
667
|
+
},
|
|
668
|
+
repeatable: true
|
|
669
|
+
};
|
|
670
|
+
case "^":
|
|
671
|
+
this.pos++;
|
|
672
|
+
return {
|
|
673
|
+
node: {
|
|
674
|
+
type: "assertion",
|
|
675
|
+
kind: flags.m ? "line-start" : "text-start"
|
|
676
|
+
},
|
|
677
|
+
repeatable: true
|
|
678
|
+
};
|
|
679
|
+
case "$":
|
|
680
|
+
this.pos++;
|
|
681
|
+
return {
|
|
682
|
+
node: {
|
|
683
|
+
type: "assertion",
|
|
684
|
+
kind: flags.m ? "line-end" : "text-end"
|
|
685
|
+
},
|
|
686
|
+
repeatable: true
|
|
687
|
+
};
|
|
688
|
+
case "\\": {
|
|
689
|
+
const escape = this.parseEscape(false);
|
|
690
|
+
switch (escape.kind) {
|
|
691
|
+
case "char": return {
|
|
692
|
+
node: {
|
|
693
|
+
type: "literal",
|
|
694
|
+
cp: escape.cp
|
|
695
|
+
},
|
|
696
|
+
repeatable: true
|
|
697
|
+
};
|
|
698
|
+
case "item": return {
|
|
699
|
+
node: {
|
|
700
|
+
type: "class",
|
|
701
|
+
set: {
|
|
702
|
+
negated: false,
|
|
703
|
+
expr: union([escape.item])
|
|
704
|
+
}
|
|
705
|
+
},
|
|
706
|
+
repeatable: true
|
|
707
|
+
};
|
|
708
|
+
case "assertion": return {
|
|
709
|
+
node: {
|
|
710
|
+
type: "assertion",
|
|
711
|
+
kind: escape.assertion
|
|
712
|
+
},
|
|
713
|
+
repeatable: true
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
case "*":
|
|
718
|
+
case "+":
|
|
719
|
+
case "?":
|
|
720
|
+
case "{": throw this.error("invalid_syntax", "repetition operator missing expression");
|
|
721
|
+
default: return {
|
|
722
|
+
node: {
|
|
723
|
+
type: "literal",
|
|
724
|
+
cp: this.readCodePoint()
|
|
725
|
+
},
|
|
726
|
+
repeatable: true
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
parseGroup(flags) {
|
|
731
|
+
const start = this.pos;
|
|
732
|
+
this.pos++;
|
|
733
|
+
if (this.src[this.pos] !== "?") return this.finishGroup(flags, start, true, void 0);
|
|
734
|
+
this.pos++;
|
|
735
|
+
switch (this.src[this.pos]) {
|
|
736
|
+
case "P": {
|
|
737
|
+
const after = this.src[this.pos + 1];
|
|
738
|
+
if (after === "<") {
|
|
739
|
+
this.pos += 2;
|
|
740
|
+
return this.finishNamedGroup(flags, start);
|
|
741
|
+
}
|
|
742
|
+
if (after === "=") throw this.error("unsupported_backreference", "backreferences are not supported by the Rust regex crate", start);
|
|
743
|
+
if (after === ">") throw this.error("unsupported_feature", "recursion is not supported by the Rust regex crate", start);
|
|
744
|
+
throw this.error("invalid_syntax", "unrecognized group syntax", start);
|
|
745
|
+
}
|
|
746
|
+
case "<": {
|
|
747
|
+
const after = this.src[this.pos + 1];
|
|
748
|
+
if (after === "=" || after === "!") throw this.error("unsupported_lookaround", "lookbehind is not supported by the Rust regex crate", start);
|
|
749
|
+
this.pos++;
|
|
750
|
+
return this.finishNamedGroup(flags, start);
|
|
751
|
+
}
|
|
752
|
+
case "=":
|
|
753
|
+
case "!": throw this.error("unsupported_lookaround", "lookahead is not supported by the Rust regex crate", start);
|
|
754
|
+
case ">": throw this.error("unsupported_feature", "atomic groups are not supported by the Rust regex crate", start);
|
|
755
|
+
default: return this.parseFlagsGroup(flags, start);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
finishNamedGroup(flags, start) {
|
|
759
|
+
const end = this.src.indexOf(">", this.pos);
|
|
760
|
+
if (end === -1) throw this.error("invalid_syntax", "unclosed capture group name", start);
|
|
761
|
+
const name = this.src.slice(this.pos, end);
|
|
762
|
+
if (name === "") throw this.error("invalid_syntax", "a capture group name cannot be empty", start);
|
|
763
|
+
if (!RUST_GROUP_NAME.test(name)) throw this.error("invalid_syntax", `the capture group name "${name}" is not valid`, start);
|
|
764
|
+
if (!JS_GROUP_NAME.test(name)) throw this.error("unsupported_feature", `the capture group name "${name}" is not valid in JavaScript`, start);
|
|
765
|
+
if (this.names.has(name)) throw this.error("invalid_syntax", `duplicate capture group name "${name}"`, start);
|
|
766
|
+
this.names.add(name);
|
|
767
|
+
this.pos = end + 1;
|
|
768
|
+
return this.finishGroup(flags, start, true, name);
|
|
769
|
+
}
|
|
770
|
+
finishGroup(flags, start, capture, name) {
|
|
771
|
+
const inner = { ...flags };
|
|
772
|
+
const iStart = inner.i;
|
|
773
|
+
this.enter();
|
|
774
|
+
const alt = this.parseAlternation(inner);
|
|
775
|
+
this.leave();
|
|
776
|
+
if (this.src[this.pos] !== ")") throw this.error("invalid_syntax", "unclosed group", start);
|
|
777
|
+
this.pos++;
|
|
778
|
+
const group = {
|
|
779
|
+
type: "group",
|
|
780
|
+
capture,
|
|
781
|
+
name,
|
|
782
|
+
iStart,
|
|
783
|
+
alt
|
|
784
|
+
};
|
|
785
|
+
return {
|
|
786
|
+
node: this.withHeight(group, 1 + this.altHeight(alt), start),
|
|
787
|
+
repeatable: true
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
/** `(?flags)`, which changes the flags until the end of the group, and `(?flags:...)`, which scopes them to its body. */
|
|
791
|
+
parseFlagsGroup(flags, start) {
|
|
792
|
+
const next = { ...flags };
|
|
793
|
+
const seen = /* @__PURE__ */ new Set();
|
|
794
|
+
let negated = false;
|
|
795
|
+
let afterNegation = 0;
|
|
796
|
+
for (;;) {
|
|
797
|
+
const ch = this.src[this.pos];
|
|
798
|
+
if (ch === void 0) throw this.error("invalid_syntax", "unclosed group", start);
|
|
799
|
+
if (ch === ")" || ch === ":") break;
|
|
800
|
+
this.pos++;
|
|
801
|
+
if (ch === "-") {
|
|
802
|
+
if (negated) throw this.error("invalid_syntax", "a flag group has more than one negation");
|
|
803
|
+
negated = true;
|
|
804
|
+
continue;
|
|
805
|
+
}
|
|
806
|
+
if (negated) afterNegation++;
|
|
807
|
+
switch (ch) {
|
|
808
|
+
case "i":
|
|
809
|
+
case "m":
|
|
810
|
+
case "s":
|
|
811
|
+
case "U":
|
|
812
|
+
case "x":
|
|
813
|
+
if (seen.has(ch)) throw this.error("invalid_syntax", `duplicate flag "${ch}"`);
|
|
814
|
+
seen.add(ch);
|
|
815
|
+
next[ch] = !negated;
|
|
816
|
+
break;
|
|
817
|
+
case "u":
|
|
818
|
+
if (negated) throw this.error("unsupported_feature", "disabling Unicode mode with (?-u) cannot be expressed in JavaScript");
|
|
819
|
+
break;
|
|
820
|
+
case "R":
|
|
821
|
+
if (!negated) throw this.error("unsupported_feature", "CRLF mode (?R) cannot be expressed in JavaScript");
|
|
822
|
+
break;
|
|
823
|
+
default: throw this.error("invalid_syntax", `unrecognized flag "${ch}"`);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
if (negated && afterNegation === 0) throw this.error("invalid_syntax", "a flag negation is missing the flags it negates");
|
|
827
|
+
if (this.src[this.pos] === ":") {
|
|
828
|
+
this.pos++;
|
|
829
|
+
this.enter();
|
|
830
|
+
const iStart = next.i;
|
|
831
|
+
const alt = this.parseAlternation(next);
|
|
832
|
+
this.leave();
|
|
833
|
+
if (this.src[this.pos] !== ")") throw this.error("invalid_syntax", "unclosed group", start);
|
|
834
|
+
this.pos++;
|
|
835
|
+
const group = {
|
|
836
|
+
type: "group",
|
|
837
|
+
capture: false,
|
|
838
|
+
name: void 0,
|
|
839
|
+
iStart,
|
|
840
|
+
alt
|
|
841
|
+
};
|
|
842
|
+
return {
|
|
843
|
+
node: this.withHeight(group, 1 + this.altHeight(alt), start),
|
|
844
|
+
repeatable: true
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
if (seen.size === 0 && !negated && this.src[this.pos - 1] === "?") throw this.error("invalid_syntax", "a flag group is missing its flags", start);
|
|
848
|
+
this.pos++;
|
|
849
|
+
Object.assign(flags, next);
|
|
850
|
+
return {
|
|
851
|
+
node: {
|
|
852
|
+
type: "flags",
|
|
853
|
+
i: flags.i
|
|
854
|
+
},
|
|
855
|
+
repeatable: false
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
parseBracket(flags) {
|
|
859
|
+
const start = this.pos;
|
|
860
|
+
this.pos++;
|
|
861
|
+
this.enter();
|
|
862
|
+
let negated = false;
|
|
863
|
+
if (this.src[this.pos] === "^") {
|
|
864
|
+
negated = true;
|
|
865
|
+
this.pos++;
|
|
866
|
+
}
|
|
867
|
+
let lhs = null;
|
|
868
|
+
let lhsHeight = 0;
|
|
869
|
+
let op = null;
|
|
870
|
+
let items = [];
|
|
871
|
+
let first = true;
|
|
872
|
+
for (;;) {
|
|
873
|
+
this.skipVerbose(flags);
|
|
874
|
+
const ch = this.src[this.pos];
|
|
875
|
+
if (ch === void 0) throw this.error("invalid_syntax", "unclosed character class", start);
|
|
876
|
+
if (ch === "]" && !first) {
|
|
877
|
+
this.pos++;
|
|
878
|
+
break;
|
|
879
|
+
}
|
|
880
|
+
first = false;
|
|
881
|
+
const operator = this.src.startsWith("&&", this.pos) ? "&&" : this.src.startsWith("--", this.pos) ? "--" : this.src.startsWith("~~", this.pos) ? "~~" : null;
|
|
882
|
+
if (operator !== null) {
|
|
883
|
+
if (items.length === 0) throw this.error("invalid_syntax", `the "${operator}" operator is missing its left operand`);
|
|
884
|
+
const operand = union(items);
|
|
885
|
+
const operandHeight = this.unionHeight(items);
|
|
886
|
+
if (lhs === null || op === null) {
|
|
887
|
+
lhs = operand;
|
|
888
|
+
lhsHeight = operandHeight;
|
|
889
|
+
} else {
|
|
890
|
+
lhs = {
|
|
891
|
+
kind: "binary",
|
|
892
|
+
op,
|
|
893
|
+
left: lhs,
|
|
894
|
+
right: operand
|
|
895
|
+
};
|
|
896
|
+
lhsHeight = 1 + Math.max(lhsHeight, operandHeight);
|
|
897
|
+
if (lhsHeight > NEST_LIMIT) throw this.nestLimit(start);
|
|
898
|
+
}
|
|
899
|
+
op = operator;
|
|
900
|
+
items = [];
|
|
901
|
+
this.pos += 2;
|
|
902
|
+
continue;
|
|
903
|
+
}
|
|
904
|
+
if (ch === "[") {
|
|
905
|
+
items.push(this.tryAsciiClass() ?? {
|
|
906
|
+
kind: "class",
|
|
907
|
+
set: this.parseBracket(flags)
|
|
908
|
+
});
|
|
909
|
+
continue;
|
|
910
|
+
}
|
|
911
|
+
if (ch === "\\") {
|
|
912
|
+
const escape = this.parseEscape(true);
|
|
913
|
+
if (escape.kind === "item") items.push(escape.item);
|
|
914
|
+
else if (escape.kind === "char") this.pushCharOrRange(items, escape.cp, flags);
|
|
915
|
+
continue;
|
|
916
|
+
}
|
|
917
|
+
this.pushCharOrRange(items, this.readCodePoint(), flags);
|
|
918
|
+
}
|
|
919
|
+
this.leave();
|
|
920
|
+
if (items.length === 0) throw this.error("invalid_syntax", op === null ? "empty character class" : `the "${op}" operator is missing its right operand`, start);
|
|
921
|
+
const lastHeight = this.unionHeight(items);
|
|
922
|
+
const expr = lhs === null || op === null ? union(items) : {
|
|
923
|
+
kind: "binary",
|
|
924
|
+
op,
|
|
925
|
+
left: lhs,
|
|
926
|
+
right: union(items)
|
|
927
|
+
};
|
|
928
|
+
const exprHeight = lhs === null || op === null ? lastHeight : 1 + Math.max(lhsHeight, lastHeight);
|
|
929
|
+
return this.withHeight({
|
|
930
|
+
negated,
|
|
931
|
+
expr
|
|
932
|
+
}, 1 + exprHeight, start);
|
|
933
|
+
}
|
|
934
|
+
/** In verbose mode (`(?x)`) whitespace may sit on either side of the dash, as in `[a - z]`. */
|
|
935
|
+
pushCharOrRange(items, lo, flags) {
|
|
936
|
+
const afterLo = this.pos;
|
|
937
|
+
this.skipVerbose(flags);
|
|
938
|
+
const dashAt = this.pos;
|
|
939
|
+
const joinsRange = this.src[dashAt] === "-" && this.src[dashAt + 1] !== "-";
|
|
940
|
+
if (joinsRange) {
|
|
941
|
+
this.pos++;
|
|
942
|
+
this.skipVerbose(flags);
|
|
943
|
+
}
|
|
944
|
+
const next = this.src[this.pos];
|
|
945
|
+
if (!joinsRange || next === void 0 || next === "]" || next === "-") {
|
|
946
|
+
this.pos = afterLo;
|
|
947
|
+
items.push({
|
|
948
|
+
kind: "char",
|
|
949
|
+
cp: lo
|
|
950
|
+
});
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
let hi;
|
|
954
|
+
if (this.src[this.pos] === "\\") {
|
|
955
|
+
const escape = this.parseEscape(true);
|
|
956
|
+
if (escape.kind !== "char") throw this.error("invalid_syntax", "the end of a range must be a single character");
|
|
957
|
+
hi = escape.cp;
|
|
958
|
+
} else if (this.src[this.pos] === "[") throw this.error("invalid_syntax", "the end of a range must be a single character");
|
|
959
|
+
else hi = this.readCodePoint();
|
|
960
|
+
if (hi < lo) throw this.error("invalid_syntax", "invalid range: the start is greater than the end");
|
|
961
|
+
items.push({
|
|
962
|
+
kind: "range",
|
|
963
|
+
lo,
|
|
964
|
+
hi
|
|
965
|
+
});
|
|
966
|
+
}
|
|
967
|
+
/** `[:alpha:]`, `[:^alpha:]`: only meaningful directly inside a bracketed class. */
|
|
968
|
+
tryAsciiClass() {
|
|
969
|
+
ASCII_CLASS.lastIndex = this.pos;
|
|
970
|
+
const match = ASCII_CLASS.exec(this.src);
|
|
971
|
+
if (match === null) return null;
|
|
972
|
+
const [whole = "", caret = "", name = ""] = match;
|
|
973
|
+
const ranges = ASCII_CLASSES[name];
|
|
974
|
+
if (ranges === void 0) throw this.error("invalid_syntax", `unrecognized ASCII class "${name}"`);
|
|
975
|
+
this.pos += whole.length;
|
|
976
|
+
return {
|
|
977
|
+
kind: "class",
|
|
978
|
+
set: {
|
|
979
|
+
negated: caret === "^",
|
|
980
|
+
expr: union(ranges.map(([lo, hi]) => ({
|
|
981
|
+
kind: "range",
|
|
982
|
+
lo,
|
|
983
|
+
hi
|
|
984
|
+
})))
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
parseEscape(inClass) {
|
|
989
|
+
const start = this.pos;
|
|
990
|
+
this.pos++;
|
|
991
|
+
if (this.pos >= this.src.length) throw this.error("invalid_syntax", "incomplete escape sequence: the pattern ends after a backslash", start);
|
|
992
|
+
const cp = this.readCodePoint();
|
|
993
|
+
const ch = String.fromCodePoint(cp);
|
|
994
|
+
switch (ch) {
|
|
995
|
+
case "t": return {
|
|
996
|
+
kind: "char",
|
|
997
|
+
cp: 9
|
|
998
|
+
};
|
|
999
|
+
case "n": return {
|
|
1000
|
+
kind: "char",
|
|
1001
|
+
cp: 10
|
|
1002
|
+
};
|
|
1003
|
+
case "r": return {
|
|
1004
|
+
kind: "char",
|
|
1005
|
+
cp: 13
|
|
1006
|
+
};
|
|
1007
|
+
case "f": return {
|
|
1008
|
+
kind: "char",
|
|
1009
|
+
cp: 12
|
|
1010
|
+
};
|
|
1011
|
+
case "v": return {
|
|
1012
|
+
kind: "char",
|
|
1013
|
+
cp: 11
|
|
1014
|
+
};
|
|
1015
|
+
case "a": return {
|
|
1016
|
+
kind: "char",
|
|
1017
|
+
cp: 7
|
|
1018
|
+
};
|
|
1019
|
+
case "x": return {
|
|
1020
|
+
kind: "char",
|
|
1021
|
+
cp: this.parseHex(2, start)
|
|
1022
|
+
};
|
|
1023
|
+
case "u": return {
|
|
1024
|
+
kind: "char",
|
|
1025
|
+
cp: this.parseHex(4, start)
|
|
1026
|
+
};
|
|
1027
|
+
case "U": return {
|
|
1028
|
+
kind: "char",
|
|
1029
|
+
cp: this.parseHex(8, start)
|
|
1030
|
+
};
|
|
1031
|
+
case "d":
|
|
1032
|
+
case "s":
|
|
1033
|
+
case "w": return {
|
|
1034
|
+
kind: "item",
|
|
1035
|
+
item: {
|
|
1036
|
+
kind: "perl",
|
|
1037
|
+
cls: ch,
|
|
1038
|
+
negated: false
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
case "D":
|
|
1042
|
+
case "S":
|
|
1043
|
+
case "W": return {
|
|
1044
|
+
kind: "item",
|
|
1045
|
+
item: {
|
|
1046
|
+
kind: "perl",
|
|
1047
|
+
cls: ch.toLowerCase(),
|
|
1048
|
+
negated: true
|
|
1049
|
+
}
|
|
1050
|
+
};
|
|
1051
|
+
case "p":
|
|
1052
|
+
case "P": return {
|
|
1053
|
+
kind: "item",
|
|
1054
|
+
item: this.parseProperty(ch === "P", start)
|
|
1055
|
+
};
|
|
1056
|
+
case "A":
|
|
1057
|
+
case "z":
|
|
1058
|
+
case "B": return this.assertion(ch === "A" ? "text-start" : ch === "z" ? "text-end" : "not-word-boundary", inClass, start);
|
|
1059
|
+
case "b":
|
|
1060
|
+
if (this.src[this.pos] === "{") throw this.error("unsupported_feature", "the \\b{...} word boundaries are not supported", start);
|
|
1061
|
+
return this.assertion("word-boundary", inClass, start);
|
|
1062
|
+
case "<":
|
|
1063
|
+
case ">": throw this.error("unsupported_feature", `the \\${ch} word boundary is not supported`, start);
|
|
1064
|
+
case "k":
|
|
1065
|
+
if (this.src[this.pos] === "<") throw this.error("unsupported_backreference", "backreferences are not supported by the Rust regex crate", start);
|
|
1066
|
+
throw this.error("invalid_syntax", "unrecognized escape sequence \"\\k\"", start);
|
|
1067
|
+
default:
|
|
1068
|
+
if (ch >= "1" && ch <= "9") throw this.error("unsupported_backreference", "backreferences are not supported by the Rust regex crate", start);
|
|
1069
|
+
if (ch === "0") throw this.error("invalid_syntax", "octal escapes are not supported, use \\x00", start);
|
|
1070
|
+
if (cp < 128 && !isAsciiAlphanumeric(ch)) return {
|
|
1071
|
+
kind: "char",
|
|
1072
|
+
cp
|
|
1073
|
+
};
|
|
1074
|
+
throw this.error("invalid_syntax", `unrecognized escape sequence "\\${ch}"`, start);
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
assertion(kind, inClass, start) {
|
|
1078
|
+
if (inClass) throw this.error("invalid_syntax", "an assertion is not allowed in a character class", start);
|
|
1079
|
+
return {
|
|
1080
|
+
kind: "assertion",
|
|
1081
|
+
assertion: kind
|
|
1082
|
+
};
|
|
1083
|
+
}
|
|
1084
|
+
/** `\xNN`, `\x{N...}`, `\uNNNN`, `\u{N...}`, `\UNNNNNNNN`, `\U{N...}`. */
|
|
1085
|
+
parseHex(digits, start) {
|
|
1086
|
+
let text;
|
|
1087
|
+
if (this.src[this.pos] === "{") {
|
|
1088
|
+
const end = this.src.indexOf("}", this.pos);
|
|
1089
|
+
if (end === -1) throw this.error("invalid_syntax", "unclosed hexadecimal escape", start);
|
|
1090
|
+
text = this.src.slice(this.pos + 1, end).trim();
|
|
1091
|
+
this.pos = end + 1;
|
|
1092
|
+
if (!/^[0-9A-Fa-f]{1,8}$/.test(text)) throw this.error("invalid_syntax", "invalid hexadecimal escape", start);
|
|
1093
|
+
} else {
|
|
1094
|
+
text = this.src.slice(this.pos, this.pos + digits);
|
|
1095
|
+
if (text.length !== digits || !/^[0-9A-Fa-f]+$/.test(text)) throw this.error("invalid_syntax", `a hexadecimal escape needs exactly ${digits} digits`, start);
|
|
1096
|
+
this.pos += digits;
|
|
1097
|
+
}
|
|
1098
|
+
const cp = Number.parseInt(text, 16);
|
|
1099
|
+
if (cp > MAX_CODE_POINT || cp >= 55296 && cp <= 57343) throw this.error("invalid_syntax", "a hexadecimal escape must be a Unicode scalar value", start);
|
|
1100
|
+
return cp;
|
|
1101
|
+
}
|
|
1102
|
+
parseProperty(negated, start) {
|
|
1103
|
+
let spec;
|
|
1104
|
+
let singleLetter = false;
|
|
1105
|
+
if (this.src[this.pos] === "{") {
|
|
1106
|
+
const end = this.src.indexOf("}", this.pos);
|
|
1107
|
+
if (end === -1) throw this.error("invalid_syntax", "unclosed Unicode class", start);
|
|
1108
|
+
spec = this.src.slice(this.pos + 1, end).trim();
|
|
1109
|
+
this.pos = end + 1;
|
|
1110
|
+
} else {
|
|
1111
|
+
spec = this.src[this.pos] ?? "";
|
|
1112
|
+
if (spec === "") throw this.error("invalid_syntax", "incomplete Unicode class: the pattern ends after \\p", start);
|
|
1113
|
+
this.pos++;
|
|
1114
|
+
singleLetter = true;
|
|
1115
|
+
}
|
|
1116
|
+
if (spec.startsWith("^")) {
|
|
1117
|
+
negated = !negated;
|
|
1118
|
+
spec = spec.slice(1).trim();
|
|
1119
|
+
}
|
|
1120
|
+
const resolved = resolveProperty(spec, singleLetter);
|
|
1121
|
+
if ("error" in resolved) throw this.error(resolved.error === "unsupported" ? "unsupported_feature" : "invalid_syntax", resolved.message, start);
|
|
1122
|
+
return {
|
|
1123
|
+
kind: "property",
|
|
1124
|
+
expr: resolved.expr,
|
|
1125
|
+
negated: negated !== resolved.negated
|
|
1126
|
+
};
|
|
1127
|
+
}
|
|
1128
|
+
};
|
|
1129
|
+
function parse(pattern, flags) {
|
|
1130
|
+
let initialI = flags.i;
|
|
1131
|
+
const alt = new Parser(pattern).parse(flags);
|
|
1132
|
+
const first = alt[0];
|
|
1133
|
+
while (first !== void 0 && first[0]?.type === "flags") {
|
|
1134
|
+
initialI = first[0].i;
|
|
1135
|
+
first.shift();
|
|
1136
|
+
}
|
|
1137
|
+
return {
|
|
1138
|
+
alt,
|
|
1139
|
+
initialI
|
|
1140
|
+
};
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
// src/lib/transpile.ts
|
|
1144
|
+
/**
|
|
1145
|
+
* The JavaScript flags a caller may add. `i`, `m`, `s` and `u` would change what the Rust pattern means, and `v` is
|
|
1146
|
+
* always set, so none of them can be added.
|
|
1147
|
+
*/
|
|
1148
|
+
const EXTRA_FLAGS = /* @__PURE__ */ new Set([
|
|
1149
|
+
"d",
|
|
1150
|
+
"g",
|
|
1151
|
+
"y"
|
|
1152
|
+
]);
|
|
1153
|
+
function checkExtraFlags(extraFlags, pattern) {
|
|
1154
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1155
|
+
for (const flag of extraFlags) {
|
|
1156
|
+
if (!EXTRA_FLAGS.has(flag)) throw new RustRegexError("invalid_option", `extraFlags accepts only "d", "g" and "y", not "${flag}"`, pattern);
|
|
1157
|
+
if (seen.has(flag)) throw new RustRegexError("invalid_option", `extraFlags repeats the flag "${flag}"`, pattern);
|
|
1158
|
+
seen.add(flag);
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
function parseFlags(flags, pattern) {
|
|
1162
|
+
const result = {
|
|
1163
|
+
i: false,
|
|
1164
|
+
m: false,
|
|
1165
|
+
s: false,
|
|
1166
|
+
U: false,
|
|
1167
|
+
x: false
|
|
1168
|
+
};
|
|
1169
|
+
for (const flag of flags) {
|
|
1170
|
+
if (flag !== "i" && flag !== "m" && flag !== "s" && flag !== "U" && flag !== "x") throw new RustRegexError("invalid_option", `unrecognized flag "${flag}"`, pattern);
|
|
1171
|
+
result[flag] = true;
|
|
1172
|
+
}
|
|
1173
|
+
return result;
|
|
1174
|
+
}
|
|
1175
|
+
/**
|
|
1176
|
+
* Converts a regular expression written for the Rust `regex` crate, the flavour Discord uses, into the source and
|
|
1177
|
+
* flags of an equivalent JavaScript `RegExp`.
|
|
1178
|
+
*
|
|
1179
|
+
* @example
|
|
1180
|
+
* ```ts
|
|
1181
|
+
* transpile(String.raw`(?i)\bfree\s+nitro\b`);
|
|
1182
|
+
* // { source: '(?:(?<=[…])(?![…])|…)free\\p{White_Space}+nitro…', flags: 'iv', usesModifiers: false }
|
|
1183
|
+
* ```
|
|
1184
|
+
*
|
|
1185
|
+
* @throws {RustRegexError} When Rust would reject the pattern, or when it uses something JavaScript cannot express.
|
|
1186
|
+
*/
|
|
1187
|
+
function transpile(pattern, options = {}) {
|
|
1188
|
+
const { alt, initialI } = parse(pattern, parseFlags(options.flags ?? "", pattern));
|
|
1189
|
+
const { source, usesModifiers } = new Emitter(pattern).emit(alt, initialI);
|
|
1190
|
+
const flags = initialI ? "iv" : "v";
|
|
1191
|
+
if (options.validate !== false) try {
|
|
1192
|
+
new RegExp(source, flags);
|
|
1193
|
+
} catch (error) {
|
|
1194
|
+
throw new RustRegexError("invalid_output", rejection(error, usesModifiers), pattern);
|
|
1195
|
+
}
|
|
1196
|
+
return {
|
|
1197
|
+
source,
|
|
1198
|
+
flags,
|
|
1199
|
+
usesModifiers
|
|
1200
|
+
};
|
|
1201
|
+
}
|
|
1202
|
+
function rejection(error, usesModifiers) {
|
|
1203
|
+
const hint = usesModifiers ? " (scoped flags need a runtime with ES2025 regular expression modifiers)" : "";
|
|
1204
|
+
return `JavaScript rejected the translated pattern: ${error instanceof Error ? error.message : String(error)}${hint}`;
|
|
1205
|
+
}
|
|
1206
|
+
/**
|
|
1207
|
+
* Same as {@link transpile}, but returns the compiled `RegExp`.
|
|
1208
|
+
*
|
|
1209
|
+
* Rust matches in time linear in the size of the text, whatever the pattern. A JavaScript `RegExp` backtracks, so a
|
|
1210
|
+
* pattern such as `(a+)+$` that is safe in Rust can take exponential time here. Do not run the result against text a
|
|
1211
|
+
* stranger controls without a limit, for example in a worker with a timeout.
|
|
1212
|
+
*
|
|
1213
|
+
* @example
|
|
1214
|
+
* ```ts
|
|
1215
|
+
* toRegExp(String.raw`(?P<year>\d{4})-(?P<month>\d{2})`).exec('2026-11')?.groups?.year; // '2026'
|
|
1216
|
+
* ```
|
|
1217
|
+
*/
|
|
1218
|
+
function toRegExp(pattern, options = {}) {
|
|
1219
|
+
const { extraFlags = "", ...rest } = options;
|
|
1220
|
+
checkExtraFlags(extraFlags, pattern);
|
|
1221
|
+
const { source, flags, usesModifiers } = transpile(pattern, {
|
|
1222
|
+
...rest,
|
|
1223
|
+
validate: false
|
|
1224
|
+
});
|
|
1225
|
+
try {
|
|
1226
|
+
return new RegExp(source, flags + extraFlags);
|
|
1227
|
+
} catch (error) {
|
|
1228
|
+
throw new RustRegexError("invalid_output", rejection(error, usesModifiers), pattern);
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
export { RustRegexError, toRegExp, transpile };
|
|
1233
|
+
//# sourceMappingURL=index.js.map
|