poe-code 14.0.74 → 14.0.75
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/index.js +926 -84
- package/dist/index.js.map +4 -4
- package/dist/metafile.json +1 -1
- package/package.json +1 -1
- package/packages/safe-js/dist/chunks/{chunk-2J6W3YGB.js → chunk-BAJI3K4B.js} +2 -2
- package/packages/safe-js/dist/chunks/{chunk-YEZOXLY4.js → chunk-EOTY4PCT.js} +901 -84
- package/packages/safe-js/dist/chunks/chunk-EOTY4PCT.js.map +7 -0
- package/packages/safe-js/dist/cli.js +2 -2
- package/packages/safe-js/dist/core.js +1 -1
- package/packages/safe-js/dist/index.js +2 -2
- package/packages/safe-js/dist/interp/globals/regex.js +108 -2
- package/packages/safe-js/dist/interp/iteration.js +5 -2
- package/packages/safe-js/dist/interp/methods/regex-split.d.ts +3 -0
- package/packages/safe-js/dist/interp/methods/regex-split.js +102 -0
- package/packages/safe-js/dist/interp/methods/regex.d.ts +1 -1
- package/packages/safe-js/dist/interp/methods/regex.js +16 -5
- package/packages/safe-js/dist/interp/methods/regexp-iterator.d.ts +5 -1
- package/packages/safe-js/dist/interp/methods/regexp-iterator.js +59 -7
- package/packages/safe-js/dist/interp/methods/string.js +7 -7
- package/packages/safe-js/dist/interp/object-model.d.ts +1 -0
- package/packages/safe-js/dist/interp/object-model.js +4 -1
- package/packages/safe-js/dist/interp/regex/compile-guard.js +26 -4
- package/packages/safe-js/dist/interp/regex/engine.d.ts +4 -0
- package/packages/safe-js/dist/interp/regex/engine.js +230 -30
- package/packages/safe-js/dist/interp/regex/parse.d.ts +42 -4
- package/packages/safe-js/dist/interp/regex/parse.js +429 -25
- package/packages/safe-js/dist/interp/regexp-iterator.d.ts +4 -2
- package/packages/safe-js/dist/interp/regexp-iterator.js +6 -1
- package/packages/safe-js/dist/interp/values.js +5 -3
- package/packages/safe-js/dist/snapshot/dump-format.js +10 -2
- package/packages/safe-js/dist/snapshot/replay-data.d.ts +3 -0
- package/packages/safe-js/dist/snapshot/replay-data.js +14 -4
- package/packages/safe-js/dist/snapshot/restore.js +8 -2
- package/packages/safe-js/dist/snapshot/serialize.d.ts +3 -0
- package/packages/safe-js/dist/snapshot/serialize.js +4 -1
- package/packages/safe-js/dist/snapshot/validation.js +8 -0
- package/packages/safe-js/dist/chunks/chunk-YEZOXLY4.js.map +0 -7
- /package/packages/safe-js/dist/chunks/{chunk-2J6W3YGB.js.map → chunk-BAJI3K4B.js.map} +0 -0
|
@@ -3,7 +3,11 @@ export type RegexMatch = {
|
|
|
3
3
|
index: number;
|
|
4
4
|
text: string;
|
|
5
5
|
captures: (string | undefined)[];
|
|
6
|
+
indices?: ([number, number] | undefined)[];
|
|
7
|
+
groups?: Record<string, string | undefined>;
|
|
8
|
+
indicesGroups?: Record<string, [number, number] | undefined>;
|
|
6
9
|
};
|
|
7
10
|
export declare function matchRegex(pattern: RegexPattern, input: string, lastIndex?: number): RegexMatch | null;
|
|
8
11
|
export declare function matchRegexFrom(pattern: RegexPattern, input: string, startIndex: number): RegexMatch | null;
|
|
9
12
|
export declare function normalizeLastIndex(lastIndex: number): number;
|
|
13
|
+
export declare function advanceStringIndex(input: string, index: number, unicode: boolean): number;
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { allocateRegexSteps } from "../budget.js";
|
|
2
2
|
export function matchRegex(pattern, input, lastIndex = 0) {
|
|
3
|
-
const startIndex = pattern.flags.global ? normalizeLastIndex(lastIndex) : 0;
|
|
3
|
+
const startIndex = pattern.flags.global || pattern.flags.sticky ? normalizeLastIndex(lastIndex) : 0;
|
|
4
4
|
return matchRegexFrom(pattern, input, startIndex);
|
|
5
5
|
}
|
|
6
6
|
export function matchRegexFrom(pattern, input, startIndex) {
|
|
7
7
|
if (startIndex > input.length) {
|
|
8
8
|
return null;
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
const unicode = pattern.flags.unicode || pattern.flags.unicodeSets;
|
|
11
|
+
if (unicode && startIndex > 0 && startIndex < input.length &&
|
|
12
|
+
input.charCodeAt(startIndex) >= 0xdc00 && input.charCodeAt(startIndex) <= 0xdfff &&
|
|
13
|
+
input.charCodeAt(startIndex - 1) >= 0xd800 && input.charCodeAt(startIndex - 1) <= 0xdbff)
|
|
14
|
+
startIndex--;
|
|
15
|
+
for (let attempt = startIndex; attempt <= input.length; attempt = advanceStringIndex(input, attempt, unicode)) {
|
|
16
|
+
const context = { input, flags: pattern.flags, groups: pattern.groups, direction: 1, work: { steps: 0 } };
|
|
12
17
|
charge(context);
|
|
13
18
|
const initialState = {
|
|
14
19
|
position: attempt,
|
|
@@ -16,26 +21,63 @@ export function matchRegexFrom(pattern, input, startIndex) {
|
|
|
16
21
|
};
|
|
17
22
|
const result = matchNode(pattern.body, initialState, context).next();
|
|
18
23
|
if (!result.done) {
|
|
19
|
-
return toRegexMatch(input, attempt, result.value);
|
|
24
|
+
return toRegexMatch(input, attempt, result.value, pattern.flags.hasIndices, pattern.groups);
|
|
20
25
|
}
|
|
26
|
+
if (pattern.flags.sticky)
|
|
27
|
+
break;
|
|
21
28
|
}
|
|
22
29
|
return null;
|
|
23
30
|
}
|
|
24
31
|
function* matchNode(node, state, context) {
|
|
25
32
|
charge(context);
|
|
33
|
+
const unicode = context.flags.unicode || context.flags.unicodeSets;
|
|
34
|
+
const character = readCharacter(context.input, state.position, context.direction, unicode);
|
|
26
35
|
switch (node.type) {
|
|
27
36
|
case "empty":
|
|
28
37
|
yield state;
|
|
29
38
|
return;
|
|
30
39
|
case "literal":
|
|
31
|
-
if (charactersEqual(
|
|
32
|
-
yield { ...state, position: state.position +
|
|
40
|
+
if (charactersEqual(character, node.value, context.flags.ignoreCase, unicode)) {
|
|
41
|
+
yield { ...state, position: state.position + context.direction * character.length };
|
|
33
42
|
}
|
|
34
43
|
return;
|
|
44
|
+
case "backreference":
|
|
45
|
+
case "namedBackreference": {
|
|
46
|
+
let capture;
|
|
47
|
+
if (node.type === "backreference")
|
|
48
|
+
capture = state.captures[node.index - 1];
|
|
49
|
+
else {
|
|
50
|
+
for (const index of context.groups?.[node.name] ?? []) {
|
|
51
|
+
charge(context);
|
|
52
|
+
if (state.captures[index - 1] !== undefined) {
|
|
53
|
+
capture = state.captures[index - 1];
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (capture === undefined) {
|
|
59
|
+
yield state;
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
let position = state.position;
|
|
63
|
+
let reference = context.direction === 1 ? capture.start : capture.end;
|
|
64
|
+
const end = context.direction === 1 ? capture.end : capture.start;
|
|
65
|
+
while (reference !== end) {
|
|
66
|
+
charge(context);
|
|
67
|
+
const expected = readCharacter(context.input, reference, context.direction, unicode);
|
|
68
|
+
const actual = readCharacter(context.input, position, context.direction, unicode);
|
|
69
|
+
if (!charactersEqual(actual, expected, context.flags.ignoreCase, unicode))
|
|
70
|
+
return;
|
|
71
|
+
reference += context.direction * expected.length;
|
|
72
|
+
position += context.direction * actual.length;
|
|
73
|
+
}
|
|
74
|
+
yield { ...state, position };
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
35
77
|
case "dot":
|
|
36
|
-
if (
|
|
37
|
-
(context.flags.dotAll || !isLineTerminator(
|
|
38
|
-
yield { ...state, position: state.position +
|
|
78
|
+
if (character !== undefined &&
|
|
79
|
+
(context.flags.dotAll || !isLineTerminator(character))) {
|
|
80
|
+
yield { ...state, position: state.position + context.direction * character.length };
|
|
39
81
|
}
|
|
40
82
|
return;
|
|
41
83
|
case "anchor":
|
|
@@ -44,18 +86,25 @@ function* matchNode(node, state, context) {
|
|
|
44
86
|
}
|
|
45
87
|
return;
|
|
46
88
|
case "wordBoundary": {
|
|
47
|
-
const previousWord = state.position > 0 &&
|
|
48
|
-
const nextWord = state.position < context.input.length &&
|
|
89
|
+
const previousWord = state.position > 0 && matchesCharacterClassItem(context.input[state.position - 1], { type: "kind", kind: "word", negated: false }, context.flags.ignoreCase, unicode);
|
|
90
|
+
const nextWord = state.position < context.input.length && matchesCharacterClassItem(context.input[state.position], { type: "kind", kind: "word", negated: false }, context.flags.ignoreCase, unicode);
|
|
49
91
|
if ((previousWord !== nextWord) !== node.negated) {
|
|
50
92
|
yield state;
|
|
51
93
|
}
|
|
52
94
|
return;
|
|
53
95
|
}
|
|
54
96
|
case "characterClass": {
|
|
55
|
-
|
|
97
|
+
if (context.flags.unicodeSets) {
|
|
98
|
+
const lengths = [...matchUnicodeSet(node, state.position, context)].sort((a, b) => b - a);
|
|
99
|
+
for (const length of lengths) {
|
|
100
|
+
charge(context);
|
|
101
|
+
yield { ...state, position: state.position + context.direction * length };
|
|
102
|
+
}
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
56
105
|
if (character !== undefined &&
|
|
57
|
-
matchesCharacterClass(character, node.items, node.negated, context
|
|
58
|
-
yield { ...state, position: state.position +
|
|
106
|
+
matchesCharacterClass(character, node.items, node.negated, context)) {
|
|
107
|
+
yield { ...state, position: state.position + context.direction * character.length };
|
|
59
108
|
}
|
|
60
109
|
return;
|
|
61
110
|
}
|
|
@@ -67,14 +116,30 @@ function* matchNode(node, state, context) {
|
|
|
67
116
|
yield* matchNode(alternative, cloneState(state), context);
|
|
68
117
|
}
|
|
69
118
|
return;
|
|
119
|
+
case "lookahead":
|
|
120
|
+
case "lookbehind": {
|
|
121
|
+
const result = matchNode(node.body, cloneState(state), {
|
|
122
|
+
...context, direction: node.type === "lookbehind" ? -1 : 1
|
|
123
|
+
}).next();
|
|
124
|
+
if (node.negated) {
|
|
125
|
+
if (result.done)
|
|
126
|
+
yield state;
|
|
127
|
+
}
|
|
128
|
+
else if (!result.done) {
|
|
129
|
+
yield { position: state.position, captures: result.value.captures };
|
|
130
|
+
}
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
70
133
|
case "group":
|
|
71
|
-
for (const result of matchNode(node.body, cloneState(state), context
|
|
134
|
+
for (const result of matchNode(node.body, cloneState(state), node.modifiers === undefined ? context : {
|
|
135
|
+
...context, flags: { ...context.flags, ...node.modifiers }
|
|
136
|
+
})) {
|
|
72
137
|
if (!node.capturing || node.index === undefined) {
|
|
73
138
|
yield result;
|
|
74
139
|
continue;
|
|
75
140
|
}
|
|
76
141
|
const captures = result.captures.slice();
|
|
77
|
-
captures[node.index - 1] = { start: state.position, end: result.position };
|
|
142
|
+
captures[node.index - 1] = { start: Math.min(state.position, result.position), end: Math.max(state.position, result.position) };
|
|
78
143
|
yield { position: result.position, captures };
|
|
79
144
|
}
|
|
80
145
|
return;
|
|
@@ -88,7 +153,8 @@ function* matchSequence(elements, index, state, context) {
|
|
|
88
153
|
yield state;
|
|
89
154
|
return;
|
|
90
155
|
}
|
|
91
|
-
|
|
156
|
+
const element = elements[context.direction === 1 ? index : elements.length - 1 - index];
|
|
157
|
+
for (const result of matchNode(element, state, context)) {
|
|
92
158
|
yield* matchSequence(elements, index + 1, result, context);
|
|
93
159
|
}
|
|
94
160
|
}
|
|
@@ -129,11 +195,35 @@ function matchesAnchor(kind, position, context) {
|
|
|
129
195
|
position < context.input.length &&
|
|
130
196
|
isLineTerminator(context.input[position])));
|
|
131
197
|
}
|
|
132
|
-
function matchesCharacterClass(character, items, negated,
|
|
133
|
-
const matched = items.some((item) =>
|
|
198
|
+
function matchesCharacterClass(character, items, negated, context) {
|
|
199
|
+
const matched = items.some((item) => {
|
|
200
|
+
if (context.flags.unicode)
|
|
201
|
+
charge(context);
|
|
202
|
+
return matchesCharacterClassItem(character, item, context.flags.ignoreCase, context.flags.unicode);
|
|
203
|
+
});
|
|
134
204
|
return negated ? !matched : matched;
|
|
135
205
|
}
|
|
136
|
-
function matchesCharacterClassItem(character, item, ignoreCase) {
|
|
206
|
+
function matchesCharacterClassItem(character, item, ignoreCase, unicode, unicodeSets = false) {
|
|
207
|
+
if (item.type === "strings" || item.type === "set")
|
|
208
|
+
return false;
|
|
209
|
+
if (unicode) {
|
|
210
|
+
// Generated atoms only: the host classifies one code point, never executes a guest pattern.
|
|
211
|
+
const hex = (value) => `\\u{${value.codePointAt(0).toString(16)}}`;
|
|
212
|
+
let atom;
|
|
213
|
+
if (item.type === "character")
|
|
214
|
+
atom = hex(item.value);
|
|
215
|
+
else if (item.type === "range")
|
|
216
|
+
atom = `[${hex(item.from)}-${hex(item.to)}]`;
|
|
217
|
+
else if (item.type === "property")
|
|
218
|
+
atom = `\\${item.negated ? "P" : "p"}{${item.value}}`;
|
|
219
|
+
else {
|
|
220
|
+
const kind = item.kind === "digit" ? "d" : item.kind === "word" ? "w" : "s";
|
|
221
|
+
atom = `\\${item.negated ? kind.toUpperCase() : kind}`;
|
|
222
|
+
}
|
|
223
|
+
return new RegExp(`^(?:${atom})$`, (ignoreCase ? "i" : "") + (unicodeSets ? "v" : "u")).test(character);
|
|
224
|
+
}
|
|
225
|
+
if (item.type === "property")
|
|
226
|
+
return false;
|
|
137
227
|
if (item.type === "character") {
|
|
138
228
|
return charactersEqual(character, item.value, ignoreCase);
|
|
139
229
|
}
|
|
@@ -147,10 +237,10 @@ function matchesCharacterClassItem(character, item, ignoreCase) {
|
|
|
147
237
|
if (!ignoreCase) {
|
|
148
238
|
return false;
|
|
149
239
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
240
|
+
// Folding endpoints does not preserve range membership. Classify one UTF-16
|
|
241
|
+
// code unit using generated hex endpoints, never a guest pattern.
|
|
242
|
+
return new RegExp(`^[\\u${from.toString(16).padStart(4, "0")}-\\u${to.toString(16).padStart(4, "0")}]$`, "i")
|
|
243
|
+
.test(character);
|
|
154
244
|
}
|
|
155
245
|
const matched = item.kind === "digit"
|
|
156
246
|
? isDigit(character)
|
|
@@ -159,12 +249,107 @@ function matchesCharacterClassItem(character, item, ignoreCase) {
|
|
|
159
249
|
: isSpaceCharacter(character);
|
|
160
250
|
return item.negated ? !matched : matched;
|
|
161
251
|
}
|
|
162
|
-
function
|
|
163
|
-
|
|
252
|
+
function matchUnicodeSet(set, position, context) {
|
|
253
|
+
charge(context);
|
|
254
|
+
let lengths = new Set();
|
|
255
|
+
for (let index = 0; index < set.items.length; index++) {
|
|
256
|
+
const next = matchUnicodeSetItem(set.items[index], position, context);
|
|
257
|
+
if (index === 0)
|
|
258
|
+
lengths = next;
|
|
259
|
+
else if (set.operation === undefined) {
|
|
260
|
+
for (const length of next) {
|
|
261
|
+
charge(context);
|
|
262
|
+
lengths.add(length);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
for (const length of lengths) {
|
|
267
|
+
charge(context);
|
|
268
|
+
if (set.operation === "intersection" ? !next.has(length) : next.has(length))
|
|
269
|
+
lengths.delete(length);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (set.negated) {
|
|
274
|
+
const character = readCharacter(context.input, position, context.direction, true);
|
|
275
|
+
return new Set(character !== undefined && !lengths.has(character.length) ? [character.length] : []);
|
|
276
|
+
}
|
|
277
|
+
return lengths;
|
|
278
|
+
}
|
|
279
|
+
function matchUnicodeSetItem(item, position, context) {
|
|
280
|
+
charge(context);
|
|
281
|
+
if (item.type === "set")
|
|
282
|
+
return matchUnicodeSet(item.value, position, context);
|
|
283
|
+
const lengths = new Set();
|
|
284
|
+
if (item.type === "strings") {
|
|
285
|
+
for (const value of item.values) {
|
|
286
|
+
charge(context);
|
|
287
|
+
let reference = context.direction === 1 ? 0 : value.length;
|
|
288
|
+
let cursor = position;
|
|
289
|
+
const end = context.direction === 1 ? value.length : 0;
|
|
290
|
+
while (reference !== end) {
|
|
291
|
+
charge(context);
|
|
292
|
+
const expected = readCharacter(value, reference, context.direction, true);
|
|
293
|
+
const actual = readCharacter(context.input, cursor, context.direction, true);
|
|
294
|
+
if (!charactersEqual(actual, expected, context.flags.ignoreCase, true))
|
|
295
|
+
break;
|
|
296
|
+
reference += context.direction * expected.length;
|
|
297
|
+
cursor += context.direction * actual.length;
|
|
298
|
+
}
|
|
299
|
+
if (reference === end)
|
|
300
|
+
lengths.add(Math.abs(cursor - position));
|
|
301
|
+
}
|
|
302
|
+
return lengths;
|
|
303
|
+
}
|
|
304
|
+
if (item.type === "property" && item.strings) {
|
|
305
|
+
// A validated property atom denotes a finite Unicode string set. The host
|
|
306
|
+
// finds its longest member only; guest alternation/backtracking stays here.
|
|
307
|
+
const atom = `\\p{${item.value}}`;
|
|
308
|
+
const flags = (context.flags.ignoreCase ? "i" : "") + "v";
|
|
309
|
+
const matcher = new RegExp(context.direction === 1 ? atom : `(?<=(${atom}))`, flags + "y");
|
|
310
|
+
matcher.lastIndex = position;
|
|
311
|
+
const match = matcher.exec(context.input);
|
|
312
|
+
if (match === null)
|
|
313
|
+
return lengths;
|
|
314
|
+
const longest = context.direction === 1 ? match[0] : match[1];
|
|
315
|
+
const member = new RegExp(`^(?:${atom})$`, flags);
|
|
316
|
+
let consumed = 0;
|
|
317
|
+
while (consumed < longest.length) {
|
|
318
|
+
charge(context);
|
|
319
|
+
const cursor = context.direction === 1 ? consumed : longest.length - consumed;
|
|
320
|
+
consumed += readCharacter(longest, cursor, context.direction, true).length;
|
|
321
|
+
const candidate = context.direction === 1 ? longest.slice(0, consumed) : longest.slice(longest.length - consumed);
|
|
322
|
+
if (member.test(candidate))
|
|
323
|
+
lengths.add(consumed);
|
|
324
|
+
}
|
|
325
|
+
return lengths;
|
|
326
|
+
}
|
|
327
|
+
const character = readCharacter(context.input, position, context.direction, true);
|
|
328
|
+
if (character !== undefined && matchesCharacterClassItem(character, item, context.flags.ignoreCase, true, true))
|
|
329
|
+
lengths.add(character.length);
|
|
330
|
+
return lengths;
|
|
331
|
+
}
|
|
332
|
+
function toRegexMatch(input, start, state, hasIndices, groups) {
|
|
333
|
+
const match = {
|
|
164
334
|
index: start,
|
|
165
335
|
text: input.slice(start, state.position),
|
|
166
336
|
captures: state.captures.map((capture) => capture === undefined ? undefined : input.slice(capture.start, capture.end))
|
|
167
337
|
};
|
|
338
|
+
if (hasIndices) {
|
|
339
|
+
match.indices = [[start, state.position], ...state.captures.map((capture) => capture === undefined ? undefined : [capture.start, capture.end])];
|
|
340
|
+
}
|
|
341
|
+
if (groups !== undefined) {
|
|
342
|
+
match.groups = Object.create(null);
|
|
343
|
+
if (hasIndices)
|
|
344
|
+
match.indicesGroups = Object.create(null);
|
|
345
|
+
for (const [name, indices] of Object.entries(groups)) {
|
|
346
|
+
const index = indices.find(index => state.captures[index - 1] !== undefined);
|
|
347
|
+
match.groups[name] = index === undefined ? undefined : match.captures[index - 1];
|
|
348
|
+
if (match.indicesGroups !== undefined)
|
|
349
|
+
match.indicesGroups[name] = index === undefined ? undefined : match.indices[index];
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return match;
|
|
168
353
|
}
|
|
169
354
|
function cloneState(state) {
|
|
170
355
|
return { position: state.position, captures: state.captures.slice() };
|
|
@@ -194,13 +379,13 @@ function clearNodeCaptures(node, captures) {
|
|
|
194
379
|
}
|
|
195
380
|
return;
|
|
196
381
|
}
|
|
197
|
-
if (node.type === "quantifier") {
|
|
382
|
+
if (node.type === "quantifier" || node.type === "lookahead" || node.type === "lookbehind") {
|
|
198
383
|
clearNodeCaptures(node.body, captures);
|
|
199
384
|
}
|
|
200
385
|
}
|
|
201
386
|
function charge(context) {
|
|
202
|
-
context.steps += 1;
|
|
203
|
-
allocateRegexSteps(context.steps);
|
|
387
|
+
context.work.steps += 1;
|
|
388
|
+
allocateRegexSteps(context.work.steps);
|
|
204
389
|
}
|
|
205
390
|
export function normalizeLastIndex(lastIndex) {
|
|
206
391
|
if (Number.isNaN(lastIndex) || lastIndex <= 0) {
|
|
@@ -208,9 +393,24 @@ export function normalizeLastIndex(lastIndex) {
|
|
|
208
393
|
}
|
|
209
394
|
return Math.min(Math.floor(lastIndex), Number.MAX_SAFE_INTEGER);
|
|
210
395
|
}
|
|
211
|
-
function charactersEqual(left, right, ignoreCase) {
|
|
396
|
+
function charactersEqual(left, right, ignoreCase, unicode = false) {
|
|
397
|
+
if (unicode && ignoreCase && left !== undefined)
|
|
398
|
+
return new RegExp(`^\\u{${right.codePointAt(0).toString(16)}}$`, "iu").test(left);
|
|
212
399
|
return left !== undefined && foldCharacter(left, ignoreCase) === foldCharacter(right, ignoreCase);
|
|
213
400
|
}
|
|
401
|
+
export function advanceStringIndex(input, index, unicode) {
|
|
402
|
+
return index + (unicode && (input.codePointAt(index) ?? 0) > 0xffff ? 2 : 1);
|
|
403
|
+
}
|
|
404
|
+
function readCharacter(input, position, direction, unicode) {
|
|
405
|
+
let index = direction === 1 ? position : position - 1;
|
|
406
|
+
if (index < 0 || index >= input.length)
|
|
407
|
+
return undefined;
|
|
408
|
+
if (unicode && direction === -1 && index > 0 &&
|
|
409
|
+
input.charCodeAt(index) >= 0xdc00 && input.charCodeAt(index) <= 0xdfff &&
|
|
410
|
+
input.charCodeAt(index - 1) >= 0xd800 && input.charCodeAt(index - 1) <= 0xdbff)
|
|
411
|
+
index--;
|
|
412
|
+
return unicode ? String.fromCodePoint(input.codePointAt(index)) : input[index];
|
|
413
|
+
}
|
|
214
414
|
function foldCharacter(character, ignoreCase) {
|
|
215
415
|
if (!ignoreCase) {
|
|
216
416
|
return character;
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { type CompileScope } from "./compile-guard.js";
|
|
2
2
|
export type RegexFlags = {
|
|
3
|
+
hasIndices: boolean;
|
|
3
4
|
global: boolean;
|
|
5
|
+
sticky: boolean;
|
|
6
|
+
unicode: boolean;
|
|
7
|
+
unicodeSets: boolean;
|
|
4
8
|
ignoreCase: boolean;
|
|
5
9
|
multiline: boolean;
|
|
6
10
|
dotAll: boolean;
|
|
7
11
|
};
|
|
8
12
|
export type CharacterKind = "digit" | "word" | "space";
|
|
13
|
+
type RegexModifiers = Partial<Pick<RegexFlags, "ignoreCase" | "multiline" | "dotAll">>;
|
|
9
14
|
export type CharacterClassItem = {
|
|
10
15
|
type: "character";
|
|
11
16
|
value: string;
|
|
@@ -17,12 +22,35 @@ export type CharacterClassItem = {
|
|
|
17
22
|
type: "kind";
|
|
18
23
|
kind: CharacterKind;
|
|
19
24
|
negated: boolean;
|
|
25
|
+
} | {
|
|
26
|
+
type: "property";
|
|
27
|
+
value: string;
|
|
28
|
+
negated: boolean;
|
|
29
|
+
strings?: boolean;
|
|
30
|
+
} | {
|
|
31
|
+
type: "strings";
|
|
32
|
+
values: string[];
|
|
33
|
+
} | {
|
|
34
|
+
type: "set";
|
|
35
|
+
value: CharacterSet;
|
|
36
|
+
};
|
|
37
|
+
export type CharacterSet = {
|
|
38
|
+
negated: boolean;
|
|
39
|
+
items: CharacterClassItem[];
|
|
40
|
+
operation?: "intersection" | "subtraction";
|
|
41
|
+
strings?: boolean;
|
|
20
42
|
};
|
|
21
43
|
export type RegexNode = {
|
|
22
44
|
type: "empty";
|
|
23
45
|
} | {
|
|
24
46
|
type: "literal";
|
|
25
47
|
value: string;
|
|
48
|
+
} | {
|
|
49
|
+
type: "backreference";
|
|
50
|
+
index: number;
|
|
51
|
+
} | {
|
|
52
|
+
type: "namedBackreference";
|
|
53
|
+
name: string;
|
|
26
54
|
} | {
|
|
27
55
|
type: "dot";
|
|
28
56
|
} | {
|
|
@@ -31,11 +59,9 @@ export type RegexNode = {
|
|
|
31
59
|
} | {
|
|
32
60
|
type: "wordBoundary";
|
|
33
61
|
negated: boolean;
|
|
34
|
-
} | {
|
|
62
|
+
} | ({
|
|
35
63
|
type: "characterClass";
|
|
36
|
-
|
|
37
|
-
items: CharacterClassItem[];
|
|
38
|
-
} | {
|
|
64
|
+
} & CharacterSet) | {
|
|
39
65
|
type: "sequence";
|
|
40
66
|
elements: RegexNode[];
|
|
41
67
|
} | {
|
|
@@ -45,6 +71,16 @@ export type RegexNode = {
|
|
|
45
71
|
type: "group";
|
|
46
72
|
capturing: boolean;
|
|
47
73
|
index?: number;
|
|
74
|
+
name?: string;
|
|
75
|
+
modifiers?: RegexModifiers;
|
|
76
|
+
body: RegexNode;
|
|
77
|
+
} | {
|
|
78
|
+
type: "lookahead";
|
|
79
|
+
negated: boolean;
|
|
80
|
+
body: RegexNode;
|
|
81
|
+
} | {
|
|
82
|
+
type: "lookbehind";
|
|
83
|
+
negated: boolean;
|
|
48
84
|
body: RegexNode;
|
|
49
85
|
} | {
|
|
50
86
|
type: "quantifier";
|
|
@@ -58,5 +94,7 @@ export type RegexPattern = {
|
|
|
58
94
|
flags: RegexFlags;
|
|
59
95
|
captureCount: number;
|
|
60
96
|
body: RegexNode;
|
|
97
|
+
groups?: Record<string, number[]>;
|
|
61
98
|
};
|
|
62
99
|
export declare function parseRegex(source: string, flags?: string, compilation?: CompileScope, valueUnits?: number): RegexPattern;
|
|
100
|
+
export {};
|