libyay 1.1.1 → 2.0.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 +163 -13
- package/package.json +6 -3
- package/yay.js +3093 -1403
package/yay.js
CHANGED
|
@@ -1,18 +1,101 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
const KEY_CHARACTER = /^[A-Za-z0-9_-]$/;
|
|
4
|
+
const LOWER_HEX = /^[0-9a-f]*$/;
|
|
5
|
+
const UNICODE_ESCAPE = /^[0-9A-Fa-f]{1,6}$/;
|
|
6
|
+
const HEX_PAIR = /^[0-9A-Fa-f]{2}/;
|
|
7
|
+
const UPPERCASE = /[A-Z]/;
|
|
8
|
+
const UNICODE_SPACE =
|
|
9
|
+
/[\u0085\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]/;
|
|
10
|
+
|
|
11
|
+
// The old inline reader scans its whole remaining text at every level. Keep
|
|
12
|
+
// the one-pass depth error for a pathological malformed flood too large for
|
|
13
|
+
// that scan, rather than turn the fallback into a quadratic denial of service.
|
|
14
|
+
const MAX_DEPTH_FALLBACK_LENGTH = 4096;
|
|
15
|
+
|
|
16
|
+
// The ASCII characters a reference's text may hold, "%" aside, which must
|
|
17
|
+
// begin a percent-encoding.
|
|
18
|
+
const URI_ASCII = new Uint8Array(128);
|
|
19
|
+
for (const ch of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=") {
|
|
20
|
+
URI_ASCII[ch.charCodeAt(0)] = 1;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const SIMPLE_ESCAPES = Object.freeze({
|
|
24
|
+
"\\": "\\",
|
|
25
|
+
"/": "/",
|
|
26
|
+
b: "\b",
|
|
27
|
+
f: "\f",
|
|
28
|
+
n: "\n",
|
|
29
|
+
r: "\r",
|
|
30
|
+
t: "\t",
|
|
31
|
+
});
|
|
32
|
+
|
|
3
33
|
/**
|
|
4
34
|
* Parses a YAY document string and returns the corresponding JavaScript value.
|
|
5
35
|
* @param {string} source - UTF-8 YAY document
|
|
6
|
-
* @param {
|
|
7
|
-
* @returns {unknown} - Parsed value
|
|
36
|
+
* @param {ParseOptions} [options] - Decoding hooks and error-message options
|
|
37
|
+
* @returns {unknown} - Parsed YAY value, including symbol and tagged records
|
|
8
38
|
*/
|
|
9
|
-
function parseYay(source,
|
|
10
|
-
|
|
39
|
+
function parseYay(source, options = {}) {
|
|
40
|
+
if (typeof options === "string") {
|
|
41
|
+
throw new TypeError(
|
|
42
|
+
"parseYay options must be an object; pass a filename as { filename }",
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
if (
|
|
46
|
+
options.decodeReference !== undefined &&
|
|
47
|
+
typeof options.decodeReference !== "function"
|
|
48
|
+
) {
|
|
49
|
+
throw new TypeError("decodeReference must be a function");
|
|
50
|
+
}
|
|
51
|
+
if (
|
|
52
|
+
options.decodeSymbol !== undefined &&
|
|
53
|
+
typeof options.decodeSymbol !== "function"
|
|
54
|
+
) {
|
|
55
|
+
throw new TypeError("decodeSymbol must be a function");
|
|
56
|
+
}
|
|
57
|
+
if (
|
|
58
|
+
options.bytes !== undefined &&
|
|
59
|
+
options.bytes !== "plain" &&
|
|
60
|
+
options.bytes !== "immutable" &&
|
|
61
|
+
typeof options.bytes !== "function"
|
|
62
|
+
) {
|
|
63
|
+
throw new TypeError('bytes must be "plain", "immutable", or a function');
|
|
64
|
+
}
|
|
65
|
+
const ctx = {
|
|
66
|
+
filename: options.filename || undefined,
|
|
67
|
+
decodeReference: options.decodeReference,
|
|
68
|
+
decodeSymbol: options.decodeSymbol || symbol,
|
|
69
|
+
bytes: options.bytes,
|
|
70
|
+
};
|
|
11
71
|
const lines = scan(source, ctx);
|
|
12
72
|
const tokens = outlineLex(lines);
|
|
13
73
|
return parseRoot(tokens, ctx);
|
|
14
74
|
}
|
|
15
75
|
|
|
76
|
+
const TEXT_ENCODER =
|
|
77
|
+
typeof TextEncoder !== "undefined" ? new TextEncoder() : undefined;
|
|
78
|
+
const TEXT_DECODER =
|
|
79
|
+
typeof TextDecoder !== "undefined" ? new TextDecoder() : undefined;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Give a parsed string its own storage. A slice of a line, which is itself a
|
|
83
|
+
* slice of the source, keeps the whole source alive for as long as the value
|
|
84
|
+
* survives. V8 already copies very short slices; the rest are copied once.
|
|
85
|
+
* @param {string} s
|
|
86
|
+
* @returns {string}
|
|
87
|
+
*/
|
|
88
|
+
function detachString(s) {
|
|
89
|
+
if (s.length <= 13) return s;
|
|
90
|
+
if (typeof Buffer !== "undefined") {
|
|
91
|
+
return Buffer.from(s, "utf8").toString("utf8");
|
|
92
|
+
}
|
|
93
|
+
if (TEXT_ENCODER !== undefined && TEXT_DECODER !== undefined) {
|
|
94
|
+
return TEXT_DECODER.decode(TEXT_ENCODER.encode(s));
|
|
95
|
+
}
|
|
96
|
+
return s.split("").join("");
|
|
97
|
+
}
|
|
98
|
+
|
|
16
99
|
function locSuffix(ctx, line, col) {
|
|
17
100
|
if (!ctx.filename) return "";
|
|
18
101
|
const oneBasedLine = line + 1;
|
|
@@ -22,6 +105,45 @@ function locSuffix(ctx, line, col) {
|
|
|
22
105
|
);
|
|
23
106
|
}
|
|
24
107
|
|
|
108
|
+
/**
|
|
109
|
+
* A location for an error that must name one even without a filename.
|
|
110
|
+
* @param {ParseContext} ctx
|
|
111
|
+
* @param {number} line
|
|
112
|
+
* @param {number} col
|
|
113
|
+
*/
|
|
114
|
+
function requiredLocSuffix(ctx, line, col) {
|
|
115
|
+
const oneBasedLine = line + 1;
|
|
116
|
+
const oneBasedCol = col + 1;
|
|
117
|
+
return (
|
|
118
|
+
" at " +
|
|
119
|
+
oneBasedLine +
|
|
120
|
+
":" +
|
|
121
|
+
oneBasedCol +
|
|
122
|
+
(ctx.filename ? " of <" + ctx.filename + ">" : "")
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Throw a parser error at a source location.
|
|
128
|
+
* @param {ParseContext} ctx
|
|
129
|
+
* @param {string} message
|
|
130
|
+
* @param {number} line
|
|
131
|
+
* @param {number} col
|
|
132
|
+
* @param {boolean=} appendLocation
|
|
133
|
+
* @returns {never}
|
|
134
|
+
*/
|
|
135
|
+
function fail(ctx, message, line, col, appendLocation = true) {
|
|
136
|
+
const error = new Error(
|
|
137
|
+
message + (appendLocation ? locSuffix(ctx, line, col) : ""),
|
|
138
|
+
);
|
|
139
|
+
Object.defineProperties(error, {
|
|
140
|
+
line: { value: line + 1 },
|
|
141
|
+
column: { value: col + 1 },
|
|
142
|
+
filename: { value: ctx.filename },
|
|
143
|
+
});
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
|
|
25
147
|
/**
|
|
26
148
|
* Check whether a code point is allowed in a YAY document.
|
|
27
149
|
* @param {number} cp
|
|
@@ -38,40 +160,63 @@ function isAllowedCodePoint(cp) {
|
|
|
38
160
|
}
|
|
39
161
|
|
|
40
162
|
/**
|
|
41
|
-
* Strip inline
|
|
42
|
-
*
|
|
163
|
+
* Strip an inline comment from a value. A comment after a value takes two
|
|
164
|
+
* spaces before its "#"; one space is refused.
|
|
43
165
|
* @param {string} s
|
|
166
|
+
* @param {ParseContext} ctx
|
|
167
|
+
* @param {number} lineNum
|
|
168
|
+
* @param {number} col
|
|
169
|
+
* @param {ReturnType<typeof scanTopLevel>=} syntax
|
|
44
170
|
* @returns {string}
|
|
45
171
|
*/
|
|
46
|
-
function stripInlineComment(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
172
|
+
function stripInlineComment(
|
|
173
|
+
s,
|
|
174
|
+
ctx = {},
|
|
175
|
+
lineNum = 0,
|
|
176
|
+
col = 0,
|
|
177
|
+
syntax = scanTopLevel(s),
|
|
178
|
+
) {
|
|
179
|
+
if (syntax.comment === undefined) return s;
|
|
180
|
+
checkCommentSpacing(s, syntax.comment, ctx, lineNum, col);
|
|
181
|
+
return s.slice(0, syntax.comment).trimEnd();
|
|
182
|
+
}
|
|
50
183
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
return s.slice(0, i).trimEnd();
|
|
67
|
-
}
|
|
184
|
+
/**
|
|
185
|
+
* Refuse a comment, at s[hash], that has one space before it rather than
|
|
186
|
+
* two.
|
|
187
|
+
* @param {string} s
|
|
188
|
+
* @param {number} hash
|
|
189
|
+
* @param {ParseContext} ctx
|
|
190
|
+
* @param {number} lineNum
|
|
191
|
+
* @param {number} col
|
|
192
|
+
*/
|
|
193
|
+
function checkCommentSpacing(s, hash, ctx, lineNum, col) {
|
|
194
|
+
if (hash >= 1 && s[hash - 1] === " " && (hash < 2 || s[hash - 2] !== " ")) {
|
|
195
|
+
throw new Error(
|
|
196
|
+
"Expected two spaces before a comment" +
|
|
197
|
+
locSuffix(ctx, lineNum, col + hash),
|
|
198
|
+
);
|
|
68
199
|
}
|
|
69
|
-
return s;
|
|
70
200
|
}
|
|
71
201
|
|
|
202
|
+
/**
|
|
203
|
+
* @typedef {Object} ParseOptions
|
|
204
|
+
* @property {string=} filename - Optional filename for error messages
|
|
205
|
+
* @property {(uri: string) => unknown=} decodeReference - Turn a
|
|
206
|
+
* reference's URI text into the caller's value
|
|
207
|
+
* @property {(name: string) => unknown=} decodeSymbol - Turn a symbol's name
|
|
208
|
+
* into the caller's value; defaults to `symbol`
|
|
209
|
+
* @property {'plain'|'immutable'|((bytes: Uint8Array) => unknown)=} bytes -
|
|
210
|
+
* How to make a byte value: a plain Uint8Array by default, an immutable
|
|
211
|
+
* byteArray when asked, or a caller's hook
|
|
212
|
+
*/
|
|
213
|
+
|
|
72
214
|
/**
|
|
73
215
|
* @typedef {Object} ParseContext
|
|
74
216
|
* @property {string=} filename
|
|
217
|
+
* @property {(uri: string) => unknown=} decodeReference
|
|
218
|
+
* @property {(name: string) => unknown} decodeSymbol
|
|
219
|
+
* @property {'plain'|'immutable'|((bytes: Uint8Array) => unknown)=} bytes
|
|
75
220
|
*/
|
|
76
221
|
|
|
77
222
|
/**
|
|
@@ -90,6 +235,23 @@ function stripInlineComment(s) {
|
|
|
90
235
|
* @property {number=} col
|
|
91
236
|
*/
|
|
92
237
|
|
|
238
|
+
/**
|
|
239
|
+
* Skip blank-line tokens and, where a caller is between blocks, stop tokens.
|
|
240
|
+
* @param {Token[]} tokens
|
|
241
|
+
* @param {number} i
|
|
242
|
+
* @param {boolean=} includeStops
|
|
243
|
+
* @returns {number}
|
|
244
|
+
*/
|
|
245
|
+
function skipBlank(tokens, i, includeStops = false) {
|
|
246
|
+
while (
|
|
247
|
+
i < tokens.length &&
|
|
248
|
+
(tokens[i].type === "break" || (includeStops && tokens[i].type === "stop"))
|
|
249
|
+
) {
|
|
250
|
+
i++;
|
|
251
|
+
}
|
|
252
|
+
return i;
|
|
253
|
+
}
|
|
254
|
+
|
|
93
255
|
// --- Scanner: source -> lines with { line, indent, leader, lineNum } ---
|
|
94
256
|
|
|
95
257
|
/**
|
|
@@ -154,7 +316,7 @@ function scan(source, ctx = {}) {
|
|
|
154
316
|
}
|
|
155
317
|
}
|
|
156
318
|
const lines = [];
|
|
157
|
-
const lineStrings = source.split(
|
|
319
|
+
const lineStrings = source.split("\n");
|
|
158
320
|
// While inside a block string, the column that ends it: the indent of a
|
|
159
321
|
// bare opener, or the key's column for a property opener. Its content
|
|
160
322
|
// lines are literal: no list leaders, no "-" or "*" checks, no comments.
|
|
@@ -215,9 +377,9 @@ function scan(source, ctx = {}) {
|
|
|
215
377
|
) {
|
|
216
378
|
throw new Error('Unexpected character "*"' + locSuffix(ctx, i, indent));
|
|
217
379
|
}
|
|
218
|
-
if (isBareBlockOpener(line)) {
|
|
380
|
+
if (isBareBlockOpener(line) || isBareBlockOpener(afterPrefix(line) ?? "")) {
|
|
219
381
|
blockOpener = indent;
|
|
220
|
-
} else if (line.endsWith(": `")) {
|
|
382
|
+
} else if (line.endsWith(": `") || isTaggedPropertyOpener(line)) {
|
|
221
383
|
blockOpener = indent + (rest.length - line.length);
|
|
222
384
|
}
|
|
223
385
|
lines.push({ line, indent, leader, lineNum: i });
|
|
@@ -275,7 +437,14 @@ function outlineLex(lines) {
|
|
|
275
437
|
// Every blank line is a break, so that a block string keeps a run of
|
|
276
438
|
// blank lines.
|
|
277
439
|
if (line.length > 0) {
|
|
278
|
-
|
|
440
|
+
// The text's column follows the list leader, "- ", if any.
|
|
441
|
+
tokens.push({
|
|
442
|
+
type: "text",
|
|
443
|
+
text: line,
|
|
444
|
+
indent,
|
|
445
|
+
lineNum,
|
|
446
|
+
col: leader ? indent + 2 : indent,
|
|
447
|
+
});
|
|
279
448
|
} else {
|
|
280
449
|
tokens.push({ type: "break", text: "", lineNum, col: indent });
|
|
281
450
|
}
|
|
@@ -289,18 +458,50 @@ function outlineLex(lines) {
|
|
|
289
458
|
|
|
290
459
|
// --- Value parser: tokens -> value ---
|
|
291
460
|
|
|
461
|
+
/**
|
|
462
|
+
* Give an object a property of its own. Assignment would take a
|
|
463
|
+
* "__proto__" key for the prototype; YAY has no such key.
|
|
464
|
+
* @param {Record<string, unknown>} obj
|
|
465
|
+
* @param {string} key
|
|
466
|
+
* @param {unknown} value
|
|
467
|
+
*/
|
|
468
|
+
function setOwn(obj, key, value) {
|
|
469
|
+
Object.defineProperty(obj, key, {
|
|
470
|
+
value,
|
|
471
|
+
writable: true,
|
|
472
|
+
enumerable: true,
|
|
473
|
+
configurable: true,
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Record where a key first appears in an object, and refuse it the second
|
|
479
|
+
* time. Each key keeps its first position, not only its name, so that the
|
|
480
|
+
* error can say where the first one is.
|
|
481
|
+
* @param {Map<string, [number, number]>} seen
|
|
482
|
+
* @param {string} key
|
|
483
|
+
* @param {ParseContext} ctx
|
|
484
|
+
* @param {number} lineNum
|
|
485
|
+
* @param {number} col
|
|
486
|
+
*/
|
|
487
|
+
function claimKey(seen, key, ctx, lineNum, col) {
|
|
488
|
+
const first = seen.get(key);
|
|
489
|
+
if (first !== undefined) {
|
|
490
|
+
throw new Error(
|
|
491
|
+
`Duplicate key "${key}" (first at ${first[0] + 1}:${first[1] + 1})` +
|
|
492
|
+
locSuffix(ctx, lineNum, col),
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
seen.set(key, [lineNum, col]);
|
|
496
|
+
}
|
|
497
|
+
|
|
292
498
|
/**
|
|
293
499
|
* @param {Token[]} tokens
|
|
294
500
|
* @param {ParseContext} ctx
|
|
295
501
|
* @returns {unknown}
|
|
296
502
|
*/
|
|
297
503
|
function parseRoot(tokens, ctx = {}) {
|
|
298
|
-
let i = 0;
|
|
299
|
-
while (
|
|
300
|
-
i < tokens.length &&
|
|
301
|
-
(tokens[i].type === "stop" || tokens[i].type === "break")
|
|
302
|
-
)
|
|
303
|
-
i++;
|
|
504
|
+
let i = skipBlank(tokens, 0, true);
|
|
304
505
|
if (i >= tokens.length) {
|
|
305
506
|
throw new Error(
|
|
306
507
|
"No value found in document" +
|
|
@@ -315,11 +516,38 @@ function parseRoot(tokens, ctx = {}) {
|
|
|
315
516
|
(ctx.filename ? " at " + line + ":1 of <" + ctx.filename + ">" : ""),
|
|
316
517
|
);
|
|
317
518
|
}
|
|
519
|
+
// A tagged root block: the tags alone on the first line, the block below
|
|
520
|
+
// at column 0
|
|
521
|
+
if (t.type === "text" && t.text.startsWith("(")) {
|
|
522
|
+
const lineNum = t.lineNum ?? 0;
|
|
523
|
+
const col = t.col ?? 0;
|
|
524
|
+
const [, firstEnd] = parseTag(t.text, ctx, lineNum, col);
|
|
525
|
+
if (t.text[firstEnd] === ":") {
|
|
526
|
+
throw new Error("A key is a string" + locSuffix(ctx, lineNum, col));
|
|
527
|
+
}
|
|
528
|
+
let tags, rest;
|
|
529
|
+
try {
|
|
530
|
+
[tags, rest] = parsePrefix(t.text, ctx, lineNum, col);
|
|
531
|
+
} catch (error) {
|
|
532
|
+
if (skipBlank(tokens, i + 1, true) < tokens.length) throw error;
|
|
533
|
+
return parseScalar(t.text, ctx, lineNum, col);
|
|
534
|
+
}
|
|
535
|
+
if (rest !== "") throw prefixWithoutBlock(ctx, t.lineNum ?? 0, t.col ?? 0);
|
|
536
|
+
const j = skipBlank(tokens, i + 1, true);
|
|
537
|
+
if (j >= tokens.length) {
|
|
538
|
+
return parseScalar(t.text, ctx, t.lineNum ?? 0, t.col ?? 0);
|
|
539
|
+
}
|
|
540
|
+
if (!rootBlockFollows(tokens, j)) {
|
|
541
|
+
throw prefixOverInlineValue(ctx, t.lineNum ?? 0, t.col ?? 0);
|
|
542
|
+
}
|
|
543
|
+
return wrapPrefix(parseRoot(tokens.slice(j), ctx), tags);
|
|
544
|
+
}
|
|
318
545
|
if (
|
|
319
546
|
t.type === "text" &&
|
|
320
547
|
findKeyColonOutsideQuotes(t.text) >= 0 &&
|
|
321
548
|
(t.indent ?? 0) === 0 &&
|
|
322
|
-
!t.text.startsWith("{")
|
|
549
|
+
!t.text.startsWith("{") &&
|
|
550
|
+
!t.text.startsWith("[")
|
|
323
551
|
) {
|
|
324
552
|
const [value, next] = parseRootObject(tokens, i, ctx);
|
|
325
553
|
return ensureAtEnd(value, tokens, next, ctx);
|
|
@@ -329,12 +557,7 @@ function parseRoot(tokens, ctx = {}) {
|
|
|
329
557
|
}
|
|
330
558
|
|
|
331
559
|
function ensureAtEnd(value, tokens, i, ctx = {}) {
|
|
332
|
-
|
|
333
|
-
while (
|
|
334
|
-
j < tokens.length &&
|
|
335
|
-
(tokens[j].type === "stop" || tokens[j].type === "break")
|
|
336
|
-
)
|
|
337
|
-
j++;
|
|
560
|
+
const j = skipBlank(tokens, i, true);
|
|
338
561
|
if (j < tokens.length) {
|
|
339
562
|
const t = tokens[j];
|
|
340
563
|
const line = (t.lineNum ?? 0) + 1;
|
|
@@ -385,87 +608,89 @@ function parseValue(tokens, i, ctx = {}) {
|
|
|
385
608
|
}
|
|
386
609
|
}
|
|
387
610
|
if (t.type === "text") {
|
|
388
|
-
const
|
|
389
|
-
const
|
|
611
|
+
const s = t.text;
|
|
612
|
+
const lineNum = t.lineNum ?? 0;
|
|
390
613
|
const sCol = t.col ?? 0;
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
if (s === "
|
|
394
|
-
if (s === "nan") return [NaN, i + 1];
|
|
395
|
-
if (s === "infinity") return [Infinity, i + 1];
|
|
396
|
-
if (s === "-infinity") return [-Infinity, i + 1];
|
|
397
|
-
const num = parseNumber(s, ctx, t.lineNum ?? 0, sCol);
|
|
398
|
-
if (num !== undefined) return [num, i + 1];
|
|
399
|
-
if (s === "`" || (s.startsWith("`") && s.length >= 2 && s[1] === " ")) {
|
|
614
|
+
const syntax = scanTopLevel(s);
|
|
615
|
+
// The block forms, which read the lines below
|
|
616
|
+
if (s === "`" || s.startsWith("` ")) {
|
|
400
617
|
const firstLine = s.length > 2 ? s.slice(2) : "";
|
|
401
618
|
// Use token's indent as base - block string content must be indented more
|
|
402
|
-
return
|
|
619
|
+
return collectBlockString(
|
|
403
620
|
tokens,
|
|
404
621
|
i,
|
|
405
622
|
firstLine,
|
|
406
623
|
false,
|
|
407
624
|
t.indent ?? 0,
|
|
625
|
+
ctx,
|
|
408
626
|
);
|
|
409
627
|
}
|
|
410
|
-
if (
|
|
411
|
-
(s.startsWith('"') && s.length > 1) ||
|
|
412
|
-
(s.startsWith("'") && s.length > 1)
|
|
413
|
-
) {
|
|
414
|
-
return [parseQuotedString(s, ctx, t.lineNum ?? 0, sCol), i + 1];
|
|
415
|
-
}
|
|
416
|
-
if (s.startsWith("[")) {
|
|
417
|
-
// Inline arrays must close on the same line.
|
|
418
|
-
if (!s.endsWith("]")) {
|
|
419
|
-
throw new Error(
|
|
420
|
-
"Unexpected newline in inline array" +
|
|
421
|
-
locSuffix(ctx, t.lineNum ?? 0, sCol),
|
|
422
|
-
);
|
|
423
|
-
}
|
|
424
|
-
validateInlineArrayWhitespace(s, ctx, t.lineNum ?? 0, sCol);
|
|
425
|
-
return [parseInlineArray(s, ctx, t.lineNum ?? 0, sCol), i + 1];
|
|
426
|
-
}
|
|
427
628
|
if (s.startsWith(">")) {
|
|
428
|
-
|
|
429
|
-
|
|
629
|
+
const firstCol = s.startsWith("> ") ? sCol + 2 : sCol + 1;
|
|
630
|
+
const firstLine = s.slice(firstCol - sCol);
|
|
430
631
|
if (firstLine.length === 0) {
|
|
431
|
-
|
|
632
|
+
fail(
|
|
633
|
+
ctx,
|
|
634
|
+
"Expected hex or comment in hex block",
|
|
635
|
+
lineNum,
|
|
636
|
+
firstCol,
|
|
637
|
+
false,
|
|
638
|
+
);
|
|
432
639
|
}
|
|
433
|
-
return parseBlockBytes(
|
|
640
|
+
return parseBlockBytes(
|
|
641
|
+
tokens,
|
|
642
|
+
i,
|
|
643
|
+
ctx,
|
|
644
|
+
firstLine,
|
|
645
|
+
t.indent ?? 0,
|
|
646
|
+
firstCol,
|
|
647
|
+
);
|
|
434
648
|
}
|
|
435
|
-
if (s.startsWith("
|
|
436
|
-
|
|
437
|
-
if (
|
|
649
|
+
if (s.startsWith("[")) {
|
|
650
|
+
const close = syntax.closeBracket;
|
|
651
|
+
if (close >= 0 && s[close + 1] === ":") {
|
|
438
652
|
throw new Error(
|
|
439
|
-
|
|
440
|
-
locSuffix(ctx,
|
|
653
|
+
'Unexpected character ":"' +
|
|
654
|
+
locSuffix(ctx, lineNum, sCol + close + 1),
|
|
441
655
|
);
|
|
442
656
|
}
|
|
443
|
-
const inlineObj = parseInlineObject(s, ctx, t.lineNum ?? 0, sCol);
|
|
444
|
-
if (inlineObj !== null) return [inlineObj, i + 1];
|
|
445
657
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
658
|
+
// A property on this line: "key: value" or "key:" over a block. An
|
|
659
|
+
// inline value with tags after it, an inline container, a quoted string
|
|
660
|
+
// and a reference or bytes never take a key here.
|
|
661
|
+
const bare = stripInlineComment(s, ctx, lineNum, sCol, syntax);
|
|
662
|
+
const first =
|
|
663
|
+
syntax.firstTag !== undefined && syntax.firstTag < bare.length
|
|
664
|
+
? syntax.firstTag
|
|
665
|
+
: undefined;
|
|
666
|
+
const tagged =
|
|
667
|
+
first !== undefined &&
|
|
668
|
+
first > 0 &&
|
|
669
|
+
findKeyColonOutsideQuotes(bare, syntax, first) < 0;
|
|
670
|
+
if (
|
|
671
|
+
!tagged &&
|
|
672
|
+
!/^["'[{<]/.test(bare) &&
|
|
673
|
+
findKeyColonOutsideQuotes(bare, syntax, bare.length) >= 0
|
|
674
|
+
) {
|
|
675
|
+
const keyValue = splitKeyValue(
|
|
676
|
+
bare,
|
|
677
|
+
sCol,
|
|
678
|
+
ctx,
|
|
679
|
+
lineNum,
|
|
680
|
+
undefined,
|
|
681
|
+
syntax,
|
|
451
682
|
);
|
|
452
|
-
}
|
|
453
|
-
const keyValue = splitKeyValue(s, sCol, ctx, t.lineNum ?? 0);
|
|
454
|
-
if (keyValue) {
|
|
455
683
|
const { key, valuePart, valueCol } = keyValue;
|
|
456
684
|
if (valuePart === "" && key.length > 0) {
|
|
457
685
|
return parseObjectOrNamedArray(tokens, i, key, ctx);
|
|
458
686
|
}
|
|
459
|
-
// Note: "key: <" without closing ">" is invalid - inline byte arrays must be closed on the same line
|
|
460
687
|
if (key.length > 0) {
|
|
461
|
-
const value =
|
|
462
|
-
valuePart === ""
|
|
463
|
-
? undefined
|
|
464
|
-
: parseScalar(valuePart, ctx, t.lineNum ?? 0, valueCol);
|
|
688
|
+
const value = parseScalar(valuePart, ctx, lineNum, valueCol);
|
|
465
689
|
return [{ [key]: value }, i + 1];
|
|
466
690
|
}
|
|
467
691
|
}
|
|
468
|
-
|
|
692
|
+
// Every inline value reads as a property's does
|
|
693
|
+
return [parseScalar(s, ctx, lineNum, sCol, syntax), i + 1];
|
|
469
694
|
}
|
|
470
695
|
return [undefined, i + 1];
|
|
471
696
|
}
|
|
@@ -476,10 +701,18 @@ function parseValue(tokens, i, ctx = {}) {
|
|
|
476
701
|
* @param {ParseContext} ctx
|
|
477
702
|
* @param {number} lineNum
|
|
478
703
|
* @param {number|undefined} inlineCol - column of opening brace for inline objects
|
|
704
|
+
* @param {ReturnType<typeof scanTopLevel>=} syntax
|
|
479
705
|
* @returns {{key: string, valuePart: string, valueCol: number}|null}
|
|
480
706
|
*/
|
|
481
|
-
function splitKeyValue(
|
|
482
|
-
|
|
707
|
+
function splitKeyValue(
|
|
708
|
+
s,
|
|
709
|
+
sCol,
|
|
710
|
+
ctx,
|
|
711
|
+
lineNum,
|
|
712
|
+
inlineCol,
|
|
713
|
+
syntax = scanTopLevel(s),
|
|
714
|
+
) {
|
|
715
|
+
const colonIdx = findKeyColonOutsideQuotes(s, syntax);
|
|
483
716
|
if (colonIdx < 0) return null;
|
|
484
717
|
const keyRaw = s.slice(0, colonIdx);
|
|
485
718
|
if (keyRaw.endsWith(" ")) {
|
|
@@ -495,21 +728,25 @@ function splitKeyValue(s, sCol, ctx, lineNum, inlineCol) {
|
|
|
495
728
|
const col = sCol + Math.max(0, keyRaw.length - 1);
|
|
496
729
|
throw new Error("Unterminated string" + locSuffix(ctx, lineNum, col));
|
|
497
730
|
}
|
|
498
|
-
key =
|
|
499
|
-
quote === '"'
|
|
500
|
-
? parseQuotedString(keyRaw, ctx, lineNum, sCol)
|
|
501
|
-
: keyRaw.slice(1, -1);
|
|
731
|
+
key = parseQuotedString(keyRaw, ctx, lineNum, sCol);
|
|
502
732
|
} else {
|
|
503
733
|
if (keyRaw.length === 0) {
|
|
504
734
|
throw new Error("Missing key" + locSuffix(ctx, lineNum, sCol + colonIdx));
|
|
505
735
|
}
|
|
736
|
+
if (
|
|
737
|
+
keyRaw.startsWith("<") &&
|
|
738
|
+
keyRaw.endsWith(">") &&
|
|
739
|
+
inlineCol === undefined
|
|
740
|
+
) {
|
|
741
|
+
// A reference or bytes is a value, never a key: the colon is stray.
|
|
742
|
+
// In an inline object, "<" is a bad key's first character, below.
|
|
743
|
+
throw new Error(
|
|
744
|
+
'Unexpected character ":"' + locSuffix(ctx, lineNum, sCol + colonIdx),
|
|
745
|
+
);
|
|
746
|
+
}
|
|
506
747
|
for (let i = 0; i < keyRaw.length; i++) {
|
|
507
748
|
const c = keyRaw[i];
|
|
508
|
-
|
|
509
|
-
const isDigit = c >= "0" && c <= "9";
|
|
510
|
-
const isUnderscore = c === "_";
|
|
511
|
-
const isHyphen = c === "-";
|
|
512
|
-
if (!isAlpha && !isDigit && !isUnderscore && !isHyphen) {
|
|
749
|
+
if (!KEY_CHARACTER.test(c)) {
|
|
513
750
|
// First character invalid = "Invalid key", subsequent = "Invalid key character"
|
|
514
751
|
const errMsg = i === 0 ? "Invalid key" : "Invalid key character";
|
|
515
752
|
// For inline objects, report column of opening brace; otherwise report character position
|
|
@@ -518,6 +755,7 @@ function splitKeyValue(s, sCol, ctx, lineNum, inlineCol) {
|
|
|
518
755
|
throw new Error(errMsg + locSuffix(ctx, lineNum, errCol));
|
|
519
756
|
}
|
|
520
757
|
}
|
|
758
|
+
key = detachString(keyRaw);
|
|
521
759
|
}
|
|
522
760
|
const valueSlice = s.slice(colonIdx + 1);
|
|
523
761
|
let valuePart = valueSlice;
|
|
@@ -542,45 +780,16 @@ function splitKeyValue(s, sCol, ctx, lineNum, inlineCol) {
|
|
|
542
780
|
|
|
543
781
|
/**
|
|
544
782
|
* @param {string} s
|
|
783
|
+
* @param {ReturnType<typeof scanTopLevel>=} syntax
|
|
784
|
+
* @param {number=} end
|
|
545
785
|
* @returns {number}
|
|
546
786
|
*/
|
|
547
|
-
function findKeyColonOutsideQuotes(
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
if (escape) {
|
|
554
|
-
escape = false;
|
|
555
|
-
continue;
|
|
556
|
-
}
|
|
557
|
-
if (inSingle) {
|
|
558
|
-
if (ch === "\\") {
|
|
559
|
-
escape = true;
|
|
560
|
-
} else if (ch === "'") {
|
|
561
|
-
inSingle = false;
|
|
562
|
-
}
|
|
563
|
-
continue;
|
|
564
|
-
}
|
|
565
|
-
if (inDouble) {
|
|
566
|
-
if (ch === "\\") {
|
|
567
|
-
escape = true;
|
|
568
|
-
} else if (ch === '"') {
|
|
569
|
-
inDouble = false;
|
|
570
|
-
}
|
|
571
|
-
continue;
|
|
572
|
-
}
|
|
573
|
-
if (ch === "'") {
|
|
574
|
-
inSingle = true;
|
|
575
|
-
continue;
|
|
576
|
-
}
|
|
577
|
-
if (ch === '"') {
|
|
578
|
-
inDouble = true;
|
|
579
|
-
continue;
|
|
580
|
-
}
|
|
581
|
-
if (ch === ":") return i;
|
|
582
|
-
}
|
|
583
|
-
return -1;
|
|
787
|
+
function findKeyColonOutsideQuotes(
|
|
788
|
+
s,
|
|
789
|
+
syntax = scanTopLevel(s),
|
|
790
|
+
end = s.length,
|
|
791
|
+
) {
|
|
792
|
+
return syntax.colon !== undefined && syntax.colon < end ? syntax.colon : -1;
|
|
584
793
|
}
|
|
585
794
|
|
|
586
795
|
/**
|
|
@@ -597,6 +806,74 @@ function isPropertyBlockLeaderOnly(valuePart, leader) {
|
|
|
597
806
|
return valuePart[i] === "#";
|
|
598
807
|
}
|
|
599
808
|
|
|
809
|
+
/**
|
|
810
|
+
* Split an inline container body at its top-level commas. Quotes, tags, and
|
|
811
|
+
* references are already opaque in the shared syntax scan.
|
|
812
|
+
* @param {string} body
|
|
813
|
+
* @param {ParseContext} ctx
|
|
814
|
+
* @param {number} lineNum
|
|
815
|
+
* @param {number} col
|
|
816
|
+
* @param {{angles?: boolean, validateArrays?: boolean}=} options
|
|
817
|
+
* @returns {{text: string, start: number}[]}
|
|
818
|
+
*/
|
|
819
|
+
function splitTopLevelCommas(
|
|
820
|
+
body,
|
|
821
|
+
ctx,
|
|
822
|
+
lineNum,
|
|
823
|
+
col,
|
|
824
|
+
{ angles = false, validateArrays = false } = {},
|
|
825
|
+
) {
|
|
826
|
+
const { masked } = scanTopLevel(body, true);
|
|
827
|
+
const parts = [];
|
|
828
|
+
let start = 0;
|
|
829
|
+
let braceDepth = 0;
|
|
830
|
+
let bracketDepth = 0;
|
|
831
|
+
let angleDepth = 0;
|
|
832
|
+
for (let i = 0; i < masked.length; i++) {
|
|
833
|
+
const c = masked[i];
|
|
834
|
+
if (validateArrays && c === "[" && body[i + 1] === " ") {
|
|
835
|
+
fail(ctx, 'Unexpected space after "["', lineNum, col + i + 1);
|
|
836
|
+
}
|
|
837
|
+
if (validateArrays && c === "]" && body[i - 1] === " ") {
|
|
838
|
+
fail(ctx, 'Unexpected space before "]"', lineNum, col + i - 1);
|
|
839
|
+
}
|
|
840
|
+
if (validateArrays && c === ",") {
|
|
841
|
+
if (i > 0 && body[i - 1] === " ") {
|
|
842
|
+
fail(ctx, 'Unexpected space before ","', lineNum, col + i - 1);
|
|
843
|
+
}
|
|
844
|
+
if (i + 1 < body.length && body[i + 1] !== " " && body[i + 1] !== "]") {
|
|
845
|
+
fail(ctx, 'Expected space after ","', lineNum, col + i);
|
|
846
|
+
}
|
|
847
|
+
if (body[i + 1] === " " && body[i + 2] === " ") {
|
|
848
|
+
fail(ctx, 'Unexpected space after ","', lineNum, col + i + 2);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
if (c === "{") braceDepth++;
|
|
852
|
+
else if (c === "}" && braceDepth > 0) braceDepth--;
|
|
853
|
+
else if (c === "[") bracketDepth++;
|
|
854
|
+
else if (c === "]" && bracketDepth > 0) bracketDepth--;
|
|
855
|
+
else if (angles && c === "<") angleDepth++;
|
|
856
|
+
else if (angles && c === ">" && angleDepth > 0) angleDepth--;
|
|
857
|
+
else if (
|
|
858
|
+
c === "," &&
|
|
859
|
+
braceDepth === 0 &&
|
|
860
|
+
bracketDepth === 0 &&
|
|
861
|
+
angleDepth === 0
|
|
862
|
+
) {
|
|
863
|
+
if (!validateArrays && i > 0 && body[i - 1] === " ") {
|
|
864
|
+
fail(ctx, 'Unexpected space before ","', lineNum, col + i - 1);
|
|
865
|
+
}
|
|
866
|
+
if (i + 1 >= body.length || body[i + 1] !== " ") {
|
|
867
|
+
fail(ctx, 'Expected space after ","', lineNum, col + i);
|
|
868
|
+
}
|
|
869
|
+
parts.push({ text: body.slice(start, i), start });
|
|
870
|
+
start = i + 2;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
parts.push({ text: body.slice(start), start });
|
|
874
|
+
return parts;
|
|
875
|
+
}
|
|
876
|
+
|
|
600
877
|
/**
|
|
601
878
|
* @param {string} s
|
|
602
879
|
* @param {ParseContext} ctx
|
|
@@ -604,7 +881,13 @@ function isPropertyBlockLeaderOnly(valuePart, leader) {
|
|
|
604
881
|
* @param {number} col
|
|
605
882
|
* @returns {Record<string, unknown>|null}
|
|
606
883
|
*/
|
|
607
|
-
function
|
|
884
|
+
function parseInlineObjectSlow(s, ctx = {}, lineNum = 0, col = 0) {
|
|
885
|
+
return nested(ctx, lineNum, col, () =>
|
|
886
|
+
parseInlineObjectSlowBody(s, ctx, lineNum, col),
|
|
887
|
+
);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
function parseInlineObjectSlowBody(s, ctx, lineNum, col) {
|
|
608
891
|
if (!s.startsWith("{")) return null;
|
|
609
892
|
if (!s.includes("}")) {
|
|
610
893
|
return null;
|
|
@@ -627,77 +910,10 @@ function parseInlineObject(s, ctx = {}, lineNum = 0, col = 0) {
|
|
|
627
910
|
);
|
|
628
911
|
}
|
|
629
912
|
const body = s.slice(1, -1);
|
|
630
|
-
const parts =
|
|
631
|
-
let start = 0;
|
|
632
|
-
let inSingle = false;
|
|
633
|
-
let inDouble = false;
|
|
634
|
-
let escape = false;
|
|
635
|
-
let braceDepth = 0;
|
|
636
|
-
let bracketDepth = 0;
|
|
637
|
-
for (let i = 0; i < body.length; i++) {
|
|
638
|
-
const ch = body[i];
|
|
639
|
-
if (escape) {
|
|
640
|
-
escape = false;
|
|
641
|
-
continue;
|
|
642
|
-
}
|
|
643
|
-
if (inSingle) {
|
|
644
|
-
if (ch === "\\") {
|
|
645
|
-
escape = true;
|
|
646
|
-
} else if (ch === "'") {
|
|
647
|
-
inSingle = false;
|
|
648
|
-
}
|
|
649
|
-
continue;
|
|
650
|
-
}
|
|
651
|
-
if (inDouble) {
|
|
652
|
-
if (ch === "\\") {
|
|
653
|
-
escape = true;
|
|
654
|
-
} else if (ch === '"') {
|
|
655
|
-
inDouble = false;
|
|
656
|
-
}
|
|
657
|
-
continue;
|
|
658
|
-
}
|
|
659
|
-
if (ch === "'") {
|
|
660
|
-
inSingle = true;
|
|
661
|
-
continue;
|
|
662
|
-
}
|
|
663
|
-
if (ch === '"') {
|
|
664
|
-
inDouble = true;
|
|
665
|
-
continue;
|
|
666
|
-
}
|
|
667
|
-
if (ch === "{") {
|
|
668
|
-
braceDepth++;
|
|
669
|
-
continue;
|
|
670
|
-
}
|
|
671
|
-
if (ch === "}") {
|
|
672
|
-
if (braceDepth > 0) braceDepth--;
|
|
673
|
-
continue;
|
|
674
|
-
}
|
|
675
|
-
if (ch === "[") {
|
|
676
|
-
bracketDepth++;
|
|
677
|
-
continue;
|
|
678
|
-
}
|
|
679
|
-
if (ch === "]") {
|
|
680
|
-
if (bracketDepth > 0) bracketDepth--;
|
|
681
|
-
continue;
|
|
682
|
-
}
|
|
683
|
-
if (ch === "," && braceDepth === 0 && bracketDepth === 0) {
|
|
684
|
-
if (i > 0 && body[i - 1] === " ") {
|
|
685
|
-
throw new Error(
|
|
686
|
-
'Unexpected space before ","' +
|
|
687
|
-
locSuffix(ctx, lineNum, col + 1 + i - 1),
|
|
688
|
-
);
|
|
689
|
-
}
|
|
690
|
-
if (i + 1 >= body.length || body[i + 1] !== " ") {
|
|
691
|
-
throw new Error(
|
|
692
|
-
'Expected space after ","' + locSuffix(ctx, lineNum, col + 1 + i),
|
|
693
|
-
);
|
|
694
|
-
}
|
|
695
|
-
parts.push({ text: body.slice(start, i), start });
|
|
696
|
-
start = i + 2;
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
parts.push({ text: body.slice(start), start });
|
|
913
|
+
const parts = splitTopLevelCommas(body, ctx, lineNum, col + 1);
|
|
700
914
|
const obj = {};
|
|
915
|
+
/** @type {Map<string, [number, number]>} */
|
|
916
|
+
const seen = new Map();
|
|
701
917
|
for (const part of parts) {
|
|
702
918
|
if (part.text.length === 0) {
|
|
703
919
|
throw new Error("Missing key" + locSuffix(ctx, lineNum, col + 1));
|
|
@@ -710,598 +926,1107 @@ function parseInlineObject(s, ctx = {}, lineNum = 0, col = 0) {
|
|
|
710
926
|
);
|
|
711
927
|
}
|
|
712
928
|
const { key, valuePart, valueCol } = keyValue;
|
|
929
|
+
claimKey(seen, key, ctx, lineNum, partCol);
|
|
713
930
|
if (valuePart === "") {
|
|
714
931
|
throw new Error("Missing value" + locSuffix(ctx, lineNum, valueCol));
|
|
715
932
|
}
|
|
716
|
-
obj
|
|
933
|
+
setOwn(obj, key, parseScalar(valuePart, ctx, lineNum, valueCol));
|
|
717
934
|
}
|
|
718
935
|
return obj;
|
|
719
936
|
}
|
|
720
937
|
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
* @returns {unknown}
|
|
727
|
-
*/
|
|
728
|
-
function parseScalar(s, ctx = {}, lineNum = 0, col = 0) {
|
|
729
|
-
// Strip inline comments first
|
|
730
|
-
s = stripInlineComment(s);
|
|
938
|
+
function parseInlineArraySlow(s, ctx = {}, lineNum = 0, col = 0) {
|
|
939
|
+
return nested(ctx, lineNum, col, () =>
|
|
940
|
+
parseInlineArraySlowBody(s, ctx, lineNum, col),
|
|
941
|
+
);
|
|
942
|
+
}
|
|
731
943
|
|
|
732
|
-
|
|
733
|
-
if (s
|
|
734
|
-
if (s
|
|
735
|
-
|
|
736
|
-
if (s === "infinity") return Infinity;
|
|
737
|
-
if (s === "-infinity") return -Infinity;
|
|
738
|
-
const num = parseNumber(s, ctx, lineNum, col);
|
|
739
|
-
if (num !== undefined) return num;
|
|
740
|
-
if (s.startsWith('"')) {
|
|
741
|
-
if (!s.endsWith('"') || s.length < 2) {
|
|
742
|
-
throw new Error(
|
|
743
|
-
"Unterminated string" + locSuffix(ctx, lineNum, col + s.length),
|
|
744
|
-
);
|
|
745
|
-
}
|
|
746
|
-
return parseQuotedString(s, ctx, lineNum, col);
|
|
747
|
-
}
|
|
748
|
-
if (s.startsWith("'")) {
|
|
749
|
-
if (!s.endsWith("'") || s.length < 2) {
|
|
750
|
-
throw new Error(
|
|
751
|
-
"Unterminated string" + locSuffix(ctx, lineNum, col + s.length),
|
|
752
|
-
);
|
|
753
|
-
}
|
|
754
|
-
return s.slice(1, -1);
|
|
944
|
+
function parseInlineArraySlowBody(s, ctx, lineNum, col) {
|
|
945
|
+
if (!s.startsWith("[")) return [];
|
|
946
|
+
if (!s.endsWith("]")) {
|
|
947
|
+
throw new Error("Unterminated inline array" + locSuffix(ctx, lineNum, col));
|
|
755
948
|
}
|
|
756
|
-
if (s
|
|
757
|
-
if (s
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
949
|
+
if (s === "[]") return [];
|
|
950
|
+
if (s[1] === " ") {
|
|
951
|
+
throw new Error(
|
|
952
|
+
'Unexpected space after "["' + locSuffix(ctx, lineNum, col + 1),
|
|
953
|
+
);
|
|
954
|
+
}
|
|
955
|
+
if (s[s.length - 2] === " ") {
|
|
956
|
+
throw new Error(
|
|
957
|
+
'Unexpected space before "]"' +
|
|
958
|
+
locSuffix(ctx, lineNum, col + s.length - 2),
|
|
959
|
+
);
|
|
960
|
+
}
|
|
961
|
+
const body = s.slice(1, -1);
|
|
962
|
+
const parts = splitTopLevelCommas(body, ctx, lineNum, col + 1, {
|
|
963
|
+
angles: true,
|
|
964
|
+
validateArrays: true,
|
|
965
|
+
});
|
|
966
|
+
const arr = [];
|
|
967
|
+
for (const part of parts) {
|
|
968
|
+
if (part.text.length === 0) {
|
|
969
|
+
throw new Error(
|
|
970
|
+
"Missing array element" + locSuffix(ctx, lineNum, col + 1 + part.start),
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
const partCol = col + 1 + part.start;
|
|
974
|
+
arr.push(parseScalar(part.text, ctx, lineNum, partCol));
|
|
975
|
+
}
|
|
976
|
+
return arr;
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
function parseInlineObject(s, ctx = {}, lineNum = 0, col = 0) {
|
|
980
|
+
return parseInlineObjectBody(s, ctx, lineNum, col);
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
function parseInlineObjectBody(s, ctx, lineNum, col) {
|
|
984
|
+
if (!s.startsWith("{")) return null;
|
|
985
|
+
if (!s.includes("}")) {
|
|
986
|
+
return null;
|
|
987
|
+
}
|
|
988
|
+
if (!s.endsWith("}")) {
|
|
989
|
+
throw new Error(
|
|
990
|
+
"Unexpected inline object content" + locSuffix(ctx, lineNum, col),
|
|
991
|
+
);
|
|
992
|
+
}
|
|
993
|
+
if (s === "{}") return {};
|
|
994
|
+
if (s[1] === " ") {
|
|
995
|
+
throw new Error(
|
|
996
|
+
'Unexpected space after "{"' + locSuffix(ctx, lineNum, col + 1),
|
|
997
|
+
);
|
|
998
|
+
}
|
|
999
|
+
if (s[s.length - 2] === " ") {
|
|
1000
|
+
throw new Error(
|
|
1001
|
+
'Unexpected space before "}"' +
|
|
1002
|
+
locSuffix(ctx, lineNum, col + s.length - 2),
|
|
1003
|
+
);
|
|
1004
|
+
}
|
|
1005
|
+
const [obj, next] = parseInlineObjectAt(s, 0, ctx, lineNum, col);
|
|
1006
|
+
if (next !== s.length) {
|
|
1007
|
+
throw new Error(
|
|
1008
|
+
"Unexpected inline object content" + locSuffix(ctx, lineNum, col),
|
|
1009
|
+
);
|
|
1010
|
+
}
|
|
1011
|
+
return obj;
|
|
764
1012
|
}
|
|
765
1013
|
|
|
766
1014
|
/**
|
|
1015
|
+
* Parse an inline object beginning at s[open], in one pass. Values are read
|
|
1016
|
+
* by their own parser, so nested containers are not rescanned at every
|
|
1017
|
+
* level. Returns the value and the index after the closing brace.
|
|
767
1018
|
* @param {string} s
|
|
768
|
-
* @
|
|
769
|
-
*/
|
|
770
|
-
/**
|
|
771
|
-
* @param {string} s
|
|
1019
|
+
* @param {number} open
|
|
772
1020
|
* @param {ParseContext} ctx
|
|
773
1021
|
* @param {number} lineNum
|
|
774
1022
|
* @param {number} col
|
|
775
|
-
* @returns {number
|
|
1023
|
+
* @returns {[Record<string, unknown>, number]}
|
|
776
1024
|
*/
|
|
777
|
-
function
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
1025
|
+
function parseInlineObjectAt(s, open, ctx, lineNum, col) {
|
|
1026
|
+
return nested(ctx, lineNum, col + open, () =>
|
|
1027
|
+
parseInlineObjectAtBody(s, open, ctx, lineNum, col),
|
|
1028
|
+
);
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
function parseInlineObjectAtBody(s, open, ctx, lineNum, col) {
|
|
1032
|
+
const obj = {};
|
|
1033
|
+
/** @type {Map<string, [number, number]>} */
|
|
1034
|
+
const seen = new Map();
|
|
1035
|
+
let i = open + 1;
|
|
1036
|
+
if (s.charCodeAt(i) === 0x7d) return [obj, i + 1];
|
|
1037
|
+
if (s.charCodeAt(i) === 0x20) {
|
|
1038
|
+
fail(ctx, 'Unexpected space after "{"', lineNum, col + i);
|
|
1039
|
+
}
|
|
1040
|
+
for (;;) {
|
|
1041
|
+
if (i >= s.length) {
|
|
788
1042
|
throw new Error(
|
|
789
|
-
"
|
|
790
|
-
locSuffix(ctx, lineNum, col +
|
|
1043
|
+
"Unexpected newline in inline object" +
|
|
1044
|
+
locSuffix(ctx, lineNum, col + open),
|
|
791
1045
|
);
|
|
792
1046
|
}
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
let hasExponent = false;
|
|
797
|
-
for (let i = 0; i < s.length; i++) {
|
|
798
|
-
const c = s[i];
|
|
799
|
-
if (c === " ") continue;
|
|
800
|
-
if (c >= "0" && c <= "9") {
|
|
801
|
-
hasDigit = true;
|
|
802
|
-
continue;
|
|
1047
|
+
const c = s.charCodeAt(i);
|
|
1048
|
+
if (c === 0x7d || c === 0x2c) {
|
|
1049
|
+
throw new Error("Missing key" + locSuffix(ctx, lineNum, col + i));
|
|
803
1050
|
}
|
|
804
|
-
|
|
805
|
-
if (
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
continue;
|
|
1051
|
+
const colon = findInlineKeyColon(s, i);
|
|
1052
|
+
if (colon < 0) {
|
|
1053
|
+
throw new Error(
|
|
1054
|
+
"Expected colon after key" + locSuffix(ctx, lineNum, col),
|
|
1055
|
+
);
|
|
810
1056
|
}
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
1057
|
+
const partCol = col + i;
|
|
1058
|
+
const key = parseInlineKeyAt(s, i, colon, ctx, lineNum, col, col + open);
|
|
1059
|
+
claimKey(seen, key, ctx, lineNum, partCol);
|
|
1060
|
+
let valueIndex = colon + 1;
|
|
1061
|
+
if (valueIndex >= s.length) {
|
|
1062
|
+
throw new Error(
|
|
1063
|
+
"Missing value" + locSuffix(ctx, lineNum, col + valueIndex),
|
|
1064
|
+
);
|
|
815
1065
|
}
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
if (
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
1066
|
+
if (s.charCodeAt(valueIndex) === 0x20) {
|
|
1067
|
+
if (s.charCodeAt(valueIndex + 1) === 0x20) {
|
|
1068
|
+
fail(ctx, 'Unexpected space after ":"', lineNum, col + valueIndex + 1);
|
|
1069
|
+
}
|
|
1070
|
+
valueIndex++;
|
|
1071
|
+
} else if (
|
|
1072
|
+
s.charCodeAt(valueIndex) !== 0x2c &&
|
|
1073
|
+
s.charCodeAt(valueIndex) !== 0x7d
|
|
1074
|
+
) {
|
|
1075
|
+
fail(ctx, 'Expected space after ":"', lineNum, col + colon);
|
|
1076
|
+
}
|
|
1077
|
+
if (
|
|
1078
|
+
valueIndex >= s.length ||
|
|
1079
|
+
s.charCodeAt(valueIndex) === 0x7d ||
|
|
1080
|
+
s.charCodeAt(valueIndex) === 0x2c
|
|
1081
|
+
) {
|
|
827
1082
|
throw new Error(
|
|
828
|
-
"
|
|
1083
|
+
"Missing value" + locSuffix(ctx, lineNum, col + valueIndex),
|
|
829
1084
|
);
|
|
830
1085
|
}
|
|
1086
|
+
const [value, next] = parseInlineValueAt(
|
|
1087
|
+
s,
|
|
1088
|
+
valueIndex,
|
|
1089
|
+
ctx,
|
|
1090
|
+
lineNum,
|
|
1091
|
+
col,
|
|
1092
|
+
0x7d,
|
|
1093
|
+
);
|
|
1094
|
+
setOwn(obj, key, value);
|
|
1095
|
+
if (next >= s.length) {
|
|
1096
|
+
throw new Error(
|
|
1097
|
+
"Unexpected newline in inline object" +
|
|
1098
|
+
locSuffix(ctx, lineNum, col + open),
|
|
1099
|
+
);
|
|
1100
|
+
}
|
|
1101
|
+
const after = s.charCodeAt(next);
|
|
1102
|
+
if (after === 0x7d) {
|
|
1103
|
+
if (s.charCodeAt(next - 1) === 0x20) {
|
|
1104
|
+
fail(ctx, 'Unexpected space before "}"', lineNum, col + next - 1);
|
|
1105
|
+
}
|
|
1106
|
+
return [obj, next + 1];
|
|
1107
|
+
}
|
|
1108
|
+
if (after === 0x2c) {
|
|
1109
|
+
if (s.charCodeAt(next - 1) === 0x20) {
|
|
1110
|
+
fail(ctx, 'Unexpected space before ","', lineNum, col + next - 1);
|
|
1111
|
+
}
|
|
1112
|
+
if (next + 1 >= s.length || s.charCodeAt(next + 1) !== 0x20) {
|
|
1113
|
+
fail(ctx, 'Expected space after ","', lineNum, col + next);
|
|
1114
|
+
}
|
|
1115
|
+
if (s.charCodeAt(next + 2) === 0x20) {
|
|
1116
|
+
fail(ctx, 'Unexpected space after ","', lineNum, col + next + 2);
|
|
1117
|
+
}
|
|
1118
|
+
i = next + 2;
|
|
1119
|
+
continue;
|
|
1120
|
+
}
|
|
1121
|
+
if (after === 0x20) {
|
|
1122
|
+
if (s.charCodeAt(next + 1) === 0x7d) {
|
|
1123
|
+
fail(ctx, 'Unexpected space before "}"', lineNum, col + next);
|
|
1124
|
+
}
|
|
1125
|
+
if (s.charCodeAt(next + 1) === 0x2c) {
|
|
1126
|
+
fail(ctx, 'Unexpected space before ","', lineNum, col + next);
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
throw new Error(
|
|
1130
|
+
`Unexpected character "${s[next]}"` + locSuffix(ctx, lineNum, col + next),
|
|
1131
|
+
);
|
|
831
1132
|
}
|
|
832
|
-
const compact = s.replace(/ /g, "");
|
|
833
|
-
if (/^-?\d+$/.test(compact)) return BigInt(compact);
|
|
834
|
-
// Float patterns: with decimal point, or with exponent, or both
|
|
835
|
-
if (
|
|
836
|
-
/^-?\d*\.\d*([eE][+-]?\d+)?$/.test(compact) ||
|
|
837
|
-
/^-?\d+[eE][+-]?\d+$/.test(compact)
|
|
838
|
-
) {
|
|
839
|
-
const n = Number(compact);
|
|
840
|
-
if (!isNaN(n)) return n;
|
|
841
|
-
}
|
|
842
|
-
return undefined;
|
|
843
1133
|
}
|
|
844
1134
|
|
|
845
1135
|
/**
|
|
1136
|
+
* Find the colon that separates an inline object's key from its value.
|
|
1137
|
+
* Quotes, tags, and references are opaque, as they are to the syntax scan.
|
|
846
1138
|
* @param {string} s
|
|
847
|
-
* @param {
|
|
848
|
-
* @
|
|
849
|
-
* @param {number} col
|
|
850
|
-
* @returns {string}
|
|
1139
|
+
* @param {number} start
|
|
1140
|
+
* @returns {number}
|
|
851
1141
|
*/
|
|
852
|
-
function
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
1142
|
+
function findInlineKeyColon(s, start) {
|
|
1143
|
+
const n = s.length;
|
|
1144
|
+
let i = start;
|
|
1145
|
+
while (i < n) {
|
|
1146
|
+
const c = s.charCodeAt(i);
|
|
1147
|
+
if (c === 0x22 || c === 0x27) {
|
|
1148
|
+
i++;
|
|
1149
|
+
while (i < n) {
|
|
1150
|
+
const q = s.charCodeAt(i);
|
|
1151
|
+
if (q === 0x5c) {
|
|
1152
|
+
i += 2;
|
|
1153
|
+
continue;
|
|
1154
|
+
}
|
|
1155
|
+
i++;
|
|
1156
|
+
if (q === c) break;
|
|
1157
|
+
}
|
|
1158
|
+
continue;
|
|
1159
|
+
}
|
|
1160
|
+
if (c === 0x3c && isReferenceStartAt(s, i)) {
|
|
1161
|
+
const end = s.indexOf(">", i);
|
|
1162
|
+
i = end < 0 ? n : end + 1;
|
|
1163
|
+
continue;
|
|
1164
|
+
}
|
|
1165
|
+
if (c === 0x28) {
|
|
1166
|
+
i++;
|
|
1167
|
+
while (i < n) {
|
|
1168
|
+
const q = s.charCodeAt(i);
|
|
1169
|
+
if (q === 0x29) {
|
|
1170
|
+
i++;
|
|
1171
|
+
break;
|
|
1172
|
+
}
|
|
1173
|
+
i += q === 0x5c ? 2 : 1;
|
|
1174
|
+
}
|
|
1175
|
+
continue;
|
|
1176
|
+
}
|
|
1177
|
+
if (c === 0x3a) return i;
|
|
1178
|
+
i++;
|
|
1179
|
+
}
|
|
1180
|
+
return -1;
|
|
856
1181
|
}
|
|
857
1182
|
|
|
858
1183
|
/**
|
|
859
|
-
*
|
|
1184
|
+
* Read an inline object's key, from start to its colon, with the checks the
|
|
1185
|
+
* shared key reader makes for a block object.
|
|
860
1186
|
* @param {string} s
|
|
1187
|
+
* @param {number} start
|
|
1188
|
+
* @param {number} colon
|
|
861
1189
|
* @param {ParseContext} ctx
|
|
862
1190
|
* @param {number} lineNum
|
|
863
1191
|
* @param {number} col
|
|
864
1192
|
* @returns {string}
|
|
865
1193
|
*/
|
|
866
|
-
function
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
1194
|
+
function parseInlineKeyAt(s, start, colon, ctx, lineNum, col, inlineCol) {
|
|
1195
|
+
const keyRaw = s.slice(start, colon);
|
|
1196
|
+
const sCol = col + start;
|
|
1197
|
+
if (keyRaw.endsWith(" ")) {
|
|
870
1198
|
throw new Error(
|
|
871
|
-
|
|
1199
|
+
'Unexpected space before ":"' +
|
|
1200
|
+
locSuffix(ctx, lineNum, sCol + keyRaw.length - 1),
|
|
872
1201
|
);
|
|
873
1202
|
}
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
i++;
|
|
893
|
-
break;
|
|
894
|
-
case "/":
|
|
895
|
-
out += "/";
|
|
896
|
-
i++;
|
|
897
|
-
break;
|
|
898
|
-
case "b":
|
|
899
|
-
out += "\b";
|
|
900
|
-
i++;
|
|
901
|
-
break;
|
|
902
|
-
case "f":
|
|
903
|
-
out += "\f";
|
|
904
|
-
i++;
|
|
905
|
-
break;
|
|
906
|
-
case "n":
|
|
907
|
-
out += "\n";
|
|
908
|
-
i++;
|
|
909
|
-
break;
|
|
910
|
-
case "r":
|
|
911
|
-
out += "\r";
|
|
912
|
-
i++;
|
|
913
|
-
break;
|
|
914
|
-
case "t":
|
|
915
|
-
out += "\t";
|
|
916
|
-
i++;
|
|
917
|
-
break;
|
|
918
|
-
case "u": {
|
|
919
|
-
// YAY uses \u{XXXXXX} syntax (variable-length with braces)
|
|
920
|
-
const braceStart = i + 2;
|
|
921
|
-
const uCol = col + i + 1; // Column of 'u' for "Bad escaped character"
|
|
922
|
-
const braceCol = col + braceStart; // Column of '{' for other errors
|
|
923
|
-
if (braceStart >= s.length - 1 || s[braceStart] !== "{") {
|
|
924
|
-
// Old-style \uXXXX syntax is not supported
|
|
925
|
-
throw new Error(
|
|
926
|
-
"Bad escaped character" + locSuffix(ctx, lineNum, uCol),
|
|
927
|
-
);
|
|
928
|
-
}
|
|
929
|
-
// Find closing brace
|
|
930
|
-
let braceEnd = braceStart + 1;
|
|
931
|
-
while (braceEnd < s.length - 1 && s[braceEnd] !== "}") {
|
|
932
|
-
braceEnd++;
|
|
933
|
-
}
|
|
934
|
-
if (braceEnd >= s.length - 1 || s[braceEnd] !== "}") {
|
|
935
|
-
throw new Error(
|
|
936
|
-
"Bad Unicode escape" + locSuffix(ctx, lineNum, braceCol),
|
|
937
|
-
);
|
|
938
|
-
}
|
|
939
|
-
const hexStart = braceStart + 1;
|
|
940
|
-
if (hexStart === braceEnd) {
|
|
941
|
-
throw new Error(
|
|
942
|
-
"Bad Unicode escape" + locSuffix(ctx, lineNum, braceCol),
|
|
943
|
-
);
|
|
944
|
-
}
|
|
945
|
-
// Check for too many hex digits (max 6)
|
|
946
|
-
if (braceEnd - hexStart > 6) {
|
|
947
|
-
throw new Error(
|
|
948
|
-
"Bad Unicode escape" + locSuffix(ctx, lineNum, braceCol),
|
|
949
|
-
);
|
|
950
|
-
}
|
|
951
|
-
let hex = "";
|
|
952
|
-
for (let j = hexStart; j < braceEnd; j++) {
|
|
953
|
-
const c = s[j];
|
|
954
|
-
if (!/[0-9a-fA-F]/.test(c)) {
|
|
955
|
-
throw new Error(
|
|
956
|
-
"Bad Unicode escape" + locSuffix(ctx, lineNum, braceCol),
|
|
957
|
-
);
|
|
958
|
-
}
|
|
959
|
-
hex += c;
|
|
960
|
-
}
|
|
961
|
-
const code = parseInt(hex, 16);
|
|
962
|
-
if (code >= 0xd800 && code <= 0xdfff) {
|
|
963
|
-
throw new Error(
|
|
964
|
-
"Illegal surrogate" + locSuffix(ctx, lineNum, braceCol),
|
|
965
|
-
);
|
|
966
|
-
}
|
|
967
|
-
if (code > 0x10ffff) {
|
|
968
|
-
throw new Error(
|
|
969
|
-
"Unicode code point out of range" +
|
|
970
|
-
locSuffix(ctx, lineNum, braceCol),
|
|
971
|
-
);
|
|
972
|
-
}
|
|
973
|
-
out += String.fromCodePoint(code);
|
|
974
|
-
i = braceEnd; // Loop will increment to braceEnd + 1
|
|
975
|
-
break;
|
|
976
|
-
}
|
|
977
|
-
default:
|
|
978
|
-
throw new Error(
|
|
979
|
-
"Bad escaped character" + locSuffix(ctx, lineNum, col + i + 1),
|
|
980
|
-
);
|
|
981
|
-
}
|
|
982
|
-
} else {
|
|
983
|
-
// Unescaped control characters are illegal in JSON strings.
|
|
984
|
-
const code = ch.charCodeAt(0);
|
|
985
|
-
if (code < 0x20) {
|
|
986
|
-
throw new Error(
|
|
987
|
-
"Bad character in string" + locSuffix(ctx, lineNum, col + i),
|
|
988
|
-
);
|
|
989
|
-
}
|
|
990
|
-
out += ch;
|
|
1203
|
+
if (keyRaw.startsWith('"') || keyRaw.startsWith("'")) {
|
|
1204
|
+
const quote = keyRaw[0];
|
|
1205
|
+
if (keyRaw.length < 2 || keyRaw[keyRaw.length - 1] !== quote) {
|
|
1206
|
+
throw new Error(
|
|
1207
|
+
"Unterminated string" +
|
|
1208
|
+
locSuffix(ctx, lineNum, sCol + keyRaw.length - 1),
|
|
1209
|
+
);
|
|
1210
|
+
}
|
|
1211
|
+
return parseQuotedString(keyRaw, ctx, lineNum, sCol);
|
|
1212
|
+
}
|
|
1213
|
+
if (keyRaw.length === 0) {
|
|
1214
|
+
throw new Error("Missing key" + locSuffix(ctx, lineNum, col + colon));
|
|
1215
|
+
}
|
|
1216
|
+
for (let i = 0; i < keyRaw.length; i++) {
|
|
1217
|
+
if (!KEY_CHARACTER.test(keyRaw[i])) {
|
|
1218
|
+
const message = i === 0 ? "Invalid key" : "Invalid key character";
|
|
1219
|
+
const errCol = i === 0 ? inlineCol : sCol + i;
|
|
1220
|
+
throw new Error(message + locSuffix(ctx, lineNum, errCol));
|
|
991
1221
|
}
|
|
992
1222
|
}
|
|
993
|
-
return
|
|
1223
|
+
return detachString(keyRaw);
|
|
994
1224
|
}
|
|
995
1225
|
|
|
996
1226
|
/**
|
|
1227
|
+
* Parse one inline value beginning at s[i], in one pass. A leaf value is
|
|
1228
|
+
* read from its whole text with the shared scalar reader; a container is
|
|
1229
|
+
* read by its own cursor, so its body is not rescanned. Returns the value
|
|
1230
|
+
* and the index of what follows its suffix, the delimiter or the offending
|
|
1231
|
+
* character.
|
|
997
1232
|
* @param {string} s
|
|
1233
|
+
* @param {number} i
|
|
998
1234
|
* @param {ParseContext} ctx
|
|
999
1235
|
* @param {number} lineNum
|
|
1000
1236
|
* @param {number} col
|
|
1001
|
-
* @
|
|
1237
|
+
* @param {number} closeCode
|
|
1238
|
+
* @returns {[unknown, number]}
|
|
1002
1239
|
*/
|
|
1003
|
-
function
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
'Unexpected space after "["' + locSuffix(ctx, lineNum, col + 1),
|
|
1012
|
-
);
|
|
1240
|
+
function parseInlineValueAt(s, i, ctx, lineNum, col, closeCode) {
|
|
1241
|
+
const c = s.charCodeAt(i);
|
|
1242
|
+
let value;
|
|
1243
|
+
let next;
|
|
1244
|
+
if (c === 0x5b) {
|
|
1245
|
+
[value, next] = parseInlineArrayAt(s, i, ctx, lineNum, col);
|
|
1246
|
+
const [tags, n] = parseSuffix(s.slice(next), ctx, lineNum, col + next);
|
|
1247
|
+
return [wrapTags(value, tags), next + n];
|
|
1013
1248
|
}
|
|
1014
|
-
if (
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
);
|
|
1249
|
+
if (c === 0x7b) {
|
|
1250
|
+
[value, next] = parseInlineObjectAt(s, i, ctx, lineNum, col);
|
|
1251
|
+
const [tags, n] = parseSuffix(s.slice(next), ctx, lineNum, col + next);
|
|
1252
|
+
return [wrapTags(value, tags), next + n];
|
|
1019
1253
|
}
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1254
|
+
next = findInlineValueEnd(s, i, closeCode, closeCode === 0x5d);
|
|
1255
|
+
if (next < s.length) {
|
|
1256
|
+
const delimiter = s.charCodeAt(next);
|
|
1257
|
+
if (delimiter === 0x2c && s.charCodeAt(next - 1) === 0x20) {
|
|
1258
|
+
fail(ctx, 'Unexpected space before ","', lineNum, col + next - 1);
|
|
1259
|
+
}
|
|
1260
|
+
if (delimiter === closeCode && s.charCodeAt(next - 1) === 0x20) {
|
|
1261
|
+
fail(
|
|
1262
|
+
ctx,
|
|
1263
|
+
`Unexpected space before "${s[next]}"`,
|
|
1264
|
+
lineNum,
|
|
1265
|
+
col + next - 1,
|
|
1266
|
+
);
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
return [parseScalar(s.slice(i, next), ctx, lineNum, col + i), next];
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
/**
|
|
1273
|
+
* The end of an inline value's own text: its next top-level comma, or the
|
|
1274
|
+
* container's closing character. Quotes, tags, references, and nested
|
|
1275
|
+
* containers are skipped structurally.
|
|
1276
|
+
* @param {string} s
|
|
1277
|
+
* @param {number} start
|
|
1278
|
+
* @param {number} closeCode
|
|
1279
|
+
* @param {boolean} angles
|
|
1280
|
+
* @returns {number}
|
|
1281
|
+
*/
|
|
1282
|
+
function findInlineValueEnd(s, start, closeCode, angles) {
|
|
1283
|
+
const n = s.length;
|
|
1284
|
+
let i = start;
|
|
1285
|
+
let quote = 0;
|
|
1026
1286
|
let braceDepth = 0;
|
|
1027
1287
|
let bracketDepth = 0;
|
|
1028
1288
|
let angleDepth = 0;
|
|
1029
|
-
|
|
1030
|
-
const
|
|
1031
|
-
if (
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
if (inSingle) {
|
|
1036
|
-
if (ch === "\\") {
|
|
1037
|
-
escape = true;
|
|
1038
|
-
} else if (ch === "'") {
|
|
1039
|
-
inSingle = false;
|
|
1040
|
-
}
|
|
1041
|
-
continue;
|
|
1042
|
-
}
|
|
1043
|
-
if (inDouble) {
|
|
1044
|
-
if (ch === "\\") {
|
|
1045
|
-
escape = true;
|
|
1046
|
-
} else if (ch === '"') {
|
|
1047
|
-
inDouble = false;
|
|
1289
|
+
while (i < n) {
|
|
1290
|
+
const c = s.charCodeAt(i);
|
|
1291
|
+
if (quote !== 0) {
|
|
1292
|
+
if (c === 0x5c) {
|
|
1293
|
+
i += 2;
|
|
1294
|
+
continue;
|
|
1048
1295
|
}
|
|
1296
|
+
if (c === quote) quote = 0;
|
|
1297
|
+
i++;
|
|
1049
1298
|
continue;
|
|
1050
1299
|
}
|
|
1051
|
-
if (
|
|
1052
|
-
|
|
1300
|
+
if (c === 0x22 || c === 0x27) {
|
|
1301
|
+
quote = c;
|
|
1302
|
+
i++;
|
|
1053
1303
|
continue;
|
|
1054
1304
|
}
|
|
1055
|
-
if (
|
|
1056
|
-
|
|
1305
|
+
if (c === 0x3c && isReferenceStartAt(s, i)) {
|
|
1306
|
+
const end = s.indexOf(">", i);
|
|
1307
|
+
i = end < 0 ? n : end + 1;
|
|
1057
1308
|
continue;
|
|
1058
1309
|
}
|
|
1059
|
-
if (
|
|
1060
|
-
|
|
1310
|
+
if (c === 0x28) {
|
|
1311
|
+
i++;
|
|
1312
|
+
while (i < n) {
|
|
1313
|
+
const q = s.charCodeAt(i);
|
|
1314
|
+
if (q === 0x29) {
|
|
1315
|
+
i++;
|
|
1316
|
+
break;
|
|
1317
|
+
}
|
|
1318
|
+
i += q === 0x5c ? 2 : 1;
|
|
1319
|
+
}
|
|
1061
1320
|
continue;
|
|
1062
1321
|
}
|
|
1063
|
-
if (
|
|
1064
|
-
if (
|
|
1065
|
-
|
|
1322
|
+
if (angles) {
|
|
1323
|
+
if (c === 0x3c) angleDepth++;
|
|
1324
|
+
else if (c === 0x3e && angleDepth > 0) angleDepth--;
|
|
1066
1325
|
}
|
|
1067
|
-
if (
|
|
1326
|
+
if (c === 0x7b) {
|
|
1327
|
+
braceDepth++;
|
|
1328
|
+
} else if (c === 0x7d && braceDepth > 0) {
|
|
1329
|
+
braceDepth--;
|
|
1330
|
+
} else if (c === 0x5b) {
|
|
1068
1331
|
bracketDepth++;
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
if (
|
|
1072
|
-
if (
|
|
1073
|
-
|
|
1074
|
-
}
|
|
1075
|
-
if (ch === "<") {
|
|
1076
|
-
angleDepth++;
|
|
1077
|
-
continue;
|
|
1078
|
-
}
|
|
1079
|
-
if (ch === ">") {
|
|
1080
|
-
if (angleDepth > 0) angleDepth--;
|
|
1081
|
-
continue;
|
|
1332
|
+
} else if (c === 0x5d && bracketDepth > 0) {
|
|
1333
|
+
bracketDepth--;
|
|
1334
|
+
} else if (braceDepth === 0 && bracketDepth === 0 && angleDepth === 0) {
|
|
1335
|
+
if (c === 0x2c) return i;
|
|
1336
|
+
if (c === closeCode) return i;
|
|
1082
1337
|
}
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1338
|
+
i++;
|
|
1339
|
+
}
|
|
1340
|
+
return n;
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
/**
|
|
1344
|
+
* @param {string} s
|
|
1345
|
+
* @param {ParseContext} ctx
|
|
1346
|
+
* @param {number} lineNum
|
|
1347
|
+
* @param {number} col
|
|
1348
|
+
* @returns {unknown}
|
|
1349
|
+
*/
|
|
1350
|
+
function parseScalar(
|
|
1351
|
+
s,
|
|
1352
|
+
ctx = {},
|
|
1353
|
+
lineNum = 0,
|
|
1354
|
+
col = 0,
|
|
1355
|
+
syntax = scanTopLevel(s),
|
|
1356
|
+
) {
|
|
1357
|
+
// Strip inline comments first
|
|
1358
|
+
s = stripInlineComment(s, ctx, lineNum, col, syntax);
|
|
1359
|
+
const first =
|
|
1360
|
+
syntax.firstTag !== undefined && syntax.firstTag < s.length
|
|
1361
|
+
? syntax.firstTag
|
|
1362
|
+
: undefined;
|
|
1363
|
+
if (first !== undefined) {
|
|
1364
|
+
if (first === 0) {
|
|
1365
|
+
const [name, n] = parseTag(s, ctx, lineNum, col);
|
|
1366
|
+
const [tags, m] = parseSuffix(s.slice(n), ctx, lineNum, col + n);
|
|
1367
|
+
if (n + m !== s.length) {
|
|
1096
1368
|
throw new Error(
|
|
1097
|
-
|
|
1369
|
+
"Unexpected text after a tag" + locSuffix(ctx, lineNum, col + n),
|
|
1098
1370
|
);
|
|
1099
1371
|
}
|
|
1100
|
-
|
|
1101
|
-
start = i + 2;
|
|
1372
|
+
return wrapTags(decodeSymbolText(name, ctx), tags);
|
|
1102
1373
|
}
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1374
|
+
const atomEnd = s.slice(0, first).trimEnd().length;
|
|
1375
|
+
const [tags, n] = parseSuffix(
|
|
1376
|
+
s.slice(atomEnd),
|
|
1377
|
+
ctx,
|
|
1378
|
+
lineNum,
|
|
1379
|
+
col + atomEnd,
|
|
1380
|
+
);
|
|
1381
|
+
if (atomEnd + n !== s.length) {
|
|
1108
1382
|
throw new Error(
|
|
1109
|
-
|
|
1383
|
+
`Unexpected character "${s[atomEnd + n]}"` +
|
|
1384
|
+
locSuffix(ctx, lineNum, col + atomEnd + n),
|
|
1110
1385
|
);
|
|
1111
1386
|
}
|
|
1112
|
-
const
|
|
1113
|
-
|
|
1387
|
+
const atom = s.slice(0, atomEnd);
|
|
1388
|
+
return wrapTags(parseScalarAtom(atom, ctx, lineNum, col), tags);
|
|
1114
1389
|
}
|
|
1115
|
-
return
|
|
1390
|
+
return parseScalarAtom(s, ctx, lineNum, col);
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
/**
|
|
1394
|
+
* Whether a huge malformed flood hit the depth limit. The old reader would
|
|
1395
|
+
* rescan a remainder this large at every level, so keep the first error.
|
|
1396
|
+
* @param {unknown} error
|
|
1397
|
+
* @param {string} s
|
|
1398
|
+
*/
|
|
1399
|
+
function isHugeDepthError(error, s) {
|
|
1400
|
+
return (
|
|
1401
|
+
s.length > MAX_DEPTH_FALLBACK_LENGTH &&
|
|
1402
|
+
error instanceof Error &&
|
|
1403
|
+
error.message.startsWith("Inline nesting deeper than")
|
|
1404
|
+
);
|
|
1116
1405
|
}
|
|
1117
1406
|
|
|
1118
1407
|
/**
|
|
1408
|
+
* Parse an inline array with the one-pass reader, falling back to the
|
|
1409
|
+
* original substring reader when the one-pass reader refuses it. The
|
|
1410
|
+
* fallback preserves the reading and errors of an ordinary malformed
|
|
1411
|
+
* document, while valid documents never pay for it.
|
|
1119
1412
|
* @param {string} s
|
|
1120
1413
|
* @param {ParseContext} ctx
|
|
1121
1414
|
* @param {number} lineNum
|
|
1122
1415
|
* @param {number} col
|
|
1416
|
+
* @returns {unknown[]}
|
|
1123
1417
|
*/
|
|
1124
|
-
function
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
const
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
}
|
|
1135
|
-
|
|
1136
|
-
if (ch === "\\") {
|
|
1137
|
-
escape = true;
|
|
1138
|
-
} else if (ch === "'") {
|
|
1139
|
-
inSingle = false;
|
|
1140
|
-
}
|
|
1141
|
-
continue;
|
|
1418
|
+
function parseInlineArrayWithFallback(s, ctx, lineNum, col) {
|
|
1419
|
+
if (ctx.inlineSlow) return parseInlineArraySlow(s, ctx, lineNum, col);
|
|
1420
|
+
try {
|
|
1421
|
+
return parseInlineArray(s, ctx, lineNum, col);
|
|
1422
|
+
} catch (error) {
|
|
1423
|
+
if (isHugeDepthError(error, s)) throw error;
|
|
1424
|
+
const previous = ctx.inlineSlow;
|
|
1425
|
+
ctx.inlineSlow = true;
|
|
1426
|
+
try {
|
|
1427
|
+
return parseInlineArraySlow(s, ctx, lineNum, col);
|
|
1428
|
+
} finally {
|
|
1429
|
+
ctx.inlineSlow = previous;
|
|
1142
1430
|
}
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
/**
|
|
1435
|
+
* Parse an inline object with the one-pass reader, falling back as
|
|
1436
|
+
* parseInlineArrayWithFallback does.
|
|
1437
|
+
* @param {string} s
|
|
1438
|
+
* @param {ParseContext} ctx
|
|
1439
|
+
* @param {number} lineNum
|
|
1440
|
+
* @param {number} col
|
|
1441
|
+
* @returns {Record<string, unknown>|null}
|
|
1442
|
+
*/
|
|
1443
|
+
function parseInlineObjectWithFallback(s, ctx, lineNum, col) {
|
|
1444
|
+
if (ctx.inlineSlow) return parseInlineObjectSlow(s, ctx, lineNum, col);
|
|
1445
|
+
try {
|
|
1446
|
+
return parseInlineObject(s, ctx, lineNum, col);
|
|
1447
|
+
} catch (error) {
|
|
1448
|
+
if (isHugeDepthError(error, s)) throw error;
|
|
1449
|
+
const previous = ctx.inlineSlow;
|
|
1450
|
+
ctx.inlineSlow = true;
|
|
1451
|
+
try {
|
|
1452
|
+
return parseInlineObjectSlow(s, ctx, lineNum, col);
|
|
1453
|
+
} finally {
|
|
1454
|
+
ctx.inlineSlow = previous;
|
|
1158
1455
|
}
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
function parseScalarAtom(s, ctx = {}, lineNum = 0, col = 0) {
|
|
1460
|
+
if (s === "null") return null;
|
|
1461
|
+
if (s === "undefined") return undefined;
|
|
1462
|
+
if (s === "true") return true;
|
|
1463
|
+
if (s === "false") return false;
|
|
1464
|
+
if (s === "nan") return NaN;
|
|
1465
|
+
if (s === "infinity") return Infinity;
|
|
1466
|
+
if (s === "-infinity") return -Infinity;
|
|
1467
|
+
const num = parseNumber(s, ctx, lineNum, col);
|
|
1468
|
+
if (num !== undefined) return num;
|
|
1469
|
+
if (s.startsWith('"')) {
|
|
1470
|
+
if (s.length < 2 || !s.endsWith('"')) {
|
|
1471
|
+
// Closed early, with more after it, or never closed
|
|
1472
|
+
let k = 1;
|
|
1473
|
+
while (k < s.length && s[k] !== '"') k += s[k] === "\\" ? 2 : 1;
|
|
1474
|
+
if (k < s.length) {
|
|
1162
1475
|
throw new Error(
|
|
1163
|
-
|
|
1476
|
+
`Unexpected character "${s[0]}"` + locSuffix(ctx, lineNum, col),
|
|
1164
1477
|
);
|
|
1165
1478
|
}
|
|
1166
|
-
continue;
|
|
1167
1479
|
}
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1480
|
+
return parseQuotedString(s, ctx, lineNum, col);
|
|
1481
|
+
}
|
|
1482
|
+
if (s.startsWith("'")) return parseSingleQuotedString(s, ctx, lineNum, col);
|
|
1483
|
+
if (s.startsWith("[")) {
|
|
1484
|
+
// Inline arrays must close on the same line.
|
|
1485
|
+
if (!s.endsWith("]")) {
|
|
1486
|
+
throw new Error(
|
|
1487
|
+
"Unexpected newline in inline array" + locSuffix(ctx, lineNum, col),
|
|
1488
|
+
);
|
|
1176
1489
|
}
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
}
|
|
1195
|
-
if (inS) {
|
|
1196
|
-
if (cj === "\\") esc = true;
|
|
1197
|
-
else if (cj === "'") inS = false;
|
|
1198
|
-
continue;
|
|
1199
|
-
}
|
|
1200
|
-
if (inD) {
|
|
1201
|
-
if (cj === "\\") esc = true;
|
|
1202
|
-
else if (cj === '"') inD = false;
|
|
1203
|
-
continue;
|
|
1204
|
-
}
|
|
1205
|
-
if (cj === "'") {
|
|
1206
|
-
inS = true;
|
|
1207
|
-
continue;
|
|
1208
|
-
}
|
|
1209
|
-
if (cj === '"') {
|
|
1210
|
-
inD = true;
|
|
1211
|
-
continue;
|
|
1212
|
-
}
|
|
1213
|
-
if (cj === "[") {
|
|
1214
|
-
lookaheadDepth++;
|
|
1215
|
-
continue;
|
|
1216
|
-
}
|
|
1217
|
-
if (cj === "]") {
|
|
1218
|
-
if (lookaheadDepth === depth) {
|
|
1219
|
-
nextIsClosingWithSpace = j > 0 && s[j - 1] === " ";
|
|
1220
|
-
break;
|
|
1221
|
-
}
|
|
1222
|
-
if (lookaheadDepth > 0) lookaheadDepth--;
|
|
1223
|
-
continue;
|
|
1224
|
-
}
|
|
1225
|
-
if (cj === "," && lookaheadDepth === depth) {
|
|
1226
|
-
break;
|
|
1227
|
-
}
|
|
1228
|
-
}
|
|
1229
|
-
if (!nextIsClosingWithSpace) {
|
|
1230
|
-
throw new Error(
|
|
1231
|
-
'Expected space after ","' + locSuffix(ctx, lineNum, col + i),
|
|
1232
|
-
);
|
|
1233
|
-
}
|
|
1234
|
-
}
|
|
1235
|
-
if (i + 2 < s.length && s[i + 1] === " " && s[i + 2] === " ") {
|
|
1236
|
-
throw new Error(
|
|
1237
|
-
'Unexpected space after ","' + locSuffix(ctx, lineNum, col + i + 2),
|
|
1238
|
-
);
|
|
1239
|
-
}
|
|
1490
|
+
return parseInlineArrayWithFallback(s, ctx, lineNum, col);
|
|
1491
|
+
}
|
|
1492
|
+
if (s.startsWith("{")) {
|
|
1493
|
+
// Inline objects must close on the same line.
|
|
1494
|
+
if (!s.includes("}")) {
|
|
1495
|
+
throw new Error(
|
|
1496
|
+
"Unexpected newline in inline object" + locSuffix(ctx, lineNum, col),
|
|
1497
|
+
);
|
|
1498
|
+
}
|
|
1499
|
+
return parseInlineObjectWithFallback(s, ctx, lineNum, col);
|
|
1500
|
+
}
|
|
1501
|
+
if (isReferenceStart(s)) {
|
|
1502
|
+
const [uri, n] = parseReference(s, ctx, lineNum, col);
|
|
1503
|
+
if (n !== s.length) {
|
|
1504
|
+
throw new Error(
|
|
1505
|
+
`Unexpected character "${s[n]}"` + locSuffix(ctx, lineNum, col + n),
|
|
1506
|
+
);
|
|
1240
1507
|
}
|
|
1508
|
+
return decodeReferenceText(uri, ctx, lineNum, col);
|
|
1241
1509
|
}
|
|
1510
|
+
if (s.startsWith("<")) return parseAngleBytes(s, ctx, lineNum, col);
|
|
1511
|
+
// The keyword with more glued to it, as `undefinedx`: the error is at
|
|
1512
|
+
// the first character past the keyword, as the other ports report it.
|
|
1513
|
+
if (s.startsWith("undefined") && s.length > "undefined".length) {
|
|
1514
|
+
const n = "undefined".length;
|
|
1515
|
+
throw new Error(
|
|
1516
|
+
`Unexpected character "${s[n]}"` + locSuffix(ctx, lineNum, col + n),
|
|
1517
|
+
);
|
|
1518
|
+
}
|
|
1519
|
+
// Bare words are not valid - strings must be quoted
|
|
1520
|
+
const firstChar = s.charAt(0) || "?";
|
|
1521
|
+
throw new Error(
|
|
1522
|
+
`Unexpected character "${firstChar}"` + locSuffix(ctx, lineNum, col),
|
|
1523
|
+
);
|
|
1242
1524
|
}
|
|
1525
|
+
|
|
1526
|
+
/**
|
|
1527
|
+
* @param {string} s
|
|
1528
|
+
* @returns {number|bigint|undefined}
|
|
1529
|
+
*/
|
|
1243
1530
|
/**
|
|
1244
1531
|
* @param {string} s
|
|
1245
1532
|
* @param {ParseContext} ctx
|
|
1246
1533
|
* @param {number} lineNum
|
|
1247
1534
|
* @param {number} col
|
|
1248
|
-
* @returns {
|
|
1535
|
+
* @returns {number|bigint|undefined}
|
|
1249
1536
|
*/
|
|
1250
|
-
function
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
)
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
locSuffix(ctx, lineNum, col + s.length - 2),
|
|
1264
|
-
);
|
|
1537
|
+
function parseNumber(s, ctx = {}, lineNum = 0, col = 0) {
|
|
1538
|
+
// Check for uppercase E in exponent (must be lowercase)
|
|
1539
|
+
// Only check if this looks like a number (starts with digit, minus, or dot)
|
|
1540
|
+
// Reject a non-number on its first character without copying the string.
|
|
1541
|
+
let firstIndex = 0;
|
|
1542
|
+
while (s.charCodeAt(firstIndex) === 0x20) firstIndex++;
|
|
1543
|
+
const firstCode = s.charCodeAt(firstIndex);
|
|
1544
|
+
if (!(
|
|
1545
|
+
(firstCode >= 0x30 && firstCode <= 0x39) ||
|
|
1546
|
+
firstCode === 0x2d ||
|
|
1547
|
+
firstCode === 0x2e
|
|
1548
|
+
)) {
|
|
1549
|
+
return undefined;
|
|
1265
1550
|
}
|
|
1266
|
-
const
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1551
|
+
const firstNonSpace = s[firstIndex];
|
|
1552
|
+
if (
|
|
1553
|
+
(firstNonSpace >= "0" && firstNonSpace <= "9") ||
|
|
1554
|
+
firstNonSpace === "-" ||
|
|
1555
|
+
firstNonSpace === "."
|
|
1556
|
+
) {
|
|
1557
|
+
const eIdx = s.indexOf("E");
|
|
1558
|
+
if (eIdx >= 0) {
|
|
1271
1559
|
throw new Error(
|
|
1272
|
-
"Uppercase
|
|
1273
|
-
locSuffix(ctx, lineNum, col +
|
|
1560
|
+
"Uppercase exponent (use lowercase 'e')" +
|
|
1561
|
+
locSuffix(ctx, lineNum, col + eIdx),
|
|
1274
1562
|
);
|
|
1275
1563
|
}
|
|
1276
1564
|
}
|
|
1277
|
-
const hex = inner.replace(/\s/g, "");
|
|
1278
|
-
if (hex.length % 2 !== 0)
|
|
1279
|
-
throw new Error(
|
|
1280
|
-
"Odd number of hex digits in byte literal" + locSuffix(ctx, lineNum, col),
|
|
1281
|
-
);
|
|
1282
|
-
if (!/^[0-9a-f]*$/.test(hex))
|
|
1283
|
-
throw new Error("Invalid hex digit" + locSuffix(ctx, lineNum, col));
|
|
1284
|
-
return Uint8Array.fromHex
|
|
1285
|
-
? Uint8Array.fromHex(hex)
|
|
1286
|
-
: hexToUint8Array(hex, ctx, lineNum, col);
|
|
1287
|
-
}
|
|
1288
1565
|
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
for (let
|
|
1292
|
-
const
|
|
1293
|
-
|
|
1294
|
-
if (
|
|
1295
|
-
|
|
1566
|
+
let hasDigit = false;
|
|
1567
|
+
let hasExponent = false;
|
|
1568
|
+
for (let i = 0; i < s.length; i++) {
|
|
1569
|
+
const c = s[i];
|
|
1570
|
+
if (c === " ") continue;
|
|
1571
|
+
if (c >= "0" && c <= "9") {
|
|
1572
|
+
hasDigit = true;
|
|
1573
|
+
continue;
|
|
1574
|
+
}
|
|
1575
|
+
if (c === ".") continue;
|
|
1576
|
+
if (c === "-" && i === 0) continue;
|
|
1577
|
+
// Allow 'e' for exponent notation (E already rejected above)
|
|
1578
|
+
if (c === "e" && hasDigit && !hasExponent) {
|
|
1579
|
+
hasExponent = true;
|
|
1580
|
+
continue;
|
|
1296
1581
|
}
|
|
1297
|
-
|
|
1582
|
+
// Allow +/- after exponent
|
|
1583
|
+
if ((c === "+" || c === "-") && hasExponent) {
|
|
1584
|
+
const prev = i > 0 ? s[i - 1] : "";
|
|
1585
|
+
if (prev === "e") continue;
|
|
1586
|
+
}
|
|
1587
|
+
// Not a numeric candidate.
|
|
1588
|
+
return undefined;
|
|
1589
|
+
}
|
|
1590
|
+
if (!hasDigit) return undefined;
|
|
1591
|
+
for (let i = 0; i < s.length; i++) {
|
|
1592
|
+
if (s[i] !== " ") continue;
|
|
1593
|
+
const prev = i > 0 ? s[i - 1] : "";
|
|
1594
|
+
const next = i + 1 < s.length ? s[i + 1] : "";
|
|
1595
|
+
const isDigitPrev = prev >= "0" && prev <= "9";
|
|
1596
|
+
const isDigitNext = next >= "0" && next <= "9";
|
|
1597
|
+
if (!(isDigitPrev && isDigitNext)) {
|
|
1598
|
+
throw new Error(
|
|
1599
|
+
"Unexpected space in number" + locSuffix(ctx, lineNum, col + i),
|
|
1600
|
+
);
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
const compact = s.indexOf(" ") < 0 ? s : s.replace(/ /g, "");
|
|
1604
|
+
if (/^-?\d+$/.test(compact)) return BigInt(compact);
|
|
1605
|
+
// Float patterns: with decimal point, or with exponent, or both
|
|
1606
|
+
if (
|
|
1607
|
+
/^-?\d*\.\d*([eE][+-]?\d+)?$/.test(compact) ||
|
|
1608
|
+
/^-?\d+[eE][+-]?\d+$/.test(compact)
|
|
1609
|
+
) {
|
|
1610
|
+
const n = Number(compact);
|
|
1611
|
+
if (!isNaN(n)) return n;
|
|
1298
1612
|
}
|
|
1299
|
-
return
|
|
1613
|
+
return undefined;
|
|
1300
1614
|
}
|
|
1301
1615
|
|
|
1302
1616
|
/**
|
|
1303
|
-
* @param {
|
|
1304
|
-
* @param {
|
|
1617
|
+
* @param {string} s
|
|
1618
|
+
* @param {ParseContext} ctx
|
|
1619
|
+
* @param {number} lineNum
|
|
1620
|
+
* @param {number} col
|
|
1621
|
+
* @returns {string}
|
|
1622
|
+
*/
|
|
1623
|
+
function parseQuotedString(s, ctx = {}, lineNum = 0, col = 0) {
|
|
1624
|
+
if (s.startsWith('"')) return parseJsonQuotedString(s, ctx, lineNum, col);
|
|
1625
|
+
if (s.startsWith("'")) return parseSingleQuotedString(s, ctx, lineNum, col);
|
|
1626
|
+
return s;
|
|
1627
|
+
}
|
|
1628
|
+
|
|
1629
|
+
/**
|
|
1630
|
+
* The deepest inline arrays and objects may nest, one in another. Deeper
|
|
1631
|
+
* nesting is refused rather than overflowing the stack.
|
|
1632
|
+
*/
|
|
1633
|
+
const MAX_INLINE_DEPTH = 256;
|
|
1634
|
+
|
|
1635
|
+
/**
|
|
1636
|
+
* Read one level of inline nesting with read(), refusing more than
|
|
1637
|
+
* MAX_INLINE_DEPTH levels.
|
|
1638
|
+
* @template T
|
|
1639
|
+
* @param {ParseContext & {depth?: number}} ctx
|
|
1640
|
+
* @param {number} lineNum
|
|
1641
|
+
* @param {number} col
|
|
1642
|
+
* @param {() => T} read
|
|
1643
|
+
* @returns {T}
|
|
1644
|
+
*/
|
|
1645
|
+
function nested(ctx, lineNum, col, read) {
|
|
1646
|
+
const depth = (ctx.depth ?? 0) + 1;
|
|
1647
|
+
if (depth > MAX_INLINE_DEPTH) {
|
|
1648
|
+
throw new Error(
|
|
1649
|
+
`Inline nesting deeper than ${MAX_INLINE_DEPTH} levels` +
|
|
1650
|
+
locSuffix(ctx, lineNum, col),
|
|
1651
|
+
);
|
|
1652
|
+
}
|
|
1653
|
+
ctx.depth = depth;
|
|
1654
|
+
try {
|
|
1655
|
+
return read();
|
|
1656
|
+
} finally {
|
|
1657
|
+
ctx.depth = depth - 1;
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
/**
|
|
1662
|
+
* Read a single-quoted string, the whole of s. Its only escapes are \' and
|
|
1663
|
+
* \\; any other backslash is literal.
|
|
1664
|
+
* @param {string} s
|
|
1665
|
+
* @param {ParseContext} ctx
|
|
1666
|
+
* @param {number} lineNum
|
|
1667
|
+
* @param {number} col
|
|
1668
|
+
* @returns {string}
|
|
1669
|
+
*/
|
|
1670
|
+
function parseSingleQuotedString(s, ctx = {}, lineNum = 0, col = 0) {
|
|
1671
|
+
const parts = [];
|
|
1672
|
+
let run = 1;
|
|
1673
|
+
for (let i = 1; i < s.length; i++) {
|
|
1674
|
+
const c = s[i];
|
|
1675
|
+
if (c === "\\" && (s[i + 1] === "'" || s[i + 1] === "\\")) {
|
|
1676
|
+
if (run < i) parts.push(s.slice(run, i));
|
|
1677
|
+
parts.push(s[i + 1]);
|
|
1678
|
+
run = i + 2;
|
|
1679
|
+
i++;
|
|
1680
|
+
} else if (c === "'") {
|
|
1681
|
+
if (i !== s.length - 1) {
|
|
1682
|
+
throw new Error(
|
|
1683
|
+
'Unexpected character "\'"' + locSuffix(ctx, lineNum, col),
|
|
1684
|
+
);
|
|
1685
|
+
}
|
|
1686
|
+
parts.push(s.slice(run, i));
|
|
1687
|
+
if (parts.length === 1) return detachString(parts[0]);
|
|
1688
|
+
return parts.join("");
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
throw new Error(
|
|
1692
|
+
"Unterminated string" + locSuffix(ctx, lineNum, col + s.length - 1),
|
|
1693
|
+
);
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
/**
|
|
1697
|
+
* Read the escape beginning at s[index]. A quoted string escapes its quote;
|
|
1698
|
+
* a tag escapes its closing parenthesis. Both otherwise use the same JSON
|
|
1699
|
+
* escapes and YAY's braced Unicode escape.
|
|
1700
|
+
* @param {string} s
|
|
1701
|
+
* @param {number} index
|
|
1702
|
+
* @param {string} terminator
|
|
1703
|
+
* @param {ParseContext} ctx
|
|
1704
|
+
* @param {number} lineNum
|
|
1705
|
+
* @param {number} col
|
|
1706
|
+
* @returns {[string, number]}
|
|
1707
|
+
*/
|
|
1708
|
+
function readEscape(s, index, terminator, ctx, lineNum, col) {
|
|
1709
|
+
const escape = s[index + 1];
|
|
1710
|
+
if (escape === terminator) return [terminator, index + 2];
|
|
1711
|
+
if (escape !== undefined && Object.hasOwn(SIMPLE_ESCAPES, escape)) {
|
|
1712
|
+
return [SIMPLE_ESCAPES[escape], index + 2];
|
|
1713
|
+
}
|
|
1714
|
+
if (escape !== "u" || s[index + 2] !== "{") {
|
|
1715
|
+
fail(ctx, "Bad escaped character", lineNum, col + index + 1);
|
|
1716
|
+
}
|
|
1717
|
+
const close = s.indexOf("}", index + 3);
|
|
1718
|
+
const hex = close < 0 ? "" : s.slice(index + 3, close);
|
|
1719
|
+
if (!UNICODE_ESCAPE.test(hex)) {
|
|
1720
|
+
fail(ctx, "Bad Unicode escape", lineNum, col + index + 2);
|
|
1721
|
+
}
|
|
1722
|
+
const code = parseInt(hex, 16);
|
|
1723
|
+
if (code >= 0xd800 && code <= 0xdfff) {
|
|
1724
|
+
fail(ctx, "Illegal surrogate", lineNum, col + index + 2);
|
|
1725
|
+
}
|
|
1726
|
+
if (code > 0x10ffff) {
|
|
1727
|
+
fail(ctx, "Unicode code point out of range", lineNum, col + index + 2);
|
|
1728
|
+
}
|
|
1729
|
+
return [String.fromCodePoint(code), close + 1];
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
/**
|
|
1733
|
+
* Minimal JSON string parser for deterministic errors.
|
|
1734
|
+
* @param {string} s
|
|
1735
|
+
* @param {ParseContext} ctx
|
|
1736
|
+
* @param {number} lineNum
|
|
1737
|
+
* @param {number} col
|
|
1738
|
+
* @returns {string}
|
|
1739
|
+
*/
|
|
1740
|
+
function parseJsonQuotedString(s, ctx = {}, lineNum = 0, col = 0) {
|
|
1741
|
+
if (s.length < 2 || s[0] !== '"') return s;
|
|
1742
|
+
if (s[s.length - 1] !== '"') {
|
|
1743
|
+
const index = Math.max(0, s.length - 1);
|
|
1744
|
+
throw new Error(
|
|
1745
|
+
"Unterminated string" + locSuffix(ctx, lineNum, col + index),
|
|
1746
|
+
);
|
|
1747
|
+
}
|
|
1748
|
+
// Collect runs of plain characters by slice and join them once, rather
|
|
1749
|
+
// than consing one character (or one slice) at a time, which leaves a rope
|
|
1750
|
+
// that retains many times the string's size.
|
|
1751
|
+
const parts = [];
|
|
1752
|
+
let run = 1;
|
|
1753
|
+
for (let i = 1; i < s.length - 1; i++) {
|
|
1754
|
+
const code = s.charCodeAt(i);
|
|
1755
|
+
if (code !== 0x5c) {
|
|
1756
|
+
// Unescaped control characters are illegal in JSON strings.
|
|
1757
|
+
if (code < 0x20) {
|
|
1758
|
+
throw new Error(
|
|
1759
|
+
"Bad character in string" + locSuffix(ctx, lineNum, col + i),
|
|
1760
|
+
);
|
|
1761
|
+
}
|
|
1762
|
+
continue;
|
|
1763
|
+
}
|
|
1764
|
+
if (run < i) parts.push(s.slice(run, i));
|
|
1765
|
+
const [value, next] = readEscape(s, i, '"', ctx, lineNum, col);
|
|
1766
|
+
parts.push(value);
|
|
1767
|
+
i = next - 1;
|
|
1768
|
+
run = i + 1;
|
|
1769
|
+
}
|
|
1770
|
+
if (run === 1) {
|
|
1771
|
+
return detachString(s.slice(1, -1));
|
|
1772
|
+
}
|
|
1773
|
+
parts.push(s.slice(run, s.length - 1));
|
|
1774
|
+
return parts.join("");
|
|
1775
|
+
}
|
|
1776
|
+
|
|
1777
|
+
/**
|
|
1778
|
+
* @param {string} s
|
|
1779
|
+
* @param {ParseContext} ctx
|
|
1780
|
+
* @param {number} lineNum
|
|
1781
|
+
* @param {number} col
|
|
1782
|
+
* @returns {unknown[]}
|
|
1783
|
+
*/
|
|
1784
|
+
function parseInlineArray(s, ctx = {}, lineNum = 0, col = 0) {
|
|
1785
|
+
return parseInlineArrayBody(s, ctx, lineNum, col);
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
function parseInlineArrayBody(s, ctx, lineNum, col) {
|
|
1789
|
+
if (!s.startsWith("[")) return [];
|
|
1790
|
+
if (!s.endsWith("]")) {
|
|
1791
|
+
throw new Error("Unterminated inline array" + locSuffix(ctx, lineNum, col));
|
|
1792
|
+
}
|
|
1793
|
+
if (s === "[]") return [];
|
|
1794
|
+
if (s[1] === " ") {
|
|
1795
|
+
throw new Error(
|
|
1796
|
+
'Unexpected space after "["' + locSuffix(ctx, lineNum, col + 1),
|
|
1797
|
+
);
|
|
1798
|
+
}
|
|
1799
|
+
if (s[s.length - 2] === " ") {
|
|
1800
|
+
throw new Error(
|
|
1801
|
+
'Unexpected space before "]"' +
|
|
1802
|
+
locSuffix(ctx, lineNum, col + s.length - 2),
|
|
1803
|
+
);
|
|
1804
|
+
}
|
|
1805
|
+
const [arr, next] = parseInlineArrayAt(s, 0, ctx, lineNum, col);
|
|
1806
|
+
if (next !== s.length) {
|
|
1807
|
+
throw new Error(
|
|
1808
|
+
`Unexpected character "${s[next]}"` + locSuffix(ctx, lineNum, col + next),
|
|
1809
|
+
);
|
|
1810
|
+
}
|
|
1811
|
+
return arr;
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
/**
|
|
1815
|
+
* Parse an inline array beginning at s[open], in one pass. Returns the array
|
|
1816
|
+
* and the index after its closing bracket.
|
|
1817
|
+
* @param {string} s
|
|
1818
|
+
* @param {number} open
|
|
1819
|
+
* @param {ParseContext} ctx
|
|
1820
|
+
* @param {number} lineNum
|
|
1821
|
+
* @param {number} col
|
|
1822
|
+
* @returns {[unknown[], number]}
|
|
1823
|
+
*/
|
|
1824
|
+
function parseInlineArrayAt(s, open, ctx, lineNum, col) {
|
|
1825
|
+
return nested(ctx, lineNum, col + open, () =>
|
|
1826
|
+
parseInlineArrayAtBody(s, open, ctx, lineNum, col),
|
|
1827
|
+
);
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1830
|
+
function parseInlineArrayAtBody(s, open, ctx, lineNum, col) {
|
|
1831
|
+
const arr = [];
|
|
1832
|
+
let i = open + 1;
|
|
1833
|
+
if (s.charCodeAt(i) === 0x5d) return [arr, i + 1];
|
|
1834
|
+
if (s.charCodeAt(i) === 0x20) {
|
|
1835
|
+
fail(ctx, 'Unexpected space after "["', lineNum, col + i);
|
|
1836
|
+
}
|
|
1837
|
+
for (;;) {
|
|
1838
|
+
if (i >= s.length) {
|
|
1839
|
+
throw new Error(
|
|
1840
|
+
"Unexpected newline in inline array" +
|
|
1841
|
+
locSuffix(ctx, lineNum, col + open),
|
|
1842
|
+
);
|
|
1843
|
+
}
|
|
1844
|
+
const c = s.charCodeAt(i);
|
|
1845
|
+
if (c === 0x5d || c === 0x2c) {
|
|
1846
|
+
throw new Error(
|
|
1847
|
+
"Missing array element" + locSuffix(ctx, lineNum, col + i),
|
|
1848
|
+
);
|
|
1849
|
+
}
|
|
1850
|
+
const [value, next] = parseInlineValueAt(s, i, ctx, lineNum, col, 0x5d);
|
|
1851
|
+
arr.push(value);
|
|
1852
|
+
if (next >= s.length) {
|
|
1853
|
+
throw new Error(
|
|
1854
|
+
"Unexpected newline in inline array" +
|
|
1855
|
+
locSuffix(ctx, lineNum, col + open),
|
|
1856
|
+
);
|
|
1857
|
+
}
|
|
1858
|
+
const after = s.charCodeAt(next);
|
|
1859
|
+
if (after === 0x5d) {
|
|
1860
|
+
if (s.charCodeAt(next - 1) === 0x20) {
|
|
1861
|
+
fail(ctx, 'Unexpected space before "]"', lineNum, col + next - 1);
|
|
1862
|
+
}
|
|
1863
|
+
return [arr, next + 1];
|
|
1864
|
+
}
|
|
1865
|
+
if (after === 0x2c) {
|
|
1866
|
+
if (s.charCodeAt(next - 1) === 0x20) {
|
|
1867
|
+
fail(ctx, 'Unexpected space before ","', lineNum, col + next - 1);
|
|
1868
|
+
}
|
|
1869
|
+
if (next + 1 >= s.length || s.charCodeAt(next + 1) !== 0x20) {
|
|
1870
|
+
fail(ctx, 'Expected space after ","', lineNum, col + next);
|
|
1871
|
+
}
|
|
1872
|
+
if (s.charCodeAt(next + 2) === 0x20) {
|
|
1873
|
+
fail(ctx, 'Unexpected space after ","', lineNum, col + next + 2);
|
|
1874
|
+
}
|
|
1875
|
+
i = next + 2;
|
|
1876
|
+
continue;
|
|
1877
|
+
}
|
|
1878
|
+
if (after === 0x20) {
|
|
1879
|
+
if (s.charCodeAt(next + 1) === 0x5d) {
|
|
1880
|
+
fail(ctx, 'Unexpected space before "]"', lineNum, col + next);
|
|
1881
|
+
}
|
|
1882
|
+
if (s.charCodeAt(next + 1) === 0x2c) {
|
|
1883
|
+
fail(ctx, 'Unexpected space before ","', lineNum, col + next);
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
throw new Error(
|
|
1887
|
+
`Unexpected character "${s[next]}"` + locSuffix(ctx, lineNum, col + next),
|
|
1888
|
+
);
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1892
|
+
/**
|
|
1893
|
+
* @param {string} s
|
|
1894
|
+
* @param {ParseContext} ctx
|
|
1895
|
+
* @param {number} lineNum
|
|
1896
|
+
* @param {number} col
|
|
1897
|
+
* @returns {Uint8Array}
|
|
1898
|
+
*/
|
|
1899
|
+
function parseAngleBytes(s, ctx = {}, lineNum = 0, col = 0) {
|
|
1900
|
+
if (!s.endsWith(">")) {
|
|
1901
|
+
throw new Error("Unmatched angle bracket" + locSuffix(ctx, lineNum, col));
|
|
1902
|
+
}
|
|
1903
|
+
if (s === "<>") return applyBytesHook(new Uint8Array(0), ctx);
|
|
1904
|
+
if (s.length > 2 && s[1] === " ") {
|
|
1905
|
+
throw new Error(
|
|
1906
|
+
'Unexpected space after "<"' + locSuffix(ctx, lineNum, col + 1),
|
|
1907
|
+
);
|
|
1908
|
+
}
|
|
1909
|
+
if (s.length > 2 && s[s.length - 2] === " ") {
|
|
1910
|
+
throw new Error(
|
|
1911
|
+
'Unexpected space before ">"' +
|
|
1912
|
+
locSuffix(ctx, lineNum, col + s.length - 2),
|
|
1913
|
+
);
|
|
1914
|
+
}
|
|
1915
|
+
const inner = s.slice(1, -1);
|
|
1916
|
+
// Check for uppercase hex digits
|
|
1917
|
+
for (let i = 0; i < inner.length; i++) {
|
|
1918
|
+
const c = inner[i];
|
|
1919
|
+
if (c >= "A" && c <= "F") {
|
|
1920
|
+
throw new Error(
|
|
1921
|
+
"Uppercase hex digit (use lowercase)" +
|
|
1922
|
+
locSuffix(ctx, lineNum, col + 1 + i),
|
|
1923
|
+
);
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
return hexToBytes(inner.replace(/\s/g, ""), ctx, lineNum, col);
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
/**
|
|
1930
|
+
* Decode lowercase hex, inline or block, the spaces already removed.
|
|
1931
|
+
* Uppercase digits are refused where they are written, before this.
|
|
1932
|
+
* @param {string} hex
|
|
1933
|
+
* @param {ParseContext} ctx
|
|
1934
|
+
* @param {number} lineNum
|
|
1935
|
+
* @param {number} col
|
|
1936
|
+
* @returns {Uint8Array}
|
|
1937
|
+
*/
|
|
1938
|
+
function hexToBytes(hex, ctx, lineNum, col) {
|
|
1939
|
+
if (hex.length % 2 !== 0)
|
|
1940
|
+
throw new Error(
|
|
1941
|
+
"Odd number of hex digits in byte literal" + locSuffix(ctx, lineNum, col),
|
|
1942
|
+
);
|
|
1943
|
+
if (!LOWER_HEX.test(hex))
|
|
1944
|
+
throw new Error("Invalid hex digit" + locSuffix(ctx, lineNum, col));
|
|
1945
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
1946
|
+
for (let j = 0, k = 0; j < bytes.length; j++, k += 2) {
|
|
1947
|
+
const a = hex.charCodeAt(k);
|
|
1948
|
+
const b = hex.charCodeAt(k + 1);
|
|
1949
|
+
bytes[j] =
|
|
1950
|
+
((a <= 0x39 ? a - 0x30 : a - 0x57) << 4) |
|
|
1951
|
+
(b <= 0x39 ? b - 0x30 : b - 0x57);
|
|
1952
|
+
}
|
|
1953
|
+
return applyBytesHook(bytes, ctx);
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
/**
|
|
1957
|
+
* Make a byte value from the parser's plain Uint8Array, by the caller's
|
|
1958
|
+
* choice. The default is the plain Uint8Array. "immutable" makes the
|
|
1959
|
+
* byteArray that the locked @endo/pass-style accepts; a function is the
|
|
1960
|
+
* caller's own hook.
|
|
1961
|
+
* @param {Uint8Array} bytes
|
|
1962
|
+
* @param {ParseContext} ctx
|
|
1963
|
+
* @returns {unknown}
|
|
1964
|
+
*/
|
|
1965
|
+
function applyBytesHook(bytes, ctx = {}) {
|
|
1966
|
+
const option = ctx.bytes;
|
|
1967
|
+
if (option === undefined || option === "plain") return bytes;
|
|
1968
|
+
if (typeof option === "function") return option(bytes);
|
|
1969
|
+
if (option === "immutable") return immutableBytes(bytes);
|
|
1970
|
+
throw new TypeError('bytes must be "plain", "immutable", or a function');
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
/**
|
|
1974
|
+
* Make a hardened immutable ArrayBuffer holding a copy of bytes. Detect the
|
|
1975
|
+
* Immutable ArrayBuffer proposal at call time, so a shim loaded after this
|
|
1976
|
+
* module, as @endo/init loads @endo/immutable-arraybuffer, serves. Fail
|
|
1977
|
+
* loudly where the environment has neither native support nor the shim.
|
|
1978
|
+
*
|
|
1979
|
+
* The locked @endo/pass-style (1.8.2) accepts a hardened immutable
|
|
1980
|
+
* ArrayBuffer as its byteArray, not a Uint8Array view over one, so this is
|
|
1981
|
+
* the passable form while that remains the locked version.
|
|
1982
|
+
* @param {Uint8Array} bytes
|
|
1983
|
+
* @returns {ArrayBuffer}
|
|
1984
|
+
*/
|
|
1985
|
+
function immutableBytes(bytes) {
|
|
1986
|
+
const copy = bytes.slice().buffer;
|
|
1987
|
+
if (typeof copy.transferToImmutable === "function") {
|
|
1988
|
+
return Object.freeze(copy.transferToImmutable());
|
|
1989
|
+
}
|
|
1990
|
+
if (typeof copy.sliceToImmutable === "function") {
|
|
1991
|
+
return Object.freeze(copy.sliceToImmutable());
|
|
1992
|
+
}
|
|
1993
|
+
throw new Error(
|
|
1994
|
+
"Immutable bytes are unavailable: load @endo/immutable-arraybuffer " +
|
|
1995
|
+
"(or @endo/init) or use the plain bytes",
|
|
1996
|
+
);
|
|
1997
|
+
}
|
|
1998
|
+
|
|
1999
|
+
/**
|
|
2000
|
+
* Read one line of block bytes: hex, then an optional comment after two
|
|
2001
|
+
* spaces, or a comment alone. Returns the hex without its spaces.
|
|
2002
|
+
* @param {string} text
|
|
2003
|
+
* @param {ParseContext} ctx
|
|
2004
|
+
* @param {number} lineNum
|
|
2005
|
+
* @param {number} col
|
|
2006
|
+
* @returns {string}
|
|
2007
|
+
*/
|
|
2008
|
+
function blockHexLine(text, ctx, lineNum, col) {
|
|
2009
|
+
if (text.startsWith("#")) return "";
|
|
2010
|
+
let body = text;
|
|
2011
|
+
const hash = text.indexOf("#");
|
|
2012
|
+
if (hash >= 0) {
|
|
2013
|
+
checkCommentSpacing(text, hash, ctx, lineNum, col);
|
|
2014
|
+
if (text[hash - 1] === " ") body = text.slice(0, hash).trimEnd();
|
|
2015
|
+
}
|
|
2016
|
+
for (let i = 0; i < body.length; i++) {
|
|
2017
|
+
if (body[i] >= "A" && body[i] <= "F") {
|
|
2018
|
+
throw new Error(
|
|
2019
|
+
"Uppercase hex digit (use lowercase)" +
|
|
2020
|
+
locSuffix(ctx, lineNum, col + i),
|
|
2021
|
+
);
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
return body.replace(/ /g, "");
|
|
2025
|
+
}
|
|
2026
|
+
|
|
2027
|
+
/**
|
|
2028
|
+
* @param {Token[]} tokens
|
|
2029
|
+
* @param {number} i
|
|
1305
2030
|
* @param {ParseContext} ctx
|
|
1306
2031
|
* @returns {[unknown[], number]}
|
|
1307
2032
|
*/
|
|
@@ -1315,821 +2040,1786 @@ function parseListArray(tokens, i, ctx = {}, minIndent = -1) {
|
|
|
1315
2040
|
const listIndent = tokens[i].indent ?? 0;
|
|
1316
2041
|
// Stop if we encounter a list item at a lower indent than expected
|
|
1317
2042
|
if (minIndent >= 0 && listIndent < minIndent) break;
|
|
1318
|
-
i
|
|
1319
|
-
while (i < tokens.length && tokens[i].type === "break") i++;
|
|
2043
|
+
i = skipBlank(tokens, i + 1);
|
|
1320
2044
|
if (i >= tokens.length) break;
|
|
1321
|
-
const
|
|
2045
|
+
const [value, j, some] = parseListItem(tokens, i, listIndent, ctx);
|
|
2046
|
+
if (some !== false) arr.push(value);
|
|
2047
|
+
i = j;
|
|
2048
|
+
// Skip stops and breaks between items
|
|
2049
|
+
i = skipBlank(tokens, i, true);
|
|
2050
|
+
}
|
|
2051
|
+
return [arr, i];
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
/**
|
|
2055
|
+
* Parse one list item's content, at tokens[i], after its "-".
|
|
2056
|
+
* @param {Token[]} tokens
|
|
2057
|
+
* @param {number} i
|
|
2058
|
+
* @param {number} listIndent
|
|
2059
|
+
* @param {ParseContext} ctx
|
|
2060
|
+
* @returns {[unknown, number, boolean?]} the value, the next index, and
|
|
2061
|
+
* false when the item holds no value
|
|
2062
|
+
*/
|
|
2063
|
+
function parseListItem(tokens, i, listIndent, ctx) {
|
|
2064
|
+
const next = tokens[i];
|
|
2065
|
+
// A tagged block: the item's content follows the tags
|
|
2066
|
+
if (next.type === "text" && next.text.startsWith("(")) {
|
|
2067
|
+
const lineNum = next.lineNum ?? 0;
|
|
2068
|
+
const col = next.col ?? 0;
|
|
2069
|
+
const [tags, rest] = parsePrefix(next.text, ctx, lineNum, col);
|
|
2070
|
+
if (rest === "") {
|
|
2071
|
+
const j = skipBlank(tokens, i + 1);
|
|
2072
|
+
if (
|
|
2073
|
+
j >= tokens.length ||
|
|
2074
|
+
tokens[j].type === "stop" ||
|
|
2075
|
+
(tokens[j].indent ?? 0) <= listIndent
|
|
2076
|
+
) {
|
|
2077
|
+
return parseValue(tokens, i, ctx);
|
|
2078
|
+
}
|
|
2079
|
+
if (!listBlockFollows(tokens, j, listIndent)) {
|
|
2080
|
+
throw prefixOverInlineValue(ctx, lineNum, col);
|
|
2081
|
+
}
|
|
2082
|
+
const [value, k] = parseListItem(tokens, j, listIndent, ctx);
|
|
2083
|
+
return [wrapPrefix(value, tags), k];
|
|
2084
|
+
}
|
|
2085
|
+
if (!prefixTakes(rest)) throw prefixOverInlineValue(ctx, lineNum, col);
|
|
2086
|
+
// Swap the one token in place and restore it, rather than copying the
|
|
2087
|
+
// whole token array for each tagged item (which is quadratic in the
|
|
2088
|
+
// number of items).
|
|
2089
|
+
const token = tokens[i];
|
|
2090
|
+
tokens[i] = {
|
|
2091
|
+
...next,
|
|
2092
|
+
text: rest,
|
|
2093
|
+
col: col + next.text.length - rest.length,
|
|
2094
|
+
};
|
|
2095
|
+
let value, k;
|
|
2096
|
+
try {
|
|
2097
|
+
[value, k] = parseListItem(tokens, i, listIndent, ctx);
|
|
2098
|
+
} finally {
|
|
2099
|
+
tokens[i] = token;
|
|
2100
|
+
}
|
|
2101
|
+
return [wrapPrefix(value, tags), k];
|
|
2102
|
+
}
|
|
2103
|
+
if (
|
|
2104
|
+
next.type === "text" &&
|
|
2105
|
+
next.text === "" &&
|
|
2106
|
+
i + 1 < tokens.length &&
|
|
2107
|
+
tokens[i + 1].type === "start" &&
|
|
2108
|
+
tokens[i + 1].text === "-"
|
|
2109
|
+
) {
|
|
2110
|
+
return parseListArray(tokens, i + 1, ctx);
|
|
2111
|
+
} else if (next.type === "start" && next.text === "-") {
|
|
2112
|
+
return parseListArray(tokens, i, ctx);
|
|
2113
|
+
} else if (
|
|
2114
|
+
next.type === "text" &&
|
|
2115
|
+
(next.indent ?? 0) >= listIndent &&
|
|
2116
|
+
isInlineBullet(next.text)
|
|
2117
|
+
) {
|
|
2118
|
+
// Inline bullet list inside a multiline list item.
|
|
2119
|
+
const group = [];
|
|
2120
|
+
let j = i;
|
|
2121
|
+
for (;;) {
|
|
2122
|
+
if (
|
|
2123
|
+
j < tokens.length &&
|
|
2124
|
+
tokens[j].type === "text" &&
|
|
2125
|
+
(tokens[j].indent ?? 0) >= listIndent &&
|
|
2126
|
+
isInlineBullet(tokens[j].text)
|
|
2127
|
+
) {
|
|
2128
|
+
const valStr = parseInlineBulletValue(
|
|
2129
|
+
tokens[j].text,
|
|
2130
|
+
ctx,
|
|
2131
|
+
tokens[j].lineNum ?? 0,
|
|
2132
|
+
tokens[j].col ?? 0,
|
|
2133
|
+
);
|
|
2134
|
+
group.push(
|
|
2135
|
+
parseNestedInlineBullet(
|
|
2136
|
+
valStr,
|
|
2137
|
+
ctx,
|
|
2138
|
+
tokens[j].lineNum ?? 0,
|
|
2139
|
+
(tokens[j].col ?? 0) + 2,
|
|
2140
|
+
),
|
|
2141
|
+
);
|
|
2142
|
+
j++;
|
|
2143
|
+
} else if (
|
|
2144
|
+
j < tokens.length &&
|
|
2145
|
+
tokens[j].type === "start" &&
|
|
2146
|
+
tokens[j].text === "-" &&
|
|
2147
|
+
(tokens[j].indent ?? 0) > listIndent &&
|
|
2148
|
+
j + 1 < tokens.length &&
|
|
2149
|
+
tokens[j + 1].type === "text" &&
|
|
2150
|
+
isInlineBullet(tokens[j + 1].text)
|
|
2151
|
+
) {
|
|
2152
|
+
const valStr = parseInlineBulletValue(
|
|
2153
|
+
tokens[j + 1].text,
|
|
2154
|
+
ctx,
|
|
2155
|
+
tokens[j + 1].lineNum ?? 0,
|
|
2156
|
+
tokens[j + 1].col ?? 0,
|
|
2157
|
+
);
|
|
2158
|
+
group.push(
|
|
2159
|
+
parseNestedInlineBullet(
|
|
2160
|
+
valStr,
|
|
2161
|
+
ctx,
|
|
2162
|
+
tokens[j + 1].lineNum ?? 0,
|
|
2163
|
+
(tokens[j + 1].col ?? 0) + 2,
|
|
2164
|
+
),
|
|
2165
|
+
);
|
|
2166
|
+
j += 2;
|
|
2167
|
+
} else {
|
|
2168
|
+
break;
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
// If next token is start(-) at deeper indent, same list continues with nested bullets (e.g. "- - a" then " - b").
|
|
2172
|
+
while (
|
|
2173
|
+
j < tokens.length &&
|
|
2174
|
+
tokens[j].type === "start" &&
|
|
2175
|
+
tokens[j].text === "-" &&
|
|
2176
|
+
(tokens[j].indent ?? 0) > listIndent
|
|
2177
|
+
) {
|
|
2178
|
+
j++;
|
|
2179
|
+
j = skipBlank(tokens, j);
|
|
2180
|
+
if (j >= tokens.length) break;
|
|
2181
|
+
const [subVal, nextJ] = parseValue(tokens, j, ctx);
|
|
2182
|
+
group.push(subVal);
|
|
2183
|
+
j = nextJ;
|
|
2184
|
+
while (j < tokens.length && tokens[j].type === "stop") j++;
|
|
2185
|
+
}
|
|
2186
|
+
return [group, j];
|
|
2187
|
+
} else if (
|
|
2188
|
+
next.type === "text" &&
|
|
2189
|
+
!next.text.startsWith("[") &&
|
|
2190
|
+
!next.text.startsWith("{") &&
|
|
2191
|
+
findKeyColonOutsideQuotes(next.text) >= 0
|
|
2192
|
+
) {
|
|
2193
|
+
const nextIndent = next.indent ?? 0;
|
|
2194
|
+
const inlineIndent = nextIndent === listIndent ? nextIndent : undefined;
|
|
2195
|
+
const baseIndent = inlineIndent !== undefined ? nextIndent + 2 : nextIndent;
|
|
2196
|
+
const [obj, nextIndex] = parseObjectBlock(
|
|
2197
|
+
tokens,
|
|
2198
|
+
i,
|
|
2199
|
+
baseIndent,
|
|
2200
|
+
ctx,
|
|
2201
|
+
inlineIndent,
|
|
2202
|
+
);
|
|
2203
|
+
return [obj, nextIndex];
|
|
2204
|
+
} else if (next.type === "text" || next.type === "start") {
|
|
2205
|
+
const [value, j] = parseValue(tokens, i, ctx);
|
|
2206
|
+
const k = skipBlank(tokens, j);
|
|
2207
|
+
const afterBreak = k < tokens.length ? tokens[k] : null;
|
|
2208
|
+
if (
|
|
2209
|
+
afterBreak &&
|
|
2210
|
+
afterBreak.type === "start" &&
|
|
2211
|
+
afterBreak.text === "-" &&
|
|
2212
|
+
(afterBreak.indent ?? 0) > listIndent
|
|
2213
|
+
) {
|
|
2214
|
+
const group = [value];
|
|
2215
|
+
i = k;
|
|
2216
|
+
while (
|
|
2217
|
+
i < tokens.length &&
|
|
2218
|
+
tokens[i].type === "start" &&
|
|
2219
|
+
tokens[i].text === "-" &&
|
|
2220
|
+
(tokens[i].indent ?? 0) > listIndent
|
|
2221
|
+
) {
|
|
2222
|
+
i = skipBlank(tokens, i + 1);
|
|
2223
|
+
if (i >= tokens.length) break;
|
|
2224
|
+
const [subVal, nextI] = parseValue(tokens, i, ctx);
|
|
2225
|
+
group.push(subVal);
|
|
2226
|
+
i = nextI;
|
|
2227
|
+
while (i < tokens.length && tokens[i].type === "stop") i++;
|
|
2228
|
+
}
|
|
2229
|
+
return [group, i];
|
|
2230
|
+
}
|
|
2231
|
+
return [value, j];
|
|
2232
|
+
}
|
|
2233
|
+
return [undefined, i + 1, false];
|
|
2234
|
+
}
|
|
2235
|
+
|
|
2236
|
+
/**
|
|
2237
|
+
* Parse a block string with an optional base indent constraint.
|
|
2238
|
+
* @param {Token[]} tokens
|
|
2239
|
+
* @param {number} i
|
|
2240
|
+
* @param {string|undefined} firstLine
|
|
2241
|
+
* @param {boolean} inPropertyContext
|
|
2242
|
+
* @param {number} baseIndent - If >= 0, stop collecting when indent <= baseIndent
|
|
2243
|
+
* @param {ParseContext} ctx
|
|
2244
|
+
* @returns {[string, number]}
|
|
2245
|
+
*/
|
|
2246
|
+
function collectBlockString(
|
|
2247
|
+
tokens,
|
|
2248
|
+
i,
|
|
2249
|
+
firstLine,
|
|
2250
|
+
inPropertyContext,
|
|
2251
|
+
baseIndent,
|
|
2252
|
+
ctx,
|
|
2253
|
+
) {
|
|
2254
|
+
const startToken = tokens[i];
|
|
2255
|
+
const lines = [];
|
|
2256
|
+
if (firstLine !== undefined) {
|
|
2257
|
+
lines.push(firstLine);
|
|
2258
|
+
i++;
|
|
2259
|
+
} else {
|
|
2260
|
+
i++;
|
|
2261
|
+
}
|
|
2262
|
+
// Collect continuation lines with their indent so we can strip minimum indent (for property block strings).
|
|
2263
|
+
const continuationLines = [];
|
|
2264
|
+
while (
|
|
2265
|
+
i < tokens.length &&
|
|
2266
|
+
(tokens[i].type === "text" || tokens[i].type === "break")
|
|
2267
|
+
) {
|
|
2268
|
+
if (tokens[i].type === "break") {
|
|
2269
|
+
continuationLines.push({ indent: undefined, text: "" });
|
|
2270
|
+
i++;
|
|
2271
|
+
} else {
|
|
2272
|
+
// If we have a base indent constraint, stop when we see a line at or below that indent
|
|
2273
|
+
if (baseIndent >= 0 && (tokens[i].indent ?? 0) <= baseIndent) {
|
|
2274
|
+
break;
|
|
2275
|
+
}
|
|
2276
|
+
continuationLines.push({
|
|
2277
|
+
indent: tokens[i].indent ?? 0,
|
|
2278
|
+
text: tokens[i].text,
|
|
2279
|
+
});
|
|
2280
|
+
i++;
|
|
2281
|
+
}
|
|
2282
|
+
}
|
|
2283
|
+
const minIndent = continuationLines
|
|
2284
|
+
.filter((x) => x.indent !== undefined)
|
|
2285
|
+
.reduce((min, x) => (x.indent < min ? x.indent : min), Infinity);
|
|
2286
|
+
const effectiveMin = minIndent === Infinity ? 0 : minIndent;
|
|
2287
|
+
for (const { indent, text } of continuationLines) {
|
|
2288
|
+
if (indent === undefined) {
|
|
2289
|
+
lines.push("");
|
|
2290
|
+
} else {
|
|
2291
|
+
// Token text is already after indent; add back (indent - minIndent) spaces for relative indent.
|
|
2292
|
+
const extraSpaces = indent - effectiveMin;
|
|
2293
|
+
lines.push((extraSpaces > 0 ? " ".repeat(extraSpaces) : "") + text);
|
|
2294
|
+
}
|
|
2295
|
+
}
|
|
2296
|
+
// Trim leading and trailing empty lines; then one leading newline and one trailing newline.
|
|
2297
|
+
let start = 0;
|
|
2298
|
+
while (start < lines.length && lines[start] === "") start++;
|
|
2299
|
+
let end = lines.length;
|
|
2300
|
+
while (end > start && lines[end - 1] === "") end--;
|
|
2301
|
+
const trimmed = lines.slice(start, end);
|
|
2302
|
+
// When block starts with quote on its own line (firstLine === ''), output has a leading newline.
|
|
2303
|
+
// But NOT in property context.
|
|
2304
|
+
const leadingNewline =
|
|
2305
|
+
firstLine === "" && trimmed.length > 0 && !inPropertyContext;
|
|
2306
|
+
const body =
|
|
2307
|
+
(leadingNewline ? "\n" : "") +
|
|
2308
|
+
trimmed.join("\n") +
|
|
2309
|
+
(trimmed.length > 0 ? "\n" : "");
|
|
2310
|
+
// Empty block strings are not allowed - use "" for empty string
|
|
2311
|
+
if (body === "") {
|
|
2312
|
+
fail(
|
|
2313
|
+
ctx,
|
|
2314
|
+
'Empty block string not allowed (use "" or "\\n" explicitly)',
|
|
2315
|
+
startToken.lineNum ?? 0,
|
|
2316
|
+
startToken.col ?? 0,
|
|
2317
|
+
false,
|
|
2318
|
+
);
|
|
2319
|
+
}
|
|
2320
|
+
return [body, i];
|
|
2321
|
+
}
|
|
2322
|
+
|
|
2323
|
+
/**
|
|
2324
|
+
* Parse concatenated quoted strings (multiple quoted strings on consecutive lines).
|
|
2325
|
+
* Returns null if there's only one string (single string on new line is invalid).
|
|
2326
|
+
* @param {Token[]} tokens
|
|
2327
|
+
* @param {number} i
|
|
2328
|
+
* @param {number} baseIndent
|
|
2329
|
+
* @param {ParseContext} ctx
|
|
2330
|
+
* @returns {[string, number] | null}
|
|
2331
|
+
*/
|
|
2332
|
+
function parseConcatenatedStrings(tokens, i, baseIndent, ctx = {}) {
|
|
2333
|
+
const parts = [];
|
|
2334
|
+
const startI = i;
|
|
2335
|
+
|
|
2336
|
+
while (i < tokens.length) {
|
|
2337
|
+
const t = tokens[i];
|
|
2338
|
+
|
|
2339
|
+
if (t.type === "break" || t.type === "stop") {
|
|
2340
|
+
i++;
|
|
2341
|
+
continue;
|
|
2342
|
+
}
|
|
2343
|
+
|
|
2344
|
+
if (t.type !== "text" || (t.indent ?? 0) < baseIndent) {
|
|
2345
|
+
break;
|
|
2346
|
+
}
|
|
2347
|
+
|
|
2348
|
+
const trimmed = t.text.trim();
|
|
2349
|
+
|
|
2350
|
+
// Check if this line is a quoted string
|
|
2351
|
+
const isDoubleQuoted =
|
|
2352
|
+
trimmed.startsWith('"') && trimmed.endsWith('"') && trimmed.length >= 2;
|
|
2353
|
+
const isSingleQuoted =
|
|
2354
|
+
trimmed.startsWith("'") && trimmed.endsWith("'") && trimmed.length >= 2;
|
|
2355
|
+
|
|
2356
|
+
if (!isDoubleQuoted && !isSingleQuoted) {
|
|
2357
|
+
break;
|
|
2358
|
+
}
|
|
2359
|
+
|
|
2360
|
+
// Parse the quoted string
|
|
2361
|
+
const parsed = parseQuotedString(trimmed, ctx, t.lineNum ?? 0, t.col ?? 0);
|
|
2362
|
+
parts.push(parsed);
|
|
2363
|
+
i++;
|
|
2364
|
+
}
|
|
2365
|
+
|
|
2366
|
+
// Require at least 2 strings for concatenation
|
|
2367
|
+
// A single string on a new line is invalid (use inline syntax instead)
|
|
2368
|
+
if (parts.length < 2) {
|
|
2369
|
+
return null;
|
|
2370
|
+
}
|
|
2371
|
+
|
|
2372
|
+
return [parts.join(""), i];
|
|
2373
|
+
}
|
|
2374
|
+
|
|
2375
|
+
/**
|
|
2376
|
+
* @param {string} text
|
|
2377
|
+
* @returns {boolean}
|
|
2378
|
+
*/
|
|
2379
|
+
function isInlineBullet(text) {
|
|
2380
|
+
let i = 0;
|
|
2381
|
+
while (i < text.length && text[i] === " ") i++;
|
|
2382
|
+
return (
|
|
2383
|
+
i < text.length &&
|
|
2384
|
+
text[i] === "-" &&
|
|
2385
|
+
i + 1 < text.length &&
|
|
2386
|
+
text[i + 1] === " "
|
|
2387
|
+
);
|
|
2388
|
+
}
|
|
2389
|
+
|
|
2390
|
+
/**
|
|
2391
|
+
* @param {string} text
|
|
2392
|
+
* @param {ParseContext} ctx
|
|
2393
|
+
* @param {number} lineNum
|
|
2394
|
+
* @param {number} col
|
|
2395
|
+
* @returns {string}
|
|
2396
|
+
*/
|
|
2397
|
+
function parseInlineBulletValue(text, ctx = {}, lineNum = 0, col = 0) {
|
|
2398
|
+
let i = 0;
|
|
2399
|
+
while (i < text.length && text[i] === " ") i++;
|
|
2400
|
+
if (i >= text.length || text[i] !== "-") return "";
|
|
2401
|
+
const dashIndex = i;
|
|
2402
|
+
const afterDash = dashIndex + 1;
|
|
2403
|
+
if (afterDash >= text.length || text[afterDash] !== " ") return "";
|
|
2404
|
+
if (afterDash + 1 < text.length && text[afterDash + 1] === " ") {
|
|
2405
|
+
throw new Error(
|
|
2406
|
+
'Unexpected space after "-"' +
|
|
2407
|
+
locSuffix(ctx, lineNum, col + afterDash + 1),
|
|
2408
|
+
);
|
|
2409
|
+
}
|
|
2410
|
+
return text.slice(afterDash + 1);
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
/**
|
|
2414
|
+
* Recursively parse an inline bullet value, handling nested "- " prefixes.
|
|
2415
|
+
* Returns the parsed value (could be a nested array or a scalar).
|
|
2416
|
+
*/
|
|
2417
|
+
function parseNestedInlineBullet(text, ctx = {}, lineNum = 0, col = 0) {
|
|
2418
|
+
// Check if the text itself is another inline bullet
|
|
2419
|
+
if (isInlineBullet(text)) {
|
|
2420
|
+
const innerText = parseInlineBulletValue(text, ctx, lineNum, col);
|
|
2421
|
+
const innerValue = parseNestedInlineBullet(
|
|
2422
|
+
innerText,
|
|
2423
|
+
ctx,
|
|
2424
|
+
lineNum,
|
|
2425
|
+
col + 2,
|
|
2426
|
+
);
|
|
2427
|
+
return [innerValue];
|
|
2428
|
+
}
|
|
2429
|
+
// Otherwise, parse as a scalar
|
|
2430
|
+
return parseScalar(text, ctx, lineNum, col);
|
|
2431
|
+
}
|
|
2432
|
+
|
|
2433
|
+
// Note: parseMultilineBytes for '*' syntax was removed as dead code.
|
|
2434
|
+
// The scanner rejects '*' syntax, so this function was unreachable.
|
|
2435
|
+
|
|
2436
|
+
/**
|
|
2437
|
+
* @param {Token[]} tokens
|
|
2438
|
+
* @param {number} i
|
|
2439
|
+
* @param {ParseContext} ctx
|
|
2440
|
+
* @param {string} firstLineRaw
|
|
2441
|
+
* @param {number} baseIndent
|
|
2442
|
+
* @returns {[Uint8Array, number]}
|
|
2443
|
+
*/
|
|
2444
|
+
function parseBlockBytes(
|
|
2445
|
+
tokens,
|
|
2446
|
+
i,
|
|
2447
|
+
ctx = {},
|
|
2448
|
+
firstLineRaw,
|
|
2449
|
+
baseIndent,
|
|
2450
|
+
firstCol = 0,
|
|
2451
|
+
) {
|
|
2452
|
+
const startToken = tokens[i];
|
|
2453
|
+
const lineNum = startToken.lineNum ?? 0;
|
|
2454
|
+
const col = startToken.col ?? 0;
|
|
2455
|
+
let hex = "";
|
|
2456
|
+
if (firstLineRaw) {
|
|
2457
|
+
hex += blockHexLine(firstLineRaw, ctx, lineNum, firstCol);
|
|
2458
|
+
}
|
|
2459
|
+
i++;
|
|
2460
|
+
while (i < tokens.length) {
|
|
2461
|
+
const t = tokens[i];
|
|
2462
|
+
if (t.type === "break") {
|
|
2463
|
+
i++;
|
|
2464
|
+
continue;
|
|
2465
|
+
}
|
|
2466
|
+
if (t.type === "text" && (t.indent ?? 0) > baseIndent) {
|
|
2467
|
+
hex += blockHexLine(t.text, ctx, t.lineNum ?? 0, t.col ?? 0);
|
|
2468
|
+
i++;
|
|
2469
|
+
continue;
|
|
2470
|
+
}
|
|
2471
|
+
break;
|
|
2472
|
+
}
|
|
2473
|
+
return [hexToBytes(hex, ctx, lineNum, col), i];
|
|
2474
|
+
}
|
|
2475
|
+
|
|
2476
|
+
// Note: parseMultilineAngleBytes was removed as dead code.
|
|
2477
|
+
// The "< hex" syntax (without closing ">") is invalid - inline byte arrays must be closed on the same line.
|
|
2478
|
+
|
|
2479
|
+
/**
|
|
2480
|
+
* Read the value of one object property. The three object readers differ only
|
|
2481
|
+
* in how an empty value chooses the following block, captured by emptyMode.
|
|
2482
|
+
* @param {Token[]} tokens
|
|
2483
|
+
* @param {number} i
|
|
2484
|
+
* @param {{key: string, valuePart: string, valueCol: number}} keyValue
|
|
2485
|
+
* @param {ParseContext} ctx
|
|
2486
|
+
* @param {{baseIndent: number, blockIndent?: number, emptyMode: "root"|"named"|"block"}} options
|
|
2487
|
+
* @returns {[unknown, number]}
|
|
2488
|
+
*/
|
|
2489
|
+
function parsePropertyValue(
|
|
2490
|
+
tokens,
|
|
2491
|
+
i,
|
|
2492
|
+
keyValue,
|
|
2493
|
+
ctx,
|
|
2494
|
+
{ baseIndent, blockIndent = baseIndent, emptyMode },
|
|
2495
|
+
) {
|
|
2496
|
+
const token = tokens[i];
|
|
2497
|
+
const lineNum = token.lineNum ?? 0;
|
|
2498
|
+
let valuePart = keyValue.valuePart;
|
|
2499
|
+
let tags = null;
|
|
2500
|
+
if (valuePart.startsWith("(")) {
|
|
2501
|
+
const symbolText = valuePart;
|
|
2502
|
+
try {
|
|
2503
|
+
[tags, valuePart] = parsePrefix(
|
|
2504
|
+
valuePart,
|
|
2505
|
+
ctx,
|
|
2506
|
+
lineNum,
|
|
2507
|
+
keyValue.valueCol,
|
|
2508
|
+
);
|
|
2509
|
+
} catch {
|
|
2510
|
+
return [
|
|
2511
|
+
parseScalar(symbolText, ctx, lineNum, keyValue.valueCol),
|
|
2512
|
+
i + 1,
|
|
2513
|
+
];
|
|
2514
|
+
}
|
|
2515
|
+
if (valuePart === "" && !propertyBlockFollows(tokens, i, baseIndent)) {
|
|
2516
|
+
const following = tokens[skipBlank(tokens, i + 1, true)];
|
|
2517
|
+
if (
|
|
2518
|
+
following &&
|
|
2519
|
+
(following.indent ?? 0) > baseIndent &&
|
|
2520
|
+
following.type === "text"
|
|
2521
|
+
) {
|
|
2522
|
+
throw prefixOverInlineValue(ctx, lineNum, keyValue.valueCol);
|
|
2523
|
+
}
|
|
2524
|
+
return [
|
|
2525
|
+
parseScalar(symbolText, ctx, lineNum, keyValue.valueCol),
|
|
2526
|
+
i + 1,
|
|
2527
|
+
];
|
|
2528
|
+
}
|
|
2529
|
+
if (!prefixTakes(valuePart)) {
|
|
2530
|
+
throw prefixOverInlineValue(ctx, lineNum, keyValue.valueCol);
|
|
2531
|
+
}
|
|
2532
|
+
}
|
|
2533
|
+
|
|
2534
|
+
let value;
|
|
2535
|
+
let next;
|
|
2536
|
+
if (valuePart.startsWith(">")) {
|
|
2537
|
+
if (!isPropertyBlockLeaderOnly(valuePart, ">")) {
|
|
2538
|
+
fail(
|
|
2539
|
+
ctx,
|
|
2540
|
+
"Expected newline after block leader in property",
|
|
2541
|
+
lineNum,
|
|
2542
|
+
keyValue.valueCol,
|
|
2543
|
+
false,
|
|
2544
|
+
);
|
|
2545
|
+
}
|
|
2546
|
+
[value, next] = parseBlockBytes(tokens, i, ctx, "", baseIndent);
|
|
2547
|
+
} else if (valuePart === "{}") {
|
|
2548
|
+
value = {};
|
|
2549
|
+
next = i + 1;
|
|
2550
|
+
} else if (valuePart.startsWith("`")) {
|
|
2551
|
+
if (valuePart !== "`") {
|
|
2552
|
+
fail(
|
|
2553
|
+
ctx,
|
|
2554
|
+
"Expected newline after block leader in property",
|
|
2555
|
+
lineNum,
|
|
2556
|
+
keyValue.valueCol,
|
|
2557
|
+
false,
|
|
2558
|
+
);
|
|
2559
|
+
}
|
|
2560
|
+
[value, next] = collectBlockString(tokens, i, "", true, blockIndent, ctx);
|
|
2561
|
+
} else if (valuePart === "") {
|
|
2562
|
+
if (emptyMode === "root") {
|
|
2563
|
+
const [valueObject, after] = parseObjectOrNamedArray(
|
|
2564
|
+
tokens,
|
|
2565
|
+
i,
|
|
2566
|
+
keyValue.key,
|
|
2567
|
+
ctx,
|
|
2568
|
+
);
|
|
2569
|
+
value = valueObject[keyValue.key];
|
|
2570
|
+
next = after;
|
|
2571
|
+
} else {
|
|
2572
|
+
next = skipBlank(tokens, i + 1, emptyMode === "block");
|
|
2573
|
+
const following = tokens[next];
|
|
2574
|
+
if (emptyMode === "block") {
|
|
2575
|
+
if (
|
|
2576
|
+
!following ||
|
|
2577
|
+
(following.indent ?? 0) < baseIndent ||
|
|
2578
|
+
((following.indent ?? 0) === baseIndent && following.type !== "start")
|
|
2579
|
+
) {
|
|
2580
|
+
if (tags) {
|
|
2581
|
+
throw prefixWithoutBlock(ctx, lineNum, keyValue.valueCol);
|
|
2582
|
+
}
|
|
2583
|
+
fail(
|
|
2584
|
+
ctx,
|
|
2585
|
+
"Expected value after property",
|
|
2586
|
+
lineNum,
|
|
2587
|
+
(token.col ?? 0) + keyValue.key.length + 1,
|
|
2588
|
+
);
|
|
2589
|
+
}
|
|
2590
|
+
}
|
|
2591
|
+
if (following?.type === "start" && following.text === "-") {
|
|
2592
|
+
[value, next] = parseListArray(
|
|
2593
|
+
tokens,
|
|
2594
|
+
next,
|
|
2595
|
+
ctx,
|
|
2596
|
+
emptyMode === "block" ? baseIndent : -1,
|
|
2597
|
+
);
|
|
2598
|
+
} else {
|
|
2599
|
+
const parentIndent =
|
|
2600
|
+
emptyMode === "block" ? baseIndent : (token.indent ?? 0);
|
|
2601
|
+
if (
|
|
2602
|
+
following?.type === "text" &&
|
|
2603
|
+
(following.indent ?? 0) > parentIndent
|
|
2604
|
+
) {
|
|
2605
|
+
[value, next] = parseObjectBlock(
|
|
2606
|
+
tokens,
|
|
2607
|
+
next,
|
|
2608
|
+
following.indent ?? 0,
|
|
2609
|
+
ctx,
|
|
2610
|
+
);
|
|
2611
|
+
} else if (emptyMode === "named") {
|
|
2612
|
+
fail(
|
|
2613
|
+
ctx,
|
|
2614
|
+
"Expected value after property",
|
|
2615
|
+
lineNum,
|
|
2616
|
+
(token.col ?? 0) + keyValue.key.length + 1,
|
|
2617
|
+
);
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
}
|
|
2621
|
+
} else {
|
|
2622
|
+
value = parseScalar(valuePart, ctx, lineNum, keyValue.valueCol);
|
|
2623
|
+
next = i + 1;
|
|
2624
|
+
}
|
|
2625
|
+
|
|
2626
|
+
if (tags) {
|
|
2627
|
+
if (isMissingBlock(value)) {
|
|
2628
|
+
throw prefixWithoutBlock(ctx, lineNum, keyValue.valueCol);
|
|
2629
|
+
}
|
|
2630
|
+
value = wrapPrefix(value, tags);
|
|
2631
|
+
}
|
|
2632
|
+
return [value, next];
|
|
2633
|
+
}
|
|
2634
|
+
|
|
2635
|
+
/**
|
|
2636
|
+
* @param {Token[]} tokens
|
|
2637
|
+
* @param {number} i
|
|
2638
|
+
* @param {string} key
|
|
2639
|
+
* @param {ParseContext} ctx
|
|
2640
|
+
* @returns {[Record<string, unknown>, number]}
|
|
2641
|
+
*/
|
|
2642
|
+
function parseObjectOrNamedArray(tokens, i, key, ctx = {}) {
|
|
2643
|
+
const keyToken = tokens[i];
|
|
2644
|
+
const keyCol = keyToken.col ?? keyToken.indent ?? 0;
|
|
2645
|
+
i = skipBlank(tokens, i + 1, true);
|
|
2646
|
+
const baseIndent = i < tokens.length ? (tokens[i].indent ?? 0) : 0;
|
|
2647
|
+
const first = i < tokens.length ? tokens[i] : null;
|
|
2648
|
+
// A block array may stand at the key's column. Every other block value
|
|
2649
|
+
// must be indented deeper, so a sibling item or property is no value.
|
|
2650
|
+
if (
|
|
2651
|
+
!first ||
|
|
2652
|
+
(first.indent ?? 0) < keyCol ||
|
|
2653
|
+
((first.indent ?? 0) === keyCol && first.type !== "start")
|
|
2654
|
+
) {
|
|
2655
|
+
const col = (keyToken.col ?? 0) + key.length + 1;
|
|
2656
|
+
throw new Error(
|
|
2657
|
+
"Expected value after property" +
|
|
2658
|
+
locSuffix(ctx, keyToken.lineNum ?? 0, col),
|
|
2659
|
+
);
|
|
2660
|
+
}
|
|
2661
|
+
if (first && first.type === "start" && first.text === "-") {
|
|
2662
|
+
const [arr, next] = parseListArray(tokens, i, ctx);
|
|
2663
|
+
return [{ [key]: arr }, next];
|
|
2664
|
+
}
|
|
2665
|
+
// Note: '*' syntax for multiline bytes is rejected by the scanner
|
|
2666
|
+
// Note: "key: <" without closing ">" is invalid - inline byte arrays must be closed on the same line
|
|
2667
|
+
// Reject a block string leader or a lone quote on a separate line
|
|
2668
|
+
if (
|
|
2669
|
+
first &&
|
|
2670
|
+
first.type === "text" &&
|
|
2671
|
+
(first.text.trim() === "`" || first.text === '"')
|
|
2672
|
+
) {
|
|
2673
|
+
throw new Error(
|
|
2674
|
+
"Unexpected indent" + locSuffix(ctx, first.lineNum ?? 0, 0),
|
|
2675
|
+
);
|
|
2676
|
+
}
|
|
2677
|
+
// Concatenated quoted strings (multiple quoted strings on consecutive lines).
|
|
2678
|
+
// A quoted line with a colon outside its quotes is a property with a quoted
|
|
2679
|
+
// key, which opens a nested object.
|
|
2680
|
+
if (first && first.type === "text") {
|
|
2681
|
+
const trimmed = first.text.trim();
|
|
1322
2682
|
if (
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
arr.push(value);
|
|
1331
|
-
i = j;
|
|
1332
|
-
} else if (next.type === "start" && next.text === "-") {
|
|
1333
|
-
const [value, j] = parseListArray(tokens, i, ctx);
|
|
1334
|
-
arr.push(value);
|
|
1335
|
-
i = j;
|
|
1336
|
-
} else if (
|
|
1337
|
-
next.type === "text" &&
|
|
1338
|
-
(next.indent ?? 0) >= listIndent &&
|
|
1339
|
-
isInlineBullet(next.text)
|
|
2683
|
+
((trimmed.startsWith('"') &&
|
|
2684
|
+
trimmed.endsWith('"') &&
|
|
2685
|
+
trimmed.length >= 2) ||
|
|
2686
|
+
(trimmed.startsWith("'") &&
|
|
2687
|
+
trimmed.endsWith("'") &&
|
|
2688
|
+
trimmed.length >= 2)) &&
|
|
2689
|
+
!splitKeyValue(first.text, first.col ?? 0, ctx, first.lineNum ?? 0)
|
|
1340
2690
|
) {
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
if (
|
|
1346
|
-
j < tokens.length &&
|
|
1347
|
-
tokens[j].type === "text" &&
|
|
1348
|
-
(tokens[j].indent ?? 0) >= listIndent &&
|
|
1349
|
-
isInlineBullet(tokens[j].text)
|
|
1350
|
-
) {
|
|
1351
|
-
const valStr = parseInlineBulletValue(
|
|
1352
|
-
tokens[j].text,
|
|
1353
|
-
ctx,
|
|
1354
|
-
tokens[j].lineNum ?? 0,
|
|
1355
|
-
tokens[j].col ?? 0,
|
|
1356
|
-
);
|
|
1357
|
-
group.push(
|
|
1358
|
-
parseNestedInlineBullet(
|
|
1359
|
-
valStr,
|
|
1360
|
-
ctx,
|
|
1361
|
-
tokens[j].lineNum ?? 0,
|
|
1362
|
-
(tokens[j].col ?? 0) + 2,
|
|
1363
|
-
),
|
|
1364
|
-
);
|
|
1365
|
-
j++;
|
|
1366
|
-
} else if (
|
|
1367
|
-
j < tokens.length &&
|
|
1368
|
-
tokens[j].type === "start" &&
|
|
1369
|
-
tokens[j].text === "-" &&
|
|
1370
|
-
(tokens[j].indent ?? 0) > listIndent &&
|
|
1371
|
-
j + 1 < tokens.length &&
|
|
1372
|
-
tokens[j + 1].type === "text" &&
|
|
1373
|
-
isInlineBullet(tokens[j + 1].text)
|
|
1374
|
-
) {
|
|
1375
|
-
const valStr = parseInlineBulletValue(
|
|
1376
|
-
tokens[j + 1].text,
|
|
1377
|
-
ctx,
|
|
1378
|
-
tokens[j + 1].lineNum ?? 0,
|
|
1379
|
-
tokens[j + 1].col ?? 0,
|
|
1380
|
-
);
|
|
1381
|
-
group.push(
|
|
1382
|
-
parseNestedInlineBullet(
|
|
1383
|
-
valStr,
|
|
1384
|
-
ctx,
|
|
1385
|
-
tokens[j + 1].lineNum ?? 0,
|
|
1386
|
-
(tokens[j + 1].col ?? 0) + 2,
|
|
1387
|
-
),
|
|
1388
|
-
);
|
|
1389
|
-
j += 2;
|
|
1390
|
-
} else {
|
|
1391
|
-
break;
|
|
1392
|
-
}
|
|
1393
|
-
}
|
|
1394
|
-
// If next token is start(-) at deeper indent, same list continues with nested bullets (e.g. "- - a" then " - b").
|
|
1395
|
-
while (
|
|
1396
|
-
j < tokens.length &&
|
|
1397
|
-
tokens[j].type === "start" &&
|
|
1398
|
-
tokens[j].text === "-" &&
|
|
1399
|
-
(tokens[j].indent ?? 0) > listIndent
|
|
1400
|
-
) {
|
|
1401
|
-
j++;
|
|
1402
|
-
while (j < tokens.length && tokens[j].type === "break") j++;
|
|
1403
|
-
if (j >= tokens.length) break;
|
|
1404
|
-
const [subVal, nextJ] = parseValue(tokens, j, ctx);
|
|
1405
|
-
group.push(subVal);
|
|
1406
|
-
j = nextJ;
|
|
1407
|
-
while (j < tokens.length && tokens[j].type === "stop") j++;
|
|
2691
|
+
const result = parseConcatenatedStrings(tokens, i, baseIndent, ctx);
|
|
2692
|
+
if (result !== null) {
|
|
2693
|
+
const [concatStr, next] = result;
|
|
2694
|
+
return [{ [key]: concatStr }, next];
|
|
1408
2695
|
}
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
next.type === "text" &&
|
|
1413
|
-
findKeyColonOutsideQuotes(next.text) >= 0
|
|
1414
|
-
) {
|
|
1415
|
-
const nextIndent = next.indent ?? 0;
|
|
1416
|
-
const inlineIndent = nextIndent === listIndent ? nextIndent : undefined;
|
|
1417
|
-
const baseIndent =
|
|
1418
|
-
inlineIndent !== undefined ? nextIndent + 2 : nextIndent;
|
|
1419
|
-
const [obj, nextIndex] = parseObjectBlock(
|
|
1420
|
-
tokens,
|
|
1421
|
-
i,
|
|
1422
|
-
baseIndent,
|
|
1423
|
-
ctx,
|
|
1424
|
-
inlineIndent,
|
|
2696
|
+
// Single string on new line is invalid - fall through to error
|
|
2697
|
+
throw new Error(
|
|
2698
|
+
"Unexpected indent" + locSuffix(ctx, first.lineNum ?? 0, 0),
|
|
1425
2699
|
);
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
2700
|
+
}
|
|
2701
|
+
}
|
|
2702
|
+
const obj = {};
|
|
2703
|
+
/** @type {Map<string, [number, number]>} */
|
|
2704
|
+
const seen = new Map();
|
|
2705
|
+
while (i < tokens.length) {
|
|
2706
|
+
const t = tokens[i];
|
|
2707
|
+
if (t.type === "stop") {
|
|
2708
|
+
i++;
|
|
2709
|
+
continue;
|
|
2710
|
+
}
|
|
2711
|
+
if (t.type === "text") {
|
|
2712
|
+
const s = t.text;
|
|
2713
|
+
// Reject inline values on separate line (they look like keys starting with special chars)
|
|
2714
|
+
if (s.startsWith("{") || s.startsWith("[") || s.startsWith("<")) {
|
|
2715
|
+
throw new Error(
|
|
2716
|
+
"Unexpected indent" + locSuffix(ctx, t.lineNum ?? 0, 0),
|
|
2717
|
+
);
|
|
2718
|
+
}
|
|
2719
|
+
const keyValue = splitKeyValue(s, t.col ?? 0, ctx, t.lineNum ?? 0);
|
|
2720
|
+
if (keyValue) {
|
|
2721
|
+
const k = keyValue.key;
|
|
2722
|
+
if ((t.indent ?? 0) < baseIndent) break;
|
|
2723
|
+
if ((t.indent ?? 0) <= baseIndent) {
|
|
2724
|
+
claimKey(seen, k, ctx, t.lineNum ?? 0, t.col ?? 0);
|
|
2725
|
+
}
|
|
2726
|
+
if (k && (t.indent ?? 0) <= baseIndent) {
|
|
2727
|
+
const [value, next] = parsePropertyValue(tokens, i, keyValue, ctx, {
|
|
2728
|
+
baseIndent: t.indent ?? 0,
|
|
2729
|
+
emptyMode: "named",
|
|
2730
|
+
});
|
|
2731
|
+
setOwn(obj, k, value);
|
|
2732
|
+
i = next;
|
|
2733
|
+
} else {
|
|
1447
2734
|
i++;
|
|
1448
|
-
while (i < tokens.length && tokens[i].type === "break") i++;
|
|
1449
|
-
if (i >= tokens.length) break;
|
|
1450
|
-
const [subVal, nextI] = parseValue(tokens, i, ctx);
|
|
1451
|
-
group.push(subVal);
|
|
1452
|
-
i = nextI;
|
|
1453
|
-
while (i < tokens.length && tokens[i].type === "stop") i++;
|
|
1454
2735
|
}
|
|
1455
|
-
arr.push(group);
|
|
1456
2736
|
} else {
|
|
1457
|
-
|
|
1458
|
-
|
|
2737
|
+
// Text without colon in nested object context is invalid
|
|
2738
|
+
// (e.g., inline array/object/bytes/string on separate line)
|
|
2739
|
+
throw new Error(
|
|
2740
|
+
"Unexpected indent" + locSuffix(ctx, t.lineNum ?? 0, 0),
|
|
2741
|
+
);
|
|
1459
2742
|
}
|
|
1460
2743
|
} else {
|
|
1461
2744
|
i++;
|
|
1462
2745
|
}
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
2746
|
+
}
|
|
2747
|
+
return [{ [key]: Object.keys(obj).length ? obj : undefined }, i];
|
|
2748
|
+
}
|
|
2749
|
+
|
|
2750
|
+
/**
|
|
2751
|
+
* @param {Token[]} tokens
|
|
2752
|
+
* @param {number} i
|
|
2753
|
+
* @param {number} baseIndent
|
|
2754
|
+
* @param {ParseContext} ctx
|
|
2755
|
+
* @param {number=} inlineIndent
|
|
2756
|
+
* @returns {[Record<string, unknown>, number]}
|
|
2757
|
+
*/
|
|
2758
|
+
function parseObjectBlock(tokens, i, baseIndent, ctx = {}, inlineIndent) {
|
|
2759
|
+
const obj = {};
|
|
2760
|
+
/** @type {Map<string, [number, number]>} */
|
|
2761
|
+
const seen = new Map();
|
|
2762
|
+
let firstText = true;
|
|
2763
|
+
while (i < tokens.length) {
|
|
2764
|
+
const t = tokens[i];
|
|
2765
|
+
if (inlineIndent !== undefined) {
|
|
2766
|
+
if (t.type === "stop") break;
|
|
2767
|
+
if (t.type === "start" && (t.indent ?? 0) <= inlineIndent) break;
|
|
2768
|
+
}
|
|
2769
|
+
if (t.type === "stop") {
|
|
2770
|
+
i++;
|
|
2771
|
+
continue;
|
|
2772
|
+
}
|
|
2773
|
+
// A list leader outdented past this object begins the next item of an
|
|
2774
|
+
// enclosing list.
|
|
2775
|
+
if (t.type === "start" && (t.indent ?? 0) < baseIndent) break;
|
|
2776
|
+
if (t.type !== "text") {
|
|
2777
|
+
i++;
|
|
2778
|
+
continue;
|
|
2779
|
+
}
|
|
2780
|
+
let indent = t.indent ?? 0;
|
|
2781
|
+
if (firstText && inlineIndent !== undefined && indent === inlineIndent) {
|
|
2782
|
+
indent = baseIndent;
|
|
2783
|
+
}
|
|
2784
|
+
firstText = false;
|
|
2785
|
+
if (indent < baseIndent) break;
|
|
2786
|
+
if (indent > baseIndent) {
|
|
2787
|
+
i++;
|
|
2788
|
+
continue;
|
|
2789
|
+
}
|
|
2790
|
+
const keyValue = splitKeyValue(t.text, t.col ?? 0, ctx, t.lineNum ?? 0);
|
|
2791
|
+
if (!keyValue) {
|
|
1468
2792
|
i++;
|
|
2793
|
+
continue;
|
|
2794
|
+
}
|
|
2795
|
+
const k = keyValue.key;
|
|
2796
|
+
claimKey(seen, k, ctx, t.lineNum ?? 0, t.col ?? 0);
|
|
2797
|
+
const [value, next] = parsePropertyValue(tokens, i, keyValue, ctx, {
|
|
2798
|
+
baseIndent,
|
|
2799
|
+
blockIndent: indent,
|
|
2800
|
+
emptyMode: "block",
|
|
2801
|
+
});
|
|
2802
|
+
setOwn(obj, k, value);
|
|
2803
|
+
i = next;
|
|
1469
2804
|
}
|
|
1470
|
-
return [
|
|
2805
|
+
return [obj, i];
|
|
1471
2806
|
}
|
|
1472
2807
|
|
|
2808
|
+
// Root as object (multiple key: value lines at indent 0)
|
|
1473
2809
|
/**
|
|
1474
|
-
* Parse a block string with an optional base indent constraint.
|
|
1475
2810
|
* @param {Token[]} tokens
|
|
1476
2811
|
* @param {number} i
|
|
1477
|
-
* @param {
|
|
1478
|
-
* @
|
|
1479
|
-
* @param {number} baseIndent - If >= 0, stop collecting when indent <= baseIndent
|
|
1480
|
-
* @returns {[string, number]}
|
|
2812
|
+
* @param {ParseContext} ctx
|
|
2813
|
+
* @returns {[Record<string, unknown>, number]}
|
|
1481
2814
|
*/
|
|
1482
|
-
function
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
2815
|
+
function parseRootObject(tokens, i, ctx = {}) {
|
|
2816
|
+
const obj = {};
|
|
2817
|
+
/** @type {Map<string, [number, number]>} */
|
|
2818
|
+
const seen = new Map();
|
|
2819
|
+
const baseIndent = 0;
|
|
2820
|
+
while (i < tokens.length) {
|
|
2821
|
+
const t = tokens[i];
|
|
2822
|
+
if (t.type === "stop") {
|
|
2823
|
+
i++;
|
|
2824
|
+
continue;
|
|
2825
|
+
}
|
|
2826
|
+
if (t.type === "text") {
|
|
2827
|
+
const s = t.text;
|
|
2828
|
+
const keyValue = splitKeyValue(s, t.col ?? 0, ctx, t.lineNum ?? 0);
|
|
2829
|
+
if (keyValue && (t.indent ?? 0) === baseIndent) {
|
|
2830
|
+
const k = keyValue.key;
|
|
2831
|
+
claimKey(seen, k, ctx, t.lineNum ?? 0, t.col ?? 0);
|
|
2832
|
+
const [value, next] = parsePropertyValue(tokens, i, keyValue, ctx, {
|
|
2833
|
+
baseIndent,
|
|
2834
|
+
emptyMode: "root",
|
|
2835
|
+
});
|
|
2836
|
+
setOwn(obj, k, value);
|
|
2837
|
+
i = next;
|
|
2838
|
+
} else if ((t.indent ?? 0) > baseIndent) {
|
|
2839
|
+
// A line indented under a property that took no block, placed at
|
|
2840
|
+
// the line's start as every other unexpected indent is
|
|
2841
|
+
throw new Error(
|
|
2842
|
+
"Unexpected indent" + locSuffix(ctx, t.lineNum ?? 0, 0),
|
|
2843
|
+
);
|
|
2844
|
+
} else {
|
|
2845
|
+
i++;
|
|
2846
|
+
}
|
|
2847
|
+
} else {
|
|
2848
|
+
i++;
|
|
2849
|
+
}
|
|
1495
2850
|
}
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
2851
|
+
return [obj, i];
|
|
2852
|
+
}
|
|
2853
|
+
|
|
2854
|
+
// --- Well-formed strings ---
|
|
2855
|
+
|
|
2856
|
+
/**
|
|
2857
|
+
* Whether a value is a string of Unicode scalar values: no unpaired UTF-16
|
|
2858
|
+
* surrogate. The shape of Endo's `isWellFormedString` (@endo/pass-style
|
|
2859
|
+
* `string.js`): the `typeof` guard first, since `isWellFormed` applies
|
|
2860
|
+
* ToString to its receiver and would pass a non-string that coerces to a
|
|
2861
|
+
* well-formed string; without `String.prototype.isWellFormed`, the string
|
|
2862
|
+
* iterator, which yields a code point in U+D800..U+DFFF only for an unpaired
|
|
2863
|
+
* surrogate.
|
|
2864
|
+
* @param {unknown} str
|
|
2865
|
+
* @returns {str is string}
|
|
2866
|
+
*/
|
|
2867
|
+
function isWellFormedString(str) {
|
|
2868
|
+
if (typeof str !== "string") return false;
|
|
2869
|
+
if (typeof String.prototype.isWellFormed === "function") {
|
|
2870
|
+
return str.isWellFormed();
|
|
2871
|
+
}
|
|
2872
|
+
for (const ch of str) {
|
|
2873
|
+
const cp = /** @type {number} */ (ch.codePointAt(0));
|
|
2874
|
+
if (cp >= 0xd800 && cp <= 0xdfff) return false;
|
|
2875
|
+
}
|
|
2876
|
+
return true;
|
|
2877
|
+
}
|
|
2878
|
+
|
|
2879
|
+
/**
|
|
2880
|
+
* Refuse a string that is not well formed, with the readers' message.
|
|
2881
|
+
* @param {string} str
|
|
2882
|
+
* @param {string} what - what the string is, for the message
|
|
2883
|
+
*/
|
|
2884
|
+
function checkWellFormed(str, what) {
|
|
2885
|
+
if (!isWellFormedString(str)) {
|
|
2886
|
+
throw new TypeError(`Illegal surrogate in ${what}: ${JSON.stringify(str)}`);
|
|
2887
|
+
}
|
|
2888
|
+
}
|
|
2889
|
+
|
|
2890
|
+
/**
|
|
2891
|
+
* Refuse a value that carries a string that is not well formed anywhere in
|
|
2892
|
+
* it: a string, a record key, or a name held in a non-enumerable property,
|
|
2893
|
+
* as an Endo tagged value's tag and a symbol record's name are held in
|
|
2894
|
+
* `Symbol.toStringTag`. Every own property is walked, by descriptor, so no
|
|
2895
|
+
* getter runs; bytes are not walked. A value seen before is not walked
|
|
2896
|
+
* again, so a cycle ends.
|
|
2897
|
+
* @param {unknown} value
|
|
2898
|
+
* @param {string} path
|
|
2899
|
+
* @param {WeakSet<object>} seen
|
|
2900
|
+
*/
|
|
2901
|
+
function checkWellFormedDeep(value, path = "$", seen = new WeakSet()) {
|
|
2902
|
+
if (typeof value === "string") {
|
|
2903
|
+
checkWellFormed(value, `the string at ${path}`);
|
|
2904
|
+
return;
|
|
2905
|
+
}
|
|
2906
|
+
if (value === null || typeof value !== "object") return;
|
|
2907
|
+
if (seen.has(value)) return;
|
|
2908
|
+
seen.add(value);
|
|
2909
|
+
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) return;
|
|
2910
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
2911
|
+
const descriptor = /** @type {PropertyDescriptor} */ (
|
|
2912
|
+
Object.getOwnPropertyDescriptor(value, key)
|
|
2913
|
+
);
|
|
2914
|
+
if (typeof key === "string") {
|
|
2915
|
+
checkWellFormed(key, `a key at ${path}`);
|
|
2916
|
+
}
|
|
2917
|
+
if (!("value" in descriptor)) continue;
|
|
2918
|
+
if (key === Symbol.toStringTag) {
|
|
2919
|
+
if (typeof descriptor.value === "string") {
|
|
2920
|
+
checkWellFormed(descriptor.value, `the name or tag at ${path}`);
|
|
2921
|
+
}
|
|
2922
|
+
continue;
|
|
2923
|
+
}
|
|
2924
|
+
if (typeof key === "symbol") continue;
|
|
2925
|
+
if (Array.isArray(value) && key === "length") continue;
|
|
2926
|
+
checkWellFormedDeep(
|
|
2927
|
+
descriptor.value,
|
|
2928
|
+
Array.isArray(value)
|
|
2929
|
+
? `${path}[${key}]`
|
|
2930
|
+
: `${path}[${JSON.stringify(key)}]`,
|
|
2931
|
+
seen,
|
|
2932
|
+
);
|
|
2933
|
+
}
|
|
2934
|
+
}
|
|
2935
|
+
|
|
2936
|
+
// --- Tagged values ---
|
|
2937
|
+
|
|
2938
|
+
const PASS_STYLE = Symbol.for("passStyle");
|
|
2939
|
+
|
|
2940
|
+
/**
|
|
2941
|
+
* A symbol record: a name distinguished from a string, in the shape Endo's
|
|
2942
|
+
* reified symbols use. The record is shallowly frozen and has no payload.
|
|
2943
|
+
* @param {string} name
|
|
2944
|
+
* @returns {object}
|
|
2945
|
+
*/
|
|
2946
|
+
function symbol(name) {
|
|
2947
|
+
if (typeof name !== "string") {
|
|
2948
|
+
throw new TypeError("A symbol's name must be a string");
|
|
2949
|
+
}
|
|
2950
|
+
checkWellFormed(name, "a symbol's name");
|
|
2951
|
+
return Object.freeze(
|
|
2952
|
+
Object.create(Object.prototype, {
|
|
2953
|
+
[PASS_STYLE]: { value: "symbol" },
|
|
2954
|
+
[Symbol.toStringTag]: { value: name },
|
|
2955
|
+
}),
|
|
2956
|
+
);
|
|
2957
|
+
}
|
|
2958
|
+
|
|
2959
|
+
/**
|
|
2960
|
+
* @param {unknown} value
|
|
2961
|
+
* @returns {boolean}
|
|
2962
|
+
*/
|
|
2963
|
+
function isSymbol(value) {
|
|
2964
|
+
if (
|
|
2965
|
+
value === null ||
|
|
2966
|
+
typeof value !== "object" ||
|
|
2967
|
+
Object.getPrototypeOf(value) !== Object.prototype ||
|
|
2968
|
+
!Object.isFrozen(value)
|
|
1501
2969
|
) {
|
|
1502
|
-
|
|
1503
|
-
|
|
2970
|
+
return false;
|
|
2971
|
+
}
|
|
2972
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
2973
|
+
const passStyle = descriptors[PASS_STYLE];
|
|
2974
|
+
const name = descriptors[Symbol.toStringTag];
|
|
2975
|
+
return (
|
|
2976
|
+
Reflect.ownKeys(descriptors).length === 2 &&
|
|
2977
|
+
passStyle?.value === "symbol" &&
|
|
2978
|
+
passStyle.enumerable === false &&
|
|
2979
|
+
passStyle.writable === false &&
|
|
2980
|
+
passStyle.configurable === false &&
|
|
2981
|
+
typeof name?.value === "string" &&
|
|
2982
|
+
name.enumerable === false &&
|
|
2983
|
+
name.writable === false &&
|
|
2984
|
+
name.configurable === false
|
|
2985
|
+
);
|
|
2986
|
+
}
|
|
2987
|
+
|
|
2988
|
+
/**
|
|
2989
|
+
* A tagged value: a tag, any string, and one payload, any YAY value.
|
|
2990
|
+
* The outer record is frozen, ready for a hardened environment to harden its
|
|
2991
|
+
* payload and accept it as Endo pass data.
|
|
2992
|
+
* @param {string} tag
|
|
2993
|
+
* @param {unknown} payload
|
|
2994
|
+
* @returns {object}
|
|
2995
|
+
*/
|
|
2996
|
+
function tagged(tag, payload) {
|
|
2997
|
+
if (typeof tag !== "string") {
|
|
2998
|
+
throw new TypeError("A tag must be a string");
|
|
2999
|
+
}
|
|
3000
|
+
checkWellFormed(tag, "a tag");
|
|
3001
|
+
checkWellFormedDeep(payload, "payload");
|
|
3002
|
+
return taggedRecord(tag, payload);
|
|
3003
|
+
}
|
|
3004
|
+
|
|
3005
|
+
/**
|
|
3006
|
+
* The tagged record, unchecked: for the parser, whose tags and strings the
|
|
3007
|
+
* scanner has already checked, so that a deeply tagged document is not
|
|
3008
|
+
* walked once per tag.
|
|
3009
|
+
* @param {string} tag
|
|
3010
|
+
* @param {unknown} payload
|
|
3011
|
+
* @returns {object}
|
|
3012
|
+
*/
|
|
3013
|
+
function taggedRecord(tag, payload) {
|
|
3014
|
+
return Object.freeze(
|
|
3015
|
+
Object.create(Object.prototype, {
|
|
3016
|
+
[PASS_STYLE]: { value: "tagged" },
|
|
3017
|
+
[Symbol.toStringTag]: { value: tag },
|
|
3018
|
+
payload: { value: payload, enumerable: true },
|
|
3019
|
+
}),
|
|
3020
|
+
);
|
|
3021
|
+
}
|
|
3022
|
+
|
|
3023
|
+
/**
|
|
3024
|
+
* @param {unknown} value
|
|
3025
|
+
* @returns {boolean}
|
|
3026
|
+
*/
|
|
3027
|
+
function isTagged(value) {
|
|
3028
|
+
if (
|
|
3029
|
+
value === null ||
|
|
3030
|
+
typeof value !== "object" ||
|
|
3031
|
+
Object.getPrototypeOf(value) !== Object.prototype ||
|
|
3032
|
+
!Object.isFrozen(value)
|
|
3033
|
+
) {
|
|
3034
|
+
return false;
|
|
3035
|
+
}
|
|
3036
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
3037
|
+
const passStyle = descriptors[PASS_STYLE];
|
|
3038
|
+
const tag = descriptors[Symbol.toStringTag];
|
|
3039
|
+
const payload = descriptors.payload;
|
|
3040
|
+
return (
|
|
3041
|
+
Reflect.ownKeys(descriptors).length === 3 &&
|
|
3042
|
+
passStyle?.value === "tagged" &&
|
|
3043
|
+
passStyle.enumerable === false &&
|
|
3044
|
+
passStyle.writable === false &&
|
|
3045
|
+
passStyle.configurable === false &&
|
|
3046
|
+
typeof tag?.value === "string" &&
|
|
3047
|
+
tag.enumerable === false &&
|
|
3048
|
+
tag.writable === false &&
|
|
3049
|
+
tag.configurable === false &&
|
|
3050
|
+
payload !== undefined &&
|
|
3051
|
+
"value" in payload &&
|
|
3052
|
+
payload.enumerable === true &&
|
|
3053
|
+
payload.writable === false &&
|
|
3054
|
+
payload.configurable === false
|
|
3055
|
+
);
|
|
3056
|
+
}
|
|
3057
|
+
|
|
3058
|
+
/**
|
|
3059
|
+
* Scan syntax that may contain quotes, tags, and references once. The masked
|
|
3060
|
+
* text replaces their payloads with "x", unit for unit, so structural scans
|
|
3061
|
+
* can use the original offsets without reimplementing quote awareness.
|
|
3062
|
+
* @param {string} s
|
|
3063
|
+
* @param {boolean=} mask - build the masked text, for a caller that scans it
|
|
3064
|
+
* @returns {{masked: string|undefined, firstTag: number|undefined, comment: number|undefined, colon: number|undefined, closeBracket: number}}
|
|
3065
|
+
*/
|
|
3066
|
+
function scanTopLevel(s, mask = false) {
|
|
3067
|
+
/** @type {[number, number][]|undefined} */
|
|
3068
|
+
const masks = mask ? [] : undefined;
|
|
3069
|
+
let firstTag;
|
|
3070
|
+
let comment;
|
|
3071
|
+
let colon;
|
|
3072
|
+
let closeBracket = -1;
|
|
3073
|
+
let quote = 0;
|
|
3074
|
+
let quoteStart = 0;
|
|
3075
|
+
let depth = 0;
|
|
3076
|
+
let bracketDepth = 0;
|
|
3077
|
+
let commentEscape = false;
|
|
3078
|
+
let i = 0;
|
|
3079
|
+
const n = s.length;
|
|
3080
|
+
while (i < n) {
|
|
3081
|
+
const c = s.charCodeAt(i);
|
|
3082
|
+
if (quote !== 0) {
|
|
3083
|
+
if (c === 0x5c) {
|
|
3084
|
+
i += Math.min(2, n - i);
|
|
3085
|
+
continue;
|
|
3086
|
+
}
|
|
3087
|
+
if (c === quote) {
|
|
3088
|
+
if (masks !== undefined) masks.push([quoteStart, i]);
|
|
3089
|
+
quote = 0;
|
|
3090
|
+
}
|
|
1504
3091
|
i++;
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
3092
|
+
continue;
|
|
3093
|
+
}
|
|
3094
|
+
// A reference's text is no syntax either
|
|
3095
|
+
if (c === 0x3c && isReferenceStartAt(s, i)) {
|
|
3096
|
+
const end = s.indexOf(">", i);
|
|
3097
|
+
if (end >= 0) {
|
|
3098
|
+
if (masks !== undefined) masks.push([i + 1, end]);
|
|
3099
|
+
i = end + 1;
|
|
3100
|
+
continue;
|
|
1509
3101
|
}
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
3102
|
+
}
|
|
3103
|
+
if (c === 0x22 || c === 0x27) {
|
|
3104
|
+
quote = c;
|
|
3105
|
+
quoteStart = i + 1;
|
|
3106
|
+
} else if (c === 0x5b || c === 0x7b) {
|
|
3107
|
+
depth++;
|
|
3108
|
+
if (c === 0x5b) bracketDepth++;
|
|
3109
|
+
} else if (c === 0x5d || c === 0x7d) {
|
|
3110
|
+
depth--;
|
|
3111
|
+
if (c === 0x5d) {
|
|
3112
|
+
bracketDepth--;
|
|
3113
|
+
if (bracketDepth === 0 && closeBracket < 0) closeBracket = i;
|
|
3114
|
+
}
|
|
3115
|
+
} else if (c === 0x28) {
|
|
3116
|
+
if (depth === 0 && firstTag === undefined) firstTag = i;
|
|
3117
|
+
const tagStart = i + 1;
|
|
1514
3118
|
i++;
|
|
3119
|
+
while (i < n && s.charCodeAt(i) !== 0x29) {
|
|
3120
|
+
i += s.charCodeAt(i) === 0x5c ? Math.min(2, n - i) : 1;
|
|
3121
|
+
}
|
|
3122
|
+
if (masks !== undefined) masks.push([tagStart, i]);
|
|
3123
|
+
continue;
|
|
3124
|
+
} else if (c === 0x3a && colon === undefined) {
|
|
3125
|
+
colon = i;
|
|
3126
|
+
}
|
|
3127
|
+
if (comment === undefined) {
|
|
3128
|
+
if (commentEscape) {
|
|
3129
|
+
commentEscape = false;
|
|
3130
|
+
} else if (c === 0x5c) {
|
|
3131
|
+
commentEscape = true;
|
|
3132
|
+
} else if (c === 0x23) {
|
|
3133
|
+
comment = i;
|
|
3134
|
+
}
|
|
1515
3135
|
}
|
|
3136
|
+
i++;
|
|
1516
3137
|
}
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
.reduce((min, x) => (x.indent < min ? x.indent : min), Infinity);
|
|
1520
|
-
const effectiveMin = minIndent === Infinity ? 0 : minIndent;
|
|
1521
|
-
for (const { indent, text } of continuationLines) {
|
|
1522
|
-
if (indent === undefined) {
|
|
1523
|
-
lines.push("");
|
|
1524
|
-
} else {
|
|
1525
|
-
// Token text is already after indent; add back (indent - minIndent) spaces for relative indent.
|
|
1526
|
-
const extraSpaces = indent - effectiveMin;
|
|
1527
|
-
lines.push((extraSpaces > 0 ? " ".repeat(extraSpaces) : "") + text);
|
|
1528
|
-
}
|
|
3138
|
+
if (masks === undefined) {
|
|
3139
|
+
return { masked: undefined, firstTag, comment, colon, closeBracket };
|
|
1529
3140
|
}
|
|
1530
|
-
|
|
1531
|
-
let
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
(leadingNewline ? "\n" : "") +
|
|
1542
|
-
trimmed.join("\n") +
|
|
1543
|
-
(trimmed.length > 0 ? "\n" : "");
|
|
1544
|
-
// Empty block strings are not allowed - use "" for empty string
|
|
1545
|
-
if (body === "") {
|
|
1546
|
-
throw new Error(
|
|
1547
|
-
'Empty block string not allowed (use "" or "\\n" explicitly)',
|
|
1548
|
-
);
|
|
3141
|
+
if (quote !== 0) masks.push([quoteStart, n]);
|
|
3142
|
+
let masked = s;
|
|
3143
|
+
if (masks.length > 0) {
|
|
3144
|
+
const chunks = [];
|
|
3145
|
+
let start = 0;
|
|
3146
|
+
for (const [from, to] of masks) {
|
|
3147
|
+
chunks.push(s.slice(start, from), "x".repeat(to - from));
|
|
3148
|
+
start = to;
|
|
3149
|
+
}
|
|
3150
|
+
chunks.push(s.slice(start));
|
|
3151
|
+
masked = chunks.join("");
|
|
1549
3152
|
}
|
|
1550
|
-
return
|
|
3153
|
+
return { masked, firstTag, comment, colon, closeBracket };
|
|
1551
3154
|
}
|
|
1552
3155
|
|
|
1553
3156
|
/**
|
|
1554
|
-
* Parse
|
|
1555
|
-
*
|
|
1556
|
-
*
|
|
1557
|
-
* @param {
|
|
1558
|
-
* @param {number} baseIndent
|
|
3157
|
+
* Parse one tag at the start of s, which begins with "(". Returns the tag
|
|
3158
|
+
* and the number of units consumed, the closing ")" included. The tag is
|
|
3159
|
+
* escaped as a double-quoted string is, with \) in the place of \".
|
|
3160
|
+
* @param {string} s
|
|
1559
3161
|
* @param {ParseContext} ctx
|
|
1560
|
-
* @
|
|
3162
|
+
* @param {number} lineNum
|
|
3163
|
+
* @param {number} col
|
|
3164
|
+
* @returns {[string, number]}
|
|
1561
3165
|
*/
|
|
1562
|
-
function
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
3166
|
+
function parseTag(s, ctx, lineNum, col) {
|
|
3167
|
+
let out = "";
|
|
3168
|
+
let i = 1;
|
|
3169
|
+
while (i < s.length) {
|
|
3170
|
+
const c = s[i];
|
|
3171
|
+
if (c === ")") return [out, i + 1];
|
|
3172
|
+
if (c === "\t") {
|
|
3173
|
+
throw new Error(
|
|
3174
|
+
"Tab not allowed (use spaces)" + locSuffix(ctx, lineNum, col + i),
|
|
3175
|
+
);
|
|
3176
|
+
}
|
|
3177
|
+
if (c !== "\\") {
|
|
3178
|
+
out += c;
|
|
1570
3179
|
i++;
|
|
1571
3180
|
continue;
|
|
1572
3181
|
}
|
|
3182
|
+
const [value, next] = readEscape(s, i, ")", ctx, lineNum, col);
|
|
3183
|
+
out += value;
|
|
3184
|
+
i = next;
|
|
3185
|
+
}
|
|
3186
|
+
throw new Error("Unterminated string" + locSuffix(ctx, lineNum, col));
|
|
3187
|
+
}
|
|
1573
3188
|
|
|
1574
|
-
|
|
1575
|
-
|
|
3189
|
+
/**
|
|
3190
|
+
* Parse a run of tags after a value: each one space, then a tag. Returns the
|
|
3191
|
+
* tags, innermost first, and the number of units consumed. A tag fused to
|
|
3192
|
+
* its value or after two spaces is refused.
|
|
3193
|
+
* @param {string} s
|
|
3194
|
+
* @param {ParseContext} ctx
|
|
3195
|
+
* @param {number} lineNum
|
|
3196
|
+
* @param {number} col
|
|
3197
|
+
* @returns {[string[], number]}
|
|
3198
|
+
*/
|
|
3199
|
+
function parseSuffix(s, ctx, lineNum, col) {
|
|
3200
|
+
const tags = [];
|
|
3201
|
+
let pos = 0;
|
|
3202
|
+
for (;;) {
|
|
3203
|
+
let spaces = 0;
|
|
3204
|
+
while (s[pos + spaces] === " ") spaces++;
|
|
3205
|
+
if (s[pos + spaces] !== "(") break;
|
|
3206
|
+
if (spaces !== 1) {
|
|
3207
|
+
throw new Error(
|
|
3208
|
+
(spaces === 0
|
|
3209
|
+
? "Expected one space before a tag"
|
|
3210
|
+
: 'Unexpected space before "("') + locSuffix(ctx, lineNum, col + pos),
|
|
3211
|
+
);
|
|
1576
3212
|
}
|
|
3213
|
+
const [tag, n] = parseTag(s.slice(pos + 1), ctx, lineNum, col + pos + 1);
|
|
3214
|
+
tags.push(tag);
|
|
3215
|
+
pos += 1 + n;
|
|
3216
|
+
}
|
|
3217
|
+
return [tags, pos];
|
|
3218
|
+
}
|
|
1577
3219
|
|
|
1578
|
-
|
|
3220
|
+
/**
|
|
3221
|
+
* Parse a run of tags before a block: tags separated by one space. Returns
|
|
3222
|
+
* the tags, outermost first, and what follows the last one (after its
|
|
3223
|
+
* space, or a comment, which is the empty string).
|
|
3224
|
+
* @param {string} s
|
|
3225
|
+
* @param {ParseContext} ctx
|
|
3226
|
+
* @param {number} lineNum
|
|
3227
|
+
* @param {number} col
|
|
3228
|
+
* @returns {[string[], string]}
|
|
3229
|
+
*/
|
|
3230
|
+
function parsePrefix(s, ctx, lineNum, col) {
|
|
3231
|
+
const [first, n] = parseTag(s, ctx, lineNum, col);
|
|
3232
|
+
const [tags, m] = parseSuffix(s.slice(n), ctx, lineNum, col + n);
|
|
3233
|
+
tags.unshift(first);
|
|
3234
|
+
const rest = s.slice(n + m);
|
|
3235
|
+
if (rest === "" || rest.startsWith(" #")) return [tags, ""];
|
|
3236
|
+
if (rest.startsWith(" ") && !rest.startsWith(" ")) {
|
|
3237
|
+
return [tags, rest.slice(1)];
|
|
3238
|
+
}
|
|
3239
|
+
throw new Error(
|
|
3240
|
+
'Unexpected space after ")"' + locSuffix(ctx, lineNum, col + n + m),
|
|
3241
|
+
);
|
|
3242
|
+
}
|
|
1579
3243
|
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
3244
|
+
/**
|
|
3245
|
+
* Wrap a payload in tags, innermost first.
|
|
3246
|
+
* @param {unknown} value
|
|
3247
|
+
* @param {string[]} tags
|
|
3248
|
+
*/
|
|
3249
|
+
function wrapTags(value, tags) {
|
|
3250
|
+
return tags.reduce((v, tag) => taggedRecord(tag, v), value);
|
|
3251
|
+
}
|
|
1585
3252
|
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
3253
|
+
/**
|
|
3254
|
+
* Wrap a payload in a prefix's tags, outermost first.
|
|
3255
|
+
* @param {unknown} value
|
|
3256
|
+
* @param {string[]} tags
|
|
3257
|
+
*/
|
|
3258
|
+
function wrapPrefix(value, tags) {
|
|
3259
|
+
return wrapTags(value, [...tags].reverse());
|
|
3260
|
+
}
|
|
1589
3261
|
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
3262
|
+
/**
|
|
3263
|
+
* Is rest, what follows a prefix, a block it may introduce on its own line?
|
|
3264
|
+
* @param {string} rest
|
|
3265
|
+
*/
|
|
3266
|
+
function prefixTakes(rest) {
|
|
3267
|
+
return rest === "" || rest.startsWith("`") || rest.startsWith(">");
|
|
3268
|
+
}
|
|
1595
3269
|
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
3270
|
+
/**
|
|
3271
|
+
* The tags at the start of a line, and the rest, or null if the line does
|
|
3272
|
+
* not begin with well-formed tags. For the scanner, which finds block
|
|
3273
|
+
* openers before the parser reports errors.
|
|
3274
|
+
* @param {string} s
|
|
3275
|
+
* @returns {string|null}
|
|
3276
|
+
*/
|
|
3277
|
+
function afterPrefix(s) {
|
|
3278
|
+
if (!s.startsWith("(")) return null;
|
|
3279
|
+
try {
|
|
3280
|
+
return parsePrefix(s, {}, 0, 0)[1];
|
|
3281
|
+
} catch {
|
|
1599
3282
|
return null;
|
|
1600
3283
|
}
|
|
3284
|
+
}
|
|
1601
3285
|
|
|
1602
|
-
|
|
3286
|
+
/**
|
|
3287
|
+
* Whether a line is a property whose tags introduce a block string, as in
|
|
3288
|
+
* "key: (tag) `".
|
|
3289
|
+
* @param {string} line
|
|
3290
|
+
*/
|
|
3291
|
+
function isTaggedPropertyOpener(line) {
|
|
3292
|
+
const colon = findKeyColonOutsideQuotes(line);
|
|
3293
|
+
if (colon < 0 || !line.startsWith(" (", colon + 1)) return false;
|
|
3294
|
+
return afterPrefix(line.slice(colon + 2)) === "`";
|
|
1603
3295
|
}
|
|
1604
3296
|
|
|
1605
3297
|
/**
|
|
1606
|
-
*
|
|
1607
|
-
*
|
|
3298
|
+
* Whether what a property's prefix tagged is no block: nothing, or the empty
|
|
3299
|
+
* list a property reads when the next line is a sibling item.
|
|
3300
|
+
* @param {unknown} value
|
|
1608
3301
|
*/
|
|
1609
|
-
function
|
|
1610
|
-
let i = 0;
|
|
1611
|
-
while (i < text.length && text[i] === " ") i++;
|
|
3302
|
+
function isMissingBlock(value) {
|
|
1612
3303
|
return (
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
text[i + 1] === " "
|
|
3304
|
+
value === undefined ||
|
|
3305
|
+
value === null ||
|
|
3306
|
+
(Array.isArray(value) && value.length === 0)
|
|
1617
3307
|
);
|
|
1618
3308
|
}
|
|
1619
3309
|
|
|
1620
3310
|
/**
|
|
1621
|
-
*
|
|
3311
|
+
* The error for a prefix that introduces no block.
|
|
1622
3312
|
* @param {ParseContext} ctx
|
|
1623
3313
|
* @param {number} lineNum
|
|
1624
3314
|
* @param {number} col
|
|
1625
|
-
* @returns {string}
|
|
1626
3315
|
*/
|
|
1627
|
-
function
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
3316
|
+
function prefixWithoutBlock(ctx, lineNum, col) {
|
|
3317
|
+
return new Error(
|
|
3318
|
+
"A tag before a value needs a block below it; write the tag after an inline value" +
|
|
3319
|
+
locSuffix(ctx, lineNum, col),
|
|
3320
|
+
);
|
|
3321
|
+
}
|
|
3322
|
+
|
|
3323
|
+
/**
|
|
3324
|
+
* The error for a prefix whose following line is an inline value. A tag line
|
|
3325
|
+
* without a following block is a symbol instead.
|
|
3326
|
+
* @param {ParseContext} ctx
|
|
3327
|
+
* @param {number} lineNum
|
|
3328
|
+
* @param {number} col
|
|
3329
|
+
*/
|
|
3330
|
+
function prefixOverInlineValue(ctx, lineNum, col) {
|
|
3331
|
+
return new Error(
|
|
3332
|
+
"A tag before an inline value is refused; a tag alone is a symbol" +
|
|
3333
|
+
locSuffix(ctx, lineNum, col),
|
|
3334
|
+
);
|
|
3335
|
+
}
|
|
3336
|
+
|
|
3337
|
+
/** Whether tokens[i] begins a root block rather than an inline value. */
|
|
3338
|
+
function rootBlockFollows(tokens, i) {
|
|
3339
|
+
const token = tokens[i];
|
|
3340
|
+
if (token?.type === "start" && token.text === "-") return true;
|
|
3341
|
+
if (token?.type !== "text") return false;
|
|
3342
|
+
const text = token.text;
|
|
3343
|
+
if (text === "`" || text.startsWith("` ") || text.startsWith(">")) {
|
|
3344
|
+
return true;
|
|
1639
3345
|
}
|
|
1640
|
-
|
|
3346
|
+
if (
|
|
3347
|
+
findKeyColonOutsideQuotes(text) >= 0 &&
|
|
3348
|
+
!text.startsWith("{") &&
|
|
3349
|
+
!text.startsWith("[")
|
|
3350
|
+
) {
|
|
3351
|
+
return true;
|
|
3352
|
+
}
|
|
3353
|
+
if (!text.startsWith("(")) return false;
|
|
3354
|
+
let rest;
|
|
3355
|
+
try {
|
|
3356
|
+
[, rest] = parsePrefix(text, {}, 0, 0);
|
|
3357
|
+
} catch {
|
|
3358
|
+
return false;
|
|
3359
|
+
}
|
|
3360
|
+
if (rest !== "") return false;
|
|
3361
|
+
const next = skipBlank(tokens, i + 1, true);
|
|
3362
|
+
return next < tokens.length && rootBlockFollows(tokens, next);
|
|
3363
|
+
}
|
|
3364
|
+
|
|
3365
|
+
/** Whether tokens[i] begins a block belonging to a list item at listIndent. */
|
|
3366
|
+
function listBlockFollows(tokens, i, listIndent) {
|
|
3367
|
+
const token = tokens[i];
|
|
3368
|
+
if (!token || (token.indent ?? 0) <= listIndent) return false;
|
|
3369
|
+
if (token.type === "start" && token.text === "-") return true;
|
|
3370
|
+
if (token.type !== "text") return false;
|
|
3371
|
+
if (
|
|
3372
|
+
findKeyColonOutsideQuotes(token.text) >= 0 &&
|
|
3373
|
+
!token.text.startsWith("{") &&
|
|
3374
|
+
!token.text.startsWith("[")
|
|
3375
|
+
) {
|
|
3376
|
+
return true;
|
|
3377
|
+
}
|
|
3378
|
+
if (!token.text.startsWith("(")) return false;
|
|
3379
|
+
let rest;
|
|
3380
|
+
try {
|
|
3381
|
+
[, rest] = parsePrefix(token.text, {}, 0, 0);
|
|
3382
|
+
} catch {
|
|
3383
|
+
return false;
|
|
3384
|
+
}
|
|
3385
|
+
if (rest !== "") return prefixTakes(rest);
|
|
3386
|
+
const next = skipBlank(tokens, i + 1);
|
|
3387
|
+
return next < tokens.length && listBlockFollows(tokens, next, listIndent);
|
|
1641
3388
|
}
|
|
1642
3389
|
|
|
3390
|
+
/** Whether a property's following token begins its block value. */
|
|
3391
|
+
function propertyBlockFollows(tokens, i, baseIndent) {
|
|
3392
|
+
const next = skipBlank(tokens, i + 1, true);
|
|
3393
|
+
if (next >= tokens.length) return false;
|
|
3394
|
+
const token = tokens[next];
|
|
3395
|
+
if (token.type === "start" && token.text === "-") {
|
|
3396
|
+
return (token.indent ?? 0) >= baseIndent;
|
|
3397
|
+
}
|
|
3398
|
+
if (token.type !== "text" || (token.indent ?? 0) <= baseIndent) {
|
|
3399
|
+
return false;
|
|
3400
|
+
}
|
|
3401
|
+
const trimmed = token.text.trim();
|
|
3402
|
+
if (trimmed.startsWith('"') || trimmed.startsWith("'")) return true;
|
|
3403
|
+
return (
|
|
3404
|
+
findKeyColonOutsideQuotes(token.text) >= 0 &&
|
|
3405
|
+
!token.text.startsWith("{") &&
|
|
3406
|
+
!token.text.startsWith("[")
|
|
3407
|
+
);
|
|
3408
|
+
}
|
|
3409
|
+
|
|
3410
|
+
// --- References ---
|
|
3411
|
+
|
|
1643
3412
|
/**
|
|
1644
|
-
*
|
|
1645
|
-
*
|
|
3413
|
+
* A reference: a URI or IRI, held as written; it designates, and is never
|
|
3414
|
+
* dereferenced. Instances are frozen. Two are equal when their texts are,
|
|
3415
|
+
* code unit for code unit, and a reference never equals a string or a plain
|
|
3416
|
+
* object.
|
|
1646
3417
|
*/
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
3418
|
+
class Reference {
|
|
3419
|
+
/**
|
|
3420
|
+
* @param {string} uri
|
|
3421
|
+
*/
|
|
3422
|
+
constructor(uri) {
|
|
3423
|
+
if (typeof uri !== "string") {
|
|
3424
|
+
throw new TypeError("A reference must be a string");
|
|
3425
|
+
}
|
|
3426
|
+
checkReferenceText(uri);
|
|
3427
|
+
this.reference = uri;
|
|
3428
|
+
Object.freeze(this);
|
|
1658
3429
|
}
|
|
1659
|
-
// Otherwise, parse as a scalar
|
|
1660
|
-
return parseScalar(text, ctx, lineNum, col);
|
|
1661
3430
|
}
|
|
1662
3431
|
|
|
1663
|
-
|
|
1664
|
-
|
|
3432
|
+
/**
|
|
3433
|
+
* Refuse a string that is not a reference's text, by the parser's own rule:
|
|
3434
|
+
* a lowercase scheme, a colon, URI or IRI characters, and well-formed
|
|
3435
|
+
* percent-encodings.
|
|
3436
|
+
* @param {string} uri
|
|
3437
|
+
*/
|
|
3438
|
+
function checkReferenceText(uri) {
|
|
3439
|
+
checkWellFormed(uri, "a reference");
|
|
3440
|
+
const text = "<" + uri + ">";
|
|
3441
|
+
let n = -1;
|
|
3442
|
+
let why = "it needs a scheme and a colon";
|
|
3443
|
+
if (isReferenceStart(text)) {
|
|
3444
|
+
try {
|
|
3445
|
+
[, n] = parseReference(text, {}, 0, 0);
|
|
3446
|
+
} catch (error) {
|
|
3447
|
+
why = /** @type {Error} */ (error).message;
|
|
3448
|
+
}
|
|
3449
|
+
}
|
|
3450
|
+
if (n !== text.length) {
|
|
3451
|
+
if (n >= 0) why = 'Unexpected character ">" in reference';
|
|
3452
|
+
throw new TypeError(`Not a reference: ${JSON.stringify(uri)} (${why})`);
|
|
3453
|
+
}
|
|
3454
|
+
}
|
|
1665
3455
|
|
|
1666
3456
|
/**
|
|
1667
|
-
*
|
|
1668
|
-
*
|
|
3457
|
+
* Make a reference, as `yay -t js` writes it and as a caller may use it for
|
|
3458
|
+
* `decodeReference`.
|
|
3459
|
+
* @param {string} uri
|
|
3460
|
+
* @returns {Reference}
|
|
3461
|
+
*/
|
|
3462
|
+
function reference(uri) {
|
|
3463
|
+
return new Reference(uri);
|
|
3464
|
+
}
|
|
3465
|
+
|
|
3466
|
+
/**
|
|
3467
|
+
* @param {unknown} value
|
|
3468
|
+
* @returns {value is Reference}
|
|
3469
|
+
*/
|
|
3470
|
+
function isReference(value) {
|
|
3471
|
+
return value instanceof Reference;
|
|
3472
|
+
}
|
|
3473
|
+
|
|
3474
|
+
/**
|
|
3475
|
+
* The length of the scheme, colon included, if s (the text after "<")
|
|
3476
|
+
* begins [A-Za-z][A-Za-z0-9+.-]*":". Uppercase letters are matched so that
|
|
3477
|
+
* the reader can name an uppercase scheme rather than call it bad hex.
|
|
3478
|
+
* @param {string} s
|
|
3479
|
+
* @returns {number|undefined}
|
|
3480
|
+
*/
|
|
3481
|
+
function schemeLength(s) {
|
|
3482
|
+
return schemeLengthAt(s, 0);
|
|
3483
|
+
}
|
|
3484
|
+
|
|
3485
|
+
/**
|
|
3486
|
+
* The length of the scheme, colon included, beginning at s[at].
|
|
3487
|
+
* @param {string} s
|
|
3488
|
+
* @param {number} at
|
|
3489
|
+
* @returns {number|undefined}
|
|
3490
|
+
*/
|
|
3491
|
+
function schemeLengthAt(s, at) {
|
|
3492
|
+
const c0 = s.charCodeAt(at) | 0x20;
|
|
3493
|
+
if (!(c0 >= 0x61 && c0 <= 0x7a)) return undefined;
|
|
3494
|
+
for (let i = at + 1; i < s.length; i++) {
|
|
3495
|
+
const c = s.charCodeAt(i);
|
|
3496
|
+
if (c === 0x3a) return i - at + 1;
|
|
3497
|
+
const l = c | 0x20;
|
|
3498
|
+
if (!(
|
|
3499
|
+
(l >= 0x61 && l <= 0x7a) ||
|
|
3500
|
+
(c >= 0x30 && c <= 0x39) ||
|
|
3501
|
+
c === 0x2b ||
|
|
3502
|
+
c === 0x2e ||
|
|
3503
|
+
c === 0x2d
|
|
3504
|
+
)) {
|
|
3505
|
+
return undefined;
|
|
3506
|
+
}
|
|
3507
|
+
}
|
|
3508
|
+
return undefined;
|
|
3509
|
+
}
|
|
3510
|
+
|
|
3511
|
+
/**
|
|
3512
|
+
* Whether s begins with "<" and a scheme, so is a reference.
|
|
3513
|
+
* @param {string} s
|
|
3514
|
+
*/
|
|
3515
|
+
function isReferenceStart(s) {
|
|
3516
|
+
return isReferenceStartAt(s, 0);
|
|
3517
|
+
}
|
|
3518
|
+
|
|
3519
|
+
/**
|
|
3520
|
+
* Whether a reference begins at s[at].
|
|
3521
|
+
* @param {string} s
|
|
3522
|
+
* @param {number} at
|
|
3523
|
+
*/
|
|
3524
|
+
function isReferenceStartAt(s, at) {
|
|
3525
|
+
return s.charCodeAt(at) === 0x3c && schemeLengthAt(s, at + 1) !== undefined;
|
|
3526
|
+
}
|
|
3527
|
+
|
|
3528
|
+
/**
|
|
3529
|
+
* Whether the character c may appear in a reference (RFC 3986 and RFC
|
|
3530
|
+
* 3987), "%" aside, which must begin a percent-encoding.
|
|
3531
|
+
* @param {string} c
|
|
3532
|
+
*/
|
|
3533
|
+
function isUriChar(c) {
|
|
3534
|
+
const cp = c.codePointAt(0) ?? 0;
|
|
3535
|
+
if (cp < 0x80) return URI_ASCII[cp] === 1;
|
|
3536
|
+
return cp >= 0xa0 && !UNICODE_SPACE.test(c) && cp !== 0xfffe && cp !== 0xffff;
|
|
3537
|
+
}
|
|
3538
|
+
|
|
3539
|
+
/**
|
|
3540
|
+
* Parse a reference at the start of s, which begins with "<" and a scheme.
|
|
3541
|
+
* Returns the URI text and the number of units consumed, the ">" included.
|
|
3542
|
+
* The text between the brackets is the URI, verbatim.
|
|
3543
|
+
* @param {string} s
|
|
1669
3544
|
* @param {ParseContext} ctx
|
|
1670
|
-
* @param {
|
|
1671
|
-
* @param {number}
|
|
1672
|
-
* @returns {[
|
|
3545
|
+
* @param {number} lineNum
|
|
3546
|
+
* @param {number} col
|
|
3547
|
+
* @returns {[string, number]}
|
|
1673
3548
|
*/
|
|
1674
|
-
function
|
|
1675
|
-
const
|
|
1676
|
-
const
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
} else {
|
|
1683
|
-
i++;
|
|
3549
|
+
function parseReference(s, ctx, lineNum, col) {
|
|
3550
|
+
const schemeEnd = /** @type {number} */ (schemeLengthAt(s, 1));
|
|
3551
|
+
const scheme = s.slice(1, schemeEnd);
|
|
3552
|
+
if (UPPERCASE.test(scheme)) {
|
|
3553
|
+
throw new Error(
|
|
3554
|
+
`Uppercase scheme "${scheme}" in reference (use lowercase)` +
|
|
3555
|
+
locSuffix(ctx, lineNum, col),
|
|
3556
|
+
);
|
|
1684
3557
|
}
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
3558
|
+
let i = schemeEnd;
|
|
3559
|
+
const n = s.length;
|
|
3560
|
+
while (i < n) {
|
|
3561
|
+
const u = s.charCodeAt(i);
|
|
3562
|
+
if (u === 0x3e) {
|
|
3563
|
+
return [detachString(s.slice(1, i)), i + 1];
|
|
3564
|
+
}
|
|
3565
|
+
if (u < 0x80) {
|
|
3566
|
+
if (u === 0x25) {
|
|
3567
|
+
if (!(
|
|
3568
|
+
isHexDigit(s.charCodeAt(i + 1)) && isHexDigit(s.charCodeAt(i + 2))
|
|
3569
|
+
)) {
|
|
3570
|
+
throw new Error(
|
|
3571
|
+
"Bad percent-encoding in reference (use % and two hex digits)" +
|
|
3572
|
+
locSuffix(ctx, lineNum, col + i),
|
|
3573
|
+
);
|
|
3574
|
+
}
|
|
3575
|
+
} else if (URI_ASCII[u] !== 1) {
|
|
3576
|
+
const c = s[i];
|
|
3577
|
+
const message =
|
|
3578
|
+
c === " "
|
|
3579
|
+
? "Unexpected space in reference"
|
|
3580
|
+
: c === "\t"
|
|
3581
|
+
? "Tab not allowed in reference"
|
|
3582
|
+
: `Unexpected character "${c}" in reference`;
|
|
3583
|
+
throw new Error(message + locSuffix(ctx, lineNum, col + i));
|
|
3584
|
+
}
|
|
1688
3585
|
i++;
|
|
1689
3586
|
continue;
|
|
1690
3587
|
}
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
3588
|
+
const c = String.fromCodePoint(/** @type {number} */ (s.codePointAt(i)));
|
|
3589
|
+
if (!isUriChar(c)) {
|
|
3590
|
+
throw new Error(
|
|
3591
|
+
`Unexpected character "${c}" in reference` +
|
|
3592
|
+
locSuffix(ctx, lineNum, col + i),
|
|
3593
|
+
);
|
|
1695
3594
|
}
|
|
1696
|
-
|
|
3595
|
+
i += c.length;
|
|
1697
3596
|
}
|
|
1698
|
-
|
|
3597
|
+
throw new Error("Unmatched angle bracket" + locSuffix(ctx, lineNum, col));
|
|
3598
|
+
}
|
|
3599
|
+
|
|
3600
|
+
/**
|
|
3601
|
+
* Turn a reference's validated URI text into the caller's value. No default
|
|
3602
|
+
* reference: a document with a reference and no hook is refused, naming the
|
|
3603
|
+
* reference and its location.
|
|
3604
|
+
* @param {string} uri
|
|
3605
|
+
* @param {ParseContext} ctx
|
|
3606
|
+
* @param {number} lineNum
|
|
3607
|
+
* @param {number} col
|
|
3608
|
+
* @returns {unknown}
|
|
3609
|
+
*/
|
|
3610
|
+
function decodeReferenceText(uri, ctx, lineNum, col) {
|
|
3611
|
+
const decode = ctx.decodeReference;
|
|
3612
|
+
if (typeof decode !== "function") {
|
|
1699
3613
|
throw new Error(
|
|
1700
|
-
|
|
3614
|
+
`Reference ${JSON.stringify(uri)} needs a decodeReference hook` +
|
|
3615
|
+
requiredLocSuffix(ctx, lineNum, col),
|
|
1701
3616
|
);
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
const result = Uint8Array.fromHex
|
|
1705
|
-
? Uint8Array.fromHex(hex)
|
|
1706
|
-
: hexToUint8Array(hex, ctx, lineNum, col);
|
|
1707
|
-
return [result, i];
|
|
3617
|
+
}
|
|
3618
|
+
return decode(uri);
|
|
1708
3619
|
}
|
|
1709
3620
|
|
|
1710
|
-
// Note: parseMultilineAngleBytes was removed as dead code.
|
|
1711
|
-
// The "< hex" syntax (without closing ">") is invalid - inline byte arrays must be closed on the same line.
|
|
1712
|
-
|
|
1713
3621
|
/**
|
|
1714
|
-
*
|
|
1715
|
-
*
|
|
1716
|
-
* @param {string}
|
|
3622
|
+
* Turn a symbol name into the caller's representation. parseYay installs
|
|
3623
|
+
* `symbol` as the default hook.
|
|
3624
|
+
* @param {string} name
|
|
1717
3625
|
* @param {ParseContext} ctx
|
|
1718
|
-
* @returns {
|
|
3626
|
+
* @returns {unknown}
|
|
1719
3627
|
*/
|
|
1720
|
-
function
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
3628
|
+
function decodeSymbolText(name, ctx) {
|
|
3629
|
+
return ctx.decodeSymbol(name);
|
|
3630
|
+
}
|
|
3631
|
+
|
|
3632
|
+
/**
|
|
3633
|
+
* Whether a code unit is an ASCII hex digit.
|
|
3634
|
+
* @param {number} c
|
|
3635
|
+
*/
|
|
3636
|
+
function isHexDigit(c) {
|
|
3637
|
+
return (
|
|
3638
|
+
(c >= 0x30 && c <= 0x39) ||
|
|
3639
|
+
(c >= 0x61 && c <= 0x66) ||
|
|
3640
|
+
(c >= 0x41 && c <= 0x46)
|
|
1727
3641
|
);
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
!first ||
|
|
1745
|
-
(first.type === "text" &&
|
|
1746
|
-
splitKeyValue(first.text, first.col ?? 0, ctx, first.lineNum ?? 0))
|
|
1747
|
-
) {
|
|
1748
|
-
const col = (keyToken.col ?? 0) + key.length + 1;
|
|
1749
|
-
throw new Error(
|
|
1750
|
-
"Expected value after property" +
|
|
1751
|
-
locSuffix(ctx, keyToken.lineNum ?? 0, col),
|
|
1752
|
-
);
|
|
1753
|
-
}
|
|
1754
|
-
}
|
|
1755
|
-
if (first && first.type === "start" && first.text === "-") {
|
|
1756
|
-
const [arr, next] = parseListArray(tokens, i);
|
|
1757
|
-
return [{ [key]: arr }, next];
|
|
1758
|
-
}
|
|
1759
|
-
// Note: '*' syntax for multiline bytes is rejected by the scanner
|
|
1760
|
-
// Note: "key: <" without closing ">" is invalid - inline byte arrays must be closed on the same line
|
|
1761
|
-
if (first && first.type === "text" && first.text === '"') {
|
|
1762
|
-
const [body, next] = parseBlockStringWithIndent(
|
|
1763
|
-
tokens,
|
|
1764
|
-
i,
|
|
1765
|
-
undefined,
|
|
1766
|
-
false,
|
|
1767
|
-
-1,
|
|
1768
|
-
);
|
|
1769
|
-
return [{ [key]: body }, next];
|
|
3642
|
+
}
|
|
3643
|
+
|
|
3644
|
+
// --- The CapData bridge ---
|
|
3645
|
+
|
|
3646
|
+
/**
|
|
3647
|
+
* Walk a YSON value, mapping each string that begins with "$" (a
|
|
3648
|
+
* reference, or a slot) through f. A tagged value's tag is a string, never
|
|
3649
|
+
* a reference, so only its payload is walked.
|
|
3650
|
+
* @param {unknown} value
|
|
3651
|
+
* @param {(s: string, path: string) => string} f
|
|
3652
|
+
* @param {string} path
|
|
3653
|
+
* @returns {unknown}
|
|
3654
|
+
*/
|
|
3655
|
+
function mapDollars(value, f, path = "$") {
|
|
3656
|
+
if (typeof value === "string") {
|
|
3657
|
+
return value.startsWith("$") ? f(value, path) : value;
|
|
1770
3658
|
}
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
throw new Error(
|
|
1774
|
-
"Unexpected indent" + locSuffix(ctx, first.lineNum ?? 0, 0),
|
|
1775
|
-
);
|
|
3659
|
+
if (Array.isArray(value)) {
|
|
3660
|
+
return value.map((v, i) => mapDollars(v, f, `${path}[${i}]`));
|
|
1776
3661
|
}
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
(trimmed.startsWith("'") &&
|
|
1787
|
-
trimmed.endsWith("'") &&
|
|
1788
|
-
trimmed.length >= 2)) &&
|
|
1789
|
-
!splitKeyValue(first.text, first.col ?? 0, ctx, first.lineNum ?? 0)
|
|
1790
|
-
) {
|
|
1791
|
-
const result = parseConcatenatedStrings(tokens, i, baseIndent, ctx);
|
|
1792
|
-
if (result !== null) {
|
|
1793
|
-
const [concatStr, next] = result;
|
|
1794
|
-
return [{ [key]: concatStr }, next];
|
|
1795
|
-
}
|
|
1796
|
-
// Single string on new line is invalid - fall through to error
|
|
1797
|
-
throw new Error(
|
|
1798
|
-
"Unexpected indent" + locSuffix(ctx, first.lineNum ?? 0, 0),
|
|
3662
|
+
if (value !== null && typeof value === "object") {
|
|
3663
|
+
const out = {};
|
|
3664
|
+
for (const [k, v] of Object.entries(value)) {
|
|
3665
|
+
setOwn(
|
|
3666
|
+
out,
|
|
3667
|
+
k,
|
|
3668
|
+
k === "#tag" && "payload" in value
|
|
3669
|
+
? v
|
|
3670
|
+
: mapDollars(v, f, `${path}[${JSON.stringify(k)}]`),
|
|
1799
3671
|
);
|
|
1800
3672
|
}
|
|
3673
|
+
return out;
|
|
1801
3674
|
}
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
}
|
|
1817
|
-
const keyValue = splitKeyValue(s, t.col ?? 0, ctx, t.lineNum ?? 0);
|
|
1818
|
-
if (keyValue) {
|
|
1819
|
-
const k = keyValue.key;
|
|
1820
|
-
const vPart = keyValue.valuePart;
|
|
1821
|
-
if ((t.indent ?? 0) < baseIndent) break;
|
|
1822
|
-
if (k && (t.indent ?? 0) <= baseIndent && obj.hasOwnProperty(k)) break;
|
|
1823
|
-
if (k && (t.indent ?? 0) <= baseIndent && !obj.hasOwnProperty(k)) {
|
|
1824
|
-
if (vPart === "{}") {
|
|
1825
|
-
obj[k] = {};
|
|
1826
|
-
i++;
|
|
1827
|
-
} else if (vPart.startsWith(">")) {
|
|
1828
|
-
// Block bytes in property context
|
|
1829
|
-
if (!isPropertyBlockLeaderOnly(vPart, ">")) {
|
|
1830
|
-
throw new Error(
|
|
1831
|
-
"Expected newline after block leader in property",
|
|
1832
|
-
);
|
|
1833
|
-
}
|
|
1834
|
-
const [bytes, next] = parseBlockBytes(
|
|
1835
|
-
tokens,
|
|
1836
|
-
i,
|
|
1837
|
-
ctx,
|
|
1838
|
-
"",
|
|
1839
|
-
t.indent ?? 0,
|
|
1840
|
-
);
|
|
1841
|
-
obj[k] = bytes;
|
|
1842
|
-
i = next;
|
|
1843
|
-
} else if (vPart.trim() === "`") {
|
|
1844
|
-
// Block string in property context: backtick alone on line
|
|
1845
|
-
const [body, next] = parseBlockStringWithIndent(
|
|
1846
|
-
tokens,
|
|
1847
|
-
i,
|
|
1848
|
-
"",
|
|
1849
|
-
true,
|
|
1850
|
-
t.indent ?? 0,
|
|
1851
|
-
);
|
|
1852
|
-
obj[k] = body;
|
|
1853
|
-
i = next;
|
|
1854
|
-
} else if (vPart === "") {
|
|
1855
|
-
i++;
|
|
1856
|
-
while (i < tokens.length && tokens[i].type === "break") i++;
|
|
1857
|
-
const nextT = tokens[i];
|
|
1858
|
-
// Note: '*' syntax for multiline bytes is rejected by the scanner
|
|
1859
|
-
if (nextT && nextT.type === "text" && nextT.text === '"') {
|
|
1860
|
-
const [body, next] = parseBlockStringWithIndent(
|
|
1861
|
-
tokens,
|
|
1862
|
-
i,
|
|
1863
|
-
undefined,
|
|
1864
|
-
false,
|
|
1865
|
-
-1,
|
|
1866
|
-
);
|
|
1867
|
-
obj[k] = body;
|
|
1868
|
-
i = next;
|
|
1869
|
-
} else if (nextT && nextT.type === "start" && nextT.text === "-") {
|
|
1870
|
-
const [arr, next] = parseListArray(tokens, i);
|
|
1871
|
-
obj[k] = arr;
|
|
1872
|
-
i = next;
|
|
1873
|
-
} else if (
|
|
1874
|
-
nextT &&
|
|
1875
|
-
nextT.type === "text" &&
|
|
1876
|
-
(nextT.indent ?? 0) > (t.indent ?? 0)
|
|
1877
|
-
) {
|
|
1878
|
-
const [child, next] = parseObjectBlock(
|
|
1879
|
-
tokens,
|
|
1880
|
-
i,
|
|
1881
|
-
nextT.indent ?? 0,
|
|
1882
|
-
ctx,
|
|
1883
|
-
);
|
|
1884
|
-
obj[k] = child;
|
|
1885
|
-
i = next;
|
|
1886
|
-
} else {
|
|
1887
|
-
// Empty property with no nested content is invalid
|
|
1888
|
-
throw new Error(
|
|
1889
|
-
"Expected value after property" +
|
|
1890
|
-
locSuffix(ctx, t.lineNum ?? 0, (t.col ?? 0) + k.length + 1),
|
|
1891
|
-
);
|
|
1892
|
-
}
|
|
1893
|
-
} else {
|
|
1894
|
-
// Inline value (scalar, array, object, bytes)
|
|
1895
|
-
obj[k] = parseScalar(vPart, ctx, t.lineNum ?? 0, keyValue.valueCol);
|
|
1896
|
-
i++;
|
|
1897
|
-
}
|
|
1898
|
-
} else {
|
|
1899
|
-
i++;
|
|
1900
|
-
}
|
|
1901
|
-
} else {
|
|
1902
|
-
// Text without colon in nested object context is invalid
|
|
1903
|
-
// (e.g., inline array/object/bytes/string on separate line)
|
|
1904
|
-
throw new Error(
|
|
1905
|
-
"Unexpected indent" + locSuffix(ctx, t.lineNum ?? 0, 0),
|
|
1906
|
-
);
|
|
1907
|
-
}
|
|
1908
|
-
} else {
|
|
1909
|
-
i++;
|
|
1910
|
-
}
|
|
3675
|
+
return value;
|
|
3676
|
+
}
|
|
3677
|
+
|
|
3678
|
+
/**
|
|
3679
|
+
* JSON text for a JSON value, as JSON.stringify writes it but for negative
|
|
3680
|
+
* zero, which JSON.stringify writes as 0 and YSON and smallcaps write and
|
|
3681
|
+
* read as -0.
|
|
3682
|
+
* @param {unknown} value
|
|
3683
|
+
* @returns {string}
|
|
3684
|
+
*/
|
|
3685
|
+
function stringifyJson(value) {
|
|
3686
|
+
if (Object.is(value, -0)) return "-0";
|
|
3687
|
+
if (Array.isArray(value)) {
|
|
3688
|
+
return `[${value.map(stringifyJson).join(",")}]`;
|
|
1911
3689
|
}
|
|
1912
|
-
|
|
3690
|
+
if (value !== null && typeof value === "object") {
|
|
3691
|
+
const members = Object.entries(value).map(
|
|
3692
|
+
([k, v]) => `${JSON.stringify(k)}:${stringifyJson(v)}`,
|
|
3693
|
+
);
|
|
3694
|
+
return `{${members.join(",")}}`;
|
|
3695
|
+
}
|
|
3696
|
+
return JSON.stringify(value);
|
|
1913
3697
|
}
|
|
1914
3698
|
|
|
1915
3699
|
/**
|
|
1916
|
-
*
|
|
1917
|
-
*
|
|
1918
|
-
*
|
|
1919
|
-
*
|
|
1920
|
-
*
|
|
1921
|
-
*
|
|
3700
|
+
* From YSON to CapData: each distinct reference goes to `slots` in order of
|
|
3701
|
+
* first appearance, and the body names it `$0`, `$1`, ... as smallcaps
|
|
3702
|
+
* names a remotable. The URI is checked against YAY's reference rule before
|
|
3703
|
+
* the decode hook sees it, and the hook's result goes to `slots`. Without
|
|
3704
|
+
* a hook, a YSON document with a reference is refused, naming the reference
|
|
3705
|
+
* and its JSON path.
|
|
3706
|
+
* @param {string} yson
|
|
3707
|
+
* @param {{decodeReference?: (uri: string) => unknown}=} options
|
|
3708
|
+
* @returns {{body: string, slots: unknown[]}}
|
|
1922
3709
|
*/
|
|
1923
|
-
function
|
|
1924
|
-
const
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
}
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
if (t.type !== "text") {
|
|
1940
|
-
i++;
|
|
1941
|
-
continue;
|
|
1942
|
-
}
|
|
1943
|
-
let indent = t.indent ?? 0;
|
|
1944
|
-
if (firstText && inlineIndent !== undefined && indent === inlineIndent) {
|
|
1945
|
-
indent = baseIndent;
|
|
1946
|
-
}
|
|
1947
|
-
firstText = false;
|
|
1948
|
-
if (indent < baseIndent) break;
|
|
1949
|
-
if (indent > baseIndent) {
|
|
1950
|
-
i++;
|
|
1951
|
-
continue;
|
|
1952
|
-
}
|
|
1953
|
-
const keyValue = splitKeyValue(t.text, t.col ?? 0, ctx, t.lineNum ?? 0);
|
|
1954
|
-
if (!keyValue) {
|
|
1955
|
-
i++;
|
|
1956
|
-
continue;
|
|
1957
|
-
}
|
|
1958
|
-
const k = keyValue.key;
|
|
1959
|
-
const vPart = keyValue.valuePart;
|
|
1960
|
-
// Note: "key: <" without closing ">" is invalid - inline byte arrays must be closed on the same line
|
|
1961
|
-
if (vPart.startsWith(">")) {
|
|
1962
|
-
if (!isPropertyBlockLeaderOnly(vPart, ">")) {
|
|
1963
|
-
throw new Error("Expected newline after block leader in property");
|
|
1964
|
-
}
|
|
1965
|
-
const [bytes, j] = parseBlockBytes(tokens, i, ctx, "", baseIndent);
|
|
1966
|
-
obj[k] = bytes;
|
|
1967
|
-
i = j;
|
|
1968
|
-
continue;
|
|
3710
|
+
function ysonToCapData(yson, options = {}) {
|
|
3711
|
+
const decodeReference = options.decodeReference;
|
|
3712
|
+
if (decodeReference !== undefined && typeof decodeReference !== "function") {
|
|
3713
|
+
throw new TypeError("decodeReference must be a function");
|
|
3714
|
+
}
|
|
3715
|
+
/** @type {unknown[]} */
|
|
3716
|
+
const slots = [];
|
|
3717
|
+
const index = new Map();
|
|
3718
|
+
const parsed = JSON.parse(yson);
|
|
3719
|
+
checkWellFormedDeep(parsed);
|
|
3720
|
+
const body = mapDollars(parsed, (s, path) => {
|
|
3721
|
+
const uri = s.slice(1);
|
|
3722
|
+
if (!isReferenceStart(`<${uri}`)) {
|
|
3723
|
+
throw new Error(
|
|
3724
|
+
`A reference needs a scheme: ${JSON.stringify(s)} at ${path}`,
|
|
3725
|
+
);
|
|
1969
3726
|
}
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
3727
|
+
const text = `<${uri}>`;
|
|
3728
|
+
let consumed;
|
|
3729
|
+
try {
|
|
3730
|
+
[, consumed] = parseReference(text, {}, 0, 0);
|
|
3731
|
+
} catch (error) {
|
|
3732
|
+
throw new Error(`${/** @type {Error} */ (error).message} at ${path}`);
|
|
1974
3733
|
}
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
// the list leader.
|
|
1979
|
-
const [body, next] = parseBlockStringWithIndent(
|
|
1980
|
-
tokens,
|
|
1981
|
-
i,
|
|
1982
|
-
"",
|
|
1983
|
-
true,
|
|
1984
|
-
indent,
|
|
3734
|
+
if (consumed !== text.length) {
|
|
3735
|
+
throw new Error(
|
|
3736
|
+
`Unexpected character ">" in reference: ${JSON.stringify(s)} at ${path}`,
|
|
1985
3737
|
);
|
|
1986
|
-
obj[k] = body;
|
|
1987
|
-
i = next;
|
|
1988
|
-
continue;
|
|
1989
3738
|
}
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
i++;
|
|
1997
|
-
const nextT = tokens[i];
|
|
1998
|
-
if (nextT && nextT.type === "start" && nextT.text === "-") {
|
|
1999
|
-
// Pass baseIndent as minIndent so nested arrays stop at the object's level
|
|
2000
|
-
const [arr, next] = parseListArray(tokens, i, ctx, baseIndent);
|
|
2001
|
-
obj[k] = arr;
|
|
2002
|
-
i = next;
|
|
2003
|
-
continue;
|
|
2004
|
-
}
|
|
2005
|
-
// Note: "key: <" without closing ">" is invalid - inline byte arrays must be closed on the same line
|
|
2006
|
-
// Note: A line with just '"' is not valid - block strings use backtick (`)
|
|
2007
|
-
if (nextT && nextT.type === "text" && (nextT.indent ?? 0) > baseIndent) {
|
|
2008
|
-
const [child, next] = parseObjectBlock(
|
|
2009
|
-
tokens,
|
|
2010
|
-
i,
|
|
2011
|
-
nextT.indent ?? 0,
|
|
2012
|
-
ctx,
|
|
3739
|
+
let slot = index.get(uri);
|
|
3740
|
+
if (slot === undefined) {
|
|
3741
|
+
if (typeof decodeReference !== "function") {
|
|
3742
|
+
throw new Error(
|
|
3743
|
+
`Reference ${JSON.stringify(uri)} needs a decodeReference hook ` +
|
|
3744
|
+
`at ${path}`,
|
|
2013
3745
|
);
|
|
2014
|
-
obj[k] = child;
|
|
2015
|
-
i = next;
|
|
2016
|
-
continue;
|
|
2017
3746
|
}
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
3747
|
+
slot = slots.length;
|
|
3748
|
+
index.set(uri, slot);
|
|
3749
|
+
slots.push(decodeReference(uri));
|
|
2021
3750
|
}
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
}
|
|
2026
|
-
return [obj, i];
|
|
3751
|
+
return `$${slot}`;
|
|
3752
|
+
});
|
|
3753
|
+
return { body: `#${stringifyJson(body)}`, slots };
|
|
2027
3754
|
}
|
|
2028
3755
|
|
|
2029
|
-
// Root as object (multiple key: value lines at indent 0)
|
|
2030
3756
|
/**
|
|
2031
|
-
*
|
|
2032
|
-
*
|
|
2033
|
-
*
|
|
2034
|
-
*
|
|
3757
|
+
* From CapData to YSON: the reverse of ysonToCapData. Each `$n` (with or
|
|
3758
|
+
* without smallcaps' interface, `$n.Alleged: x`, which YAY does not keep)
|
|
3759
|
+
* becomes `"$" + slots[n]`. A slot that is already URI text is checked and
|
|
3760
|
+
* used; any other slot is the caller's value, which the encode hook turns
|
|
3761
|
+
* back into URI text. URI text is checked after the hook returns. A body
|
|
3762
|
+
* with promises (`&n`) is refused.
|
|
3763
|
+
* @param {{body: string, slots: unknown[]}} capData
|
|
3764
|
+
* @param {{encodeReference?: (value: unknown) => string}=} options
|
|
3765
|
+
* @returns {string}
|
|
2035
3766
|
*/
|
|
2036
|
-
function
|
|
2037
|
-
const
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
while (
|
|
2070
|
-
i < tokens.length &&
|
|
2071
|
-
(tokens[i].type === "break" || tokens[i].type === "stop")
|
|
2072
|
-
)
|
|
2073
|
-
i++;
|
|
2074
|
-
const nextT = tokens[i];
|
|
2075
|
-
if (nextT && nextT.type === "text" && nextT.text === "`") {
|
|
2076
|
-
// Empty block string (just opening and closing backticks) is not allowed
|
|
2077
|
-
throw new Error(
|
|
2078
|
-
'Empty block string not allowed (use "" or "\\n" explicitly)',
|
|
2079
|
-
);
|
|
2080
|
-
} else {
|
|
2081
|
-
const withIndent = [];
|
|
2082
|
-
while (
|
|
2083
|
-
i < tokens.length &&
|
|
2084
|
-
((tokens[i].type === "text" &&
|
|
2085
|
-
(tokens[i].indent ?? 0) > baseIndent) ||
|
|
2086
|
-
tokens[i].type === "break")
|
|
2087
|
-
) {
|
|
2088
|
-
if (tokens[i].type === "break") {
|
|
2089
|
-
withIndent.push({ indent: undefined, text: "" });
|
|
2090
|
-
i++;
|
|
2091
|
-
} else {
|
|
2092
|
-
withIndent.push({
|
|
2093
|
-
indent: tokens[i].indent ?? 0,
|
|
2094
|
-
text: tokens[i].text,
|
|
2095
|
-
});
|
|
2096
|
-
i++;
|
|
2097
|
-
}
|
|
2098
|
-
}
|
|
2099
|
-
const minIndent = withIndent
|
|
2100
|
-
.filter((x) => x.indent !== undefined)
|
|
2101
|
-
.reduce((min, x) => (x.indent < min ? x.indent : min), Infinity);
|
|
2102
|
-
const effectiveMin = minIndent === Infinity ? 0 : minIndent;
|
|
2103
|
-
const bodyLines = withIndent.map(({ indent, text }) =>
|
|
2104
|
-
indent === undefined
|
|
2105
|
-
? ""
|
|
2106
|
-
: (indent - effectiveMin > 0
|
|
2107
|
-
? " ".repeat(indent - effectiveMin)
|
|
2108
|
-
: "") + text,
|
|
2109
|
-
);
|
|
2110
|
-
let endLine = bodyLines.length;
|
|
2111
|
-
while (endLine > 0 && bodyLines[endLine - 1] === "") endLine--;
|
|
2112
|
-
const trimmedLines = bodyLines.slice(0, endLine);
|
|
2113
|
-
obj[k] =
|
|
2114
|
-
trimmedLines.join("\n") + (trimmedLines.length > 0 ? "\n" : "");
|
|
2115
|
-
}
|
|
2116
|
-
} else if (vPart === "") {
|
|
2117
|
-
const [valueObj, next] = parseObjectOrNamedArray(tokens, i, k, ctx);
|
|
2118
|
-
obj[k] = valueObj[k];
|
|
2119
|
-
i = next;
|
|
2120
|
-
} else {
|
|
2121
|
-
// Inline value (scalar, array, object, bytes)
|
|
2122
|
-
obj[k] = parseScalar(vPart, ctx, t.lineNum ?? 0, keyValue.valueCol);
|
|
2123
|
-
i++;
|
|
2124
|
-
}
|
|
2125
|
-
} else {
|
|
2126
|
-
i++;
|
|
2127
|
-
}
|
|
3767
|
+
function capDataToYson({ body, slots }, options = {}) {
|
|
3768
|
+
const encodeReference = options.encodeReference;
|
|
3769
|
+
if (encodeReference !== undefined && typeof encodeReference !== "function") {
|
|
3770
|
+
throw new TypeError("encodeReference must be a function");
|
|
3771
|
+
}
|
|
3772
|
+
if (!body.startsWith("#")) {
|
|
3773
|
+
throw new Error("Expected a smallcaps body, beginning with #");
|
|
3774
|
+
}
|
|
3775
|
+
const value = JSON.parse(body.slice(1));
|
|
3776
|
+
// A string, key, tag or symbol name with a lone surrogate has no YAY form
|
|
3777
|
+
checkWellFormedDeep(value);
|
|
3778
|
+
const check = (v) => {
|
|
3779
|
+
if (typeof v === "string" && v.startsWith("&")) {
|
|
3780
|
+
throw new Error(`A promise has no YAY form: ${JSON.stringify(v)}`);
|
|
3781
|
+
}
|
|
3782
|
+
if (Array.isArray(v)) v.forEach(check);
|
|
3783
|
+
else if (v !== null && typeof v === "object") {
|
|
3784
|
+
Object.values(v).forEach(check);
|
|
3785
|
+
}
|
|
3786
|
+
};
|
|
3787
|
+
check(value);
|
|
3788
|
+
const yson = mapDollars(value, (s) => {
|
|
3789
|
+
const match = /^\$(0|[1-9][0-9]*)(\..*)?$/.exec(s);
|
|
3790
|
+
const n = match ? Number(match[1]) : -1;
|
|
3791
|
+
if (n < 0 || n >= slots.length) {
|
|
3792
|
+
throw new Error(`No slot for ${JSON.stringify(s)}`);
|
|
3793
|
+
}
|
|
3794
|
+
const slot = slots[n];
|
|
3795
|
+
let uri;
|
|
3796
|
+
if (typeof encodeReference === "function") {
|
|
3797
|
+
uri = encodeReference(slot);
|
|
3798
|
+
} else if (typeof slot === "string") {
|
|
3799
|
+
uri = slot;
|
|
2128
3800
|
} else {
|
|
2129
|
-
|
|
3801
|
+
throw new Error(`Reference slot ${n} needs an encodeReference hook`);
|
|
2130
3802
|
}
|
|
2131
|
-
|
|
2132
|
-
|
|
3803
|
+
if (typeof uri !== "string") {
|
|
3804
|
+
throw new TypeError(`encodeReference must return a string for slot ${n}`);
|
|
3805
|
+
}
|
|
3806
|
+
// A slot is a reference's text, as YSON's "$" strings are
|
|
3807
|
+
checkReferenceText(uri);
|
|
3808
|
+
return `$${uri}`;
|
|
3809
|
+
});
|
|
3810
|
+
return stringifyJson(yson);
|
|
2133
3811
|
}
|
|
2134
3812
|
|
|
2135
|
-
export {
|
|
3813
|
+
export {
|
|
3814
|
+
parseYay,
|
|
3815
|
+
isWellFormedString,
|
|
3816
|
+
symbol,
|
|
3817
|
+
isSymbol,
|
|
3818
|
+
tagged,
|
|
3819
|
+
isTagged,
|
|
3820
|
+
reference,
|
|
3821
|
+
isReference,
|
|
3822
|
+
Reference,
|
|
3823
|
+
ysonToCapData,
|
|
3824
|
+
capDataToYson,
|
|
3825
|
+
};
|