ssqch 0.7.0 → 0.8.0
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/README.md +6 -4
- package/bin/ssqch.js +14 -48
- package/bundled/README.md +1247 -0
- package/bundled/completions/bash +840 -0
- package/bundled/completions/elvish +286 -0
- package/bundled/completions/fish +182 -0
- package/bundled/completions/powershell +318 -0
- package/bundled/completions/zsh +510 -0
- package/bundled/ssqch.d.ts +532 -0
- package/bundled/starter/engines/index.edge +11 -0
- package/bundled/starter/engines/index.ejs +11 -0
- package/bundled/starter/engines/index.hbs +11 -0
- package/bundled/starter/engines/index.liquid +11 -0
- package/bundled/starter/engines/layout.edge +35 -0
- package/bundled/starter/engines/layout.ejs +35 -0
- package/bundled/starter/engines/layout.hbs +35 -0
- package/bundled/starter/engines/layout.liquid +35 -0
- package/bundled/starter/gitignore +3 -0
- package/bundled/starter/jsconfig.json +11 -0
- package/bundled/starter/src/404.md +8 -0
- package/bundled/starter/src/_blueprints/posts.md +5 -0
- package/bundled/starter/src/_data/site.json +4 -0
- package/bundled/starter/src/_includes/layout.jinja +35 -0
- package/bundled/starter/src/index.jinja +11 -0
- package/bundled/starter/src/posts/i-know-what-i-saw.md +11 -0
- package/bundled/starter/src/posts/posts.json +4 -0
- package/bundled/starter/src/posts/raiders-of-the-lost-path.md +27 -0
- package/bundled/starter/src/posts/the-cache-of-the-wild.md +26 -0
- package/bundled/starter/src/posts/the-stump-has-moved.md +15 -0
- package/bundled/starter/src/posts/upon-further-review-a-stump.md +18 -0
- package/bundled/starter/ssqch.config.js +29 -0
- package/bundled/starter-docs/engines/edge/base.edge +41 -0
- package/bundled/starter-docs/engines/edge/containers/details.edge +5 -0
- package/bundled/starter-docs/engines/edge/containers/tab.edge +4 -0
- package/bundled/starter-docs/engines/edge/containers/tabs.edge +5 -0
- package/bundled/starter-docs/engines/edge/doc.edge +59 -0
- package/bundled/starter-docs/engines/edge/hooks/heading.edge +2 -0
- package/bundled/starter-docs/engines/ejs/base.ejs +41 -0
- package/bundled/starter-docs/engines/ejs/containers/details.ejs +5 -0
- package/bundled/starter-docs/engines/ejs/containers/tab.ejs +4 -0
- package/bundled/starter-docs/engines/ejs/containers/tabs.ejs +5 -0
- package/bundled/starter-docs/engines/ejs/doc.ejs +59 -0
- package/bundled/starter-docs/engines/ejs/hooks/heading.ejs +2 -0
- package/bundled/starter-docs/engines/hbs/base.hbs +41 -0
- package/bundled/starter-docs/engines/hbs/containers/details.hbs +5 -0
- package/bundled/starter-docs/engines/hbs/containers/tab.hbs +4 -0
- package/bundled/starter-docs/engines/hbs/containers/tabs.hbs +5 -0
- package/bundled/starter-docs/engines/hbs/doc.hbs +58 -0
- package/bundled/starter-docs/engines/hbs/hooks/heading.hbs +2 -0
- package/bundled/starter-docs/engines/liquid/base.liquid +41 -0
- package/bundled/starter-docs/engines/liquid/containers/details.liquid +5 -0
- package/bundled/starter-docs/engines/liquid/containers/tab.liquid +4 -0
- package/bundled/starter-docs/engines/liquid/containers/tabs.liquid +5 -0
- package/bundled/starter-docs/engines/liquid/doc.liquid +60 -0
- package/bundled/starter-docs/engines/liquid/hooks/heading.liquid +2 -0
- package/bundled/starter-docs/gitignore +4 -0
- package/bundled/starter-docs/src/404.md +12 -0
- package/bundled/starter-docs/src/_blueprints/docs.md +5 -0
- package/bundled/starter-docs/src/_data/nav.yml +10 -0
- package/bundled/starter-docs/src/_data/site.json +5 -0
- package/bundled/starter-docs/src/_includes/base.jinja +41 -0
- package/bundled/starter-docs/src/_includes/containers/details.jinja +5 -0
- package/bundled/starter-docs/src/_includes/containers/tab.jinja +4 -0
- package/bundled/starter-docs/src/_includes/containers/tabs.jinja +5 -0
- package/bundled/starter-docs/src/_includes/doc.jinja +59 -0
- package/bundled/starter-docs/src/_includes/hooks/heading.jinja +2 -0
- package/bundled/starter-docs/src/assets/docs.css +257 -0
- package/bundled/starter-docs/src/assets/docs.js +67 -0
- package/bundled/starter-docs/src/docs/docs.json +1 -0
- package/bundled/starter-docs/src/docs/guides/deploying.md +77 -0
- package/bundled/starter-docs/src/docs/guides/writing.md +65 -0
- package/bundled/starter-docs/src/docs/index.md +29 -0
- package/bundled/starter-docs/src/docs/install.md +42 -0
- package/bundled/starter-docs/src/index.md +13 -0
- package/bundled/starter-docs/ssqch.config.js +40 -0
- package/bundled/trailheads/from-component-frameworks.md +116 -0
- package/bundled/trailheads/from-docusaurus.md +55 -0
- package/bundled/trailheads/from-eleventy.md +140 -0
- package/bundled/trailheads/from-hexo.md +92 -0
- package/bundled/trailheads/from-mkdocs.md +45 -0
- package/bundled/trailheads/from-zola.md +71 -0
- package/lib/assets.js +235 -3
- package/lib/chunk-37KSIVNY.js +29 -0
- package/lib/chunk-3GYLOC6Y.js +1017 -0
- package/lib/chunk-3NYSSA2C.js +22 -0
- package/lib/chunk-3PHCFZEU.js +209 -0
- package/lib/chunk-4Q6WMTER.js +32 -0
- package/lib/chunk-6XYSFG6V.js +4423 -0
- package/lib/chunk-6XYSFG6V.js.LEGAL.txt +41 -0
- package/lib/chunk-AD3HAW3W.js +10 -0
- package/lib/chunk-AMBFWPZ4.js +118 -0
- package/lib/chunk-BOBPVAX2.js +37088 -0
- package/lib/chunk-BOBPVAX2.js.LEGAL.txt +27 -0
- package/lib/chunk-BRK4MXXO.js +565 -0
- package/lib/chunk-C35KIPAR.js +247 -0
- package/lib/chunk-CDEPYU7W.js +25 -0
- package/lib/chunk-CMUWRPSQ.js +113 -0
- package/lib/chunk-DDV6JEC2.js +46 -0
- package/lib/chunk-DHHQKW4F.js +71 -0
- package/lib/chunk-EIT4ZDT4.js +1485 -0
- package/lib/chunk-EZZUUPI4.js +20 -0
- package/lib/chunk-F65YUXZ7.js +334 -0
- package/lib/chunk-FVZ5JBRI.js +115 -0
- package/lib/chunk-FYGE634X.js +44 -0
- package/lib/chunk-GOE4ZSAB.js +75 -0
- package/lib/chunk-GSKVRAHF.js +44 -0
- package/lib/chunk-HR2RU2LY.js +999 -0
- package/lib/chunk-HXCLCXRT.js +20 -0
- package/lib/chunk-I3PS26CI.js +48 -0
- package/lib/chunk-IAFM5ITE.js +11 -0
- package/lib/chunk-IRHRK7XB.js +54 -0
- package/lib/chunk-JRT7TQQP.js +57 -0
- package/lib/chunk-JSPSMIJ2.js +217 -0
- package/lib/chunk-KBGE5MCY.js +175 -0
- package/lib/chunk-N5QTGQ5N.js +207 -0
- package/lib/chunk-NKXEPHRZ.js +136 -0
- package/lib/chunk-NUKCUDC4.js +146 -0
- package/lib/chunk-RI2L3VGO.js +376 -0
- package/lib/chunk-TDGBQNGW.js +251 -0
- package/lib/chunk-TN6EEDX2.js +3935 -0
- package/lib/chunk-TWCVHOKO.js +1229 -0
- package/lib/chunk-UGEGUUBT.js +29 -0
- package/lib/chunk-UU43WIZG.js +29 -0
- package/lib/chunk-VJWQSYEG.js +119 -0
- package/lib/chunk-VVPZ5PDP.js +160 -0
- package/lib/chunk-WMOVCQO6.js +1287 -0
- package/lib/chunk-WP3RPVUM.js +211 -0
- package/lib/chunk-YSH2NMIE.js +502 -0
- package/lib/cli.js +540 -0
- package/lib/elements.js +180 -3
- package/lib/image.js +27 -3
- package/lib/links.js +464 -3
- package/lib/minify.js +69 -3
- package/lib/redirects.js +152 -3
- package/lib/rss.js +139 -3
- package/lib/sass.js +221 -3
- package/lib/sitemap.js +88 -3
- package/lib/ssqch.js +28 -0
- package/package.json +83 -80
- package/src/serve/live.js +174 -0
- package/src/serve/not-found.html +31 -0
- package/types/globals.d.ts +3 -2
- package/types/image.d.ts +7 -3
- package/types/index.d.ts +18 -5
- package/types/links.d.ts +1 -1
- package/types/minify.d.ts +1 -1
- package/types/rss.d.ts +1 -1
- package/types/sass.d.ts +1 -1
- package/wasm/markdown.wasm +0 -0
- package/wasm/minify.wasm +0 -0
- package/wasm/sass.wasm +0 -0
- package/lib/index.js +0 -3
- package/platforms.json +0 -6
|
@@ -0,0 +1,1017 @@
|
|
|
1
|
+
import { createRequire as __ssqchCreateRequire } from "node:module";
|
|
2
|
+
const require = __ssqchCreateRequire(import.meta.url);
|
|
3
|
+
|
|
4
|
+
// src/rust-regex.ts
|
|
5
|
+
var RegexError = class extends Error {
|
|
6
|
+
};
|
|
7
|
+
function rustRegExp(pattern, flags = "", options = {}) {
|
|
8
|
+
const parser = new Parser(pattern);
|
|
9
|
+
const ast = parser.parse();
|
|
10
|
+
const ir = translate(ast, parser);
|
|
11
|
+
return emit(ir, parser, flags, MODIFIERS && options.flagGroups !== false);
|
|
12
|
+
}
|
|
13
|
+
var isWhitespace = (c) => c !== void 0 && new RegExp("^\\p{White_Space}$", "u").test(c);
|
|
14
|
+
var isHex = (c) => c !== void 0 && /^[0-9a-fA-F]$/.test(c);
|
|
15
|
+
var META = "\\.+*?()|[]{}^$#&-~";
|
|
16
|
+
var isEscapeable = (c) => META.includes(c) || c.codePointAt(0) <= 127 && !/^[0-9A-Za-z<>]$/.test(c);
|
|
17
|
+
var ASCII_CLASSES = {
|
|
18
|
+
alnum: [[48, 57], [65, 90], [97, 122]],
|
|
19
|
+
alpha: [[65, 90], [97, 122]],
|
|
20
|
+
ascii: [[0, 127]],
|
|
21
|
+
blank: [[9, 9], [32, 32]],
|
|
22
|
+
cntrl: [[0, 31], [127, 127]],
|
|
23
|
+
digit: [[48, 57]],
|
|
24
|
+
graph: [[33, 126]],
|
|
25
|
+
lower: [[97, 122]],
|
|
26
|
+
print: [[32, 126]],
|
|
27
|
+
punct: [[33, 47], [58, 64], [91, 96], [123, 126]],
|
|
28
|
+
space: [[9, 13], [32, 32]],
|
|
29
|
+
upper: [[65, 90]],
|
|
30
|
+
word: [[48, 57], [65, 90], [95, 95], [97, 122]],
|
|
31
|
+
xdigit: [[48, 57], [65, 70], [97, 102]]
|
|
32
|
+
};
|
|
33
|
+
var Parser = class {
|
|
34
|
+
pattern;
|
|
35
|
+
cs;
|
|
36
|
+
pos = 0;
|
|
37
|
+
ignoreWhitespace = false;
|
|
38
|
+
names = /* @__PURE__ */ new Map();
|
|
39
|
+
groups = [];
|
|
40
|
+
classes = [];
|
|
41
|
+
constructor(pattern) {
|
|
42
|
+
this.pattern = pattern;
|
|
43
|
+
this.cs = Array.from(pattern);
|
|
44
|
+
}
|
|
45
|
+
get eof() {
|
|
46
|
+
return this.pos >= this.cs.length;
|
|
47
|
+
}
|
|
48
|
+
/** The character at the parser, or "" at the end. */
|
|
49
|
+
ch() {
|
|
50
|
+
return this.cs[this.pos] ?? "";
|
|
51
|
+
}
|
|
52
|
+
here() {
|
|
53
|
+
return { start: this.pos, end: this.pos };
|
|
54
|
+
}
|
|
55
|
+
charSpan() {
|
|
56
|
+
return { start: this.pos, end: this.pos + 1 };
|
|
57
|
+
}
|
|
58
|
+
/** Moves past one character, and answers whether there's another. */
|
|
59
|
+
bump() {
|
|
60
|
+
if (this.eof) return false;
|
|
61
|
+
this.pos++;
|
|
62
|
+
return !this.eof;
|
|
63
|
+
}
|
|
64
|
+
bumpIf(prefix) {
|
|
65
|
+
const chars = Array.from(prefix);
|
|
66
|
+
for (let k = 0; k < chars.length; k++) if (this.cs[this.pos + k] !== chars[k]) return false;
|
|
67
|
+
this.pos += chars.length;
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
/** With `x`, past whitespace and `#` comments. */
|
|
71
|
+
bumpSpace() {
|
|
72
|
+
if (!this.ignoreWhitespace) return;
|
|
73
|
+
while (!this.eof) {
|
|
74
|
+
if (isWhitespace(this.ch())) this.pos++;
|
|
75
|
+
else if (this.ch() === "#") {
|
|
76
|
+
this.pos++;
|
|
77
|
+
while (!this.eof) {
|
|
78
|
+
const c = this.ch();
|
|
79
|
+
this.pos++;
|
|
80
|
+
if (c === "\n") break;
|
|
81
|
+
}
|
|
82
|
+
} else break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
bumpAndBumpSpace() {
|
|
86
|
+
if (!this.bump()) return false;
|
|
87
|
+
this.bumpSpace();
|
|
88
|
+
return !this.eof;
|
|
89
|
+
}
|
|
90
|
+
peek() {
|
|
91
|
+
return this.cs[this.pos + 1];
|
|
92
|
+
}
|
|
93
|
+
/** The next character, past whitespace and comments with `x`, as the crate finds it. */
|
|
94
|
+
peekSpace() {
|
|
95
|
+
if (!this.ignoreWhitespace) return this.peek();
|
|
96
|
+
if (this.eof) return void 0;
|
|
97
|
+
let start = this.pos + 1;
|
|
98
|
+
let inComment = false;
|
|
99
|
+
for (let i = start; i < this.cs.length; i++) {
|
|
100
|
+
const c = this.cs[i];
|
|
101
|
+
if (isWhitespace(c)) continue;
|
|
102
|
+
else if (!inComment && c === "#") inComment = true;
|
|
103
|
+
else if (inComment && c === "\n") inComment = false;
|
|
104
|
+
else {
|
|
105
|
+
start = i;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return this.cs[start];
|
|
110
|
+
}
|
|
111
|
+
error(span, message, aux) {
|
|
112
|
+
throw new RegexError(formatError(this.cs, span, message, aux));
|
|
113
|
+
}
|
|
114
|
+
parse() {
|
|
115
|
+
let concat = [];
|
|
116
|
+
for (; ; ) {
|
|
117
|
+
this.bumpSpace();
|
|
118
|
+
if (this.eof) break;
|
|
119
|
+
switch (this.ch()) {
|
|
120
|
+
case "(":
|
|
121
|
+
concat = this.pushGroup(concat);
|
|
122
|
+
break;
|
|
123
|
+
case ")":
|
|
124
|
+
concat = this.popGroup(concat);
|
|
125
|
+
break;
|
|
126
|
+
case "|":
|
|
127
|
+
concat = this.pushAlternate(concat);
|
|
128
|
+
break;
|
|
129
|
+
case "[":
|
|
130
|
+
concat.push(this.parseSetClass());
|
|
131
|
+
break;
|
|
132
|
+
case "?":
|
|
133
|
+
concat = this.repetition(concat, 0, 1);
|
|
134
|
+
break;
|
|
135
|
+
case "*":
|
|
136
|
+
concat = this.repetition(concat, 0, Infinity);
|
|
137
|
+
break;
|
|
138
|
+
case "+":
|
|
139
|
+
concat = this.repetition(concat, 1, Infinity);
|
|
140
|
+
break;
|
|
141
|
+
case "{":
|
|
142
|
+
concat = this.countedRepetition(concat);
|
|
143
|
+
break;
|
|
144
|
+
default:
|
|
145
|
+
concat.push(this.parsePrimitive());
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return this.popGroupEnd(concat);
|
|
149
|
+
}
|
|
150
|
+
pushAlternate(concat) {
|
|
151
|
+
const top = this.groups[this.groups.length - 1];
|
|
152
|
+
if (top?.k === "alt") top.asts.push(asConcat(concat));
|
|
153
|
+
else this.groups.push({ k: "alt", asts: [asConcat(concat)] });
|
|
154
|
+
this.bump();
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
157
|
+
pushGroup(concat) {
|
|
158
|
+
const open = this.charSpan();
|
|
159
|
+
this.bump();
|
|
160
|
+
this.bumpSpace();
|
|
161
|
+
if (this.bumpIf("?=") || this.bumpIf("?!") || this.bumpIf("?<=") || this.bumpIf("?<!")) {
|
|
162
|
+
this.error({ start: open.start, end: this.pos }, "look-around, including look-ahead and look-behind, is not supported");
|
|
163
|
+
}
|
|
164
|
+
const inner = this.here();
|
|
165
|
+
if (this.bumpIf("?P<") || this.bumpIf("?<")) {
|
|
166
|
+
this.captureName();
|
|
167
|
+
return this.enterGroup(concat, { capture: true, open });
|
|
168
|
+
}
|
|
169
|
+
if (this.bumpIf("?")) {
|
|
170
|
+
if (this.eof) this.error(open, "unclosed group");
|
|
171
|
+
const flags = this.parseFlags();
|
|
172
|
+
const end = this.ch();
|
|
173
|
+
this.bump();
|
|
174
|
+
if (end === ")") {
|
|
175
|
+
if (flags.length === 0) this.error(inner, "repetition operator missing expression");
|
|
176
|
+
const x = flagState(flags, "x");
|
|
177
|
+
if (x !== void 0) this.ignoreWhitespace = x;
|
|
178
|
+
concat.push({ k: "flags", items: flags });
|
|
179
|
+
return concat;
|
|
180
|
+
}
|
|
181
|
+
return this.enterGroup(concat, { capture: false, flags, open });
|
|
182
|
+
}
|
|
183
|
+
return this.enterGroup(concat, { capture: true, open });
|
|
184
|
+
}
|
|
185
|
+
enterGroup(concat, group) {
|
|
186
|
+
this.groups.push({ k: "group", concat, ...group, ignoreWhitespace: this.ignoreWhitespace });
|
|
187
|
+
const x = group.flags ? flagState(group.flags, "x") : void 0;
|
|
188
|
+
if (x !== void 0) this.ignoreWhitespace = x;
|
|
189
|
+
return [];
|
|
190
|
+
}
|
|
191
|
+
captureName() {
|
|
192
|
+
if (this.eof) this.error(this.here(), "unclosed capture group name");
|
|
193
|
+
const start = this.pos;
|
|
194
|
+
for (; ; ) {
|
|
195
|
+
if (this.ch() === ">") break;
|
|
196
|
+
const first = this.pos === start;
|
|
197
|
+
if (!(this.ch() === "_" || new RegExp("^\\p{Alphabetic}$", "u").test(this.ch()) || !first && (this.ch() === "." || this.ch() === "[" || this.ch() === "]" || new RegExp("^\\p{N}$", "u").test(this.ch())))) {
|
|
198
|
+
this.error(this.charSpan(), "invalid capture group character");
|
|
199
|
+
}
|
|
200
|
+
if (!this.bump()) break;
|
|
201
|
+
}
|
|
202
|
+
const end = this.pos;
|
|
203
|
+
if (this.eof) this.error(this.here(), "unclosed capture group name");
|
|
204
|
+
this.bump();
|
|
205
|
+
if (end === start) this.error({ start, end: start }, "empty capture group name");
|
|
206
|
+
const name = this.cs.slice(start, end).join("");
|
|
207
|
+
const span = { start, end };
|
|
208
|
+
const before = this.names.get(name);
|
|
209
|
+
if (before) this.error(span, "duplicate capture group name", before);
|
|
210
|
+
this.names.set(name, span);
|
|
211
|
+
}
|
|
212
|
+
parseFlags() {
|
|
213
|
+
const items = [];
|
|
214
|
+
let dangling;
|
|
215
|
+
while (this.ch() !== ":" && this.ch() !== ")") {
|
|
216
|
+
if (this.ch() === "-") {
|
|
217
|
+
dangling = this.charSpan();
|
|
218
|
+
const before = items.find((i) => i.flag === "-");
|
|
219
|
+
if (before) this.error(this.charSpan(), "flag negation operator repeated", before.span);
|
|
220
|
+
items.push({ flag: "-", span: this.charSpan() });
|
|
221
|
+
} else {
|
|
222
|
+
dangling = void 0;
|
|
223
|
+
if (!"imsUuRx".includes(this.ch()) || this.ch() === "") this.error(this.charSpan(), "unrecognized flag");
|
|
224
|
+
const before = items.find((i) => i.flag === this.ch());
|
|
225
|
+
if (before) this.error(this.charSpan(), "duplicate flag", before.span);
|
|
226
|
+
items.push({ flag: this.ch(), span: this.charSpan() });
|
|
227
|
+
}
|
|
228
|
+
if (!this.bump()) this.error(this.here(), "expected flag but got end of regex");
|
|
229
|
+
}
|
|
230
|
+
if (dangling) this.error(dangling, "dangling flag negation operator");
|
|
231
|
+
return items;
|
|
232
|
+
}
|
|
233
|
+
popGroup(concat) {
|
|
234
|
+
let top = this.groups.pop();
|
|
235
|
+
let alt;
|
|
236
|
+
if (top?.k === "alt") {
|
|
237
|
+
alt = top.asts;
|
|
238
|
+
top = this.groups.pop();
|
|
239
|
+
}
|
|
240
|
+
if (top?.k !== "group") this.error(this.charSpan(), "unopened group");
|
|
241
|
+
this.ignoreWhitespace = top.ignoreWhitespace;
|
|
242
|
+
this.bump();
|
|
243
|
+
const body = alt ? { k: "alt", asts: [...alt, asConcat(concat)] } : asConcat(concat);
|
|
244
|
+
top.concat.push({ k: "group", capture: top.capture, flags: top.flags, ast: body });
|
|
245
|
+
return top.concat;
|
|
246
|
+
}
|
|
247
|
+
popGroupEnd(concat) {
|
|
248
|
+
const top = this.groups.pop();
|
|
249
|
+
let ast;
|
|
250
|
+
if (top === void 0) ast = asConcat(concat);
|
|
251
|
+
else if (top.k === "alt") ast = { k: "alt", asts: [...top.asts, asConcat(concat)] };
|
|
252
|
+
else this.error(top.open, "unclosed group");
|
|
253
|
+
const next = this.groups.pop();
|
|
254
|
+
if (next?.k === "group") this.error(next.open, "unclosed group");
|
|
255
|
+
return ast;
|
|
256
|
+
}
|
|
257
|
+
repetition(concat, min, max) {
|
|
258
|
+
const ast = concat.pop();
|
|
259
|
+
if (ast === void 0 || ast.k === "empty" || ast.k === "flags") this.error(this.here(), "repetition operator missing expression");
|
|
260
|
+
let greedy = true;
|
|
261
|
+
if (this.bump() && this.ch() === "?") {
|
|
262
|
+
greedy = false;
|
|
263
|
+
this.bump();
|
|
264
|
+
}
|
|
265
|
+
concat.push({ k: "rep", ast, min, max, greedy });
|
|
266
|
+
return concat;
|
|
267
|
+
}
|
|
268
|
+
countedRepetition(concat) {
|
|
269
|
+
const start = this.pos;
|
|
270
|
+
const ast = concat.pop();
|
|
271
|
+
if (ast === void 0 || ast.k === "empty" || ast.k === "flags") this.error(this.here(), "repetition operator missing expression");
|
|
272
|
+
const unclosed = () => this.error({ start, end: this.pos }, "unclosed counted repetition");
|
|
273
|
+
const need = (n) => typeof n === "number" ? n : this.error(n, "repetition quantifier expects a valid decimal");
|
|
274
|
+
if (!this.bumpAndBumpSpace()) unclosed();
|
|
275
|
+
const first = this.decimal();
|
|
276
|
+
if (this.eof) unclosed();
|
|
277
|
+
let min, max;
|
|
278
|
+
if (this.ch() === ",") {
|
|
279
|
+
if (!this.bumpAndBumpSpace()) unclosed();
|
|
280
|
+
if (this.ch() !== "}") {
|
|
281
|
+
min = need(first);
|
|
282
|
+
max = need(this.decimal());
|
|
283
|
+
} else {
|
|
284
|
+
min = need(first);
|
|
285
|
+
max = Infinity;
|
|
286
|
+
}
|
|
287
|
+
} else {
|
|
288
|
+
min = max = need(first);
|
|
289
|
+
}
|
|
290
|
+
if (this.eof || this.ch() !== "}") unclosed();
|
|
291
|
+
let greedy = true;
|
|
292
|
+
if (this.bumpAndBumpSpace() && this.ch() === "?") {
|
|
293
|
+
greedy = false;
|
|
294
|
+
this.bump();
|
|
295
|
+
}
|
|
296
|
+
if (min > max) this.error({ start, end: this.pos }, "invalid repetition count range, the start must be <= the end");
|
|
297
|
+
concat.push({ k: "rep", ast, min, max, greedy });
|
|
298
|
+
return concat;
|
|
299
|
+
}
|
|
300
|
+
/** Digits with whitespace around them (whatever `x` says), or the span where they should be. */
|
|
301
|
+
decimal() {
|
|
302
|
+
while (!this.eof && isWhitespace(this.ch())) this.bump();
|
|
303
|
+
const start = this.pos;
|
|
304
|
+
let digits = "";
|
|
305
|
+
while (!this.eof && this.ch() >= "0" && this.ch() <= "9") {
|
|
306
|
+
digits += this.ch();
|
|
307
|
+
this.bumpAndBumpSpace();
|
|
308
|
+
}
|
|
309
|
+
const span = { start, end: this.pos };
|
|
310
|
+
while (!this.eof && isWhitespace(this.ch())) this.bumpAndBumpSpace();
|
|
311
|
+
if (digits === "") return span;
|
|
312
|
+
const n = Number(digits);
|
|
313
|
+
if (n > 4294967295) this.error(span, "decimal literal invalid");
|
|
314
|
+
return n;
|
|
315
|
+
}
|
|
316
|
+
parsePrimitive() {
|
|
317
|
+
const span = this.charSpan();
|
|
318
|
+
switch (this.ch()) {
|
|
319
|
+
case "\\":
|
|
320
|
+
return this.parseEscape();
|
|
321
|
+
case ".":
|
|
322
|
+
this.bump();
|
|
323
|
+
return { k: "dot", span };
|
|
324
|
+
case "^":
|
|
325
|
+
this.bump();
|
|
326
|
+
return { k: "look", kind: "startLine", span };
|
|
327
|
+
case "$":
|
|
328
|
+
this.bump();
|
|
329
|
+
return { k: "look", kind: "endLine", span };
|
|
330
|
+
}
|
|
331
|
+
const c = this.ch().codePointAt(0);
|
|
332
|
+
this.bump();
|
|
333
|
+
return { k: "lit", c, byte: false, span };
|
|
334
|
+
}
|
|
335
|
+
parseEscape() {
|
|
336
|
+
const start = this.pos;
|
|
337
|
+
if (!this.bump()) this.error({ start, end: this.pos }, "incomplete escape sequence, reached end of pattern prematurely");
|
|
338
|
+
const c = this.ch();
|
|
339
|
+
if (c >= "0" && c <= "9") this.error({ start, end: this.pos + 1 }, "backreferences are not supported");
|
|
340
|
+
if (c === "x" || c === "u" || c === "U") {
|
|
341
|
+
const lit = this.parseHex();
|
|
342
|
+
lit.span.start = start;
|
|
343
|
+
return lit;
|
|
344
|
+
}
|
|
345
|
+
if (c === "p" || c === "P") {
|
|
346
|
+
const prop = this.parseUnicodeClass();
|
|
347
|
+
prop.span.start = start;
|
|
348
|
+
return prop;
|
|
349
|
+
}
|
|
350
|
+
if ("dswDSW".includes(c)) {
|
|
351
|
+
this.bump();
|
|
352
|
+
return { k: "perl", kind: c.toLowerCase(), neg: c === c.toUpperCase(), span: { start, end: this.pos } };
|
|
353
|
+
}
|
|
354
|
+
this.bump();
|
|
355
|
+
const span = { start, end: this.pos };
|
|
356
|
+
if (isEscapeable(c)) return { k: "lit", c: c.codePointAt(0), byte: false, span };
|
|
357
|
+
const special = { a: 7, f: 12, t: 9, n: 10, r: 13, v: 11 };
|
|
358
|
+
if (special[c] !== void 0) return { k: "lit", c: special[c], byte: false, span };
|
|
359
|
+
switch (c) {
|
|
360
|
+
case "A":
|
|
361
|
+
return { k: "look", kind: "start", span };
|
|
362
|
+
case "z":
|
|
363
|
+
return { k: "look", kind: "end", span };
|
|
364
|
+
case "B":
|
|
365
|
+
return { k: "look", kind: "notWord", span };
|
|
366
|
+
case "<":
|
|
367
|
+
return { k: "look", kind: "wordStart", span };
|
|
368
|
+
case ">":
|
|
369
|
+
return { k: "look", kind: "wordEnd", span };
|
|
370
|
+
case "b": {
|
|
371
|
+
const kind = !this.eof && this.ch() === "{" ? this.specialWordBoundary(start) : void 0;
|
|
372
|
+
return { k: "look", kind: kind ?? "word", span: { start, end: this.pos } };
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return this.error(span, "unrecognized escape sequence");
|
|
376
|
+
}
|
|
377
|
+
/** `\b{start}` and the others, or nothing when the `{` begins a repetition (`\b{2}`). */
|
|
378
|
+
specialWordBoundary(wbStart) {
|
|
379
|
+
const valid2 = (c) => /^[A-Za-z-]$/.test(c);
|
|
380
|
+
const start = this.pos;
|
|
381
|
+
if (!this.bumpAndBumpSpace()) this.error({ start: wbStart, end: this.pos }, "found either the beginning of a special word boundary or a bounded repetition on a \\b with an opening brace, but no closing brace");
|
|
382
|
+
const contents = this.pos;
|
|
383
|
+
if (!valid2(this.ch())) {
|
|
384
|
+
this.pos = start;
|
|
385
|
+
return void 0;
|
|
386
|
+
}
|
|
387
|
+
let name = "";
|
|
388
|
+
while (!this.eof && valid2(this.ch())) {
|
|
389
|
+
name += this.ch();
|
|
390
|
+
this.bumpAndBumpSpace();
|
|
391
|
+
}
|
|
392
|
+
if (this.eof || this.ch() !== "}") this.error({ start, end: this.pos }, "special word boundary assertion is either unclosed or contains an invalid character");
|
|
393
|
+
const end = this.pos;
|
|
394
|
+
this.bump();
|
|
395
|
+
const kinds = { start: "wordStart", end: "wordEnd", "start-half": "wordStartHalf", "end-half": "wordEndHalf" };
|
|
396
|
+
return kinds[name] ?? this.error({ start: contents, end }, "unrecognized special word boundary assertion, valid choices are: start, end, start-half or end-half");
|
|
397
|
+
}
|
|
398
|
+
parseHex() {
|
|
399
|
+
const digits = this.ch() === "x" ? 2 : this.ch() === "u" ? 4 : 8;
|
|
400
|
+
const byte = this.ch() === "x";
|
|
401
|
+
if (!this.bumpAndBumpSpace()) this.error(this.here(), "incomplete escape sequence, reached end of pattern prematurely");
|
|
402
|
+
let hex = "";
|
|
403
|
+
let start, end;
|
|
404
|
+
if (this.ch() === "{") {
|
|
405
|
+
const brace = this.pos;
|
|
406
|
+
start = this.pos + 1;
|
|
407
|
+
while (this.bumpAndBumpSpace() && this.ch() !== "}") {
|
|
408
|
+
if (!isHex(this.ch())) this.error(this.charSpan(), "invalid hexadecimal digit");
|
|
409
|
+
hex += this.ch();
|
|
410
|
+
}
|
|
411
|
+
if (this.eof) this.error({ start: brace, end: this.pos }, "incomplete escape sequence, reached end of pattern prematurely");
|
|
412
|
+
end = this.pos;
|
|
413
|
+
this.bumpAndBumpSpace();
|
|
414
|
+
if (hex === "") this.error({ start: brace, end: this.pos }, "hexadecimal literal empty");
|
|
415
|
+
} else {
|
|
416
|
+
start = this.pos;
|
|
417
|
+
for (let i = 0; i < digits; i++) {
|
|
418
|
+
if (i > 0 && !this.bumpAndBumpSpace()) this.error(this.here(), "incomplete escape sequence, reached end of pattern prematurely");
|
|
419
|
+
if (!isHex(this.ch())) this.error(this.charSpan(), "invalid hexadecimal digit");
|
|
420
|
+
hex += this.ch();
|
|
421
|
+
}
|
|
422
|
+
this.bumpAndBumpSpace();
|
|
423
|
+
end = this.pos;
|
|
424
|
+
}
|
|
425
|
+
const c = hex.length > 8 ? Infinity : parseInt(hex, 16);
|
|
426
|
+
if (!(c <= 1114111) || c >= 55296 && c <= 57343) this.error({ start, end }, "hexadecimal literal is not a Unicode scalar value");
|
|
427
|
+
return { k: "lit", c, byte: byte && c <= 255, span: { start, end: this.pos } };
|
|
428
|
+
}
|
|
429
|
+
parseUnicodeClass() {
|
|
430
|
+
const neg = this.ch() === "P";
|
|
431
|
+
if (!this.bumpAndBumpSpace()) this.error(this.here(), "incomplete escape sequence, reached end of pattern prematurely");
|
|
432
|
+
if (this.ch() === "{") {
|
|
433
|
+
const start2 = this.pos + 1;
|
|
434
|
+
let name = "";
|
|
435
|
+
while (this.bumpAndBumpSpace() && this.ch() !== "}") name += this.ch();
|
|
436
|
+
if (this.eof) this.error(this.here(), "incomplete escape sequence, reached end of pattern prematurely");
|
|
437
|
+
this.bump();
|
|
438
|
+
const span = { start: start2, end: this.pos };
|
|
439
|
+
let at = name.indexOf("!=");
|
|
440
|
+
if (at !== -1) return { k: "prop", neg, name: name.slice(0, at), op: "!=", value: name.slice(at + 2), span };
|
|
441
|
+
at = name.indexOf(":");
|
|
442
|
+
if (at === -1) at = name.indexOf("=");
|
|
443
|
+
if (at !== -1) return { k: "prop", neg, name: name.slice(0, at), op: "=", value: name.slice(at + 1), span };
|
|
444
|
+
return { k: "prop", neg, name, span };
|
|
445
|
+
}
|
|
446
|
+
const start = this.pos;
|
|
447
|
+
const c = this.ch();
|
|
448
|
+
if (c === "\\") this.error(this.charSpan(), "invalid Unicode character class");
|
|
449
|
+
this.bumpAndBumpSpace();
|
|
450
|
+
return { k: "prop", neg, name: c, span: { start, end: this.pos } };
|
|
451
|
+
}
|
|
452
|
+
parseSetClass() {
|
|
453
|
+
let union = [];
|
|
454
|
+
for (; ; ) {
|
|
455
|
+
this.bumpSpace();
|
|
456
|
+
if (this.eof) this.unclosedClass();
|
|
457
|
+
if (this.ch() === "[") {
|
|
458
|
+
if (this.classes.length) {
|
|
459
|
+
const ascii = this.asciiClass();
|
|
460
|
+
if (ascii) {
|
|
461
|
+
union.push(ascii);
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
union = this.pushClassOpen(union);
|
|
466
|
+
} else if (this.ch() === "]") {
|
|
467
|
+
const done = this.popClass(union);
|
|
468
|
+
if (done.k === "bracket") return done;
|
|
469
|
+
union = done.union;
|
|
470
|
+
} else if (this.ch() === "&" && this.peek() === "&") {
|
|
471
|
+
this.pos += 2;
|
|
472
|
+
union = this.pushClassOp("&&", union);
|
|
473
|
+
} else if (this.ch() === "-" && this.peek() === "-") {
|
|
474
|
+
this.pos += 2;
|
|
475
|
+
union = this.pushClassOp("--", union);
|
|
476
|
+
} else if (this.ch() === "~" && this.peek() === "~") {
|
|
477
|
+
this.pos += 2;
|
|
478
|
+
union = this.pushClassOp("~~", union);
|
|
479
|
+
} else {
|
|
480
|
+
union.push(this.setClassRange());
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
unclosedClass() {
|
|
485
|
+
for (let i = this.classes.length - 1; i >= 0; i--) {
|
|
486
|
+
const state = this.classes[i];
|
|
487
|
+
if (state.k === "open") this.error({ start: state.start, end: state.end }, "unclosed character class");
|
|
488
|
+
}
|
|
489
|
+
throw new Error("no open character class");
|
|
490
|
+
}
|
|
491
|
+
setClassRange() {
|
|
492
|
+
const first = this.setClassItem();
|
|
493
|
+
this.bumpSpace();
|
|
494
|
+
if (this.eof) this.unclosedClass();
|
|
495
|
+
if (this.ch() !== "-" || this.peekSpace() === "]" || this.peekSpace() === "-") {
|
|
496
|
+
if (first.k === "lit" || first.k === "perl" || first.k === "prop") return first;
|
|
497
|
+
return this.error(first.span, "invalid escape sequence found in character class");
|
|
498
|
+
}
|
|
499
|
+
if (!this.bumpAndBumpSpace()) this.unclosedClass();
|
|
500
|
+
const second = this.setClassItem();
|
|
501
|
+
if (first.k !== "lit") this.error(first.span, "invalid range boundary, must be a literal");
|
|
502
|
+
if (second.k !== "lit") this.error(second.span, "invalid range boundary, must be a literal");
|
|
503
|
+
const span = { start: first.span.start, end: second.span.end };
|
|
504
|
+
if (first.c > second.c) this.error(span, "invalid character class range, the start must be <= the end");
|
|
505
|
+
return { k: "range", lo: first, hi: second, span };
|
|
506
|
+
}
|
|
507
|
+
setClassItem() {
|
|
508
|
+
if (this.ch() === "\\") return this.parseEscape();
|
|
509
|
+
const lit = { k: "lit", c: this.ch().codePointAt(0), byte: false, span: this.charSpan() };
|
|
510
|
+
this.bump();
|
|
511
|
+
return lit;
|
|
512
|
+
}
|
|
513
|
+
pushClassOpen(parent) {
|
|
514
|
+
const start = this.pos;
|
|
515
|
+
const unclosed = () => this.error({ start, end: this.pos }, "unclosed character class");
|
|
516
|
+
if (!this.bumpAndBumpSpace()) unclosed();
|
|
517
|
+
let neg = false;
|
|
518
|
+
if (this.ch() === "^") {
|
|
519
|
+
neg = true;
|
|
520
|
+
if (!this.bumpAndBumpSpace()) unclosed();
|
|
521
|
+
}
|
|
522
|
+
const union = [];
|
|
523
|
+
while (this.ch() === "-") {
|
|
524
|
+
union.push({ k: "lit", c: 45, byte: false, span: this.charSpan() });
|
|
525
|
+
if (!this.bumpAndBumpSpace()) this.error({ start, end: start }, "unclosed character class");
|
|
526
|
+
}
|
|
527
|
+
if (union.length === 0 && this.ch() === "]") {
|
|
528
|
+
union.push({ k: "lit", c: 93, byte: false, span: this.charSpan() });
|
|
529
|
+
if (!this.bumpAndBumpSpace()) unclosed();
|
|
530
|
+
}
|
|
531
|
+
this.classes.push({ k: "open", union: parent, neg, start, end: this.pos });
|
|
532
|
+
return union;
|
|
533
|
+
}
|
|
534
|
+
popClass(union) {
|
|
535
|
+
const set = this.popClassOp({ k: "union", items: union });
|
|
536
|
+
const state = this.classes.pop();
|
|
537
|
+
if (state?.k !== "open") throw new Error("unexpected character class state");
|
|
538
|
+
this.bump();
|
|
539
|
+
const bracket = { k: "bracket", neg: state.neg, set, span: { start: state.start, end: this.pos } };
|
|
540
|
+
if (this.classes.length === 0) return bracket;
|
|
541
|
+
state.union.push(bracket);
|
|
542
|
+
return { k: "nested", union: state.union };
|
|
543
|
+
}
|
|
544
|
+
pushClassOp(op, union) {
|
|
545
|
+
const lhs = this.popClassOp({ k: "union", items: union });
|
|
546
|
+
this.classes.push({ k: "op", op, lhs });
|
|
547
|
+
return [];
|
|
548
|
+
}
|
|
549
|
+
popClassOp(rhs) {
|
|
550
|
+
const top = this.classes[this.classes.length - 1];
|
|
551
|
+
if (top?.k !== "op") return rhs;
|
|
552
|
+
this.classes.pop();
|
|
553
|
+
return { k: "op", op: top.op, lhs: top.lhs, rhs };
|
|
554
|
+
}
|
|
555
|
+
/** `[:alpha:]` inside a class, or nothing (and the parser back where it was) when it isn't one. */
|
|
556
|
+
asciiClass() {
|
|
557
|
+
const start = this.pos;
|
|
558
|
+
const back = () => {
|
|
559
|
+
this.pos = start;
|
|
560
|
+
return void 0;
|
|
561
|
+
};
|
|
562
|
+
if (!this.bump() || this.ch() !== ":") return back();
|
|
563
|
+
if (!this.bump()) return back();
|
|
564
|
+
let neg = false;
|
|
565
|
+
if (this.ch() === "^") {
|
|
566
|
+
neg = true;
|
|
567
|
+
if (!this.bump()) return back();
|
|
568
|
+
}
|
|
569
|
+
const nameStart = this.pos;
|
|
570
|
+
while (this.ch() !== ":" && this.bump()) {
|
|
571
|
+
}
|
|
572
|
+
if (this.eof) return back();
|
|
573
|
+
const name = this.cs.slice(nameStart, this.pos).join("");
|
|
574
|
+
if (!this.bumpIf(":]")) return back();
|
|
575
|
+
if (!Object.hasOwn(ASCII_CLASSES, name)) return back();
|
|
576
|
+
return { k: "ascii", name, neg, span: { start, end: this.pos } };
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
function asConcat(asts) {
|
|
580
|
+
if (asts.length === 0) return { k: "empty" };
|
|
581
|
+
return asts.length === 1 ? asts[0] : { k: "concat", asts };
|
|
582
|
+
}
|
|
583
|
+
function flagState(items, flag) {
|
|
584
|
+
let on = true;
|
|
585
|
+
for (const item of items) {
|
|
586
|
+
if (item.flag === "-") on = false;
|
|
587
|
+
else if (item.flag === flag) return on;
|
|
588
|
+
}
|
|
589
|
+
return void 0;
|
|
590
|
+
}
|
|
591
|
+
function formatError(cs, span, message, aux) {
|
|
592
|
+
const pattern = cs.join("");
|
|
593
|
+
const position = (index) => {
|
|
594
|
+
let line = 1, column = 1;
|
|
595
|
+
for (let i = 0; i < index && i < cs.length; i++) {
|
|
596
|
+
if (cs[i] === "\n") {
|
|
597
|
+
line++;
|
|
598
|
+
column = 1;
|
|
599
|
+
} else column++;
|
|
600
|
+
}
|
|
601
|
+
return { line, column };
|
|
602
|
+
};
|
|
603
|
+
const lines = pattern.split("\n");
|
|
604
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
605
|
+
for (let i = 0; i < lines.length; i++) if (lines[i].endsWith("\r")) lines[i] = lines[i].slice(0, -1);
|
|
606
|
+
const lineCount = lines.length + (pattern.endsWith("\n") ? 1 : 0);
|
|
607
|
+
const width = lineCount <= 1 ? 0 : String(lineCount).length;
|
|
608
|
+
const byLine = Array.from({ length: Math.max(lineCount, 1) }, () => []);
|
|
609
|
+
const multi = [];
|
|
610
|
+
for (const s of aux ? [span, aux] : [span]) {
|
|
611
|
+
const start = position(s.start), end = position(s.end);
|
|
612
|
+
if (start.line === end.line) {
|
|
613
|
+
byLine[start.line - 1]?.push({ start, end, offset: s.start });
|
|
614
|
+
byLine[start.line - 1]?.sort((a, b) => a.offset - b.offset);
|
|
615
|
+
} else multi.push({ start, end });
|
|
616
|
+
}
|
|
617
|
+
let notated = "";
|
|
618
|
+
lines.forEach((line, i) => {
|
|
619
|
+
notated += width > 0 ? `${String(i + 1).padStart(width)}: ` : " ";
|
|
620
|
+
notated += `${line}
|
|
621
|
+
`;
|
|
622
|
+
const spans = byLine[i];
|
|
623
|
+
if (spans?.length) {
|
|
624
|
+
let notes = " ".repeat(width === 0 ? 4 : 2 + width);
|
|
625
|
+
let pos = 0;
|
|
626
|
+
for (const s of spans) {
|
|
627
|
+
for (; pos < s.start.column - 1; pos++) notes += " ";
|
|
628
|
+
const len = Math.max(1, s.end.column - s.start.column);
|
|
629
|
+
notes += "^".repeat(len);
|
|
630
|
+
pos += len;
|
|
631
|
+
}
|
|
632
|
+
notated += `${notes}
|
|
633
|
+
`;
|
|
634
|
+
}
|
|
635
|
+
});
|
|
636
|
+
if (pattern.includes("\n")) {
|
|
637
|
+
const divider = "~".repeat(79);
|
|
638
|
+
let out = `regex parse error:
|
|
639
|
+
${divider}
|
|
640
|
+
${notated}${divider}
|
|
641
|
+
`;
|
|
642
|
+
if (multi.length) out += `${multi.map((s) => `on line ${s.start.line} (column ${s.start.column}) through line ${s.end.line} (column ${s.end.column - 1})`).join("\n")}
|
|
643
|
+
`;
|
|
644
|
+
return `${out}error: ${message}`;
|
|
645
|
+
}
|
|
646
|
+
return `regex parse error:
|
|
647
|
+
${notated}error: ${message}`;
|
|
648
|
+
}
|
|
649
|
+
var WORD = "[\\p{Alphabetic}\\p{M}\\p{Nd}\\p{Pc}\\p{Join_Control}]";
|
|
650
|
+
var ASCII_WORD = "[0-9A-Za-z_]";
|
|
651
|
+
function translate(ast, parser) {
|
|
652
|
+
const fail = (span, message) => parser.error(span, message);
|
|
653
|
+
const tr = (ast2, flags) => {
|
|
654
|
+
switch (ast2.k) {
|
|
655
|
+
case "empty":
|
|
656
|
+
return { k: "cat", items: [] };
|
|
657
|
+
// A branch of an alternation can be flags alone (`(?i)|b`); they last to the end of the group.
|
|
658
|
+
case "flags":
|
|
659
|
+
apply(flags, ast2.items);
|
|
660
|
+
return { k: "cat", items: [] };
|
|
661
|
+
case "concat": {
|
|
662
|
+
const items = [];
|
|
663
|
+
for (const a of ast2.asts) {
|
|
664
|
+
if (a.k === "flags") apply(flags, a.items);
|
|
665
|
+
else items.push(tr(a, flags));
|
|
666
|
+
}
|
|
667
|
+
return { k: "cat", items };
|
|
668
|
+
}
|
|
669
|
+
case "alt":
|
|
670
|
+
return { k: "alt", items: ast2.asts.map((a) => tr(a, flags)) };
|
|
671
|
+
case "group": {
|
|
672
|
+
const inner = { ...flags };
|
|
673
|
+
if (ast2.flags) apply(inner, ast2.flags);
|
|
674
|
+
return { k: "group", capture: ast2.capture, body: tr(ast2.ast, inner) };
|
|
675
|
+
}
|
|
676
|
+
case "rep":
|
|
677
|
+
return { k: "rep", body: tr(ast2.ast, flags), min: ast2.min, max: ast2.max, greedy: ast2.greedy !== flags.U };
|
|
678
|
+
case "lit": {
|
|
679
|
+
if (!flags.u && ast2.byte && ast2.c > 127) fail(ast2.span, "pattern can match invalid UTF-8");
|
|
680
|
+
return { k: "lit", c: ast2.c, ci: flags.i ? flags.u ? 1 : 2 : 0 };
|
|
681
|
+
}
|
|
682
|
+
case "dot": {
|
|
683
|
+
if (!flags.u) fail(ast2.span, "pattern can match invalid UTF-8");
|
|
684
|
+
return { k: "raw", js: flags.s ? "[\\u{0}-\\u{10ffff}]" : flags.R ? "[^\\n\\r]" : "[^\\n]" };
|
|
685
|
+
}
|
|
686
|
+
case "look": {
|
|
687
|
+
return { k: "raw", js: look(ast2.kind, flags) };
|
|
688
|
+
}
|
|
689
|
+
case "perl": {
|
|
690
|
+
if (!flags.u && ast2.neg) fail(ast2.span, "pattern can match invalid UTF-8");
|
|
691
|
+
return { k: "set", set: { k: "u", neg: false, items: [perl(ast2, flags.u)] }, ci: flags.i ? flags.u ? 1 : 2 : 0 };
|
|
692
|
+
}
|
|
693
|
+
case "prop": {
|
|
694
|
+
if (!flags.u) fail(ast2.span, "Unicode not allowed here");
|
|
695
|
+
return { k: "set", set: { k: "u", neg: false, items: [property(ast2, fail)] }, ci: flags.i ? 1 : 0 };
|
|
696
|
+
}
|
|
697
|
+
case "bracket": {
|
|
698
|
+
if (!flags.u) return { k: "set", set: byteSet(ast2, flags.i, fail), ci: flags.i ? 2 : 0 };
|
|
699
|
+
return { k: "set", set: unicodeSet(ast2.set, ast2.neg, fail), ci: flags.i ? 1 : 0 };
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
return tr(ast, { i: false, m: false, s: false, U: false, u: true, R: false });
|
|
704
|
+
}
|
|
705
|
+
function apply(flags, items) {
|
|
706
|
+
let on = true;
|
|
707
|
+
for (const item of items) {
|
|
708
|
+
if (item.flag === "-") on = false;
|
|
709
|
+
else if (item.flag !== "x") flags[item.flag] = on;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
function look(kind, flags) {
|
|
713
|
+
const w = flags.u ? WORD : ASCII_WORD;
|
|
714
|
+
switch (kind) {
|
|
715
|
+
case "start":
|
|
716
|
+
return "(?:^)";
|
|
717
|
+
case "end":
|
|
718
|
+
return "(?:$)";
|
|
719
|
+
// JavaScript's `m` would also break lines at `\r` and U+2028.
|
|
720
|
+
case "startLine":
|
|
721
|
+
return !flags.m ? "(?:^)" : flags.R ? "(?:^|(?<=\\n)|(?<=\\r)(?!\\n))" : "(?<![^\\n])";
|
|
722
|
+
case "endLine":
|
|
723
|
+
return !flags.m ? "(?:$)" : flags.R ? "(?:$|(?=\\r)|(?<!\\r)(?=\\n))" : "(?![^\\n])";
|
|
724
|
+
case "word":
|
|
725
|
+
return `(?:(?<=${w})(?!${w})|(?<!${w})(?=${w}))`;
|
|
726
|
+
case "notWord":
|
|
727
|
+
return `(?:(?<=${w})(?=${w})|(?<!${w})(?!${w}))`;
|
|
728
|
+
case "wordStart":
|
|
729
|
+
return `(?:(?<!${w})(?=${w}))`;
|
|
730
|
+
case "wordEnd":
|
|
731
|
+
return `(?:(?<=${w})(?!${w}))`;
|
|
732
|
+
case "wordStartHalf":
|
|
733
|
+
return `(?:(?<!${w}))`;
|
|
734
|
+
case "wordEndHalf":
|
|
735
|
+
return `(?:(?!${w}))`;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
function perl(p, unicode) {
|
|
739
|
+
const sets = unicode ? { d: "\\p{Nd}", s: "\\p{White_Space}", w: WORD } : { d: "[0-9]", s: "[\\t\\n\\v\\f\\r ]", w: ASCII_WORD };
|
|
740
|
+
let js = sets[p.kind];
|
|
741
|
+
if (p.neg) js = js.startsWith("\\p") ? `\\P${js.slice(2)}` : `[^${js.slice(1)}`;
|
|
742
|
+
return { k: "p", js, folds: false, span: p.span };
|
|
743
|
+
}
|
|
744
|
+
function unicodeSet(set, neg, fail) {
|
|
745
|
+
if (set.k === "op") return { k: "op", neg, op: set.op, a: unicodeSet(set.lhs, false, fail), b: unicodeSet(set.rhs, false, fail) };
|
|
746
|
+
const items = set.items.map((item) => {
|
|
747
|
+
switch (item.k) {
|
|
748
|
+
case "lit":
|
|
749
|
+
return { k: "r", lo: item.c, hi: item.c };
|
|
750
|
+
case "range":
|
|
751
|
+
return { k: "r", lo: item.lo.c, hi: item.hi.c };
|
|
752
|
+
case "ascii":
|
|
753
|
+
return { k: "s", set: { k: "u", neg: item.neg, items: ASCII_CLASSES[item.name].map(([lo, hi]) => ({ k: "r", lo, hi })) } };
|
|
754
|
+
case "perl":
|
|
755
|
+
return perl(item, true);
|
|
756
|
+
case "prop":
|
|
757
|
+
return property(item, fail);
|
|
758
|
+
case "bracket":
|
|
759
|
+
return { k: "s", set: unicodeSet(item.set, item.neg, fail) };
|
|
760
|
+
}
|
|
761
|
+
});
|
|
762
|
+
return { k: "u", neg, items };
|
|
763
|
+
}
|
|
764
|
+
function byteSet(bracket, fold, fail) {
|
|
765
|
+
const evaluate = (set) => {
|
|
766
|
+
if (set.k === "op") {
|
|
767
|
+
const a = evaluate(set.lhs), b = evaluate(set.rhs);
|
|
768
|
+
return a.map((x, i) => set.op === "&&" ? x && b[i] : set.op === "--" ? x && !b[i] : x !== b[i]);
|
|
769
|
+
}
|
|
770
|
+
const out = new Array(256).fill(false);
|
|
771
|
+
const add = (lo, hi) => {
|
|
772
|
+
for (let i = lo; i <= hi; i++) out[i] = true;
|
|
773
|
+
};
|
|
774
|
+
const byte = (lit) => {
|
|
775
|
+
if (lit.byte && lit.c > 127) fail(lit.span, "pattern can match invalid UTF-8");
|
|
776
|
+
return lit.byte || lit.c <= 127 ? lit.c : fail(lit.span, "Unicode not allowed here");
|
|
777
|
+
};
|
|
778
|
+
for (const item of set.items) {
|
|
779
|
+
switch (item.k) {
|
|
780
|
+
case "lit":
|
|
781
|
+
add(byte(item), byte(item));
|
|
782
|
+
break;
|
|
783
|
+
case "range":
|
|
784
|
+
add(byte(item.lo), byte(item.hi));
|
|
785
|
+
break;
|
|
786
|
+
case "ascii": {
|
|
787
|
+
const inner = new Array(256).fill(false);
|
|
788
|
+
for (const [lo, hi] of ASCII_CLASSES[item.name]) for (let i = lo; i <= hi; i++) inner[i] = true;
|
|
789
|
+
inner.forEach((x, i) => {
|
|
790
|
+
if (x !== item.neg) out[i] = true;
|
|
791
|
+
});
|
|
792
|
+
break;
|
|
793
|
+
}
|
|
794
|
+
case "perl": {
|
|
795
|
+
const ranges = { d: [[48, 57]], s: [[9, 13], [32, 32]], w: ASCII_CLASSES.word }[item.kind];
|
|
796
|
+
const inner = new Array(256).fill(false);
|
|
797
|
+
for (const [lo, hi] of ranges) for (let i = lo; i <= hi; i++) inner[i] = true;
|
|
798
|
+
inner.forEach((x, i) => {
|
|
799
|
+
if (x !== item.neg) out[i] = true;
|
|
800
|
+
});
|
|
801
|
+
break;
|
|
802
|
+
}
|
|
803
|
+
case "prop":
|
|
804
|
+
fail(item.span, "Unicode not allowed here");
|
|
805
|
+
break;
|
|
806
|
+
case "bracket":
|
|
807
|
+
bracketBytes(item).forEach((x, i) => {
|
|
808
|
+
if (x) out[i] = true;
|
|
809
|
+
});
|
|
810
|
+
break;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
return out;
|
|
814
|
+
};
|
|
815
|
+
const bracketBytes = (b) => {
|
|
816
|
+
let bytes2 = evaluate(b.set);
|
|
817
|
+
if (fold) bytes2 = bytes2.map((x, i) => x || i >= 65 && i <= 90 && bytes2[i + 32] || i >= 97 && i <= 122 && bytes2[i - 32]);
|
|
818
|
+
if (b.neg) bytes2 = bytes2.map((x) => !x);
|
|
819
|
+
if (bytes2.some((x, i) => x && i > 127)) fail(b.span, "pattern can match invalid UTF-8");
|
|
820
|
+
return bytes2;
|
|
821
|
+
};
|
|
822
|
+
const bytes = bracketBytes(bracket);
|
|
823
|
+
const items = [];
|
|
824
|
+
for (let i = 0; i < 128; i++) {
|
|
825
|
+
if (!bytes[i]) continue;
|
|
826
|
+
let j = i;
|
|
827
|
+
while (j + 1 < 128 && bytes[j + 1]) j++;
|
|
828
|
+
items.push({ k: "r", lo: i, hi: j });
|
|
829
|
+
i = j;
|
|
830
|
+
}
|
|
831
|
+
return { k: "u", neg: false, items };
|
|
832
|
+
}
|
|
833
|
+
function normalize(name) {
|
|
834
|
+
let text = name;
|
|
835
|
+
const is = /^is/i.test(text);
|
|
836
|
+
if (is) text = text.slice(2);
|
|
837
|
+
let out = "";
|
|
838
|
+
for (const ch of text) {
|
|
839
|
+
if (ch === " " || ch === "_" || ch === "-") continue;
|
|
840
|
+
const code = ch.codePointAt(0);
|
|
841
|
+
if (code >= 65 && code <= 90) out += ch.toLowerCase();
|
|
842
|
+
else if (code <= 127) out += ch;
|
|
843
|
+
}
|
|
844
|
+
return is && out === "c" ? "isc" : out;
|
|
845
|
+
}
|
|
846
|
+
function table(text) {
|
|
847
|
+
const out = /* @__PURE__ */ new Map();
|
|
848
|
+
for (const entry of text.split("|")) {
|
|
849
|
+
const [canonical, ...aliases] = entry.split(" ");
|
|
850
|
+
out.set(normalize(canonical), canonical);
|
|
851
|
+
for (const alias of aliases) out.set(alias, canonical);
|
|
852
|
+
}
|
|
853
|
+
return out;
|
|
854
|
+
}
|
|
855
|
+
var BINARY = table("ASCII_Hex_Digit ahex|Alphabetic alpha|Bidi_Control bidic|Bidi_Mirrored bidim|Cased|Case_Ignorable ci|Changes_When_Casefolded cwcf|Changes_When_Casemapped cwcm|Changes_When_Lowercased cwl|Changes_When_Titlecased cwt|Changes_When_Uppercased cwu|Dash|Default_Ignorable_Code_Point di|Deprecated dep|Diacritic dia|Emoji_Modifier_Base ebase|Emoji_Component ecomp|Emoji_Modifier emod|Emoji|Emoji_Presentation epres|Extender ext|Extended_Pictographic extpict|Grapheme_Base grbase|Grapheme_Extend grext|Grapheme_Link grlink|Hex_Digit hex|Hyphen|ID_Continue idc|ID_Compat_Math_Continue|ID_Compat_Math_Start|Ideographic ideo|ID_Start ids|IDS_Binary_Operator idsb|IDS_Trinary_Operator idst|IDS_Unary_Operator idsu|Join_Control joinc|Logical_Order_Exception loe|Lowercase lower|Math|Modifier_Combining_Mark mcm|Noncharacter_Code_Point nchar|Other_Alphabetic oalpha|Other_Default_Ignorable_Code_Point odi|Other_Grapheme_Extend ogrext|Other_ID_Continue oidc|Other_ID_Start oids|Other_Lowercase olower|Other_Math omath|Other_Uppercase oupper|Pattern_Syntax patsyn|Pattern_White_Space patws|Prepended_Concatenation_Mark pcm|Quotation_Mark qmark|Radical|Regional_Indicator ri|Soft_Dotted sd|Sentence_Terminal sterm|White_Space space wspace|Terminal_Punctuation term|Unified_Ideograph uideo|Uppercase upper|Variation_Selector vs|XID_Continue xidc|XID_Start xids");
|
|
856
|
+
var NOT_IN_JAVASCRIPT = /* @__PURE__ */ new Set(["Grapheme_Link", "Hyphen", "ID_Compat_Math_Continue", "ID_Compat_Math_Start", "IDS_Unary_Operator", "Modifier_Combining_Mark", "Other_Alphabetic", "Other_Default_Ignorable_Code_Point", "Other_Grapheme_Extend", "Other_ID_Continue", "Other_ID_Start", "Other_Lowercase", "Other_Math", "Other_Uppercase", "Prepended_Concatenation_Mark"]);
|
|
857
|
+
var CATEGORIES = table("Other c|Cased_Letter lc|Control cc cntrl|Format cf|Close_Punctuation pe|Unassigned cn|Private_Use co|Mark m combiningmark|Connector_Punctuation pc|Surrogate cs|Currency_Symbol sc|Dash_Punctuation pd|Decimal_Number nd digit|Enclosing_Mark me|Final_Punctuation pf|Initial_Punctuation pi|Letter l|Letter_Number nl|Line_Separator zl|Lowercase_Letter ll|Modifier_Letter lm|Other_Letter lo|Titlecase_Letter lt|Uppercase_Letter lu|Math_Symbol sm|Spacing_Mark mc|Nonspacing_Mark mn|Modifier_Symbol sk|Number n|Other_Number no|Open_Punctuation ps|Other_Punctuation po|Other_Symbol so|Punctuation p punct|Paragraph_Separator zp|Symbol s|Separator z|Space_Separator zs");
|
|
858
|
+
var SPECIAL = { any: "Any", assigned: "Assigned", ascii: "ASCII" };
|
|
859
|
+
var OTHER_PROPERTIES = new Set("age bc bidiclass bidimirroringglyph bidipairedbracket bidipairedbrackettype blk block bmg bpb bpt canonicalcombiningclass casefolding ccc ce cf changeswhennfkccasefolded cjkaccountingnumeric cjkcompatibilityvariant cjkiicore cjkirggsource cjkirghsource cjkirgjsource cjkirgkpsource cjkirgksource cjkirgmsource cjkirgssource cjkirgtsource cjkirguksource cjkirgusource cjkirgvsource cjkothernumeric cjkprimarynumeric cjkrsunicode compex compositionexclusion cwkcf decompositionmapping decompositiontype dm dt ea eastasianwidth equideo equivalentunifiedideograph expandsonnfc expandsonnfd expandsonnfkc expandsonnfkd fcnfkc fcnfkcclosure fullcompositionexclusion gc gcb generalcategory graphemeclusterbreak hangulsyllabletype hst incb indicconjunctbreak indicpositionalcategory indicsyllabiccategory inpc insc isc jamoshortname jg joininggroup joiningtype jsn jt kaccountingnumeric kcompatibilityvariant kehcat kehdesc kehhg kehifao kehjsesh kehnomirror kehnorotate kiicore kirggsource kirghsource kirgjsource kirgkpsource kirgksource kirgmsource kirgssource kirgtsource kirguksource kirgusource kirgvsource kothernumeric kprimarynumeric krsunicode lb lc linebreak lowercasemapping na na1 name namealias nfcqc nfcquickcheck nfdqc nfdquickcheck nfkccasefold nfkccf nfkcqc nfkcquickcheck nfkcscf nfkcsimplecasefold nfkdqc nfkdquickcheck nt numerictype numericvalue nv ocomment sb sc scf script scriptextensions scx sentencebreak sfc simplecasefolding simplelowercasemapping simpletitlecasemapping simpleuppercasemapping slc stc suc tc titlecasemapping uc unicode1name unicoderadicalstroke uppercasemapping urs verticalorientation vo wb wordbreak xonfc xonfd xonfkc xonfkd".split(" "));
|
|
860
|
+
var SCRIPTS = new Map("Caucasian_Albanian Anatolian_Hieroglyphs Imperial_Aramaic Bassa_Vah Canadian_Aboriginal Cypro_Minoan Dives_Akuru Egyptian_Hieroglyphs Gunjala_Gondi Masaram_Gondi Gurung_Khema Hanifi_Rohingya Pahawh_Hmong Nyiakeng_Puachue_Hmong Katakana_Or_Hiragana Old_Hungarian Inscriptional_Pahlavi Inscriptional_Parthian Old_Italic Kayah_Li Khitan_Small_Script Kirat_Rai Tai_Tham Linear_A Linear_B Meetei_Mayek Mende_Kikakui Meroitic_Cursive Meroitic_Hieroglyphs Nag_Mundari Old_North_Arabian New_Tai_Lue Ol_Chiki Old_Permic Old_Persian Old_Sogdian Old_South_Arabian Old_Turkic Old_Uyghur Ol_Onal Pau_Cin_Hau Phags_Pa Psalter_Pahlavi SignWriting Sora_Sompeng Syloti_Nagri Tai_Le Tai_Viet Tulu_Tigalari Warang_Citi Zanabazar_Square".split(" ").map((name) => [normalize(name), name]));
|
|
861
|
+
var VALUED = { age: "Age", gcb: "Grapheme_Cluster_Break", graphemeclusterbreak: "Grapheme_Cluster_Break", wb: "Word_Break", wordbreak: "Word_Break", sb: "Sentence_Break", sentencebreak: "Sentence_Break" };
|
|
862
|
+
var valid = (js) => {
|
|
863
|
+
try {
|
|
864
|
+
new RegExp(js, "v");
|
|
865
|
+
return true;
|
|
866
|
+
} catch {
|
|
867
|
+
return false;
|
|
868
|
+
}
|
|
869
|
+
};
|
|
870
|
+
function script(name, property2) {
|
|
871
|
+
const words = (s) => s.replace(/([a-z])([A-Z])/g, "$1_$2").split(/[\s_-]+/).filter((w) => w !== "").map((w) => w[0].toUpperCase() + w.slice(1).toLowerCase()).join("_");
|
|
872
|
+
const bare = name.replace(/^is[\s_-]?/i, "");
|
|
873
|
+
for (const candidate of [SCRIPTS.get(normalize(name)) ?? name, words(name), bare, words(bare)]) {
|
|
874
|
+
if (candidate === "Unknown" || candidate === "Zzzz") return "Unknown";
|
|
875
|
+
if (candidate !== "" && /^[A-Za-z_]+$/.test(candidate) && valid(`\\p{${property2}=${candidate}}`)) return candidate;
|
|
876
|
+
}
|
|
877
|
+
return void 0;
|
|
878
|
+
}
|
|
879
|
+
function property(p, fail) {
|
|
880
|
+
const missing = (name) => fail(p.span, `Unicode property ${name} isn't in JavaScript's regular expressions`);
|
|
881
|
+
const category = (n) => SPECIAL[n] ? SPECIAL[n] : CATEGORIES.has(n) ? `General_Category=${CATEGORIES.get(n)}` : void 0;
|
|
882
|
+
let js;
|
|
883
|
+
let neg = p.neg;
|
|
884
|
+
if (p.value === void 0) {
|
|
885
|
+
const n = normalize(p.name);
|
|
886
|
+
const reserved = n === "cf" || n === "sc" || n === "lc";
|
|
887
|
+
const binary = reserved ? void 0 : BINARY.get(n);
|
|
888
|
+
if (binary === void 0 && !reserved && OTHER_PROPERTIES.has(n)) fail(p.span, "Unicode property not found");
|
|
889
|
+
if (binary !== void 0) {
|
|
890
|
+
if (NOT_IN_JAVASCRIPT.has(binary)) missing(binary);
|
|
891
|
+
js = binary;
|
|
892
|
+
} else {
|
|
893
|
+
js = category(n);
|
|
894
|
+
const sc = js === void 0 ? script(p.name, "Script") : void 0;
|
|
895
|
+
if (sc === "Unknown") fail(p.span, "Unicode property value not found");
|
|
896
|
+
if (sc !== void 0) js = `Script=${sc}`;
|
|
897
|
+
}
|
|
898
|
+
if (js === void 0) fail(p.span, "Unicode property not found");
|
|
899
|
+
} else {
|
|
900
|
+
if (p.op === "!=") neg = !neg;
|
|
901
|
+
const name = normalize(p.name);
|
|
902
|
+
const value = normalize(p.value);
|
|
903
|
+
const extensions = name === "scx" || name === "scriptextensions";
|
|
904
|
+
if (name === "gc" || name === "generalcategory") js = category(value);
|
|
905
|
+
else if (extensions || name === "sc" || name === "script") {
|
|
906
|
+
const property2 = extensions ? "Script_Extensions" : "Script";
|
|
907
|
+
const sc = script(p.value.trim(), property2);
|
|
908
|
+
if (sc !== void 0 && sc !== "Unknown") js = `${property2}=${sc}`;
|
|
909
|
+
} else if (VALUED[name]) missing(VALUED[name]);
|
|
910
|
+
else if (!BINARY.has(name) && !OTHER_PROPERTIES.has(name)) fail(p.span, "Unicode property not found");
|
|
911
|
+
if (js === void 0) fail(p.span, "Unicode property value not found");
|
|
912
|
+
}
|
|
913
|
+
const text = `\\${neg ? "P" : "p"}{${js}}`;
|
|
914
|
+
return { k: "p", js: text, folds: true, span: p.span };
|
|
915
|
+
}
|
|
916
|
+
var MODIFIERS = valid("(?i:a)");
|
|
917
|
+
function emit(ir, parser, extra, flagGroups) {
|
|
918
|
+
const atoms = [];
|
|
919
|
+
const collect = (n) => {
|
|
920
|
+
if (n.k === "lit" || n.k === "set") atoms.push(n.ci);
|
|
921
|
+
else if (n.k === "group" || n.k === "rep") collect(n.body);
|
|
922
|
+
else if (n.k === "alt" || n.k === "cat") n.items.forEach(collect);
|
|
923
|
+
};
|
|
924
|
+
collect(ir);
|
|
925
|
+
const mode = atoms.length > 0 && atoms.every((ci) => ci === 1) ? "flag" : flagGroups ? "groups" : "spell";
|
|
926
|
+
const fail = (span, message) => parser.error(span, message);
|
|
927
|
+
const write = (n) => {
|
|
928
|
+
switch (n.k) {
|
|
929
|
+
case "lit":
|
|
930
|
+
if (n.ci === 2) return asciiFolded(n.c);
|
|
931
|
+
if (n.ci === 0 || mode === "flag") return esc(n.c);
|
|
932
|
+
return mode === "groups" ? `(?i:${esc(n.c)})` : folded(n.c);
|
|
933
|
+
case "set":
|
|
934
|
+
if (n.ci === 1 && mode === "groups") return `(?i:${setText(n.set, false, fail)})`;
|
|
935
|
+
return setText(n.set, n.ci === 1 && mode === "spell", fail);
|
|
936
|
+
case "raw":
|
|
937
|
+
return n.js;
|
|
938
|
+
case "group":
|
|
939
|
+
return `(${n.capture ? "" : "?:"}${write(n.body)})`;
|
|
940
|
+
case "alt":
|
|
941
|
+
return n.items.map(write).join("|");
|
|
942
|
+
case "cat":
|
|
943
|
+
return n.items.map(write).join("");
|
|
944
|
+
case "rep": {
|
|
945
|
+
const body = n.body.k === "lit" || n.body.k === "set" || n.body.k === "group" ? write(n.body) : `(?:${write(n.body)})`;
|
|
946
|
+
const quantifier = n.min === 0 && n.max === Infinity ? "*" : n.min === 1 && n.max === Infinity ? "+" : n.min === 0 && n.max === 1 ? "?" : n.max === Infinity ? `{${n.min},}` : n.min === n.max ? `{${n.min}}` : `{${n.min},${n.max}}`;
|
|
947
|
+
return `${body}${quantifier}${n.greedy ? "" : "?"}`;
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
};
|
|
951
|
+
const source = write(ir);
|
|
952
|
+
try {
|
|
953
|
+
return new RegExp(source, `v${mode === "flag" ? "i" : ""}${extra}`);
|
|
954
|
+
} catch (e) {
|
|
955
|
+
return parser.error({ start: 0, end: parser.cs.length }, e.message);
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
function esc(c) {
|
|
959
|
+
return c >= 48 && c <= 57 || c >= 65 && c <= 90 || c >= 97 && c <= 122 ? String.fromCodePoint(c) : `\\u{${c.toString(16)}}`;
|
|
960
|
+
}
|
|
961
|
+
function setText(set, fold, fail) {
|
|
962
|
+
if (set.k === "op") {
|
|
963
|
+
const a = setText(set.a, fold, fail), b = setText(set.b, fold, fail);
|
|
964
|
+
return `[${set.neg ? "^" : ""}${set.op === "~~" ? `[${a}--${b}][${b}--${a}]` : `${a}${set.op}${b}`}]`;
|
|
965
|
+
}
|
|
966
|
+
let body = "";
|
|
967
|
+
for (const item of set.items) {
|
|
968
|
+
if (item.k === "s") body += setText(item.set, fold, fail);
|
|
969
|
+
else if (item.k === "p") {
|
|
970
|
+
if (fold && item.folds) fail(item.span, "matching a Unicode property without regard to case in only part of a pattern needs Node 23 or newer; put (?i) at the start of the pattern instead");
|
|
971
|
+
body += item.js;
|
|
972
|
+
} else {
|
|
973
|
+
body += item.lo === item.hi ? esc(item.lo) : `${esc(item.lo)}-${esc(item.hi)}`;
|
|
974
|
+
if (fold) {
|
|
975
|
+
const extra = /* @__PURE__ */ new Set();
|
|
976
|
+
for (let c = item.lo; c <= item.hi; c++) for (const o of orbit(c)) if (o < item.lo || o > item.hi) extra.add(o);
|
|
977
|
+
for (const o of [...extra].sort((x, y) => x - y)) body += esc(o);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
return `[${set.neg ? "^" : ""}${body}]`;
|
|
982
|
+
}
|
|
983
|
+
var UNREACHED = [181, 383, 453, 456, 459, 498, 837, 912, 944, 962, 976, 977, 981, 982, 1008, 1009, 1012, 1013, 7296, 7297, 7298, 7299, 7300, 7301, 7302, 7303, 7304, 7835, 7838, 8072, 8073, 8074, 8075, 8076, 8077, 8078, 8079, 8088, 8089, 8090, 8091, 8092, 8093, 8094, 8095, 8104, 8105, 8106, 8107, 8108, 8109, 8110, 8111, 8124, 8126, 8140, 8147, 8163, 8188, 8486, 8490, 8491, 64261, 64262];
|
|
984
|
+
var UNREACHED_SET = new Set(UNREACHED);
|
|
985
|
+
function orbit(c) {
|
|
986
|
+
const ch = String.fromCodePoint(c);
|
|
987
|
+
if (ch.toLowerCase() === ch && ch.toUpperCase() === ch && !UNREACHED_SET.has(c)) return [c];
|
|
988
|
+
const seen = /* @__PURE__ */ new Set([c]);
|
|
989
|
+
const queue = [ch];
|
|
990
|
+
while (queue.length) {
|
|
991
|
+
const x = queue.pop();
|
|
992
|
+
for (const y of [x.toLowerCase(), x.toUpperCase()]) {
|
|
993
|
+
const cp = y.codePointAt(0);
|
|
994
|
+
if (y === String.fromCodePoint(cp) && !seen.has(cp)) {
|
|
995
|
+
seen.add(cp);
|
|
996
|
+
queue.push(y);
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
for (const u of UNREACHED) seen.add(u);
|
|
1001
|
+
const same = new RegExp(`^${esc(c)}$`, "iv");
|
|
1002
|
+
return [...seen].filter((x) => x === c || same.test(String.fromCodePoint(x))).sort((a, b) => a - b);
|
|
1003
|
+
}
|
|
1004
|
+
function folded(c) {
|
|
1005
|
+
const all = orbit(c);
|
|
1006
|
+
return all.length === 1 ? esc(c) : `[${all.map(esc).join("")}]`;
|
|
1007
|
+
}
|
|
1008
|
+
function asciiFolded(c) {
|
|
1009
|
+
if (c >= 65 && c <= 90) return `[${esc(c)}${esc(c + 32)}]`;
|
|
1010
|
+
if (c >= 97 && c <= 122) return `[${esc(c - 32)}${esc(c)}]`;
|
|
1011
|
+
return esc(c);
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
export {
|
|
1015
|
+
RegexError,
|
|
1016
|
+
rustRegExp
|
|
1017
|
+
};
|