@oseo/compiler 0.0.0-stage → 0.1.0-canary.368
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 +674 -0
- package/README.md +59 -2
- package/dist/agent-programs.d.ts +28 -0
- package/dist/agent-programs.d.ts.map +1 -0
- package/dist/agent-programs.js +134 -0
- package/dist/agent-programs.js.map +1 -0
- package/dist/agent-templates.d.ts +33 -0
- package/dist/agent-templates.d.ts.map +1 -0
- package/dist/agent-templates.js +45 -0
- package/dist/agent-templates.js.map +1 -0
- package/dist/compile.d.ts +20 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +75 -0
- package/dist/compile.js.map +1 -0
- package/dist/fragments.d.ts +72 -0
- package/dist/fragments.d.ts.map +1 -0
- package/dist/fragments.js +158 -0
- package/dist/fragments.js.map +1 -0
- package/dist/hir-build.d.ts +80 -0
- package/dist/hir-build.d.ts.map +1 -0
- package/dist/hir-build.js +3077 -0
- package/dist/hir-build.js.map +1 -0
- package/dist/hir-print.d.ts +10 -0
- package/dist/hir-print.d.ts.map +1 -0
- package/dist/hir-print.js +271 -0
- package/dist/hir-print.js.map +1 -0
- package/dist/hir.d.ts +1246 -0
- package/dist/hir.d.ts.map +1 -0
- package/dist/hir.js +147 -0
- package/dist/hir.js.map +1 -0
- package/dist/host-scripts.d.ts +14 -0
- package/dist/host-scripts.d.ts.map +1 -0
- package/dist/host-scripts.js +204 -0
- package/dist/host-scripts.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/mir-build.d.ts +8 -0
- package/dist/mir-build.d.ts.map +1 -0
- package/dist/mir-build.js +5113 -0
- package/dist/mir-build.js.map +1 -0
- package/dist/mir-print.d.ts +7 -0
- package/dist/mir-print.d.ts.map +1 -0
- package/dist/mir-print.js +65 -0
- package/dist/mir-print.js.map +1 -0
- package/dist/mir-specialize.d.ts +7 -0
- package/dist/mir-specialize.d.ts.map +1 -0
- package/dist/mir-specialize.js +182 -0
- package/dist/mir-specialize.js.map +1 -0
- package/dist/mir.d.ts +571 -0
- package/dist/mir.d.ts.map +1 -0
- package/dist/mir.js +1 -0
- package/dist/module-builtin.d.ts +20 -0
- package/dist/module-builtin.d.ts.map +1 -0
- package/dist/module-builtin.js +57 -0
- package/dist/module-builtin.js.map +1 -0
- package/dist/module-compile.d.ts +64 -0
- package/dist/module-compile.d.ts.map +1 -0
- package/dist/module-compile.js +690 -0
- package/dist/module-compile.js.map +1 -0
- package/dist/module-json.d.ts +7 -0
- package/dist/module-json.d.ts.map +1 -0
- package/dist/module-json.js +88 -0
- package/dist/module-json.js.map +1 -0
- package/dist/modules.d.ts +87 -0
- package/dist/modules.d.ts.map +1 -0
- package/dist/modules.js +734 -0
- package/dist/modules.js.map +1 -0
- package/dist/native-fragments.d.ts +36 -0
- package/dist/native-fragments.d.ts.map +1 -0
- package/dist/native-fragments.js +110 -0
- package/dist/native-fragments.js.map +1 -0
- package/dist/native.d.ts +282 -0
- package/dist/native.d.ts.map +1 -0
- package/dist/native.js +47 -0
- package/dist/native.js.map +1 -0
- package/dist/regexp-execute.d.ts +74 -0
- package/dist/regexp-execute.d.ts.map +1 -0
- package/dist/regexp-execute.js +480 -0
- package/dist/regexp-execute.js.map +1 -0
- package/dist/regexp-matcher.d.ts +279 -0
- package/dist/regexp-matcher.d.ts.map +1 -0
- package/dist/regexp-matcher.js +1098 -0
- package/dist/regexp-matcher.js.map +1 -0
- package/dist/regexp-parse.d.ts +22 -0
- package/dist/regexp-parse.d.ts.map +1 -0
- package/dist/regexp-parse.js +1440 -0
- package/dist/regexp-parse.js.map +1 -0
- package/dist/regexp-print.d.ts +24 -0
- package/dist/regexp-print.d.ts.map +1 -0
- package/dist/regexp-print.js +147 -0
- package/dist/regexp-print.js.map +1 -0
- package/dist/regexp.d.ts +324 -0
- package/dist/regexp.d.ts.map +1 -0
- package/dist/regexp.js +22 -0
- package/dist/regexp.js.map +1 -0
- package/dist/source.d.ts +38 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +10 -0
- package/dist/source.js.map +1 -0
- package/dist/syntax.d.ts +1232 -0
- package/dist/syntax.d.ts.map +1 -0
- package/dist/syntax.js +1 -0
- package/package.json +32 -3
|
@@ -0,0 +1,1098 @@
|
|
|
1
|
+
import { regExpUnicodeMode } from "./regexp.js";
|
|
2
|
+
|
|
3
|
+
//#region src/regexp-matcher.ts
|
|
4
|
+
function includePropertiesWhen(properties) {
|
|
5
|
+
return properties() ?? {};
|
|
6
|
+
}
|
|
7
|
+
/** The largest Unicode code point. */
|
|
8
|
+
const maximumCodePoint = 1114111;
|
|
9
|
+
/** One past the largest code point, the only non-code-point boundary. */
|
|
10
|
+
const codePointLimit = 1114112;
|
|
11
|
+
/** Whether one inversion list holds one code point. */
|
|
12
|
+
function setHas(set, codePoint) {
|
|
13
|
+
let low = 0;
|
|
14
|
+
let high = set.length;
|
|
15
|
+
while (low < high) {
|
|
16
|
+
const middle = low + high >> 1;
|
|
17
|
+
if ((set[middle] ?? 0) <= codePoint) low = middle + 1;
|
|
18
|
+
else high = middle;
|
|
19
|
+
}
|
|
20
|
+
return (low & 1) === 1;
|
|
21
|
+
}
|
|
22
|
+
/** One inclusive code-point range as an inversion list. */
|
|
23
|
+
function setOfRange(start, end) {
|
|
24
|
+
return start > end ? [] : [start, end + 1];
|
|
25
|
+
}
|
|
26
|
+
/** The union of two inversion lists. */
|
|
27
|
+
function setUnion(left, right) {
|
|
28
|
+
const boundaries = [];
|
|
29
|
+
let leftIndex = 0;
|
|
30
|
+
let rightIndex = 0;
|
|
31
|
+
let depth = 0;
|
|
32
|
+
while (leftIndex < left.length || rightIndex < right.length) {
|
|
33
|
+
const leftValue = left[leftIndex] ?? 1114113;
|
|
34
|
+
const rightValue = right[rightIndex] ?? 1114113;
|
|
35
|
+
const value = Math.min(leftValue, rightValue);
|
|
36
|
+
let opening = 0;
|
|
37
|
+
if (leftValue === value) {
|
|
38
|
+
opening += (leftIndex & 1) === 0 ? 1 : -1;
|
|
39
|
+
leftIndex += 1;
|
|
40
|
+
}
|
|
41
|
+
if (rightValue === value) {
|
|
42
|
+
opening += (rightIndex & 1) === 0 ? 1 : -1;
|
|
43
|
+
rightIndex += 1;
|
|
44
|
+
}
|
|
45
|
+
const previous = depth;
|
|
46
|
+
depth += opening;
|
|
47
|
+
if (previous === 0 && depth > 0) boundaries.push(value);
|
|
48
|
+
else if (previous > 0 && depth === 0) boundaries.push(value);
|
|
49
|
+
}
|
|
50
|
+
return boundaries;
|
|
51
|
+
}
|
|
52
|
+
/** Add one inversion list to a running union. */
|
|
53
|
+
function addSetUnion(slots, set) {
|
|
54
|
+
let carry = set;
|
|
55
|
+
for (let rank = 0;; rank += 1) {
|
|
56
|
+
const held = slots[rank];
|
|
57
|
+
if (held == null) {
|
|
58
|
+
slots[rank] = carry;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
slots[rank] = void 0;
|
|
62
|
+
carry = setUnion(held, carry);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** The union every slot of a running union holds together. */
|
|
66
|
+
function finishSetUnion(slots) {
|
|
67
|
+
let union = [];
|
|
68
|
+
for (const held of slots) if (held != null) union = setUnion(union, held);
|
|
69
|
+
return union;
|
|
70
|
+
}
|
|
71
|
+
/** The complement of one inversion list over the whole code-point range. */
|
|
72
|
+
function setComplement(set) {
|
|
73
|
+
const boundaries = [];
|
|
74
|
+
let position = 0;
|
|
75
|
+
for (let index = 0; index + 1 < set.length; index += 2) {
|
|
76
|
+
const start = set[index] ?? 0;
|
|
77
|
+
if (start > position) boundaries.push(position, start);
|
|
78
|
+
position = set[index + 1] ?? 0;
|
|
79
|
+
}
|
|
80
|
+
if (position < codePointLimit) boundaries.push(position, codePointLimit);
|
|
81
|
+
return boundaries;
|
|
82
|
+
}
|
|
83
|
+
function combineSets(left, right, include) {
|
|
84
|
+
const points = [.../* @__PURE__ */ new Set([
|
|
85
|
+
0,
|
|
86
|
+
...left,
|
|
87
|
+
...right,
|
|
88
|
+
codePointLimit
|
|
89
|
+
])].toSorted((first, second) => first - second);
|
|
90
|
+
const result = [];
|
|
91
|
+
let included = false;
|
|
92
|
+
for (let index = 0; index + 1 < points.length; index += 1) {
|
|
93
|
+
const point = points[index] ?? 0;
|
|
94
|
+
const next = include(setHas(left, point), setHas(right, point));
|
|
95
|
+
if (next !== included) result.push(point);
|
|
96
|
+
included = next;
|
|
97
|
+
}
|
|
98
|
+
if (included) result.push(codePointLimit);
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
/** The intersection of two inversion lists. */
|
|
102
|
+
function setIntersection(left, right) {
|
|
103
|
+
return combineSets(left, right, (inLeft, inRight) => inLeft && inRight);
|
|
104
|
+
}
|
|
105
|
+
/** The members of the first inversion list absent from the second. */
|
|
106
|
+
function setDifference(left, right) {
|
|
107
|
+
return combineSets(left, right, (inLeft, inRight) => inLeft && !inRight);
|
|
108
|
+
}
|
|
109
|
+
/** Build a normalized inversion list from unsorted inclusive ranges. */
|
|
110
|
+
function setOfRanges(ranges) {
|
|
111
|
+
const sorted = ranges.filter(([start, end]) => start <= end).toSorted((first, second) => first[0] - second[0]);
|
|
112
|
+
const boundaries = [];
|
|
113
|
+
for (const [start, end] of sorted) {
|
|
114
|
+
const last = boundaries.length - 1;
|
|
115
|
+
const previousEnd = boundaries[last];
|
|
116
|
+
if (previousEnd != null && start <= previousEnd) {
|
|
117
|
+
if (end + 1 > previousEnd) boundaries[last] = end + 1;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
boundaries.push(start, end + 1);
|
|
121
|
+
}
|
|
122
|
+
return boundaries;
|
|
123
|
+
}
|
|
124
|
+
/** The four code points the LineTerminator production names. */
|
|
125
|
+
const lineTerminators = setOfRanges([
|
|
126
|
+
[10, 10],
|
|
127
|
+
[13, 13],
|
|
128
|
+
[8232, 8233]
|
|
129
|
+
]);
|
|
130
|
+
/** The sixty-three characters `WordCharacters` calls basic. */
|
|
131
|
+
const basicWordCharacters = setOfRanges([
|
|
132
|
+
[48, 57],
|
|
133
|
+
[65, 90],
|
|
134
|
+
[95, 95],
|
|
135
|
+
[97, 122]
|
|
136
|
+
]);
|
|
137
|
+
/** The decimal digits `\d` names. */
|
|
138
|
+
const decimalDigits = setOfRange(48, 57);
|
|
139
|
+
/** The whitespace `\s` names apart from `General_Category=Space_Separator`. */
|
|
140
|
+
const namedWhiteSpace = setOfRanges([
|
|
141
|
+
[9, 9],
|
|
142
|
+
[11, 12],
|
|
143
|
+
[65279, 65279]
|
|
144
|
+
]);
|
|
145
|
+
/** The reviewed default artifact limits. */
|
|
146
|
+
const defaultRegExpMatcherLimits = {
|
|
147
|
+
instructions: 1048576,
|
|
148
|
+
registers: 262144
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Reject a boundary a caller supplied that is not a count.
|
|
152
|
+
*
|
|
153
|
+
* A limit that is not a nonnegative safe integer would silently disable
|
|
154
|
+
* the boundary it names, because every comparison against it is false.
|
|
155
|
+
* That is a caller defect rather than a pattern the unit cannot describe,
|
|
156
|
+
* so it throws instead of reporting a located pattern error.
|
|
157
|
+
*/
|
|
158
|
+
function checkRegExpMatcherBound(value, subject) {
|
|
159
|
+
if (!Number.isSafeInteger(value) || value < 0) throw new RangeError(`${subject} must be a nonnegative safe integer.`);
|
|
160
|
+
}
|
|
161
|
+
/** One rejected construct, thrown so a deep production keeps its span. */
|
|
162
|
+
var MatcherFailure = class extends Error {
|
|
163
|
+
error;
|
|
164
|
+
constructor(error) {
|
|
165
|
+
super(error.message);
|
|
166
|
+
this.error = error;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
function refuse(kind, message, span) {
|
|
170
|
+
throw new MatcherFailure({
|
|
171
|
+
kind,
|
|
172
|
+
message,
|
|
173
|
+
section: "pattern",
|
|
174
|
+
span
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/** The address a label holds until it is placed. */
|
|
178
|
+
const unplacedLabel = -1;
|
|
179
|
+
function newLabel(builder) {
|
|
180
|
+
builder.addresses.push(unplacedLabel);
|
|
181
|
+
return builder.addresses.length - 1;
|
|
182
|
+
}
|
|
183
|
+
function placeLabel(builder, label) {
|
|
184
|
+
builder.addresses[label] = builder.instructions.length;
|
|
185
|
+
}
|
|
186
|
+
function emit(builder, instruction, span) {
|
|
187
|
+
const cost = instruction.kind === "backreference" ? Math.max(instruction.slots.length, 1) : 1;
|
|
188
|
+
if (builder.lowered + cost > builder.limits.instructions) refuse("limit", "A pattern needs more matcher instructions than the reviewed limit.", span);
|
|
189
|
+
builder.lowered += cost;
|
|
190
|
+
builder.instructions.push(instruction);
|
|
191
|
+
}
|
|
192
|
+
function allocateRegisters(builder, count, span) {
|
|
193
|
+
if (builder.registers + count > builder.limits.registers) refuse("limit", "A pattern needs more matcher registers than the reviewed limit.", span);
|
|
194
|
+
const base = builder.registers;
|
|
195
|
+
builder.registers += count;
|
|
196
|
+
return base;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Intern one set, so equal sets share one artifact entry.
|
|
200
|
+
*
|
|
201
|
+
* The stored list is a frozen copy. A set may have arrived from the
|
|
202
|
+
* caller's Unicode tables, and an artifact must neither freeze a table the
|
|
203
|
+
* caller still owns nor keep a list the caller could rewrite later.
|
|
204
|
+
*/
|
|
205
|
+
function internSet(builder, set) {
|
|
206
|
+
const key = set.join(",");
|
|
207
|
+
const existing = builder.setIndices.get(key);
|
|
208
|
+
if (existing != null) return existing;
|
|
209
|
+
const index = builder.sets.length;
|
|
210
|
+
builder.sets.push(Object.freeze([...set]));
|
|
211
|
+
builder.setIndices.set(key, index);
|
|
212
|
+
return index;
|
|
213
|
+
}
|
|
214
|
+
function caseClasses(builder, span) {
|
|
215
|
+
const cached = builder.classes;
|
|
216
|
+
if (cached != null) return cached;
|
|
217
|
+
const provide = builder.unicodeData.caseEquivalenceClasses;
|
|
218
|
+
if (provide == null) refuse("unsupported", "An ignore-case pattern needs case folding data that is not linked.", span);
|
|
219
|
+
const classes = provide(builder.unicodeMode);
|
|
220
|
+
builder.classes = classes;
|
|
221
|
+
return classes;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Close one set under the pattern's canonicalization.
|
|
225
|
+
*
|
|
226
|
+
* `CharacterSetMatcher` compares the canonical form of the input character
|
|
227
|
+
* with the canonical form of every set member, so a set matches exactly
|
|
228
|
+
* the characters whose equivalence class meets it. Only a character with a
|
|
229
|
+
* case sibling can join a set it was not already in, so closing walks the
|
|
230
|
+
* equivalence classes rather than the whole code-point range.
|
|
231
|
+
*/
|
|
232
|
+
function closeSet(builder, set, span) {
|
|
233
|
+
if (!builder.ignoreCase) return set;
|
|
234
|
+
const added = [];
|
|
235
|
+
for (const members of caseClasses(builder, span)) {
|
|
236
|
+
if (!members.some((member) => setHas(set, member))) continue;
|
|
237
|
+
for (const member of members) if (!setHas(set, member)) added.push([member, member]);
|
|
238
|
+
}
|
|
239
|
+
return added.length === 0 ? set : setUnion(set, setOfRanges(added));
|
|
240
|
+
}
|
|
241
|
+
/** Map each cased character to one stable representative of its class. */
|
|
242
|
+
function caseRepresentatives(builder, span) {
|
|
243
|
+
const cached = builder.caseRepresentatives;
|
|
244
|
+
if (cached != null) return cached;
|
|
245
|
+
const representatives = /* @__PURE__ */ new Map();
|
|
246
|
+
for (const members of caseClasses(builder, span)) {
|
|
247
|
+
const representative = members.reduce((lowest, member) => Math.min(lowest, member), members[0] ?? 0);
|
|
248
|
+
for (const member of members) representatives.set(member, representative);
|
|
249
|
+
}
|
|
250
|
+
builder.caseRepresentatives = representatives;
|
|
251
|
+
return representatives;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* The `WordCharacters` of this pattern.
|
|
255
|
+
*
|
|
256
|
+
* The edition adds a character outside the basic sixty-three only when
|
|
257
|
+
* both `i` and a unicode-mode flag are set, and closing the basic set
|
|
258
|
+
* under the non-unicode case rule adds nothing, so one expression covers
|
|
259
|
+
* every flag combination.
|
|
260
|
+
*/
|
|
261
|
+
function wordCharacters(builder, span) {
|
|
262
|
+
return closeSet(builder, basicWordCharacters, span);
|
|
263
|
+
}
|
|
264
|
+
function spaceCharacters(builder, span) {
|
|
265
|
+
const separators = builder.unicodeData.spaceSeparators;
|
|
266
|
+
if (separators == null) refuse("unsupported", "A whitespace class escape needs Unicode category data that is not linked.", span);
|
|
267
|
+
return setUnion(setUnion(namedWhiteSpace, separators), lineTerminators);
|
|
268
|
+
}
|
|
269
|
+
/** The raw set one `\d`, `\s`, or `\w` escape names, before closing. */
|
|
270
|
+
function classEscapeSet(builder, set, span) {
|
|
271
|
+
if (set === "digit") return decimalDigits;
|
|
272
|
+
if (set === "space") return spaceCharacters(builder, span);
|
|
273
|
+
return wordCharacters(builder, span);
|
|
274
|
+
}
|
|
275
|
+
function propertyEscapeSet(builder, escape) {
|
|
276
|
+
const resolve = builder.unicodeData.propertySet;
|
|
277
|
+
if (resolve == null) refuse("unsupported", "A Unicode property escape needs property data that is not linked.", escape.span);
|
|
278
|
+
const resolved = resolve(escape);
|
|
279
|
+
if (resolved == null) refuse("unsupported", "This Unicode property names no code-point set.", escape.span);
|
|
280
|
+
if (!escape.negated) return resolved;
|
|
281
|
+
if (builder.unicodeSets && builder.ignoreCase) return setComplement(closeSet(builder, resolved, escape.span));
|
|
282
|
+
return setComplement(resolved);
|
|
283
|
+
}
|
|
284
|
+
/** The key two class strings share exactly when they hold the same run. */
|
|
285
|
+
function classStringKey(sequence) {
|
|
286
|
+
return sequence.join(",");
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Fold one class-set value into a running union of values.
|
|
290
|
+
*
|
|
291
|
+
* The edition holds a one-character `\q{...}` alternative as a code point
|
|
292
|
+
* rather than as a string, and holds each remaining alternative once. An
|
|
293
|
+
* alternative already in `unique` is left as it was written, so a repeated
|
|
294
|
+
* alternative keeps the spelling and the position of its first appearance,
|
|
295
|
+
* which is the order the class string trie is then built in.
|
|
296
|
+
*
|
|
297
|
+
* `slots` and `unique` are the accumulator: folding is written this way so
|
|
298
|
+
* that a union of many operands folds each operand a bounded number of
|
|
299
|
+
* times and releases it, rather than refolding everything it already
|
|
300
|
+
* holds once more for every operand that follows.
|
|
301
|
+
*/
|
|
302
|
+
function addClassValue(slots, unique, value) {
|
|
303
|
+
const single = [];
|
|
304
|
+
for (const sequence of value.strings) {
|
|
305
|
+
const character = sequence.length === 1 ? sequence[0] : void 0;
|
|
306
|
+
if (character == null) {
|
|
307
|
+
const key = classStringKey(sequence);
|
|
308
|
+
if (!unique.has(key)) unique.set(key, sequence);
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
single.push([character, character]);
|
|
312
|
+
}
|
|
313
|
+
addSetUnion(slots, value.characters);
|
|
314
|
+
if (single.length > 0) addSetUnion(slots, setOfRanges(single));
|
|
315
|
+
}
|
|
316
|
+
/** One class-set value with its one-character alternatives folded in. */
|
|
317
|
+
function normalizeClassValue(value) {
|
|
318
|
+
const slots = [];
|
|
319
|
+
const unique = /* @__PURE__ */ new Map();
|
|
320
|
+
addClassValue(slots, unique, value);
|
|
321
|
+
return {
|
|
322
|
+
characters: finishSetUnion(slots),
|
|
323
|
+
strings: [...unique.values()]
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
function propertyEscapeClassValue(builder, escape) {
|
|
327
|
+
if (builder.unicodeData.propertySet?.(escape) != null) return {
|
|
328
|
+
characters: propertyEscapeSet(builder, escape),
|
|
329
|
+
strings: []
|
|
330
|
+
};
|
|
331
|
+
const strings = builder.unicodeData.stringPropertySet?.(escape);
|
|
332
|
+
if (strings == null || escape.negated) refuse("unsupported", "A Unicode property of strings needs data that is not linked.", escape.span);
|
|
333
|
+
return normalizeClassValue({
|
|
334
|
+
characters: [],
|
|
335
|
+
strings
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Whether one escape is a lone General_Category value such as `\p{Ll}`.
|
|
340
|
+
*
|
|
341
|
+
* `CompileToCharSet` returns that spelling without folding it, while the
|
|
342
|
+
* `\p{gc=Ll}` spelling and every binary property end in
|
|
343
|
+
* `MaybeSimpleCaseFolding`. The asymmetry is deliberate and is only
|
|
344
|
+
* observable inside `v`-mode set algebra, where folding an operand before
|
|
345
|
+
* the operation removes members the edition keeps.
|
|
346
|
+
*
|
|
347
|
+
* The builder asks the question the edition asks, whether the name is a
|
|
348
|
+
* General_Category value, through the caller's own data. Holding a table of
|
|
349
|
+
* category names here would put Unicode knowledge in the compiler core,
|
|
350
|
+
* which the package boundary does not allow.
|
|
351
|
+
*
|
|
352
|
+
* The caller answers it directly rather than the builder inferring it from
|
|
353
|
+
* whether some other spelling resolves. A provider that resolves a lone
|
|
354
|
+
* escape but not the classification would otherwise fold an operand the
|
|
355
|
+
* edition keeps exact and match nothing, silently, where every other
|
|
356
|
+
* missing Unicode fact reports an owned refusal.
|
|
357
|
+
*/
|
|
358
|
+
function loneGeneralCategory(builder, escape) {
|
|
359
|
+
if (escape.value != null) return false;
|
|
360
|
+
const classify = builder.unicodeData.generalCategoryValue;
|
|
361
|
+
if (classify == null) refuse("unsupported", "A lone Unicode property escape needs the General_Category value classification that is not linked.", escape.span);
|
|
362
|
+
return classify(escape.property);
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Whether one operand keeps its exact code points through set algebra.
|
|
366
|
+
*
|
|
367
|
+
* The edition folds a `ClassSetCharacter` and a `ClassStringDisjunction`
|
|
368
|
+
* operand but returns a `NestedClass` operand unchanged, so a nested class
|
|
369
|
+
* carries whatever its own operands decided rather than being folded again.
|
|
370
|
+
* Closing a nested value that is already closed changes nothing, so this
|
|
371
|
+
* only matters for the one leaf the edition leaves raw.
|
|
372
|
+
*/
|
|
373
|
+
function preservesExactOperand(builder, operand) {
|
|
374
|
+
if (operand.kind === "class-set") return true;
|
|
375
|
+
return operand.kind === "unicode-property" && !operand.negated && loneGeneralCategory(builder, operand);
|
|
376
|
+
}
|
|
377
|
+
function classSetOperandValue(builder, operand) {
|
|
378
|
+
let value;
|
|
379
|
+
if (operand.kind === "class-set") value = classSetValue(builder, operand);
|
|
380
|
+
else if (operand.kind === "class-strings") value = normalizeClassValue({
|
|
381
|
+
characters: [],
|
|
382
|
+
strings: operand.strings.map((characters) => characters.map((character) => character.value))
|
|
383
|
+
});
|
|
384
|
+
else if (operand.kind === "unicode-property") value = propertyEscapeClassValue(builder, operand);
|
|
385
|
+
else value = {
|
|
386
|
+
characters: classItemSet(builder, operand),
|
|
387
|
+
strings: []
|
|
388
|
+
};
|
|
389
|
+
if (!builder.unicodeSets || !builder.ignoreCase) return value;
|
|
390
|
+
const representatives = caseRepresentatives(builder, operand.span);
|
|
391
|
+
return normalizeClassValue({
|
|
392
|
+
characters: preservesExactOperand(builder, operand) ? value.characters : closeSet(builder, value.characters, operand.span),
|
|
393
|
+
strings: value.strings.map((sequence) => sequence.map((character) => representatives.get(character) ?? character))
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
/** The first operand's value, or the empty value for no operand at all. */
|
|
397
|
+
function leadingClassValue(builder, operands) {
|
|
398
|
+
const first = operands[0];
|
|
399
|
+
if (first == null) return {
|
|
400
|
+
characters: [],
|
|
401
|
+
strings: []
|
|
402
|
+
};
|
|
403
|
+
return normalizeClassValue(classSetOperandValue(builder, first));
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* The union of every operand, accumulated as the operands resolve.
|
|
407
|
+
*
|
|
408
|
+
* Unioning two values at a time renormalized every alternative the
|
|
409
|
+
* accumulator already held, once per operand, which is the square of the
|
|
410
|
+
* operand count: an admitted one-mebibyte source can write more than a
|
|
411
|
+
* hundred thousand `\q{...}` operands into one union. Each operand is
|
|
412
|
+
* folded on its own instead, and the alternatives accumulate in a map
|
|
413
|
+
* keyed the way `normalizeClassValue` keys them, so a repeated alternative
|
|
414
|
+
* still keeps the position and spelling of its first appearance and the
|
|
415
|
+
* result is the value the pairwise fold produced.
|
|
416
|
+
*
|
|
417
|
+
* One operand is resolved at a time and released once it has been folded
|
|
418
|
+
* in, because a union of many operands that each name thousands of
|
|
419
|
+
* alternatives cannot hold every operand's value at once.
|
|
420
|
+
*/
|
|
421
|
+
function unionClassSet(builder, operands) {
|
|
422
|
+
const slots = [];
|
|
423
|
+
const unique = /* @__PURE__ */ new Map();
|
|
424
|
+
for (const operand of operands) addClassValue(slots, unique, classSetOperandValue(builder, operand));
|
|
425
|
+
return {
|
|
426
|
+
characters: finishSetUnion(slots),
|
|
427
|
+
strings: [...unique.values()]
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* The intersection of the first operand with every operand after it.
|
|
432
|
+
*
|
|
433
|
+
* An alternative of the first operand survives exactly when every later
|
|
434
|
+
* operand also holds it, which one counting pass decides. Re-filtering the
|
|
435
|
+
* survivors against each operand in turn would instead cost the first
|
|
436
|
+
* operand's size once per operand. Only an alternative the first operand
|
|
437
|
+
* wrote is counted, so the counts stay within its size however many
|
|
438
|
+
* operands follow.
|
|
439
|
+
*/
|
|
440
|
+
function intersectClassSet(builder, operands) {
|
|
441
|
+
const left = leadingClassValue(builder, operands);
|
|
442
|
+
const candidates = new Set(left.strings.map(classStringKey));
|
|
443
|
+
const holders = /* @__PURE__ */ new Map();
|
|
444
|
+
let characters = left.characters;
|
|
445
|
+
let rights = 0;
|
|
446
|
+
for (const [index, operand] of operands.entries()) {
|
|
447
|
+
if (index === 0) continue;
|
|
448
|
+
const value = classSetOperandValue(builder, operand);
|
|
449
|
+
characters = setIntersection(characters, value.characters);
|
|
450
|
+
for (const key of new Set(value.strings.map(classStringKey))) {
|
|
451
|
+
if (!candidates.has(key)) continue;
|
|
452
|
+
holders.set(key, (holders.get(key) ?? 0) + 1);
|
|
453
|
+
}
|
|
454
|
+
rights += 1;
|
|
455
|
+
}
|
|
456
|
+
return {
|
|
457
|
+
characters,
|
|
458
|
+
strings: left.strings.filter((value) => (holders.get(classStringKey(value)) ?? 0) === rights)
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* The first operand less every operand after it.
|
|
463
|
+
*
|
|
464
|
+
* Difference is left-associative, and removing the operands in turn
|
|
465
|
+
* removes exactly what removing their union removes, so an alternative
|
|
466
|
+
* leaves the survivors the first time a later operand writes it rather
|
|
467
|
+
* than being looked for again in every operand, which would cost the first
|
|
468
|
+
* operand's size once per operand. Only what the first operand wrote is
|
|
469
|
+
* tracked, so the survivors stay within its size however many alternatives
|
|
470
|
+
* the later operands name, and each of their values is released once it
|
|
471
|
+
* has been folded in.
|
|
472
|
+
*/
|
|
473
|
+
function subtractClassSet(builder, operands) {
|
|
474
|
+
const left = leadingClassValue(builder, operands);
|
|
475
|
+
const slots = [];
|
|
476
|
+
const surviving = new Set(left.strings.map(classStringKey));
|
|
477
|
+
for (const [index, operand] of operands.entries()) {
|
|
478
|
+
if (index === 0) continue;
|
|
479
|
+
const value = classSetOperandValue(builder, operand);
|
|
480
|
+
addSetUnion(slots, value.characters);
|
|
481
|
+
for (const sequence of value.strings) surviving.delete(classStringKey(sequence));
|
|
482
|
+
}
|
|
483
|
+
return {
|
|
484
|
+
characters: setDifference(left.characters, finishSetUnion(slots)),
|
|
485
|
+
strings: left.strings.filter((value) => surviving.has(classStringKey(value)))
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
function classSetValue(builder, node) {
|
|
489
|
+
let value;
|
|
490
|
+
if (node.operation === "union") value = unionClassSet(builder, node.operands);
|
|
491
|
+
else if (node.operation === "intersection") value = intersectClassSet(builder, node.operands);
|
|
492
|
+
else value = subtractClassSet(builder, node.operands);
|
|
493
|
+
if (!node.negated) return value;
|
|
494
|
+
if (value.strings.length > 0) refuse("unsupported", "A negated class set cannot contain strings.", node.span);
|
|
495
|
+
return {
|
|
496
|
+
characters: setComplement(builder.unicodeSets && builder.ignoreCase ? closeSet(builder, value.characters, node.span) : value.characters),
|
|
497
|
+
strings: []
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
/** The raw set one class member names, before the class is closed. */
|
|
501
|
+
function classItemSet(builder, item) {
|
|
502
|
+
if (item.kind === "character") return setOfRange(item.value, item.value);
|
|
503
|
+
if (item.kind === "range") return setOfRange(item.start.value, item.end.value);
|
|
504
|
+
if (item.kind === "class-escape") {
|
|
505
|
+
const named = classEscapeSet(builder, item.set, item.span);
|
|
506
|
+
return item.negated ? setComplement(named) : named;
|
|
507
|
+
}
|
|
508
|
+
return propertyEscapeSet(builder, item);
|
|
509
|
+
}
|
|
510
|
+
function characterClassSet(builder, node) {
|
|
511
|
+
let union = [];
|
|
512
|
+
for (const item of node.items) union = setUnion(union, classItemSet(builder, item));
|
|
513
|
+
return union;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* The set one consuming atom matches, or undefined for another atom.
|
|
517
|
+
*
|
|
518
|
+
* Closing comes before negation because `CharacterSetMatcher` inverts a
|
|
519
|
+
* class after canonicalizing its members: `[^a]` under `i` rejects `A`,
|
|
520
|
+
* while closing the complement instead would accept it.
|
|
521
|
+
*/
|
|
522
|
+
function characterAtomSet(builder, atom) {
|
|
523
|
+
if (atom.kind === "character") return closeSet(builder, setOfRange(atom.value, atom.value), atom.span);
|
|
524
|
+
if (atom.kind === "dot") return closeSet(builder, builder.dotAll ? setOfRange(0, maximumCodePoint) : setComplement(lineTerminators), atom.span);
|
|
525
|
+
if (atom.kind === "class-escape") {
|
|
526
|
+
const named = classEscapeSet(builder, atom.set, atom.span);
|
|
527
|
+
return closeSet(builder, atom.negated ? setComplement(named) : named, atom.span);
|
|
528
|
+
}
|
|
529
|
+
if (atom.kind === "unicode-property") {
|
|
530
|
+
if (builder.unicodeData.propertySet?.(atom) == null && builder.unicodeData.stringPropertySet?.(atom) != null) return;
|
|
531
|
+
return closeSet(builder, propertyEscapeSet(builder, atom), atom.span);
|
|
532
|
+
}
|
|
533
|
+
if (atom.kind === "class-set") return void 0;
|
|
534
|
+
if (atom.kind !== "character-class") return void 0;
|
|
535
|
+
const closed = closeSet(builder, characterClassSet(builder, atom), atom.span);
|
|
536
|
+
return atom.negated ? setComplement(closed) : closed;
|
|
537
|
+
}
|
|
538
|
+
function emitClassValueBranch(builder, sequence, characters, backward, span) {
|
|
539
|
+
if (characters) {
|
|
540
|
+
emit(builder, {
|
|
541
|
+
backward,
|
|
542
|
+
kind: "consume",
|
|
543
|
+
set: internSet(builder, closeSet(builder, sequence, span))
|
|
544
|
+
}, span);
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
const ordered = backward ? sequence.toReversed() : sequence;
|
|
548
|
+
for (const character of ordered) emit(builder, {
|
|
549
|
+
backward,
|
|
550
|
+
kind: "consume",
|
|
551
|
+
set: internSet(builder, closeSet(builder, setOfRange(character, character), span))
|
|
552
|
+
}, span);
|
|
553
|
+
}
|
|
554
|
+
function classStringTrie(sequences, backward) {
|
|
555
|
+
const root = {
|
|
556
|
+
children: /* @__PURE__ */ new Map(),
|
|
557
|
+
terminal: false
|
|
558
|
+
};
|
|
559
|
+
for (const source of sequences.toSorted((first, second) => second.length - first.length)) {
|
|
560
|
+
const sequence = backward ? source.toReversed() : source;
|
|
561
|
+
let node = root;
|
|
562
|
+
for (const character of sequence) {
|
|
563
|
+
const child = node.children.get(character) ?? {
|
|
564
|
+
children: /* @__PURE__ */ new Map(),
|
|
565
|
+
terminal: false
|
|
566
|
+
};
|
|
567
|
+
node.children.set(character, child);
|
|
568
|
+
node = child;
|
|
569
|
+
}
|
|
570
|
+
node.terminal = true;
|
|
571
|
+
}
|
|
572
|
+
return root;
|
|
573
|
+
}
|
|
574
|
+
function emitClassAlternatives(builder, branches, span) {
|
|
575
|
+
if (branches.length === 0) return;
|
|
576
|
+
const end = newLabel(builder);
|
|
577
|
+
for (const [index, branch] of branches.entries()) {
|
|
578
|
+
if (index + 1 === branches.length) {
|
|
579
|
+
branch();
|
|
580
|
+
break;
|
|
581
|
+
}
|
|
582
|
+
const preferred = newLabel(builder);
|
|
583
|
+
const next = newLabel(builder);
|
|
584
|
+
emit(builder, {
|
|
585
|
+
alternative: next,
|
|
586
|
+
kind: "fork",
|
|
587
|
+
preferred
|
|
588
|
+
}, span);
|
|
589
|
+
placeLabel(builder, preferred);
|
|
590
|
+
branch();
|
|
591
|
+
emit(builder, {
|
|
592
|
+
kind: "jump",
|
|
593
|
+
target: end
|
|
594
|
+
}, span);
|
|
595
|
+
placeLabel(builder, next);
|
|
596
|
+
}
|
|
597
|
+
placeLabel(builder, end);
|
|
598
|
+
}
|
|
599
|
+
function emitClassStringTrie(builder, root, backward, span) {
|
|
600
|
+
const steps = [{
|
|
601
|
+
kind: "node",
|
|
602
|
+
node: root
|
|
603
|
+
}];
|
|
604
|
+
for (;;) {
|
|
605
|
+
const step = steps.pop();
|
|
606
|
+
if (step == null) return;
|
|
607
|
+
if (step.kind === "consume") {
|
|
608
|
+
emitClassValueBranch(builder, [step.character], false, backward, span);
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
if (step.kind === "finish") {
|
|
612
|
+
if (step.jump != null) emit(builder, {
|
|
613
|
+
kind: "jump",
|
|
614
|
+
target: step.jump
|
|
615
|
+
}, span);
|
|
616
|
+
placeLabel(builder, step.label);
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
if (step.kind === "node") {
|
|
620
|
+
const entries = [...step.node.children];
|
|
621
|
+
const terminal = step.node.terminal;
|
|
622
|
+
if (entries.length === 0 && !terminal) continue;
|
|
623
|
+
steps.push({
|
|
624
|
+
end: newLabel(builder),
|
|
625
|
+
entries,
|
|
626
|
+
index: 0,
|
|
627
|
+
kind: "branch",
|
|
628
|
+
terminal
|
|
629
|
+
});
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
const { end, entries, index, terminal } = step;
|
|
633
|
+
if (index + 1 === entries.length + (terminal ? 1 : 0)) steps.push({
|
|
634
|
+
jump: void 0,
|
|
635
|
+
kind: "finish",
|
|
636
|
+
label: end
|
|
637
|
+
});
|
|
638
|
+
else {
|
|
639
|
+
const preferred = newLabel(builder);
|
|
640
|
+
const next = newLabel(builder);
|
|
641
|
+
emit(builder, {
|
|
642
|
+
alternative: next,
|
|
643
|
+
kind: "fork",
|
|
644
|
+
preferred
|
|
645
|
+
}, span);
|
|
646
|
+
placeLabel(builder, preferred);
|
|
647
|
+
steps.push({
|
|
648
|
+
...step,
|
|
649
|
+
index: index + 1
|
|
650
|
+
});
|
|
651
|
+
steps.push({
|
|
652
|
+
jump: end,
|
|
653
|
+
kind: "finish",
|
|
654
|
+
label: next
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
const entry = entries[index];
|
|
658
|
+
if (entry != null) {
|
|
659
|
+
steps.push({
|
|
660
|
+
kind: "node",
|
|
661
|
+
node: entry[1]
|
|
662
|
+
});
|
|
663
|
+
steps.push({
|
|
664
|
+
character: entry[0],
|
|
665
|
+
kind: "consume"
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
function buildClassValue(builder, value, backward, span) {
|
|
671
|
+
if (value.strings.length === 0 && value.characters.length === 0) {
|
|
672
|
+
emitClassValueBranch(builder, [], true, backward, span);
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
const branches = [];
|
|
676
|
+
const nonEmptyStrings = value.strings.filter((sequence) => sequence.length > 0);
|
|
677
|
+
if (nonEmptyStrings.length > 0) {
|
|
678
|
+
const trie = classStringTrie(nonEmptyStrings, backward);
|
|
679
|
+
branches.push(() => emitClassStringTrie(builder, trie, backward, span));
|
|
680
|
+
}
|
|
681
|
+
if (value.characters.length > 0) branches.push(() => emitClassValueBranch(builder, value.characters, true, backward, span));
|
|
682
|
+
if (value.strings.some((sequence) => sequence.length === 0)) branches.push(() => void 0);
|
|
683
|
+
emitClassAlternatives(builder, branches, span);
|
|
684
|
+
}
|
|
685
|
+
/** The lowest and highest capture index one atom encloses. */
|
|
686
|
+
function captureBounds(atom) {
|
|
687
|
+
let first;
|
|
688
|
+
let last;
|
|
689
|
+
const note = (index) => {
|
|
690
|
+
first = first == null ? index : Math.min(first, index);
|
|
691
|
+
last = last == null ? index : Math.max(last, index);
|
|
692
|
+
};
|
|
693
|
+
const visitDisjunction = (node) => {
|
|
694
|
+
for (const alternative of node.alternatives) for (const term of alternative.terms) visitTerm(term);
|
|
695
|
+
};
|
|
696
|
+
const visitAtom = (node) => {
|
|
697
|
+
if (node.kind === "capturing-group") {
|
|
698
|
+
note(node.index);
|
|
699
|
+
visitDisjunction(node.body);
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
if (node.kind === "group" || node.kind === "modifier-group") visitDisjunction(node.body);
|
|
703
|
+
};
|
|
704
|
+
const visitTerm = (term) => {
|
|
705
|
+
if (term.kind === "assertion") return;
|
|
706
|
+
if (term.kind === "lookaround") {
|
|
707
|
+
visitDisjunction(term.body);
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
visitAtom(term.kind === "quantified" ? term.atom : term);
|
|
711
|
+
};
|
|
712
|
+
visitAtom(atom);
|
|
713
|
+
if (first == null || last == null) return void 0;
|
|
714
|
+
return [first, last];
|
|
715
|
+
}
|
|
716
|
+
function buildAtom(builder, atom, backward) {
|
|
717
|
+
const set = characterAtomSet(builder, atom);
|
|
718
|
+
if (set != null) {
|
|
719
|
+
emit(builder, {
|
|
720
|
+
backward,
|
|
721
|
+
kind: "consume",
|
|
722
|
+
set: internSet(builder, set)
|
|
723
|
+
}, atom.span);
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
if (atom.kind === "class-set") {
|
|
727
|
+
buildClassValue(builder, classSetValue(builder, atom), backward, atom.span);
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
if (atom.kind === "unicode-property") {
|
|
731
|
+
buildClassValue(builder, propertyEscapeClassValue(builder, atom), backward, atom.span);
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
if (atom.kind === "capturing-group") {
|
|
735
|
+
const start = 2 * atom.index;
|
|
736
|
+
const end = start + 1;
|
|
737
|
+
emit(builder, {
|
|
738
|
+
kind: "save",
|
|
739
|
+
slot: backward ? end : start
|
|
740
|
+
}, atom.span);
|
|
741
|
+
buildDisjunction(builder, atom.body, backward);
|
|
742
|
+
emit(builder, {
|
|
743
|
+
kind: "save",
|
|
744
|
+
slot: backward ? start : end
|
|
745
|
+
}, atom.span);
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
if (atom.kind === "group") {
|
|
749
|
+
buildDisjunction(builder, atom.body, backward);
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
if (atom.kind === "modifier-group") {
|
|
753
|
+
const dotAll = builder.dotAll;
|
|
754
|
+
const ignoreCase = builder.ignoreCase;
|
|
755
|
+
const multiline = builder.multiline;
|
|
756
|
+
builder.dotAll = atom.enabled.includes("s") || dotAll && !atom.disabled.includes("s");
|
|
757
|
+
builder.ignoreCase = atom.enabled.includes("i") || ignoreCase && !atom.disabled.includes("i");
|
|
758
|
+
builder.multiline = atom.enabled.includes("m") || multiline && !atom.disabled.includes("m");
|
|
759
|
+
buildDisjunction(builder, atom.body, backward);
|
|
760
|
+
builder.dotAll = dotAll;
|
|
761
|
+
builder.ignoreCase = ignoreCase;
|
|
762
|
+
builder.multiline = multiline;
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
if (atom.kind === "backreference" || atom.kind === "named-backreference") {
|
|
766
|
+
const slots = atom.kind === "backreference" ? [2 * atom.index] : atom.indices.map((index) => 2 * index);
|
|
767
|
+
emit(builder, {
|
|
768
|
+
backward,
|
|
769
|
+
ignoreCase: builder.ignoreCase,
|
|
770
|
+
kind: "backreference",
|
|
771
|
+
slots
|
|
772
|
+
}, atom.span);
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
refuse("unsupported", "This pattern atom has no matcher lowering yet.", atom.span);
|
|
776
|
+
}
|
|
777
|
+
function buildQuantified(builder, term, backward) {
|
|
778
|
+
const counter = allocateRegisters(builder, 2, term.span);
|
|
779
|
+
const position = counter + 1;
|
|
780
|
+
const bounds = captureBounds(term.atom);
|
|
781
|
+
const head = newLabel(builder);
|
|
782
|
+
const enter = newLabel(builder);
|
|
783
|
+
const body = newLabel(builder);
|
|
784
|
+
const exit = newLabel(builder);
|
|
785
|
+
emit(builder, {
|
|
786
|
+
counter,
|
|
787
|
+
kind: "repeat-init"
|
|
788
|
+
}, term.span);
|
|
789
|
+
placeLabel(builder, head);
|
|
790
|
+
emit(builder, {
|
|
791
|
+
counter,
|
|
792
|
+
enter,
|
|
793
|
+
exit,
|
|
794
|
+
greedy: term.quantifier.greedy,
|
|
795
|
+
kind: "repeat",
|
|
796
|
+
maximum: term.quantifier.maximum,
|
|
797
|
+
minimum: term.quantifier.minimum
|
|
798
|
+
}, term.span);
|
|
799
|
+
placeLabel(builder, enter);
|
|
800
|
+
emit(builder, {
|
|
801
|
+
body,
|
|
802
|
+
clearFrom: bounds == null ? 0 : 2 * bounds[0],
|
|
803
|
+
clearTo: bounds == null ? 0 : 2 * bounds[1] + 2,
|
|
804
|
+
counter,
|
|
805
|
+
kind: "repeat-enter",
|
|
806
|
+
position
|
|
807
|
+
}, term.span);
|
|
808
|
+
placeLabel(builder, body);
|
|
809
|
+
buildAtom(builder, term.atom, backward);
|
|
810
|
+
emit(builder, {
|
|
811
|
+
counter,
|
|
812
|
+
head,
|
|
813
|
+
kind: "repeat-end",
|
|
814
|
+
minimum: term.quantifier.minimum,
|
|
815
|
+
position
|
|
816
|
+
}, term.span);
|
|
817
|
+
placeLabel(builder, exit);
|
|
818
|
+
}
|
|
819
|
+
function buildAssertion(builder, term) {
|
|
820
|
+
if (term.assertion === "start" || term.assertion === "end") {
|
|
821
|
+
emit(builder, {
|
|
822
|
+
assertion: term.assertion,
|
|
823
|
+
kind: "edge",
|
|
824
|
+
multiline: builder.multiline
|
|
825
|
+
}, term.span);
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
const set = builder.wordSets.get(builder.ignoreCase) ?? internSet(builder, wordCharacters(builder, term.span));
|
|
829
|
+
builder.wordSets.set(builder.ignoreCase, set);
|
|
830
|
+
emit(builder, {
|
|
831
|
+
kind: "boundary",
|
|
832
|
+
negated: term.assertion === "non-word-boundary",
|
|
833
|
+
set
|
|
834
|
+
}, term.span);
|
|
835
|
+
}
|
|
836
|
+
function buildLookaround(builder, term) {
|
|
837
|
+
const frame = allocateRegisters(builder, 1, term.span);
|
|
838
|
+
const body = newLabel(builder);
|
|
839
|
+
const exit = newLabel(builder);
|
|
840
|
+
emit(builder, {
|
|
841
|
+
body,
|
|
842
|
+
frame,
|
|
843
|
+
kind: "look-start",
|
|
844
|
+
negated: term.negated,
|
|
845
|
+
onFail: term.negated ? exit : builder.failLabel
|
|
846
|
+
}, term.span);
|
|
847
|
+
placeLabel(builder, body);
|
|
848
|
+
buildDisjunction(builder, term.body, term.direction === "behind");
|
|
849
|
+
emit(builder, {
|
|
850
|
+
exit,
|
|
851
|
+
frame,
|
|
852
|
+
kind: "look-end",
|
|
853
|
+
negated: term.negated
|
|
854
|
+
}, term.span);
|
|
855
|
+
placeLabel(builder, exit);
|
|
856
|
+
}
|
|
857
|
+
function buildTerm(builder, term, backward) {
|
|
858
|
+
if (term.kind === "assertion") {
|
|
859
|
+
buildAssertion(builder, term);
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
if (term.kind === "lookaround") {
|
|
863
|
+
buildLookaround(builder, term);
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
if (term.kind === "quantified") {
|
|
867
|
+
buildQuantified(builder, term, backward);
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
buildAtom(builder, term, backward);
|
|
871
|
+
}
|
|
872
|
+
/**
|
|
873
|
+
* Emit one alternative in evaluation order.
|
|
874
|
+
*
|
|
875
|
+
* A backward alternative evaluates its rightmost term first, which is what
|
|
876
|
+
* makes a lookbehind body read toward the start of the input.
|
|
877
|
+
*/
|
|
878
|
+
function buildAlternative(builder, alternative, backward) {
|
|
879
|
+
const terms = backward ? alternative.terms.toReversed() : alternative.terms;
|
|
880
|
+
for (const term of terms) buildTerm(builder, term, backward);
|
|
881
|
+
}
|
|
882
|
+
function buildDisjunction(builder, disjunction, backward) {
|
|
883
|
+
const alternatives = disjunction.alternatives;
|
|
884
|
+
const last = alternatives.length - 1;
|
|
885
|
+
if (last === 0) {
|
|
886
|
+
const only = alternatives[0];
|
|
887
|
+
if (only != null) buildAlternative(builder, only, backward);
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
const end = newLabel(builder);
|
|
891
|
+
for (const [index, alternative] of alternatives.entries()) {
|
|
892
|
+
if (index === last) {
|
|
893
|
+
buildAlternative(builder, alternative, backward);
|
|
894
|
+
break;
|
|
895
|
+
}
|
|
896
|
+
const preferred = newLabel(builder);
|
|
897
|
+
const next = newLabel(builder);
|
|
898
|
+
emit(builder, {
|
|
899
|
+
alternative: next,
|
|
900
|
+
kind: "fork",
|
|
901
|
+
preferred
|
|
902
|
+
}, alternative.span);
|
|
903
|
+
placeLabel(builder, preferred);
|
|
904
|
+
buildAlternative(builder, alternative, backward);
|
|
905
|
+
emit(builder, {
|
|
906
|
+
kind: "jump",
|
|
907
|
+
target: end
|
|
908
|
+
}, alternative.span);
|
|
909
|
+
placeLabel(builder, next);
|
|
910
|
+
}
|
|
911
|
+
placeLabel(builder, end);
|
|
912
|
+
}
|
|
913
|
+
/** Rewrite every label reference into the address it was placed at. */
|
|
914
|
+
function resolveTargets(instruction, addresses) {
|
|
915
|
+
const address = (label) => addresses[label] ?? 0;
|
|
916
|
+
if (instruction.kind === "fork") return {
|
|
917
|
+
alternative: address(instruction.alternative),
|
|
918
|
+
kind: "fork",
|
|
919
|
+
preferred: address(instruction.preferred)
|
|
920
|
+
};
|
|
921
|
+
if (instruction.kind === "jump") return {
|
|
922
|
+
kind: "jump",
|
|
923
|
+
target: address(instruction.target)
|
|
924
|
+
};
|
|
925
|
+
if (instruction.kind === "repeat") return {
|
|
926
|
+
...instruction,
|
|
927
|
+
enter: address(instruction.enter),
|
|
928
|
+
exit: address(instruction.exit)
|
|
929
|
+
};
|
|
930
|
+
if (instruction.kind === "repeat-enter") return {
|
|
931
|
+
...instruction,
|
|
932
|
+
body: address(instruction.body)
|
|
933
|
+
};
|
|
934
|
+
if (instruction.kind === "repeat-end") return {
|
|
935
|
+
...instruction,
|
|
936
|
+
head: address(instruction.head)
|
|
937
|
+
};
|
|
938
|
+
if (instruction.kind === "look-start") return {
|
|
939
|
+
...instruction,
|
|
940
|
+
body: address(instruction.body),
|
|
941
|
+
onFail: address(instruction.onFail)
|
|
942
|
+
};
|
|
943
|
+
if (instruction.kind === "look-end") return {
|
|
944
|
+
...instruction,
|
|
945
|
+
exit: address(instruction.exit)
|
|
946
|
+
};
|
|
947
|
+
return instruction;
|
|
948
|
+
}
|
|
949
|
+
/** Record every character whose canonical form is another character. */
|
|
950
|
+
function canonicalizationTable(classes) {
|
|
951
|
+
const pairs = [];
|
|
952
|
+
for (const members of classes) {
|
|
953
|
+
if (members.length < 2) continue;
|
|
954
|
+
const representative = members.reduce((lowest, member) => Math.min(lowest, member), members[0] ?? 0);
|
|
955
|
+
for (const member of members) if (member !== representative) pairs.push([member, representative]);
|
|
956
|
+
}
|
|
957
|
+
const sorted = pairs.toSorted((first, second) => first[0] - second[0]);
|
|
958
|
+
return {
|
|
959
|
+
canonical: sorted.map(([, canonical]) => canonical),
|
|
960
|
+
characters: sorted.map(([character]) => character)
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
/** Whether one artifact compares two input characters under `i`. */
|
|
964
|
+
function comparesCharacters(instructions) {
|
|
965
|
+
return instructions.some((instruction) => instruction.kind === "backreference" && instruction.ignoreCase);
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* Copy the pattern metadata one artifact keeps.
|
|
969
|
+
*
|
|
970
|
+
* An artifact must not alias a value its caller still owns. Freezing the
|
|
971
|
+
* caller's arrays would change the pattern the caller passed in, and
|
|
972
|
+
* sharing them would let a later write to that pattern change how the
|
|
973
|
+
* artifact matches, which is the whole of the immutability contract.
|
|
974
|
+
*/
|
|
975
|
+
function ownedFlags(flags) {
|
|
976
|
+
return Object.freeze({
|
|
977
|
+
dotAll: flags.dotAll,
|
|
978
|
+
global: flags.global,
|
|
979
|
+
hasIndices: flags.hasIndices,
|
|
980
|
+
ignoreCase: flags.ignoreCase,
|
|
981
|
+
multiline: flags.multiline,
|
|
982
|
+
sticky: flags.sticky,
|
|
983
|
+
text: flags.text,
|
|
984
|
+
unicode: flags.unicode,
|
|
985
|
+
unicodeSets: flags.unicodeSets
|
|
986
|
+
});
|
|
987
|
+
}
|
|
988
|
+
function ownedCaptures(captures) {
|
|
989
|
+
return Object.freeze(captures.map((capture) => Object.freeze({
|
|
990
|
+
index: capture.index,
|
|
991
|
+
span: Object.freeze({
|
|
992
|
+
end: capture.span.end,
|
|
993
|
+
start: capture.span.start
|
|
994
|
+
}),
|
|
995
|
+
...includePropertiesWhen(() => {
|
|
996
|
+
if (capture.name == null) return void 0;
|
|
997
|
+
return { name: capture.name };
|
|
998
|
+
})
|
|
999
|
+
})));
|
|
1000
|
+
}
|
|
1001
|
+
function freezeProgram(program) {
|
|
1002
|
+
for (const instruction of program.instructions) {
|
|
1003
|
+
if (instruction.kind === "backreference") Object.freeze(instruction.slots);
|
|
1004
|
+
Object.freeze(instruction);
|
|
1005
|
+
}
|
|
1006
|
+
Object.freeze(program.sets);
|
|
1007
|
+
Object.freeze(program.instructions);
|
|
1008
|
+
const table = program.canonicalization;
|
|
1009
|
+
if (table != null) {
|
|
1010
|
+
Object.freeze(table.canonical);
|
|
1011
|
+
Object.freeze(table.characters);
|
|
1012
|
+
Object.freeze(table);
|
|
1013
|
+
}
|
|
1014
|
+
return Object.freeze(program);
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* Build the generic matcher artifact of one validated pattern.
|
|
1018
|
+
*
|
|
1019
|
+
* Every construct the pattern parser admits either compiles to defined
|
|
1020
|
+
* behavior here or is refused with a located error. Nothing is
|
|
1021
|
+
* approximated: a construct whose matcher semantics this unit does not own
|
|
1022
|
+
* and a Unicode fact its caller did not supply both stop the build rather
|
|
1023
|
+
* than producing an artifact that matches something else.
|
|
1024
|
+
*/
|
|
1025
|
+
function buildRegExpMatcher(pattern, options = {}) {
|
|
1026
|
+
const limits = options.limits ?? defaultRegExpMatcherLimits;
|
|
1027
|
+
checkRegExpMatcherBound(limits.instructions, "An instruction limit");
|
|
1028
|
+
checkRegExpMatcherBound(limits.registers, "A register limit");
|
|
1029
|
+
const flags = pattern.flags;
|
|
1030
|
+
const builder = {
|
|
1031
|
+
addresses: [unplacedLabel],
|
|
1032
|
+
caseRepresentatives: void 0,
|
|
1033
|
+
classes: void 0,
|
|
1034
|
+
dotAll: flags.dotAll,
|
|
1035
|
+
failLabel: 0,
|
|
1036
|
+
ignoreCase: flags.ignoreCase,
|
|
1037
|
+
instructions: [],
|
|
1038
|
+
limits,
|
|
1039
|
+
lowered: 0,
|
|
1040
|
+
multiline: flags.multiline,
|
|
1041
|
+
registers: 2 * (pattern.captures.length + 1),
|
|
1042
|
+
setIndices: /* @__PURE__ */ new Map(),
|
|
1043
|
+
sets: [],
|
|
1044
|
+
unicodeData: options.unicodeData ?? {},
|
|
1045
|
+
unicodeMode: regExpUnicodeMode(flags),
|
|
1046
|
+
unicodeSets: flags.unicodeSets,
|
|
1047
|
+
wordSets: /* @__PURE__ */ new Map()
|
|
1048
|
+
};
|
|
1049
|
+
const whole = {
|
|
1050
|
+
end: pattern.source.length,
|
|
1051
|
+
start: 0
|
|
1052
|
+
};
|
|
1053
|
+
try {
|
|
1054
|
+
if (builder.registers > limits.registers) refuse("limit", "A pattern needs more matcher registers than the reviewed limit.", whole);
|
|
1055
|
+
emit(builder, {
|
|
1056
|
+
kind: "save",
|
|
1057
|
+
slot: 0
|
|
1058
|
+
}, whole);
|
|
1059
|
+
buildDisjunction(builder, pattern.body, false);
|
|
1060
|
+
emit(builder, {
|
|
1061
|
+
kind: "save",
|
|
1062
|
+
slot: 1
|
|
1063
|
+
}, whole);
|
|
1064
|
+
emit(builder, { kind: "accept" }, whole);
|
|
1065
|
+
placeLabel(builder, builder.failLabel);
|
|
1066
|
+
emit(builder, { kind: "fail" }, whole);
|
|
1067
|
+
const instructions = builder.instructions.map((instruction) => resolveTargets(instruction, builder.addresses));
|
|
1068
|
+
return {
|
|
1069
|
+
built: true,
|
|
1070
|
+
errors: [],
|
|
1071
|
+
program: freezeProgram({
|
|
1072
|
+
captures: ownedCaptures(pattern.captures),
|
|
1073
|
+
flags: ownedFlags(flags),
|
|
1074
|
+
groupNames: Object.freeze([...pattern.groupNames]),
|
|
1075
|
+
instructions,
|
|
1076
|
+
kind: "matcher",
|
|
1077
|
+
registers: builder.registers,
|
|
1078
|
+
sets: builder.sets,
|
|
1079
|
+
source: pattern.source,
|
|
1080
|
+
unicodeMode: builder.unicodeMode,
|
|
1081
|
+
...includePropertiesWhen(() => {
|
|
1082
|
+
if (!comparesCharacters(instructions)) return void 0;
|
|
1083
|
+
return { canonicalization: canonicalizationTable(caseClasses(builder, whole)) };
|
|
1084
|
+
})
|
|
1085
|
+
})
|
|
1086
|
+
};
|
|
1087
|
+
} catch (error) {
|
|
1088
|
+
if (error instanceof MatcherFailure) return {
|
|
1089
|
+
built: false,
|
|
1090
|
+
errors: [error.error]
|
|
1091
|
+
};
|
|
1092
|
+
throw error;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
//#endregion
|
|
1097
|
+
export { buildRegExpMatcher, checkRegExpMatcherBound, defaultRegExpMatcherLimits };
|
|
1098
|
+
//# sourceMappingURL=regexp-matcher.js.map
|