@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,1440 @@
|
|
|
1
|
+
import { defaultRegExpPatternLimits, regExpUnicodeMode } from "./regexp.js";
|
|
2
|
+
|
|
3
|
+
//#region src/regexp-parse.ts
|
|
4
|
+
function includePropertiesWhen(properties) {
|
|
5
|
+
return properties() ?? {};
|
|
6
|
+
}
|
|
7
|
+
/** Characters the grammar reserves, which a pattern character excludes. */
|
|
8
|
+
const syntaxCharacters = /* @__PURE__ */ new Set("^$\\.*+?()[]{}|");
|
|
9
|
+
/** The five ControlEscape spellings and the code points they name. */
|
|
10
|
+
const controlEscapes = /* @__PURE__ */ new Map([
|
|
11
|
+
["f", 12],
|
|
12
|
+
["n", 10],
|
|
13
|
+
["r", 13],
|
|
14
|
+
["t", 9],
|
|
15
|
+
["v", 11]
|
|
16
|
+
]);
|
|
17
|
+
/** Modifier flags one inline modifier group may enable or disable. */
|
|
18
|
+
const modifierFlags = /* @__PURE__ */ new Set([
|
|
19
|
+
"i",
|
|
20
|
+
"m",
|
|
21
|
+
"s"
|
|
22
|
+
]);
|
|
23
|
+
/** Binary properties of strings admitted only by positive `v` escapes. */
|
|
24
|
+
const unicodeStringProperties = /* @__PURE__ */ new Set([
|
|
25
|
+
"Basic_Emoji",
|
|
26
|
+
"Emoji_Keycap_Sequence",
|
|
27
|
+
"RGI_Emoji",
|
|
28
|
+
"RGI_Emoji_Flag_Sequence",
|
|
29
|
+
"RGI_Emoji_Modifier_Sequence",
|
|
30
|
+
"RGI_Emoji_Tag_Sequence",
|
|
31
|
+
"RGI_Emoji_ZWJ_Sequence"
|
|
32
|
+
]);
|
|
33
|
+
/** Characters that must be escaped inside a Unicode class set. */
|
|
34
|
+
const classSetSyntaxCharacters = /* @__PURE__ */ new Set("(){}[/-|");
|
|
35
|
+
/** Punctuators reserved when two equal copies occur in a class set. */
|
|
36
|
+
const classSetReservedDoublePunctuators = /* @__PURE__ */ new Set("!#$%&*+,.:;<=>?@^`~");
|
|
37
|
+
function readClassSetCharacterEscape(state, start) {
|
|
38
|
+
const character = at(state, 0);
|
|
39
|
+
if (character != null && (state.unicodeMode && character === "-" || state.flags.unicodeSets && classSetReservedDoublePunctuators.has(character))) {
|
|
40
|
+
state.index += 1;
|
|
41
|
+
return character.codePointAt(0) ?? 0;
|
|
42
|
+
}
|
|
43
|
+
return readCharacterEscape(state, start, true);
|
|
44
|
+
}
|
|
45
|
+
const noExtensions = { admitted: [] };
|
|
46
|
+
/**
|
|
47
|
+
* One rejected construct, thrown so a deep production reports its own
|
|
48
|
+
* location without every caller forwarding a failure value.
|
|
49
|
+
*/
|
|
50
|
+
var PatternFailure = class extends Error {
|
|
51
|
+
error;
|
|
52
|
+
constructor(error) {
|
|
53
|
+
super(error.message);
|
|
54
|
+
this.error = error;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
function span(start, end) {
|
|
58
|
+
return {
|
|
59
|
+
end,
|
|
60
|
+
start
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function fail(kind, message, location, section = "pattern") {
|
|
64
|
+
throw new PatternFailure({
|
|
65
|
+
kind,
|
|
66
|
+
message,
|
|
67
|
+
section,
|
|
68
|
+
span: location
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Describe a construct that only Annex B admits, for a parse that reached
|
|
73
|
+
* it without Annex B's grammar.
|
|
74
|
+
*
|
|
75
|
+
* Under `u` or `v` Annex B never applied, so the rejection is the early
|
|
76
|
+
* error the edition itself requires and reports as invalid text. Without
|
|
77
|
+
* them the text is one a web browser accepts, and only a caller that asked
|
|
78
|
+
* for the core grammar alone gets here, so the rejection names Annex B as
|
|
79
|
+
* an `unsupported` construct rather than as a defect.
|
|
80
|
+
*/
|
|
81
|
+
function annexBMessage(subject) {
|
|
82
|
+
return `${subject} is admitted only by Annex B, which this parse excludes.`;
|
|
83
|
+
}
|
|
84
|
+
function annexBError(state, invalidMessage, subject, location) {
|
|
85
|
+
return state.unicodeMode ? {
|
|
86
|
+
kind: "invalid",
|
|
87
|
+
message: invalidMessage,
|
|
88
|
+
section: "pattern",
|
|
89
|
+
span: location
|
|
90
|
+
} : {
|
|
91
|
+
kind: "unsupported",
|
|
92
|
+
message: annexBMessage(subject),
|
|
93
|
+
section: "pattern",
|
|
94
|
+
span: location
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/** Reject a construct that only Annex B admits, keeping its span. */
|
|
98
|
+
function refuseAnnexB(state, invalidMessage, subject, location) {
|
|
99
|
+
throw new PatternFailure(annexBError(state, invalidMessage, subject, location));
|
|
100
|
+
}
|
|
101
|
+
function admits(state, point) {
|
|
102
|
+
return state.extensions.admitted.includes(point);
|
|
103
|
+
}
|
|
104
|
+
function at(state, offset) {
|
|
105
|
+
return state.source[state.index + offset];
|
|
106
|
+
}
|
|
107
|
+
function done(state) {
|
|
108
|
+
return state.index >= state.source.length;
|
|
109
|
+
}
|
|
110
|
+
function eat(state, text) {
|
|
111
|
+
if (!state.source.startsWith(text, state.index)) return false;
|
|
112
|
+
state.index += text.length;
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Read one source character.
|
|
117
|
+
*
|
|
118
|
+
* A pattern is a sequence of code points under `u` or `v` and a sequence
|
|
119
|
+
* of UTF-16 code units otherwise, which is the only place the flag set
|
|
120
|
+
* changes how text is consumed rather than what it means.
|
|
121
|
+
*/
|
|
122
|
+
function readCharacter(state) {
|
|
123
|
+
const start = state.index;
|
|
124
|
+
const unit = state.source.charCodeAt(start);
|
|
125
|
+
const value = state.unicodeMode ? state.source.codePointAt(start) ?? 0 : Number.isNaN(unit) ? 0 : unit;
|
|
126
|
+
state.index += value > 65535 ? 2 : 1;
|
|
127
|
+
return {
|
|
128
|
+
kind: "character",
|
|
129
|
+
span: span(start, state.index),
|
|
130
|
+
value
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function isDecimalDigit(character) {
|
|
134
|
+
return character != null && character >= "0" && character <= "9";
|
|
135
|
+
}
|
|
136
|
+
function isHexDigit(character) {
|
|
137
|
+
if (character == null) return false;
|
|
138
|
+
return character >= "0" && character <= "9" || character >= "a" && character <= "f" || character >= "A" && character <= "F";
|
|
139
|
+
}
|
|
140
|
+
function readDecimalDigits(state) {
|
|
141
|
+
const start = state.index;
|
|
142
|
+
while (isDecimalDigit(at(state, 0))) state.index += 1;
|
|
143
|
+
return state.source.slice(start, state.index);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Whether one code point has the Unicode `ID_Start` or `ID_Continue`
|
|
147
|
+
* property.
|
|
148
|
+
*
|
|
149
|
+
* The compiler core links no Unicode data, so ASCII is decided here and
|
|
150
|
+
* anything else is delegated to the caller. Without a classifier a
|
|
151
|
+
* non-ASCII code point is refused rather than guessed, which keeps an
|
|
152
|
+
* unadmitted name out of the accepted grammar instead of admitting one
|
|
153
|
+
* whose validity was never checked. The two properties are exactly what
|
|
154
|
+
* the grammar names: the `$`, `_`, and zero-width joiner allowances of an
|
|
155
|
+
* identifier belong to the caller of this predicate, not to it.
|
|
156
|
+
*/
|
|
157
|
+
function identifierProperty(state, codePoint, position, subject, location) {
|
|
158
|
+
if (codePoint < 128) {
|
|
159
|
+
const character = String.fromCharCode(codePoint);
|
|
160
|
+
if (character >= "a" && character <= "z") return true;
|
|
161
|
+
if (character >= "A" && character <= "Z") return true;
|
|
162
|
+
if (position === "start") return false;
|
|
163
|
+
return character >= "0" && character <= "9" || character === "_";
|
|
164
|
+
}
|
|
165
|
+
const classify = position === "start" ? state.extensions.identifierStart : state.extensions.identifierPart;
|
|
166
|
+
if (classify == null) fail("unsupported", `${subject} outside ASCII needs Unicode identifier data that is not linked.`, location);
|
|
167
|
+
return classify(codePoint);
|
|
168
|
+
}
|
|
169
|
+
function readHexValue(state, digits) {
|
|
170
|
+
const start = state.index;
|
|
171
|
+
for (let offset = 0; offset < digits; offset += 1) if (!isHexDigit(at(state, offset))) return -1;
|
|
172
|
+
state.index += digits;
|
|
173
|
+
return Number.parseInt(state.source.slice(start, state.index), 16);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Read one `\u` escape after its `u`.
|
|
177
|
+
*
|
|
178
|
+
* `unicodeSyntax` selects the RegExpUnicodeEscapeSequence grammar
|
|
179
|
+
* parameter. Only the unicode form admits a braced code point and joins a
|
|
180
|
+
* lead surrogate escape to a following trail surrogate escape, which is
|
|
181
|
+
* what makes an escaped supplementary code point equal to a written one.
|
|
182
|
+
* Without it the same text is two independent code units. A group name
|
|
183
|
+
* always uses the unicode form, whatever the flag set says.
|
|
184
|
+
*/
|
|
185
|
+
function readUnicodeEscape(state, unicodeSyntax, start) {
|
|
186
|
+
if (unicodeSyntax && at(state, 0) === "{") {
|
|
187
|
+
state.index += 1;
|
|
188
|
+
const digitStart = state.index;
|
|
189
|
+
while (isHexDigit(at(state, 0))) state.index += 1;
|
|
190
|
+
if (state.index === digitStart || at(state, 0) !== "}") fail("invalid", "A braced Unicode escape is incomplete.", {
|
|
191
|
+
end: state.index,
|
|
192
|
+
start
|
|
193
|
+
});
|
|
194
|
+
const value = Number.parseInt(state.source.slice(digitStart, state.index), 16);
|
|
195
|
+
state.index += 1;
|
|
196
|
+
if (value > 1114111) fail("invalid", "A Unicode escape is above the code point range.", {
|
|
197
|
+
end: state.index,
|
|
198
|
+
start
|
|
199
|
+
});
|
|
200
|
+
return value;
|
|
201
|
+
}
|
|
202
|
+
const value = readHexValue(state, 4);
|
|
203
|
+
if (value < 0) {
|
|
204
|
+
const location = {
|
|
205
|
+
end: state.index,
|
|
206
|
+
start
|
|
207
|
+
};
|
|
208
|
+
const message = "A Unicode escape needs four hexadecimal digits.";
|
|
209
|
+
if (unicodeSyntax) fail("invalid", message, location);
|
|
210
|
+
if (state.annexB) return 117;
|
|
211
|
+
refuseAnnexB(state, message, "An incomplete Unicode escape", location);
|
|
212
|
+
}
|
|
213
|
+
if (unicodeSyntax && value >= 55296 && value <= 56319 && state.source.startsWith("\\u", state.index)) {
|
|
214
|
+
const resume = state.index;
|
|
215
|
+
state.index += 2;
|
|
216
|
+
const trail = readHexValue(state, 4);
|
|
217
|
+
if (trail >= 56320 && trail <= 57343) return (value - 55296) * 1024 + (trail - 56320) + 65536;
|
|
218
|
+
state.index = resume;
|
|
219
|
+
}
|
|
220
|
+
return value;
|
|
221
|
+
}
|
|
222
|
+
/** Whether one character is an OctalDigit. */
|
|
223
|
+
function isOctalDigit(character) {
|
|
224
|
+
return character != null && character >= "0" && character <= "7";
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Read one LegacyOctalEscapeSequence at its first digit.
|
|
228
|
+
*
|
|
229
|
+
* The grammar takes three digits only when the first is at most 3, so the
|
|
230
|
+
* value never exceeds 0o377, and it takes the longest sequence its
|
|
231
|
+
* lookahead restrictions allow: `\47` is one escape and `\477` is that
|
|
232
|
+
* escape followed by a `7`.
|
|
233
|
+
*/
|
|
234
|
+
function readLegacyOctalEscape(state) {
|
|
235
|
+
const first = at(state, 0) ?? "0";
|
|
236
|
+
let value = Number(first);
|
|
237
|
+
state.index += 1;
|
|
238
|
+
if (!isOctalDigit(at(state, 0))) return value;
|
|
239
|
+
value = value * 8 + Number(at(state, 0));
|
|
240
|
+
state.index += 1;
|
|
241
|
+
if (first > "3" || !isOctalDigit(at(state, 0))) return value;
|
|
242
|
+
value = value * 8 + Number(at(state, 0));
|
|
243
|
+
state.index += 1;
|
|
244
|
+
return value;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Read one character escape after its backslash and return a code point.
|
|
248
|
+
*
|
|
249
|
+
* Without Annex B's grammar, a legacy octal escape and an identity escape
|
|
250
|
+
* over an identifier character are rejected rather than accepted for a
|
|
251
|
+
* pattern without `u` or `v`. With it, `inClass` decides how `\c` without
|
|
252
|
+
* an ASCII letter reads: a class admits a decimal digit or `_` as its
|
|
253
|
+
* control letter, and otherwise the backslash is a character of its own
|
|
254
|
+
* and the `c` stays for the next atom.
|
|
255
|
+
*/
|
|
256
|
+
function readCharacterEscape(state, start, inClass) {
|
|
257
|
+
const character = at(state, 0);
|
|
258
|
+
if (character == null) fail("invalid", "A pattern cannot end with a backslash.", {
|
|
259
|
+
end: state.index,
|
|
260
|
+
start
|
|
261
|
+
});
|
|
262
|
+
const control = controlEscapes.get(character);
|
|
263
|
+
if (control != null) {
|
|
264
|
+
state.index += 1;
|
|
265
|
+
return control;
|
|
266
|
+
}
|
|
267
|
+
if (character === "c") {
|
|
268
|
+
const letter = at(state, 1);
|
|
269
|
+
if (letter == null || !(letter >= "a" && letter <= "z" || letter >= "A" && letter <= "Z")) {
|
|
270
|
+
if (state.annexB) {
|
|
271
|
+
if (inClass && (isDecimalDigit(letter) || letter === "_")) {
|
|
272
|
+
state.index += 2;
|
|
273
|
+
return (letter?.charCodeAt(0) ?? 0) % 32;
|
|
274
|
+
}
|
|
275
|
+
return 92;
|
|
276
|
+
}
|
|
277
|
+
refuseAnnexB(state, "A control escape needs an ASCII letter.", "A control escape without an ASCII letter", {
|
|
278
|
+
end: state.index + 1,
|
|
279
|
+
start
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
state.index += 2;
|
|
283
|
+
return letter.charCodeAt(0) % 32;
|
|
284
|
+
}
|
|
285
|
+
if (state.annexB && isOctalDigit(character)) return readLegacyOctalEscape(state);
|
|
286
|
+
if (character === "0") {
|
|
287
|
+
if (isDecimalDigit(at(state, 1))) refuseAnnexB(state, "A legacy octal escape is outside the candidate edition.", "A legacy octal escape", {
|
|
288
|
+
end: state.index + 2,
|
|
289
|
+
start
|
|
290
|
+
});
|
|
291
|
+
state.index += 1;
|
|
292
|
+
return 0;
|
|
293
|
+
}
|
|
294
|
+
if (character === "x") {
|
|
295
|
+
state.index += 1;
|
|
296
|
+
const value = readHexValue(state, 2);
|
|
297
|
+
if (value < 0) {
|
|
298
|
+
if (state.annexB) return 120;
|
|
299
|
+
refuseAnnexB(state, "A hexadecimal escape needs two hexadecimal digits.", "An incomplete hexadecimal escape", {
|
|
300
|
+
end: state.index,
|
|
301
|
+
start
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
return value;
|
|
305
|
+
}
|
|
306
|
+
if (character === "u") {
|
|
307
|
+
state.index += 1;
|
|
308
|
+
return readUnicodeEscape(state, state.unicodeMode, start);
|
|
309
|
+
}
|
|
310
|
+
const escaped = readCharacter(state);
|
|
311
|
+
if (state.unicodeMode) {
|
|
312
|
+
const text = String.fromCodePoint(escaped.value);
|
|
313
|
+
if (!syntaxCharacters.has(text) && text !== "/") fail("invalid", "This identity escape is not allowed.", {
|
|
314
|
+
end: state.index,
|
|
315
|
+
start
|
|
316
|
+
});
|
|
317
|
+
return escaped.value;
|
|
318
|
+
}
|
|
319
|
+
const location = {
|
|
320
|
+
end: state.index,
|
|
321
|
+
start
|
|
322
|
+
};
|
|
323
|
+
if (escaped.value === 107 && state.groupNameDeclared) fail("invalid", "This identity escape is not allowed.", location);
|
|
324
|
+
if (state.annexB) return escaped.value;
|
|
325
|
+
if (identifierProperty(state, escaped.value, "continue", "An identity escape", location)) refuseAnnexB(state, "This identity escape is not allowed.", "This identity escape", location);
|
|
326
|
+
return escaped.value;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* The six CharacterClassEscape spellings, keyed by exact letter.
|
|
330
|
+
*
|
|
331
|
+
* The uppercase spelling of each is its complement, and no other letter
|
|
332
|
+
* names a class escape, so the table is matched literally rather than
|
|
333
|
+
* through a case conversion that another letter could reach.
|
|
334
|
+
*/
|
|
335
|
+
const classEscapeSets = /* @__PURE__ */ new Map([
|
|
336
|
+
["D", {
|
|
337
|
+
negated: true,
|
|
338
|
+
set: "digit"
|
|
339
|
+
}],
|
|
340
|
+
["S", {
|
|
341
|
+
negated: true,
|
|
342
|
+
set: "space"
|
|
343
|
+
}],
|
|
344
|
+
["W", {
|
|
345
|
+
negated: true,
|
|
346
|
+
set: "word"
|
|
347
|
+
}],
|
|
348
|
+
["d", {
|
|
349
|
+
negated: false,
|
|
350
|
+
set: "digit"
|
|
351
|
+
}],
|
|
352
|
+
["s", {
|
|
353
|
+
negated: false,
|
|
354
|
+
set: "space"
|
|
355
|
+
}],
|
|
356
|
+
["w", {
|
|
357
|
+
negated: false,
|
|
358
|
+
set: "word"
|
|
359
|
+
}]
|
|
360
|
+
]);
|
|
361
|
+
function readClassEscape(state, start) {
|
|
362
|
+
const character = at(state, 0);
|
|
363
|
+
const selected = character == null ? void 0 : classEscapeSets.get(character);
|
|
364
|
+
if (selected == null) return void 0;
|
|
365
|
+
state.index += 1;
|
|
366
|
+
return {
|
|
367
|
+
kind: "class-escape",
|
|
368
|
+
negated: selected.negated,
|
|
369
|
+
set: selected.set,
|
|
370
|
+
span: span(start, state.index)
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Whether one text is a UnicodePropertyName.
|
|
375
|
+
*
|
|
376
|
+
* The grammar spells a name with ASCII letters and low lines and a value
|
|
377
|
+
* with those and decimal digits, so a name is checked here rather than
|
|
378
|
+
* left to whichever unit resolves it against Unicode data.
|
|
379
|
+
*/
|
|
380
|
+
function propertyNameCharacters(text) {
|
|
381
|
+
return text.length > 0 && /^[A-Za-z_]+$/u.test(text);
|
|
382
|
+
}
|
|
383
|
+
/** Whether one text is a UnicodePropertyValue or a lone name or value. */
|
|
384
|
+
function propertyValueCharacters(text) {
|
|
385
|
+
return text.length > 0 && /^[A-Za-z_0-9]+$/u.test(text);
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Read one `\p` or `\P` escape after its backslash.
|
|
389
|
+
*
|
|
390
|
+
* The escape only exists under `u` and `v`; elsewhere `p` is an
|
|
391
|
+
* identifier character, so the backslash form is an invalid identity
|
|
392
|
+
* escape rather than a property reference. A property name is an early
|
|
393
|
+
* error unless a caller both admits the point and resolves the name, so
|
|
394
|
+
* an unresolvable name is never accepted for lack of a resolver.
|
|
395
|
+
*/
|
|
396
|
+
function readUnicodePropertyEscape(state, start) {
|
|
397
|
+
const character = at(state, 0);
|
|
398
|
+
if (!state.unicodeMode || character !== "p" && character !== "P") return;
|
|
399
|
+
state.index += 1;
|
|
400
|
+
if (!eat(state, "{")) fail("invalid", "A Unicode property escape needs a braced name.", {
|
|
401
|
+
end: state.index,
|
|
402
|
+
start
|
|
403
|
+
});
|
|
404
|
+
const nameStart = state.index;
|
|
405
|
+
while (!done(state) && at(state, 0) !== "}" && at(state, 0) !== "=") state.index += 1;
|
|
406
|
+
const first = state.source.slice(nameStart, state.index);
|
|
407
|
+
let second;
|
|
408
|
+
if (at(state, 0) === "=") {
|
|
409
|
+
state.index += 1;
|
|
410
|
+
const valueStart = state.index;
|
|
411
|
+
while (!done(state) && at(state, 0) !== "}") state.index += 1;
|
|
412
|
+
second = state.source.slice(valueStart, state.index);
|
|
413
|
+
}
|
|
414
|
+
if (!eat(state, "}")) fail("invalid", "A Unicode property escape is unterminated.", {
|
|
415
|
+
end: state.index,
|
|
416
|
+
start
|
|
417
|
+
});
|
|
418
|
+
const location = span(start, state.index);
|
|
419
|
+
if (!(second == null ? propertyValueCharacters(first) : propertyNameCharacters(first) && propertyValueCharacters(second))) fail("invalid", "A Unicode property escape names nothing valid.", location);
|
|
420
|
+
if (!admits(state, "unicode-property-escapes")) fail("unsupported", "A Unicode property escape is not admitted yet.", location);
|
|
421
|
+
const resolve = state.extensions.unicodeProperty;
|
|
422
|
+
if (resolve == null) fail("unsupported", "A Unicode property escape needs property data that is not linked.", location);
|
|
423
|
+
const escape = {
|
|
424
|
+
kind: "unicode-property",
|
|
425
|
+
negated: character === "P",
|
|
426
|
+
property: first,
|
|
427
|
+
span: location,
|
|
428
|
+
...includePropertiesWhen(() => {
|
|
429
|
+
if (second == null) return void 0;
|
|
430
|
+
return { value: second };
|
|
431
|
+
})
|
|
432
|
+
};
|
|
433
|
+
if (!state.flags.unicodeSets && escape.value == null && unicodeStringProperties.has(escape.property)) fail("invalid", "A Unicode property of strings requires the v flag.", location);
|
|
434
|
+
if (!resolve(escape)) {
|
|
435
|
+
if (state.flags.unicodeSets && !escape.negated && escape.value == null && unicodeStringProperties.has(escape.property)) fail("unsupported", "A Unicode property of strings is not admitted yet.", location);
|
|
436
|
+
fail("invalid", "This Unicode property is not defined.", location);
|
|
437
|
+
}
|
|
438
|
+
return escape;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Count the left-capturing parentheses in a pattern text.
|
|
442
|
+
*
|
|
443
|
+
* This is CountLeftCapturingParensWithin over the whole pattern, which
|
|
444
|
+
* Annex B needs before the parse reaches the groups. The scan skips an
|
|
445
|
+
* escape and the inside of a character class, and it counts `(?<` only
|
|
446
|
+
* when a group name rather than a lookbehind introducer follows. A text
|
|
447
|
+
* the parse later rejects may count differently, but then no reading of
|
|
448
|
+
* its escapes is ever returned.
|
|
449
|
+
*/
|
|
450
|
+
function countCapturingGroups(source) {
|
|
451
|
+
let count = 0;
|
|
452
|
+
let insideClass = false;
|
|
453
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
454
|
+
const character = source[index];
|
|
455
|
+
if (character === "\\") {
|
|
456
|
+
index += 1;
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
if (insideClass) {
|
|
460
|
+
if (character === "]") insideClass = false;
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
if (character === "[") {
|
|
464
|
+
insideClass = true;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
if (character !== "(") continue;
|
|
468
|
+
if (source[index + 1] !== "?") {
|
|
469
|
+
count += 1;
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
if (source[index + 2] !== "<") continue;
|
|
473
|
+
const next = source[index + 3];
|
|
474
|
+
if (next !== "=" && next !== "!") count += 1;
|
|
475
|
+
}
|
|
476
|
+
return count;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Whether the pattern text holds a GroupSpecifier.
|
|
480
|
+
*
|
|
481
|
+
* The scan skips an escape and the inside of a character class, where
|
|
482
|
+
* `(?<` is ordinary text rather than an introducer, and it excludes the
|
|
483
|
+
* two lookbehind introducers that share the first three characters.
|
|
484
|
+
*/
|
|
485
|
+
function scanForGroupName(source) {
|
|
486
|
+
let insideClass = false;
|
|
487
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
488
|
+
const character = source[index];
|
|
489
|
+
if (character === "\\") {
|
|
490
|
+
index += 1;
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
if (insideClass) {
|
|
494
|
+
if (character === "]") insideClass = false;
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
if (character === "[") {
|
|
498
|
+
insideClass = true;
|
|
499
|
+
continue;
|
|
500
|
+
}
|
|
501
|
+
if (character !== "(") continue;
|
|
502
|
+
if (source[index + 1] !== "?" || source[index + 2] !== "<") continue;
|
|
503
|
+
const next = source[index + 3];
|
|
504
|
+
if (next !== "=" && next !== "!") return true;
|
|
505
|
+
}
|
|
506
|
+
return false;
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Read one `<name>` group specifier after its introducer.
|
|
510
|
+
*
|
|
511
|
+
* Both spellings that reach this reader parse with the grammar's
|
|
512
|
+
* named-reference production active: a declaration is a group specifier
|
|
513
|
+
* by construction, and a `\k` reference reaches it only where the pattern
|
|
514
|
+
* declares a group name or carries `u` or `v`. A malformed name is
|
|
515
|
+
* therefore the edition's own early error rather than an Annex B
|
|
516
|
+
* boundary.
|
|
517
|
+
*/
|
|
518
|
+
function readGroupName(state, start) {
|
|
519
|
+
if (!eat(state, "<")) fail("invalid", "A group name must be enclosed in angle brackets.", {
|
|
520
|
+
end: state.index,
|
|
521
|
+
start
|
|
522
|
+
});
|
|
523
|
+
let name = "";
|
|
524
|
+
while (!done(state) && at(state, 0) !== ">") {
|
|
525
|
+
const characterStart = state.index;
|
|
526
|
+
let codePoint;
|
|
527
|
+
if (eat(state, "\\")) {
|
|
528
|
+
if (!eat(state, "u")) fail("invalid", "A group name escape must be a Unicode escape.", {
|
|
529
|
+
end: state.index,
|
|
530
|
+
start: characterStart
|
|
531
|
+
});
|
|
532
|
+
codePoint = readUnicodeEscape(state, true, characterStart);
|
|
533
|
+
} else {
|
|
534
|
+
const start2 = state.index;
|
|
535
|
+
codePoint = state.source.codePointAt(start2) ?? 0;
|
|
536
|
+
state.index += codePoint > 65535 ? 2 : 1;
|
|
537
|
+
}
|
|
538
|
+
const location = span(characterStart, state.index);
|
|
539
|
+
const dollarOrLow = codePoint === 36 || codePoint === 95;
|
|
540
|
+
if (!(name.length === 0 ? dollarOrLow || identifierProperty(state, codePoint, "start", "A group name", location) : dollarOrLow || codePoint === 8204 || codePoint === 8205 || identifierProperty(state, codePoint, "continue", "A group name", location))) fail("invalid", "This character is not allowed in a group name.", location);
|
|
541
|
+
name += String.fromCodePoint(codePoint);
|
|
542
|
+
}
|
|
543
|
+
if (name.length === 0 || !eat(state, ">")) fail("invalid", "A group name is incomplete.", {
|
|
544
|
+
end: state.index,
|
|
545
|
+
start
|
|
546
|
+
});
|
|
547
|
+
return name;
|
|
548
|
+
}
|
|
549
|
+
function readModifierFlags(state, start) {
|
|
550
|
+
const selected = [];
|
|
551
|
+
while (!done(state)) {
|
|
552
|
+
const character = at(state, 0);
|
|
553
|
+
if (character == null || !modifierFlags.has(character)) break;
|
|
554
|
+
if (selected.includes(character)) fail("invalid", "A modifier group repeats a flag.", {
|
|
555
|
+
end: state.index + 1,
|
|
556
|
+
start
|
|
557
|
+
});
|
|
558
|
+
selected.push(character);
|
|
559
|
+
state.index += 1;
|
|
560
|
+
}
|
|
561
|
+
return selected;
|
|
562
|
+
}
|
|
563
|
+
function classItemValue(item) {
|
|
564
|
+
return item.kind === "character" ? item : void 0;
|
|
565
|
+
}
|
|
566
|
+
function parseClassStringDisjunction(state) {
|
|
567
|
+
const start = state.index;
|
|
568
|
+
state.index += 3;
|
|
569
|
+
const strings = [];
|
|
570
|
+
let characters = [];
|
|
571
|
+
for (;;) {
|
|
572
|
+
if (done(state)) fail("invalid", "A class string disjunction is unterminated.", {
|
|
573
|
+
end: state.index,
|
|
574
|
+
start
|
|
575
|
+
});
|
|
576
|
+
if (eat(state, "|")) {
|
|
577
|
+
strings.push(characters);
|
|
578
|
+
characters = [];
|
|
579
|
+
continue;
|
|
580
|
+
}
|
|
581
|
+
if (eat(state, "}")) {
|
|
582
|
+
strings.push(characters);
|
|
583
|
+
break;
|
|
584
|
+
}
|
|
585
|
+
const characterStart = state.index;
|
|
586
|
+
let value;
|
|
587
|
+
if (eat(state, "\\")) if (eat(state, "b")) value = 8;
|
|
588
|
+
else value = readClassSetCharacterEscape(state, characterStart);
|
|
589
|
+
else {
|
|
590
|
+
validateClassSetCharacter(state);
|
|
591
|
+
value = readCharacter(state).value;
|
|
592
|
+
}
|
|
593
|
+
characters.push({
|
|
594
|
+
kind: "character",
|
|
595
|
+
span: span(characterStart, state.index),
|
|
596
|
+
value
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
return {
|
|
600
|
+
kind: "class-strings",
|
|
601
|
+
span: span(start, state.index),
|
|
602
|
+
strings
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
function validateClassSetCharacter(state) {
|
|
606
|
+
const firstCharacter = at(state, 0);
|
|
607
|
+
if (firstCharacter == null || firstCharacter === "]" || state.source.startsWith("&&", state.index) || state.source.startsWith("--", state.index)) fail("invalid", "A class set operator needs an operand.", span(state.index, Math.min(state.index + 2, state.source.length)));
|
|
608
|
+
if (classSetSyntaxCharacters.has(firstCharacter)) fail("invalid", "A class set syntax character must be escaped.", span(state.index, state.index + 1));
|
|
609
|
+
if (firstCharacter === at(state, 1) && classSetReservedDoublePunctuators.has(firstCharacter)) fail("invalid", "A doubled class set punctuator must be escaped.", span(state.index, state.index + 2));
|
|
610
|
+
}
|
|
611
|
+
function parseClassSetOperand(state) {
|
|
612
|
+
if (at(state, 0) === "[") return parseUnicodeClassSet(state);
|
|
613
|
+
if (state.source.startsWith("\\q{", state.index)) return parseClassStringDisjunction(state);
|
|
614
|
+
validateClassSetCharacter(state);
|
|
615
|
+
const first = parseClassAtom(state);
|
|
616
|
+
if (first.kind === "character" && at(state, 0) === "-" && at(state, 1) !== "-" && at(state, 1) !== "]" && at(state, 1) != null) {
|
|
617
|
+
state.index += 1;
|
|
618
|
+
if (at(state, 0) === "[") fail("invalid", "A nested class set cannot be a range bound.", span(first.span.start, state.index + 1));
|
|
619
|
+
validateClassSetCharacter(state);
|
|
620
|
+
const second = parseClassAtom(state);
|
|
621
|
+
if (second.kind !== "character" || first.value > second.value) fail("invalid", "A character class range is invalid.", {
|
|
622
|
+
end: state.index,
|
|
623
|
+
start: first.span.start
|
|
624
|
+
});
|
|
625
|
+
return {
|
|
626
|
+
end: second,
|
|
627
|
+
kind: "range",
|
|
628
|
+
span: span(first.span.start, state.index),
|
|
629
|
+
start: first
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
return first;
|
|
633
|
+
}
|
|
634
|
+
function classSetOperandMayContainStrings(operand) {
|
|
635
|
+
if (operand.kind === "class-strings") return operand.strings.some((characters) => characters.length !== 1);
|
|
636
|
+
if (operand.kind === "unicode-property") return !operand.negated && operand.value == null && unicodeStringProperties.has(operand.property);
|
|
637
|
+
if (operand.kind !== "class-set") return false;
|
|
638
|
+
if (operand.operation === "union") return operand.operands.some(classSetOperandMayContainStrings);
|
|
639
|
+
if (operand.operation === "intersection") return operand.operands.length > 0 && operand.operands.every(classSetOperandMayContainStrings);
|
|
640
|
+
const first = operand.operands[0];
|
|
641
|
+
return first != null && classSetOperandMayContainStrings(first);
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Parse one `v`-mode class set, charging the reviewed nesting budget.
|
|
645
|
+
*
|
|
646
|
+
* A nested class is an operand of its enclosing class, so this production
|
|
647
|
+
* recurses through `parseClassSetOperand` exactly as a group recurses
|
|
648
|
+
* through `parseDisjunction`. It shares the one `nestingDepth` budget so a
|
|
649
|
+
* pattern that nests either way, or both at once, reports the owned located
|
|
650
|
+
* `limit` error rather than exhausting the host stack.
|
|
651
|
+
*/
|
|
652
|
+
function parseUnicodeClassSet(state) {
|
|
653
|
+
const start = state.index;
|
|
654
|
+
state.depth += 1;
|
|
655
|
+
if (state.depth > state.limits.nestingDepth) fail("limit", "A pattern nests above the reviewed depth limit.", {
|
|
656
|
+
end: state.index,
|
|
657
|
+
start
|
|
658
|
+
});
|
|
659
|
+
state.index += 1;
|
|
660
|
+
const negated = eat(state, "^");
|
|
661
|
+
const operands = [];
|
|
662
|
+
if (at(state, 0) !== "]") operands.push(parseClassSetOperand(state));
|
|
663
|
+
let operation = "union";
|
|
664
|
+
if (state.source.startsWith("&&", state.index)) operation = "intersection";
|
|
665
|
+
else if (state.source.startsWith("--", state.index)) operation = "subtraction";
|
|
666
|
+
if (operation !== "union" && operands[0]?.kind === "range") fail("invalid", "A class set range cannot be an intersection or subtraction operand.", operands[0].span);
|
|
667
|
+
while (!done(state) && at(state, 0) !== "]") {
|
|
668
|
+
if (operation === "intersection") {
|
|
669
|
+
if (!state.source.startsWith("&&", state.index)) fail("invalid", "A class set mixes its operators.", {
|
|
670
|
+
end: state.index + 1,
|
|
671
|
+
start: state.index
|
|
672
|
+
});
|
|
673
|
+
state.index += 2;
|
|
674
|
+
if (at(state, 0) === "&") fail("invalid", "A class set operator needs an operand.", span(state.index - 2, state.index + 1));
|
|
675
|
+
} else if (operation === "subtraction") {
|
|
676
|
+
if (!state.source.startsWith("--", state.index)) fail("invalid", "A class set mixes its operators.", {
|
|
677
|
+
end: state.index + 1,
|
|
678
|
+
start: state.index
|
|
679
|
+
});
|
|
680
|
+
state.index += 2;
|
|
681
|
+
}
|
|
682
|
+
const operand = parseClassSetOperand(state);
|
|
683
|
+
if (operation !== "union" && operand.kind === "range") fail("invalid", "A class set range cannot be an intersection or subtraction operand.", operand.span);
|
|
684
|
+
operands.push(operand);
|
|
685
|
+
}
|
|
686
|
+
if (!eat(state, "]")) fail("invalid", "A character class is unterminated.", {
|
|
687
|
+
end: state.index,
|
|
688
|
+
start
|
|
689
|
+
});
|
|
690
|
+
const classSet = {
|
|
691
|
+
kind: "class-set",
|
|
692
|
+
negated,
|
|
693
|
+
operands,
|
|
694
|
+
operation,
|
|
695
|
+
span: span(start, state.index)
|
|
696
|
+
};
|
|
697
|
+
if (negated && classSetOperandMayContainStrings(classSet)) fail("invalid", "A negated character class may contain strings.", classSet.span);
|
|
698
|
+
state.depth -= 1;
|
|
699
|
+
return classSet;
|
|
700
|
+
}
|
|
701
|
+
function parseCharacterClass(state) {
|
|
702
|
+
const start = state.index;
|
|
703
|
+
if (state.flags.unicodeSets) {
|
|
704
|
+
if (!admits(state, "class-set-notation")) fail("unsupported", "Class set notation is not admitted yet.", span(start, start + 1));
|
|
705
|
+
return parseUnicodeClassSet(state);
|
|
706
|
+
}
|
|
707
|
+
state.index += 1;
|
|
708
|
+
const negated = eat(state, "^");
|
|
709
|
+
const items = [];
|
|
710
|
+
for (;;) {
|
|
711
|
+
if (done(state)) fail("invalid", "A character class is unterminated.", {
|
|
712
|
+
end: state.index,
|
|
713
|
+
start
|
|
714
|
+
});
|
|
715
|
+
if (eat(state, "]")) break;
|
|
716
|
+
const first = parseClassAtom(state);
|
|
717
|
+
if (at(state, 0) === "-" && at(state, 1) !== "]" && at(state, 1) != null) {
|
|
718
|
+
const dash = state.index;
|
|
719
|
+
state.index += 1;
|
|
720
|
+
const second = parseClassAtom(state);
|
|
721
|
+
const low = classItemValue(first);
|
|
722
|
+
const high = classItemValue(second);
|
|
723
|
+
if ((low == null || high == null) && state.annexB) {
|
|
724
|
+
items.push(first, {
|
|
725
|
+
kind: "character",
|
|
726
|
+
span: span(dash, dash + 1),
|
|
727
|
+
value: 45
|
|
728
|
+
}, second);
|
|
729
|
+
continue;
|
|
730
|
+
}
|
|
731
|
+
if (low == null || high == null) refuseAnnexB(state, "A character class range bound cannot be a class escape.", "A class escape as a character class range bound", {
|
|
732
|
+
end: state.index,
|
|
733
|
+
start: first.span.start
|
|
734
|
+
});
|
|
735
|
+
if (low.value > high.value) fail("invalid", "A character class range is out of order.", {
|
|
736
|
+
end: state.index,
|
|
737
|
+
start: first.span.start
|
|
738
|
+
});
|
|
739
|
+
items.push({
|
|
740
|
+
end: high,
|
|
741
|
+
kind: "range",
|
|
742
|
+
span: span(first.span.start, state.index),
|
|
743
|
+
start: low
|
|
744
|
+
});
|
|
745
|
+
continue;
|
|
746
|
+
}
|
|
747
|
+
items.push(first);
|
|
748
|
+
}
|
|
749
|
+
return {
|
|
750
|
+
items,
|
|
751
|
+
kind: "character-class",
|
|
752
|
+
negated,
|
|
753
|
+
span: span(start, state.index)
|
|
754
|
+
};
|
|
755
|
+
}
|
|
756
|
+
function parseClassAtom(state) {
|
|
757
|
+
const start = state.index;
|
|
758
|
+
if (!eat(state, "\\")) return readCharacter(state);
|
|
759
|
+
if (at(state, 0) === "b") {
|
|
760
|
+
state.index += 1;
|
|
761
|
+
return {
|
|
762
|
+
kind: "character",
|
|
763
|
+
span: span(start, state.index),
|
|
764
|
+
value: 8
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
const classEscape = readClassEscape(state, start);
|
|
768
|
+
if (classEscape != null) return classEscape;
|
|
769
|
+
const property = readUnicodePropertyEscape(state, start);
|
|
770
|
+
if (property != null) return property;
|
|
771
|
+
return {
|
|
772
|
+
kind: "character",
|
|
773
|
+
span: span(start, state.index),
|
|
774
|
+
value: readClassSetCharacterEscape(state, start)
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
function parseAtomEscape(state, start) {
|
|
778
|
+
const character = at(state, 0);
|
|
779
|
+
if (character != null && character >= "1" && character <= "9") {
|
|
780
|
+
const digitStart = state.index;
|
|
781
|
+
const digits = readDecimalDigits(state);
|
|
782
|
+
const location = span(start, state.index);
|
|
783
|
+
if (state.annexB && Number(digits) > state.captureCount) {
|
|
784
|
+
state.index = digitStart;
|
|
785
|
+
if (character >= "8") {
|
|
786
|
+
state.index += 1;
|
|
787
|
+
return {
|
|
788
|
+
kind: "character",
|
|
789
|
+
span: span(start, state.index),
|
|
790
|
+
value: character.charCodeAt(0)
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
const value = readLegacyOctalEscape(state);
|
|
794
|
+
return {
|
|
795
|
+
kind: "character",
|
|
796
|
+
span: span(start, state.index),
|
|
797
|
+
value
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
const index = Number(digits);
|
|
801
|
+
state.backreferences.push({
|
|
802
|
+
index,
|
|
803
|
+
span: location
|
|
804
|
+
});
|
|
805
|
+
return {
|
|
806
|
+
index,
|
|
807
|
+
kind: "backreference",
|
|
808
|
+
span: location
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
if (character === "k") {
|
|
812
|
+
state.index += 1;
|
|
813
|
+
if (!state.unicodeMode && !state.groupNameDeclared) {
|
|
814
|
+
if (state.annexB) return {
|
|
815
|
+
kind: "character",
|
|
816
|
+
span: span(start, state.index),
|
|
817
|
+
value: 107
|
|
818
|
+
};
|
|
819
|
+
fail("unsupported", annexBMessage("A named backreference in a pattern that declares no group name"), span(start, state.index));
|
|
820
|
+
}
|
|
821
|
+
const name = readGroupName(state, start);
|
|
822
|
+
const location = span(start, state.index);
|
|
823
|
+
state.namedReferences.push({
|
|
824
|
+
name,
|
|
825
|
+
span: location
|
|
826
|
+
});
|
|
827
|
+
const indices = state.namedReferenceIndices.get(name) ?? [];
|
|
828
|
+
state.namedReferenceIndices.set(name, indices);
|
|
829
|
+
return {
|
|
830
|
+
indices,
|
|
831
|
+
kind: "named-backreference",
|
|
832
|
+
name,
|
|
833
|
+
span: location
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
const classEscape = readClassEscape(state, start);
|
|
837
|
+
if (classEscape != null) return classEscape;
|
|
838
|
+
const property = readUnicodePropertyEscape(state, start);
|
|
839
|
+
if (property != null) return property;
|
|
840
|
+
return {
|
|
841
|
+
kind: "character",
|
|
842
|
+
span: span(start, state.index),
|
|
843
|
+
value: readCharacterEscape(state, start, false)
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
function parseGroup(state) {
|
|
847
|
+
const start = state.index;
|
|
848
|
+
state.index += 1;
|
|
849
|
+
if (eat(state, "?")) {
|
|
850
|
+
if (eat(state, ":")) {
|
|
851
|
+
const body = parseDisjunction(state);
|
|
852
|
+
expectGroupEnd(state, start);
|
|
853
|
+
return {
|
|
854
|
+
body,
|
|
855
|
+
kind: "group",
|
|
856
|
+
span: span(start, state.index)
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
if (at(state, 0) === "<") {
|
|
860
|
+
const name = readGroupName(state, start);
|
|
861
|
+
const capture = declareCapture(state, start, name);
|
|
862
|
+
const body = parseDisjunction(state);
|
|
863
|
+
expectGroupEnd(state, start);
|
|
864
|
+
return {
|
|
865
|
+
body,
|
|
866
|
+
index: capture,
|
|
867
|
+
kind: "capturing-group",
|
|
868
|
+
name,
|
|
869
|
+
span: span(start, state.index)
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
const enabled = readModifierFlags(state, start);
|
|
873
|
+
const disabled = eat(state, "-") ? readModifierFlags(state, start) : [];
|
|
874
|
+
if (!eat(state, ":")) fail("invalid", "This group prefix is not a valid group.", {
|
|
875
|
+
end: state.index + 1,
|
|
876
|
+
start
|
|
877
|
+
});
|
|
878
|
+
if (enabled.length === 0 && disabled.length === 0) fail("invalid", "A modifier group names no flag.", {
|
|
879
|
+
end: state.index,
|
|
880
|
+
start
|
|
881
|
+
});
|
|
882
|
+
if (enabled.some((flag) => disabled.includes(flag))) fail("invalid", "A modifier group both adds and removes a flag.", {
|
|
883
|
+
end: state.index,
|
|
884
|
+
start
|
|
885
|
+
});
|
|
886
|
+
if (!admits(state, "modifiers")) fail("unsupported", "An inline modifier group is not admitted yet.", {
|
|
887
|
+
end: state.index,
|
|
888
|
+
start
|
|
889
|
+
});
|
|
890
|
+
const body = parseDisjunction(state);
|
|
891
|
+
expectGroupEnd(state, start);
|
|
892
|
+
return {
|
|
893
|
+
body,
|
|
894
|
+
disabled,
|
|
895
|
+
enabled,
|
|
896
|
+
kind: "modifier-group",
|
|
897
|
+
span: span(start, state.index)
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
const capture = declareCapture(state, start);
|
|
901
|
+
const body = parseDisjunction(state);
|
|
902
|
+
expectGroupEnd(state, start);
|
|
903
|
+
return {
|
|
904
|
+
body,
|
|
905
|
+
index: capture,
|
|
906
|
+
kind: "capturing-group",
|
|
907
|
+
span: span(start, state.index)
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
function declareCapture(state, start, name) {
|
|
911
|
+
if (state.captures.length >= state.limits.capturingGroups) fail("limit", "A pattern declares too many capturing groups.", {
|
|
912
|
+
end: state.index,
|
|
913
|
+
start
|
|
914
|
+
});
|
|
915
|
+
const index = state.captures.length + 1;
|
|
916
|
+
const location = span(start, state.index);
|
|
917
|
+
state.captures.push({
|
|
918
|
+
index,
|
|
919
|
+
span: location,
|
|
920
|
+
...includePropertiesWhen(() => {
|
|
921
|
+
if (name == null) return void 0;
|
|
922
|
+
return { name };
|
|
923
|
+
})
|
|
924
|
+
});
|
|
925
|
+
if (name != null) state.namedGroups.push({
|
|
926
|
+
name,
|
|
927
|
+
path: [...state.path],
|
|
928
|
+
span: location
|
|
929
|
+
});
|
|
930
|
+
return index;
|
|
931
|
+
}
|
|
932
|
+
function expectGroupEnd(state, start) {
|
|
933
|
+
if (!eat(state, ")")) fail("invalid", "A group is unterminated.", {
|
|
934
|
+
end: state.index,
|
|
935
|
+
start
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
function parseAtom(state) {
|
|
939
|
+
const start = state.index;
|
|
940
|
+
const character = at(state, 0);
|
|
941
|
+
if (character === ".") {
|
|
942
|
+
state.index += 1;
|
|
943
|
+
return {
|
|
944
|
+
kind: "dot",
|
|
945
|
+
span: span(start, state.index)
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
if (character === "(") return parseGroup(state);
|
|
949
|
+
if (character === "[") return parseCharacterClass(state);
|
|
950
|
+
if (character === "\\") {
|
|
951
|
+
state.index += 1;
|
|
952
|
+
return parseAtomEscape(state, start);
|
|
953
|
+
}
|
|
954
|
+
if (character === "*" || character === "+" || character === "?") fail("invalid", "A quantifier has no atom to repeat.", {
|
|
955
|
+
end: start + 1,
|
|
956
|
+
start
|
|
957
|
+
});
|
|
958
|
+
if (character === "{") {
|
|
959
|
+
if (bracedQuantifierFollows(state)) fail("invalid", "A quantifier has no atom to repeat.", {
|
|
960
|
+
end: start + 1,
|
|
961
|
+
start
|
|
962
|
+
});
|
|
963
|
+
if (state.annexB) return readCharacter(state);
|
|
964
|
+
refuseAnnexB(state, "An unescaped brace is not a pattern character.", "An unescaped brace", {
|
|
965
|
+
end: start + 1,
|
|
966
|
+
start
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
if ((character === "]" || character === "}") && !state.annexB) refuseAnnexB(state, "This character must be escaped.", "This unescaped character", {
|
|
970
|
+
end: start + 1,
|
|
971
|
+
start
|
|
972
|
+
});
|
|
973
|
+
return readCharacter(state);
|
|
974
|
+
}
|
|
975
|
+
/** Compare two decimal digit strings by exact mathematical value. */
|
|
976
|
+
function compareDecimalDigits(first, second) {
|
|
977
|
+
const left = first.replace(/^0+(?=\d)/u, "");
|
|
978
|
+
const right = second.replace(/^0+(?=\d)/u, "");
|
|
979
|
+
if (left.length !== right.length) return left.length - right.length;
|
|
980
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Read a braced quantifier body after its `{`, or return undefined when
|
|
984
|
+
* the text is not one. Annex B is outside the claim, so a `{` that starts
|
|
985
|
+
* no quantifier is an error at the caller rather than a literal brace.
|
|
986
|
+
*
|
|
987
|
+
* Recognition is separate from validation: a quantifier that no atom
|
|
988
|
+
* precedes is an error whatever its bounds say, so the bound order and
|
|
989
|
+
* the reviewed bound limit are checked only where a quantifier is
|
|
990
|
+
* grammatically admitted.
|
|
991
|
+
*/
|
|
992
|
+
function readBracedQuantifier(state) {
|
|
993
|
+
const minimumDigits = readDecimalDigits(state);
|
|
994
|
+
if (minimumDigits.length === 0) return void 0;
|
|
995
|
+
const minimum = Number(minimumDigits);
|
|
996
|
+
let maximum = minimum;
|
|
997
|
+
let maximumDigits = minimumDigits;
|
|
998
|
+
if (eat(state, ",")) {
|
|
999
|
+
const written = readDecimalDigits(state);
|
|
1000
|
+
maximumDigits = written.length === 0 ? void 0 : written;
|
|
1001
|
+
maximum = maximumDigits == null ? Number.POSITIVE_INFINITY : Number(written);
|
|
1002
|
+
}
|
|
1003
|
+
if (!eat(state, "}")) return void 0;
|
|
1004
|
+
return {
|
|
1005
|
+
maximum,
|
|
1006
|
+
...includePropertiesWhen(() => {
|
|
1007
|
+
if (maximumDigits == null) return void 0;
|
|
1008
|
+
return { maximumDigits };
|
|
1009
|
+
}),
|
|
1010
|
+
minimum,
|
|
1011
|
+
minimumDigits
|
|
1012
|
+
};
|
|
1013
|
+
}
|
|
1014
|
+
function validateQuantifierBounds(state, bounds, start) {
|
|
1015
|
+
const location = {
|
|
1016
|
+
end: state.index,
|
|
1017
|
+
start
|
|
1018
|
+
};
|
|
1019
|
+
if (bounds.maximumDigits != null && compareDecimalDigits(bounds.minimumDigits, bounds.maximumDigits) > 0) fail("invalid", "A quantifier's lower bound is above its upper bound.", location);
|
|
1020
|
+
const limit = String(state.limits.quantifierBound);
|
|
1021
|
+
const above = (digits) => compareDecimalDigits(digits, limit) > 0;
|
|
1022
|
+
if (above(bounds.minimumDigits) || bounds.maximumDigits != null && above(bounds.maximumDigits)) fail("limit", "A quantifier bound is above the reviewed limit.", location);
|
|
1023
|
+
return bounds;
|
|
1024
|
+
}
|
|
1025
|
+
function parseQuantifier(state) {
|
|
1026
|
+
const start = state.index;
|
|
1027
|
+
const character = at(state, 0);
|
|
1028
|
+
let bounds;
|
|
1029
|
+
if (character === "*") {
|
|
1030
|
+
state.index += 1;
|
|
1031
|
+
bounds = {
|
|
1032
|
+
maximum: Number.POSITIVE_INFINITY,
|
|
1033
|
+
minimum: 0
|
|
1034
|
+
};
|
|
1035
|
+
} else if (character === "+") {
|
|
1036
|
+
state.index += 1;
|
|
1037
|
+
bounds = {
|
|
1038
|
+
maximum: Number.POSITIVE_INFINITY,
|
|
1039
|
+
minimum: 1
|
|
1040
|
+
};
|
|
1041
|
+
} else if (character === "?") {
|
|
1042
|
+
state.index += 1;
|
|
1043
|
+
bounds = {
|
|
1044
|
+
maximum: 1,
|
|
1045
|
+
minimum: 0
|
|
1046
|
+
};
|
|
1047
|
+
} else if (character === "{") {
|
|
1048
|
+
state.index += 1;
|
|
1049
|
+
const braced = readBracedQuantifier(state);
|
|
1050
|
+
if (braced == null) {
|
|
1051
|
+
state.index = start;
|
|
1052
|
+
if (state.annexB) return void 0;
|
|
1053
|
+
refuseAnnexB(state, "An unescaped brace is not a pattern character.", "An unescaped brace", {
|
|
1054
|
+
end: start + 1,
|
|
1055
|
+
start
|
|
1056
|
+
});
|
|
1057
|
+
}
|
|
1058
|
+
bounds = validateQuantifierBounds(state, braced, start);
|
|
1059
|
+
}
|
|
1060
|
+
if (bounds == null) return void 0;
|
|
1061
|
+
return {
|
|
1062
|
+
greedy: !eat(state, "?"),
|
|
1063
|
+
maximum: bounds.maximum,
|
|
1064
|
+
minimum: bounds.minimum,
|
|
1065
|
+
span: span(start, state.index)
|
|
1066
|
+
};
|
|
1067
|
+
}
|
|
1068
|
+
function parseLookaround(state) {
|
|
1069
|
+
const start = state.index;
|
|
1070
|
+
const behind = state.source.startsWith("(?<=", start) ? false : state.source.startsWith("(?<!", start) ? true : void 0;
|
|
1071
|
+
const ahead = state.source.startsWith("(?=", start) ? false : state.source.startsWith("(?!", start) ? true : void 0;
|
|
1072
|
+
if (behind == null && ahead == null) return void 0;
|
|
1073
|
+
const negated = behind ?? ahead ?? false;
|
|
1074
|
+
state.index += behind == null ? 3 : 4;
|
|
1075
|
+
const body = parseDisjunction(state);
|
|
1076
|
+
expectGroupEnd(state, start);
|
|
1077
|
+
return {
|
|
1078
|
+
body,
|
|
1079
|
+
direction: behind == null ? "ahead" : "behind",
|
|
1080
|
+
kind: "lookaround",
|
|
1081
|
+
negated,
|
|
1082
|
+
span: span(start, state.index)
|
|
1083
|
+
};
|
|
1084
|
+
}
|
|
1085
|
+
function parseTerm(state) {
|
|
1086
|
+
const start = state.index;
|
|
1087
|
+
const lookaround = parseLookaround(state);
|
|
1088
|
+
if (lookaround != null) {
|
|
1089
|
+
if (state.annexB && lookaround.direction === "ahead") return quantifyLookahead(state, lookaround);
|
|
1090
|
+
return rejectQuantifier(state, lookaround);
|
|
1091
|
+
}
|
|
1092
|
+
const character = at(state, 0);
|
|
1093
|
+
if (character === "^" || character === "$") {
|
|
1094
|
+
state.index += 1;
|
|
1095
|
+
return rejectQuantifier(state, {
|
|
1096
|
+
assertion: character === "^" ? "start" : "end",
|
|
1097
|
+
kind: "assertion",
|
|
1098
|
+
span: span(start, state.index)
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
if (character === "\\" && (at(state, 1) === "b" || at(state, 1) === "B")) {
|
|
1102
|
+
const negated = at(state, 1) === "B";
|
|
1103
|
+
state.index += 2;
|
|
1104
|
+
return rejectQuantifier(state, {
|
|
1105
|
+
assertion: negated ? "non-word-boundary" : "word-boundary",
|
|
1106
|
+
kind: "assertion",
|
|
1107
|
+
span: span(start, state.index)
|
|
1108
|
+
});
|
|
1109
|
+
}
|
|
1110
|
+
const atom = parseAtom(state);
|
|
1111
|
+
const quantifier = parseQuantifier(state);
|
|
1112
|
+
if (quantifier == null) return atom;
|
|
1113
|
+
return {
|
|
1114
|
+
atom,
|
|
1115
|
+
kind: "quantified",
|
|
1116
|
+
quantifier,
|
|
1117
|
+
span: span(start, state.index)
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Read Annex B's QuantifiableAssertion Quantifier.
|
|
1122
|
+
*
|
|
1123
|
+
* The edition gives a quantified lookahead the semantics of a quantified
|
|
1124
|
+
* atom whose matcher is the assertion, which is exactly a non-capturing
|
|
1125
|
+
* group around it. The group therefore stands in for the lookahead as the
|
|
1126
|
+
* repeated atom, with the lookahead's span, so the matcher and the C
|
|
1127
|
+
* backend need no separate repetition form.
|
|
1128
|
+
*/
|
|
1129
|
+
function quantifyLookahead(state, lookahead) {
|
|
1130
|
+
const quantifier = parseQuantifier(state);
|
|
1131
|
+
if (quantifier == null) return lookahead;
|
|
1132
|
+
const location = lookahead.span;
|
|
1133
|
+
return {
|
|
1134
|
+
atom: {
|
|
1135
|
+
body: {
|
|
1136
|
+
alternatives: [{
|
|
1137
|
+
kind: "alternative",
|
|
1138
|
+
span: location,
|
|
1139
|
+
terms: [lookahead]
|
|
1140
|
+
}],
|
|
1141
|
+
kind: "disjunction",
|
|
1142
|
+
span: location
|
|
1143
|
+
},
|
|
1144
|
+
kind: "group",
|
|
1145
|
+
span: location
|
|
1146
|
+
},
|
|
1147
|
+
kind: "quantified",
|
|
1148
|
+
quantifier,
|
|
1149
|
+
span: span(location.start, state.index)
|
|
1150
|
+
};
|
|
1151
|
+
}
|
|
1152
|
+
/**
|
|
1153
|
+
* Reject a quantifier applied to an assertion. The edition admits a
|
|
1154
|
+
* quantified assertion only through Annex B, and Annex B quantifies a
|
|
1155
|
+
* lookahead alone, which `quantifyLookahead` reads. An edge assertion, a
|
|
1156
|
+
* word boundary, and a lookbehind are unquantifiable in every mode, so
|
|
1157
|
+
* only a lookahead in a parse without Annex B's grammar reports the
|
|
1158
|
+
* `unsupported` kind.
|
|
1159
|
+
*/
|
|
1160
|
+
function rejectQuantifier(state, term) {
|
|
1161
|
+
const character = at(state, 0);
|
|
1162
|
+
if (character === "*" || character === "+" || character === "?" || character === "{" && bracedQuantifierFollows(state)) {
|
|
1163
|
+
const location = {
|
|
1164
|
+
end: state.index + 1,
|
|
1165
|
+
start: state.index
|
|
1166
|
+
};
|
|
1167
|
+
const message = "An assertion cannot be quantified.";
|
|
1168
|
+
if (term.kind !== "lookaround" || term.direction !== "ahead") fail("invalid", message, location);
|
|
1169
|
+
refuseAnnexB(state, message, "A quantified lookahead", location);
|
|
1170
|
+
}
|
|
1171
|
+
return term;
|
|
1172
|
+
}
|
|
1173
|
+
/**
|
|
1174
|
+
* Whether the `{` at the cursor starts a quantifier.
|
|
1175
|
+
*
|
|
1176
|
+
* A brace that starts no quantifier is its own early error, reported
|
|
1177
|
+
* where the next term reads it, so an assertion followed by one is not
|
|
1178
|
+
* described as a quantified assertion.
|
|
1179
|
+
*/
|
|
1180
|
+
function bracedQuantifierFollows(state) {
|
|
1181
|
+
const resume = state.index;
|
|
1182
|
+
try {
|
|
1183
|
+
state.index += 1;
|
|
1184
|
+
return readBracedQuantifier(state) != null;
|
|
1185
|
+
} finally {
|
|
1186
|
+
state.index = resume;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
function parseAlternative(state) {
|
|
1190
|
+
const start = state.index;
|
|
1191
|
+
const terms = [];
|
|
1192
|
+
while (!done(state) && at(state, 0) !== "|" && at(state, 0) !== ")") terms.push(parseTerm(state));
|
|
1193
|
+
return {
|
|
1194
|
+
kind: "alternative",
|
|
1195
|
+
span: span(start, state.index),
|
|
1196
|
+
terms
|
|
1197
|
+
};
|
|
1198
|
+
}
|
|
1199
|
+
function parseDisjunction(state) {
|
|
1200
|
+
const start = state.index;
|
|
1201
|
+
state.depth += 1;
|
|
1202
|
+
if (state.depth > state.limits.nestingDepth) fail("limit", "A pattern nests above the reviewed depth limit.", {
|
|
1203
|
+
end: state.index,
|
|
1204
|
+
start
|
|
1205
|
+
});
|
|
1206
|
+
const disjunction = state.disjunctionCount;
|
|
1207
|
+
state.disjunctionCount += 1;
|
|
1208
|
+
const alternatives = [];
|
|
1209
|
+
for (let index = 0;; index += 1) {
|
|
1210
|
+
state.path.push({
|
|
1211
|
+
alternative: index,
|
|
1212
|
+
disjunction
|
|
1213
|
+
});
|
|
1214
|
+
alternatives.push(parseAlternative(state));
|
|
1215
|
+
state.path.pop();
|
|
1216
|
+
if (!eat(state, "|")) break;
|
|
1217
|
+
}
|
|
1218
|
+
state.depth -= 1;
|
|
1219
|
+
return {
|
|
1220
|
+
alternatives,
|
|
1221
|
+
kind: "disjunction",
|
|
1222
|
+
span: span(start, state.index)
|
|
1223
|
+
};
|
|
1224
|
+
}
|
|
1225
|
+
function parseFlags(text) {
|
|
1226
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1227
|
+
const admitted = "dgimsuvy";
|
|
1228
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
1229
|
+
const character = text[index] ?? "";
|
|
1230
|
+
if (!admitted.includes(character)) fail("invalid", "This regular expression flag is not defined.", span(index, index + 1), "flags");
|
|
1231
|
+
if (seen.has(character)) fail("invalid", "This regular expression flag is repeated.", span(index, index + 1), "flags");
|
|
1232
|
+
seen.add(character);
|
|
1233
|
+
}
|
|
1234
|
+
if (seen.has("u") && seen.has("v")) fail("invalid", "The u and v flags cannot be combined.", span(0, text.length), "flags");
|
|
1235
|
+
return {
|
|
1236
|
+
dotAll: seen.has("s"),
|
|
1237
|
+
global: seen.has("g"),
|
|
1238
|
+
hasIndices: seen.has("d"),
|
|
1239
|
+
ignoreCase: seen.has("i"),
|
|
1240
|
+
multiline: seen.has("m"),
|
|
1241
|
+
sticky: seen.has("y"),
|
|
1242
|
+
text,
|
|
1243
|
+
unicode: seen.has("u"),
|
|
1244
|
+
unicodeSets: seen.has("v")
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1247
|
+
/**
|
|
1248
|
+
* Whether two groups with one name can both participate in a match.
|
|
1249
|
+
*
|
|
1250
|
+
* They cannot when some enclosing disjunction holds them in different
|
|
1251
|
+
* alternatives, which is the condition the candidate edition uses to
|
|
1252
|
+
* admit a repeated group name.
|
|
1253
|
+
*/
|
|
1254
|
+
function mightBothParticipate(first, second) {
|
|
1255
|
+
const shared = Math.min(first.length, second.length);
|
|
1256
|
+
for (let index = 0; index < shared; index += 1) {
|
|
1257
|
+
const left = first[index];
|
|
1258
|
+
const right = second[index];
|
|
1259
|
+
if (left == null || right == null) break;
|
|
1260
|
+
if (left.disjunction !== right.disjunction) break;
|
|
1261
|
+
if (left.alternative !== right.alternative) return false;
|
|
1262
|
+
}
|
|
1263
|
+
return true;
|
|
1264
|
+
}
|
|
1265
|
+
/**
|
|
1266
|
+
* Order two alternative paths so that adjacent pairs decide the whole
|
|
1267
|
+
* set.
|
|
1268
|
+
*
|
|
1269
|
+
* Exclusivity is transitive along this order: if a sorted neighbor pair
|
|
1270
|
+
* diverges at a disjunction, every wider pair diverges at one too, so a
|
|
1271
|
+
* sorted scan replaces a comparison of every pair with every other.
|
|
1272
|
+
*/
|
|
1273
|
+
function comparePaths(first, second) {
|
|
1274
|
+
const shared = Math.min(first.length, second.length);
|
|
1275
|
+
for (let index = 0; index < shared; index += 1) {
|
|
1276
|
+
const left = first[index];
|
|
1277
|
+
const right = second[index];
|
|
1278
|
+
if (left == null || right == null) break;
|
|
1279
|
+
if (left.disjunction !== right.disjunction) return left.disjunction - right.disjunction;
|
|
1280
|
+
if (left.alternative !== right.alternative) return left.alternative - right.alternative;
|
|
1281
|
+
}
|
|
1282
|
+
return first.length - second.length;
|
|
1283
|
+
}
|
|
1284
|
+
function duplicateNameErrors(groups) {
|
|
1285
|
+
const errors = [];
|
|
1286
|
+
const byName = /* @__PURE__ */ new Map();
|
|
1287
|
+
for (const group of groups) {
|
|
1288
|
+
const existing = byName.get(group.name) ?? [];
|
|
1289
|
+
existing.push(group);
|
|
1290
|
+
byName.set(group.name, existing);
|
|
1291
|
+
}
|
|
1292
|
+
for (const named of byName.values()) {
|
|
1293
|
+
if (named.length < 2) continue;
|
|
1294
|
+
const ordered = named.toSorted((first, second) => comparePaths(first.path, second.path));
|
|
1295
|
+
for (let index = 1; index < ordered.length; index += 1) {
|
|
1296
|
+
const earlier = ordered[index - 1];
|
|
1297
|
+
const later = ordered[index];
|
|
1298
|
+
if (earlier == null || later == null) continue;
|
|
1299
|
+
if (!mightBothParticipate(earlier.path, later.path)) continue;
|
|
1300
|
+
const reported = later.span.start >= earlier.span.start ? later : earlier;
|
|
1301
|
+
errors.push({
|
|
1302
|
+
kind: "invalid",
|
|
1303
|
+
message: "This group name repeats one that can participate in the same match.",
|
|
1304
|
+
section: "pattern",
|
|
1305
|
+
span: reported.span
|
|
1306
|
+
});
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
return errors;
|
|
1310
|
+
}
|
|
1311
|
+
function validate(state) {
|
|
1312
|
+
const errors = [];
|
|
1313
|
+
const total = state.captures.length;
|
|
1314
|
+
for (const reference of state.backreferences) if (reference.index > total) errors.push(annexBError(state, "This backreference names no capturing group.", "A backreference that names no capturing group", reference.span));
|
|
1315
|
+
errors.push(...duplicateNameErrors(state.namedGroups));
|
|
1316
|
+
const indexByName = /* @__PURE__ */ new Map();
|
|
1317
|
+
for (const capture of state.captures) {
|
|
1318
|
+
if (capture.name == null) continue;
|
|
1319
|
+
const indices = indexByName.get(capture.name) ?? [];
|
|
1320
|
+
indices.push(capture.index);
|
|
1321
|
+
indexByName.set(capture.name, indices);
|
|
1322
|
+
}
|
|
1323
|
+
for (const reference of state.namedReferences) {
|
|
1324
|
+
if (indexByName.has(reference.name)) continue;
|
|
1325
|
+
errors.push({
|
|
1326
|
+
kind: "invalid",
|
|
1327
|
+
message: "This named backreference names no capturing group.",
|
|
1328
|
+
section: "pattern",
|
|
1329
|
+
span: reference.span
|
|
1330
|
+
});
|
|
1331
|
+
}
|
|
1332
|
+
for (const [name, shared] of state.namedReferenceIndices) shared.push(...indexByName.get(name) ?? []);
|
|
1333
|
+
errors.sort((first, second) => first.span.start - second.span.start);
|
|
1334
|
+
return errors;
|
|
1335
|
+
}
|
|
1336
|
+
/**
|
|
1337
|
+
* Keep one reported span inside the text it addresses.
|
|
1338
|
+
*
|
|
1339
|
+
* A production that reports the character it expected can name an offset
|
|
1340
|
+
* one past the end when the pattern stops early, so the reported span is
|
|
1341
|
+
* clamped once here rather than at every such production.
|
|
1342
|
+
*/
|
|
1343
|
+
function clampError(error, input) {
|
|
1344
|
+
const length = error.section === "flags" ? input.flags.length : input.source.length;
|
|
1345
|
+
const start = Math.min(Math.max(error.span.start, 0), length);
|
|
1346
|
+
const end = Math.min(Math.max(error.span.end, start), length);
|
|
1347
|
+
if (start === error.span.start && end === error.span.end) return error;
|
|
1348
|
+
return {
|
|
1349
|
+
...error,
|
|
1350
|
+
span: {
|
|
1351
|
+
end,
|
|
1352
|
+
start
|
|
1353
|
+
}
|
|
1354
|
+
};
|
|
1355
|
+
}
|
|
1356
|
+
/**
|
|
1357
|
+
* Parse and validate one pattern and flag text.
|
|
1358
|
+
*
|
|
1359
|
+
* The result is the owned representation of the pattern; no bootstrap
|
|
1360
|
+
* parser node reaches it, and no construct is admitted whose semantics
|
|
1361
|
+
* this unit cannot describe.
|
|
1362
|
+
*/
|
|
1363
|
+
function parseRegExpPattern(input) {
|
|
1364
|
+
const limits = input.limits ?? defaultRegExpPatternLimits;
|
|
1365
|
+
try {
|
|
1366
|
+
if (input.source.length > limits.sourceLength) fail("limit", "A pattern is longer than the reviewed limit.", {
|
|
1367
|
+
end: input.source.length,
|
|
1368
|
+
start: 0
|
|
1369
|
+
});
|
|
1370
|
+
const flags = parseFlags(input.flags);
|
|
1371
|
+
const unicodeMode = regExpUnicodeMode(flags);
|
|
1372
|
+
const annexB = input.annexB !== false && !unicodeMode;
|
|
1373
|
+
const state = {
|
|
1374
|
+
annexB,
|
|
1375
|
+
backreferences: [],
|
|
1376
|
+
captureCount: annexB ? countCapturingGroups(input.source) : 0,
|
|
1377
|
+
captures: [],
|
|
1378
|
+
depth: 0,
|
|
1379
|
+
disjunctionCount: 0,
|
|
1380
|
+
extensions: input.extensions ?? noExtensions,
|
|
1381
|
+
flags,
|
|
1382
|
+
groupNameDeclared: scanForGroupName(input.source),
|
|
1383
|
+
index: 0,
|
|
1384
|
+
limits,
|
|
1385
|
+
namedGroups: [],
|
|
1386
|
+
namedReferenceIndices: /* @__PURE__ */ new Map(),
|
|
1387
|
+
namedReferences: [],
|
|
1388
|
+
path: [],
|
|
1389
|
+
source: input.source,
|
|
1390
|
+
unicodeMode
|
|
1391
|
+
};
|
|
1392
|
+
const body = parseDisjunction(state);
|
|
1393
|
+
if (!done(state)) fail("invalid", "This character must be escaped.", {
|
|
1394
|
+
end: state.index + 1,
|
|
1395
|
+
start: state.index
|
|
1396
|
+
});
|
|
1397
|
+
const errors = validate(state).map((error) => clampError(error, input));
|
|
1398
|
+
if (errors.length > 0) return {
|
|
1399
|
+
errors,
|
|
1400
|
+
parsed: false
|
|
1401
|
+
};
|
|
1402
|
+
const groupNames = [...new Set(state.captures.flatMap((capture) => capture.name == null ? [] : [capture.name]))];
|
|
1403
|
+
return {
|
|
1404
|
+
errors: [],
|
|
1405
|
+
parsed: true,
|
|
1406
|
+
pattern: {
|
|
1407
|
+
body,
|
|
1408
|
+
captures: state.captures,
|
|
1409
|
+
flags,
|
|
1410
|
+
groupNames,
|
|
1411
|
+
kind: "pattern",
|
|
1412
|
+
source: input.source
|
|
1413
|
+
}
|
|
1414
|
+
};
|
|
1415
|
+
} catch (error) {
|
|
1416
|
+
if (error instanceof PatternFailure) return {
|
|
1417
|
+
errors: [clampError(error.error, input)],
|
|
1418
|
+
parsed: false
|
|
1419
|
+
};
|
|
1420
|
+
throw error;
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
/**
|
|
1424
|
+
* Parse one regular expression literal recorded by a frontend.
|
|
1425
|
+
*
|
|
1426
|
+
* The literal already carries its own source location, so the returned
|
|
1427
|
+
* spans stay relative to the pattern and flag text and the frontend maps
|
|
1428
|
+
* them back into the source it owns.
|
|
1429
|
+
*/
|
|
1430
|
+
function parseRegExpLiteral(literal, options = {}) {
|
|
1431
|
+
return parseRegExpPattern({
|
|
1432
|
+
flags: literal.flags,
|
|
1433
|
+
source: literal.pattern,
|
|
1434
|
+
...options
|
|
1435
|
+
});
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
//#endregion
|
|
1439
|
+
export { parseRegExpLiteral, parseRegExpPattern };
|
|
1440
|
+
//# sourceMappingURL=regexp-parse.js.map
|