@intellectual-frontiers/formats 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/archive.d.ts +147 -0
- package/dist/archive.js +765 -0
- package/dist/base64.d.ts +24 -0
- package/dist/base64.js +52 -0
- package/dist/cbor.d.ts +39 -0
- package/dist/cbor.js +244 -0
- package/dist/csv.d.ts +38 -0
- package/dist/csv.js +130 -0
- package/dist/der.d.ts +67 -0
- package/dist/der.js +221 -0
- package/dist/glob.d.ts +64 -0
- package/dist/glob.js +218 -0
- package/dist/hex.d.ts +17 -0
- package/dist/hex.js +30 -0
- package/dist/http.d.ts +46 -0
- package/dist/http.js +102 -0
- package/dist/json5.d.ts +46 -0
- package/dist/json5.js +291 -0
- package/dist/media-types.d.ts +28 -0
- package/dist/media-types.js +121 -0
- package/dist/mod.d.ts +39 -0
- package/dist/mod.js +31 -0
- package/dist/png.d.ts +124 -0
- package/dist/png.js +607 -0
- package/dist/semver.d.ts +94 -0
- package/dist/semver.js +331 -0
- package/dist/toml.d.ts +51 -0
- package/dist/toml.js +583 -0
- package/dist/ts-literal.d.ts +62 -0
- package/dist/ts-literal.js +173 -0
- package/dist/xml.d.ts +73 -0
- package/dist/xml.js +282 -0
- package/dist/yaml.d.ts +44 -0
- package/dist/yaml.js +706 -0
- package/package.json +118 -4
- package/src/archive.ts +831 -0
- package/src/base64.ts +51 -0
- package/src/cbor.ts +235 -0
- package/src/csv.ts +132 -0
- package/src/der.ts +224 -0
- package/src/glob.ts +210 -0
- package/src/hex.ts +28 -0
- package/src/http.ts +104 -0
- package/src/json5.ts +267 -0
- package/src/media-types.ts +127 -0
- package/src/mod.ts +102 -0
- package/src/png.ts +625 -0
- package/src/semver.ts +373 -0
- package/src/toml.ts +525 -0
- package/src/ts-literal.ts +183 -0
- package/src/xml.ts +291 -0
- package/src/yaml.ts +616 -0
package/dist/yaml.js
ADDED
|
@@ -0,0 +1,706 @@
|
|
|
1
|
+
import { err, ok } from "@intellectual-frontiers/core";
|
|
2
|
+
/** A YAML error as one line: `3:14: anchors and aliases are not read`. */
|
|
3
|
+
export function formatYamlError(e) {
|
|
4
|
+
return `${e.line}:${e.column}: ${e.message}`;
|
|
5
|
+
}
|
|
6
|
+
class Stop extends Error {
|
|
7
|
+
at;
|
|
8
|
+
constructor(at, message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.at = at;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const NULL = /^(?:~|null|Null|NULL|)$/;
|
|
14
|
+
const BOOL = /^(?:true|True|TRUE|false|False|FALSE)$/;
|
|
15
|
+
const INT = /^[-+]?[0-9]+$/;
|
|
16
|
+
const OCT = /^0o[0-7]+$/;
|
|
17
|
+
const HEX = /^0x[0-9a-fA-F]+$/;
|
|
18
|
+
const FLOAT = /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)(?:[eE][-+]?[0-9]+)?$/;
|
|
19
|
+
const INF = /^[-+]?(?:\.inf|\.Inf|\.INF)$/;
|
|
20
|
+
const NAN = /^(?:\.nan|\.NaN|\.NAN)$/;
|
|
21
|
+
/** A plain scalar resolved by YAML 1.2's core schema. */
|
|
22
|
+
export function resolveCore(s) {
|
|
23
|
+
if (NULL.test(s))
|
|
24
|
+
return null;
|
|
25
|
+
if (BOOL.test(s))
|
|
26
|
+
return s.toLowerCase() === "true";
|
|
27
|
+
if (INT.test(s))
|
|
28
|
+
return Number(s);
|
|
29
|
+
if (OCT.test(s))
|
|
30
|
+
return Number.parseInt(s.slice(2), 8);
|
|
31
|
+
if (HEX.test(s))
|
|
32
|
+
return Number.parseInt(s.slice(2), 16);
|
|
33
|
+
if (FLOAT.test(s))
|
|
34
|
+
return Number(s);
|
|
35
|
+
if (INF.test(s))
|
|
36
|
+
return s.startsWith("-") ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
|
|
37
|
+
if (NAN.test(s))
|
|
38
|
+
return Number.NaN;
|
|
39
|
+
return s;
|
|
40
|
+
}
|
|
41
|
+
const DOUBLE_ESCAPES = {
|
|
42
|
+
"0": "\0",
|
|
43
|
+
a: "\x07",
|
|
44
|
+
b: "\b",
|
|
45
|
+
t: "\t",
|
|
46
|
+
"\t": "\t",
|
|
47
|
+
n: "\n",
|
|
48
|
+
v: "\v",
|
|
49
|
+
f: "\f",
|
|
50
|
+
r: "\r",
|
|
51
|
+
e: "\x1b",
|
|
52
|
+
" ": " ",
|
|
53
|
+
'"': '"',
|
|
54
|
+
"/": "/",
|
|
55
|
+
"\\": "\\",
|
|
56
|
+
N: "\x85",
|
|
57
|
+
_: "\xa0",
|
|
58
|
+
L: "\u2028",
|
|
59
|
+
P: "\u2029",
|
|
60
|
+
};
|
|
61
|
+
/** A YAML text read as a JSON value (an empty document is `null`), or where and why it is not YAML this reader takes. */
|
|
62
|
+
export function parseYaml(text, options = {}) {
|
|
63
|
+
const failsafe = options.schema === "failsafe";
|
|
64
|
+
const src = text.startsWith("\ufeff") ? text.slice(1) : text;
|
|
65
|
+
let i = 0;
|
|
66
|
+
const fail = (message, at = i) => {
|
|
67
|
+
throw new Stop(at, message);
|
|
68
|
+
};
|
|
69
|
+
const peek = (n = 0) => src[i + n] ?? "";
|
|
70
|
+
const lineStart = (at) => src.lastIndexOf("\n", at - 1) + 1;
|
|
71
|
+
const column = (at = i) => at - lineStart(at);
|
|
72
|
+
const atLineEnd = () => peek() === "" || peek() === "\n" || (peek() === "\r" && peek(1) === "\n");
|
|
73
|
+
const eatLineEnd = () => {
|
|
74
|
+
if (peek() === "\r")
|
|
75
|
+
i++;
|
|
76
|
+
if (peek() === "\n")
|
|
77
|
+
i++;
|
|
78
|
+
};
|
|
79
|
+
const spaces = () => {
|
|
80
|
+
while (peek() === " " || peek() === "\t")
|
|
81
|
+
i++;
|
|
82
|
+
};
|
|
83
|
+
/** The rest of a line: space, then a comment or nothing. */
|
|
84
|
+
const restOfLine = () => {
|
|
85
|
+
spaces();
|
|
86
|
+
if (peek() === "#")
|
|
87
|
+
while (!atLineEnd())
|
|
88
|
+
i++;
|
|
89
|
+
if (!atLineEnd())
|
|
90
|
+
fail("the end of the line");
|
|
91
|
+
eatLineEnd();
|
|
92
|
+
};
|
|
93
|
+
const marker = (m) => column() === 0 && src.startsWith(m, i) && /^[ \t\r\n]?$/.test(src[i + 3] ?? "");
|
|
94
|
+
/** Past blank lines and comment lines, to the first character of the next content (or the end); refusing a tab in indentation. */
|
|
95
|
+
const toContent = () => {
|
|
96
|
+
for (;;) {
|
|
97
|
+
const start = i;
|
|
98
|
+
while (peek() === " ")
|
|
99
|
+
i++;
|
|
100
|
+
if (peek() === "\t") {
|
|
101
|
+
let j = i;
|
|
102
|
+
while (src[j] === " " || src[j] === "\t")
|
|
103
|
+
j++;
|
|
104
|
+
if (src[j] === "\n" || src[j] === "\r" || src[j] === "#" || src[j] === undefined)
|
|
105
|
+
i = j;
|
|
106
|
+
else
|
|
107
|
+
fail("spaces for indentation, not a tab");
|
|
108
|
+
}
|
|
109
|
+
if (peek() === "#")
|
|
110
|
+
while (!atLineEnd())
|
|
111
|
+
i++;
|
|
112
|
+
if (peek() === "" || !atLineEnd()) {
|
|
113
|
+
if (peek() !== "")
|
|
114
|
+
i = start + (i - start);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
eatLineEnd();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const refuseSpecial = () => {
|
|
121
|
+
const c = peek();
|
|
122
|
+
if (c === "&" || c === "*")
|
|
123
|
+
fail("no anchor or alias (they are not read, so no value is ever repeated or expanded)");
|
|
124
|
+
if (c === "!")
|
|
125
|
+
fail("no tag (a value's type comes from the schema)");
|
|
126
|
+
if (c === "?" && /[ \t\r\n]/.test(peek(1) || "\n"))
|
|
127
|
+
fail("a simple key (complex keys are not read)");
|
|
128
|
+
if (c === "%" && column() === 0)
|
|
129
|
+
fail("no directive");
|
|
130
|
+
if (c === "@" || c === "`")
|
|
131
|
+
fail(`no ${c} at the start of a plain scalar (it is reserved)`);
|
|
132
|
+
};
|
|
133
|
+
const hex = (n) => {
|
|
134
|
+
const h = src.slice(i, i + n);
|
|
135
|
+
if (h.length !== n || !/^[0-9a-fA-F]+$/.test(h))
|
|
136
|
+
fail(`${n} hexadecimal digits`);
|
|
137
|
+
i += n;
|
|
138
|
+
return String.fromCodePoint(Number.parseInt(h, 16));
|
|
139
|
+
};
|
|
140
|
+
/** Line breaks inside a quoted scalar folded: one break is a space, each further empty line a line break. */
|
|
141
|
+
const foldQuoted = () => {
|
|
142
|
+
let breaks = 0;
|
|
143
|
+
while (atLineEnd() && peek() !== "") {
|
|
144
|
+
eatLineEnd();
|
|
145
|
+
breaks++;
|
|
146
|
+
spaces();
|
|
147
|
+
}
|
|
148
|
+
return breaks === 1 ? " " : "\n".repeat(breaks - 1);
|
|
149
|
+
};
|
|
150
|
+
const trimEnd = (s) => s.replace(/[ \t]+$/, "");
|
|
151
|
+
const doubleQuoted = () => {
|
|
152
|
+
i++;
|
|
153
|
+
let out = "";
|
|
154
|
+
for (;;) {
|
|
155
|
+
const c = peek();
|
|
156
|
+
if (c === "")
|
|
157
|
+
fail("the closing quote");
|
|
158
|
+
if (c === '"') {
|
|
159
|
+
i++;
|
|
160
|
+
return out;
|
|
161
|
+
}
|
|
162
|
+
if (c === "\\") {
|
|
163
|
+
i++;
|
|
164
|
+
const e = peek();
|
|
165
|
+
if (atLineEnd() && e !== "") {
|
|
166
|
+
eatLineEnd();
|
|
167
|
+
spaces();
|
|
168
|
+
while (atLineEnd() && peek() !== "") {
|
|
169
|
+
eatLineEnd();
|
|
170
|
+
out += "\n";
|
|
171
|
+
spaces();
|
|
172
|
+
}
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
i++;
|
|
176
|
+
const simple = DOUBLE_ESCAPES[e];
|
|
177
|
+
if (simple !== undefined)
|
|
178
|
+
out += simple;
|
|
179
|
+
else if (e === "x")
|
|
180
|
+
out += hex(2);
|
|
181
|
+
else if (e === "u")
|
|
182
|
+
out += hex(4);
|
|
183
|
+
else if (e === "U")
|
|
184
|
+
out += hex(8);
|
|
185
|
+
else
|
|
186
|
+
fail(`a known escape, not \\${e}`, i - 2);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (atLineEnd()) {
|
|
190
|
+
out = trimEnd(out) + foldQuoted();
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
out += c;
|
|
194
|
+
i++;
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
const singleQuoted = () => {
|
|
198
|
+
i++;
|
|
199
|
+
let out = "";
|
|
200
|
+
for (;;) {
|
|
201
|
+
const c = peek();
|
|
202
|
+
if (c === "")
|
|
203
|
+
fail("the closing quote");
|
|
204
|
+
if (c === "'") {
|
|
205
|
+
if (peek(1) === "'") {
|
|
206
|
+
out += "'";
|
|
207
|
+
i += 2;
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
i++;
|
|
211
|
+
return out;
|
|
212
|
+
}
|
|
213
|
+
if (atLineEnd()) {
|
|
214
|
+
out = trimEnd(out) + foldQuoted();
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
out += c;
|
|
218
|
+
i++;
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
const scalarOf = (s) => (failsafe ? s : resolveCore(s));
|
|
222
|
+
/** A plain scalar: in flow, up to `,[]{}`; in block, folded over lines indented more than `parent`. */
|
|
223
|
+
const plain = (parent, flow) => {
|
|
224
|
+
refuseSpecial();
|
|
225
|
+
const first = peek();
|
|
226
|
+
if (first === "-" && /[ \t\r\n]/.test(peek(1) || "\n"))
|
|
227
|
+
fail("a value, not a sequence entry here");
|
|
228
|
+
if (/[|>]/.test(first))
|
|
229
|
+
fail("a block scalar only after a key or a dash");
|
|
230
|
+
if (/[,\]}]/.test(first) && flow)
|
|
231
|
+
fail("a value");
|
|
232
|
+
let out = "";
|
|
233
|
+
for (;;) {
|
|
234
|
+
let line = "";
|
|
235
|
+
for (;;) {
|
|
236
|
+
const c = peek();
|
|
237
|
+
if (atLineEnd())
|
|
238
|
+
break;
|
|
239
|
+
if (c === ":" && (/[ \t\r\n]/.test(peek(1) || "\n") || (flow && /[,\]}[{]/.test(peek(1)))))
|
|
240
|
+
break;
|
|
241
|
+
if (c === "#" && /[ \t]$/.test(line))
|
|
242
|
+
break;
|
|
243
|
+
if (flow && /[,[\]{}]/.test(c))
|
|
244
|
+
break;
|
|
245
|
+
line += c;
|
|
246
|
+
i++;
|
|
247
|
+
}
|
|
248
|
+
out += trimEnd(line);
|
|
249
|
+
if (peek() === "#" || peek() === ":" || (flow && /[,[\]{}]/.test(peek())) || !atLineEnd() || peek() === "")
|
|
250
|
+
return out;
|
|
251
|
+
const save = i;
|
|
252
|
+
let breaks = 0;
|
|
253
|
+
while (atLineEnd() && peek() !== "") {
|
|
254
|
+
eatLineEnd();
|
|
255
|
+
breaks++;
|
|
256
|
+
while (peek() === " " || peek() === "\t")
|
|
257
|
+
i++;
|
|
258
|
+
if (peek() === "#") {
|
|
259
|
+
i = save;
|
|
260
|
+
return out;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (peek() === "" || (!flow && column() <= parent) || marker("---") || marker("...")) {
|
|
264
|
+
i = save;
|
|
265
|
+
return out;
|
|
266
|
+
}
|
|
267
|
+
if (!flow && peek() === "-" && /[ \t\r\n]/.test(peek(1) || "\n") && column() <= parent + 1) {
|
|
268
|
+
i = save;
|
|
269
|
+
return out;
|
|
270
|
+
}
|
|
271
|
+
out += breaks === 1 ? " " : "\n".repeat(breaks - 1);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
const blockScalar = (parent) => {
|
|
275
|
+
const literal = peek() === "|";
|
|
276
|
+
i++;
|
|
277
|
+
let chomp = "clip";
|
|
278
|
+
let explicit = 0;
|
|
279
|
+
for (let k = 0; k < 2; k++) {
|
|
280
|
+
const c = peek();
|
|
281
|
+
if (c === "-" || c === "+") {
|
|
282
|
+
chomp = c === "-" ? "strip" : "keep";
|
|
283
|
+
i++;
|
|
284
|
+
}
|
|
285
|
+
else if (/[1-9]/.test(c)) {
|
|
286
|
+
explicit = Number(c);
|
|
287
|
+
i++;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
restOfLine();
|
|
291
|
+
const lines = [];
|
|
292
|
+
let indent = explicit > 0 ? parent + explicit : -1;
|
|
293
|
+
let trailingBreak = false;
|
|
294
|
+
for (;;) {
|
|
295
|
+
if (peek() === "")
|
|
296
|
+
break;
|
|
297
|
+
const start = i;
|
|
298
|
+
let n = 0;
|
|
299
|
+
while (peek() === " ") {
|
|
300
|
+
i++;
|
|
301
|
+
n++;
|
|
302
|
+
}
|
|
303
|
+
if (atLineEnd()) {
|
|
304
|
+
if (indent >= 0 && n > indent)
|
|
305
|
+
lines.push(src.slice(start + indent, i));
|
|
306
|
+
else
|
|
307
|
+
lines.push("");
|
|
308
|
+
if (peek() === "") {
|
|
309
|
+
i = start + n;
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
eatLineEnd();
|
|
313
|
+
trailingBreak = true;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
if (indent < 0) {
|
|
317
|
+
if (n <= parent) {
|
|
318
|
+
i = start;
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
indent = n;
|
|
322
|
+
}
|
|
323
|
+
if (n < indent || ((marker("---") || marker("...")) && n === 0)) {
|
|
324
|
+
i = start;
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
let end = i;
|
|
328
|
+
while (!atLineEnd())
|
|
329
|
+
end = ++i;
|
|
330
|
+
lines.push(src.slice(start + indent, end));
|
|
331
|
+
trailingBreak = peek() !== "";
|
|
332
|
+
eatLineEnd();
|
|
333
|
+
}
|
|
334
|
+
let last = lines.length;
|
|
335
|
+
while (last > 0 && lines[last - 1] === "")
|
|
336
|
+
last--;
|
|
337
|
+
const content = lines.slice(0, last);
|
|
338
|
+
const trailing = lines.length - last;
|
|
339
|
+
let body;
|
|
340
|
+
if (literal)
|
|
341
|
+
body = content.join("\n");
|
|
342
|
+
else {
|
|
343
|
+
body = "";
|
|
344
|
+
let empty = 0;
|
|
345
|
+
let moreIndented = false;
|
|
346
|
+
let started = false;
|
|
347
|
+
for (const line of content) {
|
|
348
|
+
if (line === "") {
|
|
349
|
+
empty++;
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
const more = line.startsWith(" ") || line.startsWith("\t");
|
|
353
|
+
if (!started)
|
|
354
|
+
body += "\n".repeat(empty);
|
|
355
|
+
else if (more || moreIndented)
|
|
356
|
+
body += "\n".repeat(empty + 1);
|
|
357
|
+
else
|
|
358
|
+
body += empty === 0 ? " " : "\n".repeat(empty);
|
|
359
|
+
body += line;
|
|
360
|
+
moreIndented = more;
|
|
361
|
+
started = true;
|
|
362
|
+
empty = 0;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
if (content.length === 0)
|
|
366
|
+
return chomp === "keep" ? "\n".repeat(trailing + (trailingBreak && trailing === 0 ? 1 : 0)) : "";
|
|
367
|
+
switch (chomp) {
|
|
368
|
+
case "strip":
|
|
369
|
+
return body;
|
|
370
|
+
case "clip":
|
|
371
|
+
return trailingBreak || trailing > 0 ? `${body}\n` : body;
|
|
372
|
+
case "keep":
|
|
373
|
+
return `${body}${"\n".repeat(trailing + (trailingBreak ? 1 : 0))}`;
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
const define = (o, k, v, at) => {
|
|
377
|
+
if (Object.hasOwn(o, k))
|
|
378
|
+
fail(`the key ${JSON.stringify(k)} only once`, at);
|
|
379
|
+
Object.defineProperty(o, k, { value: v, enumerable: true, writable: true, configurable: true });
|
|
380
|
+
};
|
|
381
|
+
const keyText = (k) => typeof k === "string" ? k : k === null ? "null" : typeof k === "number" || typeof k === "boolean" ? String(k) : fail("a scalar key");
|
|
382
|
+
/** Space, line breaks and comments inside a flow collection. */
|
|
383
|
+
const flowGap = () => {
|
|
384
|
+
for (;;) {
|
|
385
|
+
spaces();
|
|
386
|
+
if (peek() === "#")
|
|
387
|
+
while (!atLineEnd())
|
|
388
|
+
i++;
|
|
389
|
+
if (atLineEnd() && peek() !== "")
|
|
390
|
+
eatLineEnd();
|
|
391
|
+
else
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
const flowNode = (depth) => {
|
|
396
|
+
if (depth > 256)
|
|
397
|
+
fail("less nesting (more than 256 levels)");
|
|
398
|
+
flowGap();
|
|
399
|
+
const c = peek();
|
|
400
|
+
if (c === "[") {
|
|
401
|
+
i++;
|
|
402
|
+
const out = [];
|
|
403
|
+
for (;;) {
|
|
404
|
+
flowGap();
|
|
405
|
+
if (peek() === "]") {
|
|
406
|
+
i++;
|
|
407
|
+
return out;
|
|
408
|
+
}
|
|
409
|
+
out.push(flowNode(depth + 1));
|
|
410
|
+
flowGap();
|
|
411
|
+
if (peek() === ":")
|
|
412
|
+
fail("no mapping inside a flow sequence");
|
|
413
|
+
if (peek() === ",")
|
|
414
|
+
i++;
|
|
415
|
+
else if (peek() !== "]")
|
|
416
|
+
fail("a comma or ]");
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (c === "{") {
|
|
420
|
+
i++;
|
|
421
|
+
const out = {};
|
|
422
|
+
for (;;) {
|
|
423
|
+
flowGap();
|
|
424
|
+
if (peek() === "}") {
|
|
425
|
+
i++;
|
|
426
|
+
return out;
|
|
427
|
+
}
|
|
428
|
+
const at = i;
|
|
429
|
+
const k = keyText(flowNode(depth + 1));
|
|
430
|
+
flowGap();
|
|
431
|
+
let v = null;
|
|
432
|
+
if (peek() === ":") {
|
|
433
|
+
i++;
|
|
434
|
+
flowGap();
|
|
435
|
+
if (peek() !== "," && peek() !== "}")
|
|
436
|
+
v = flowNode(depth + 1);
|
|
437
|
+
}
|
|
438
|
+
define(out, k, v, at);
|
|
439
|
+
flowGap();
|
|
440
|
+
if (peek() === ",")
|
|
441
|
+
i++;
|
|
442
|
+
else if (peek() !== "}")
|
|
443
|
+
fail("a comma or }");
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (c === '"')
|
|
447
|
+
return doubleQuoted();
|
|
448
|
+
if (c === "'")
|
|
449
|
+
return singleQuoted();
|
|
450
|
+
return scalarOf(plain(-1, true));
|
|
451
|
+
};
|
|
452
|
+
/** Whether a mapping key starts here: a scalar followed, on the same line, by `:` and space or the line's end. */
|
|
453
|
+
const startsKey = () => {
|
|
454
|
+
const save = i;
|
|
455
|
+
try {
|
|
456
|
+
const c = peek();
|
|
457
|
+
if (c === '"')
|
|
458
|
+
doubleQuoted();
|
|
459
|
+
else if (c === "'")
|
|
460
|
+
singleQuoted();
|
|
461
|
+
else if (c === "[" || c === "{")
|
|
462
|
+
return false;
|
|
463
|
+
else {
|
|
464
|
+
if (c === "-" && /[ \t\r\n]/.test(peek(1) || "\n"))
|
|
465
|
+
return false;
|
|
466
|
+
if (/[&*!|>%@`]/.test(c) || (c === "?" && /[ \t\r\n]/.test(peek(1) || "\n"))) {
|
|
467
|
+
for (let j = i; j < src.length && src[j] !== "\n"; j++)
|
|
468
|
+
if (src[j] === ":" && /[ \t\r\n]/.test(src[j + 1] ?? "\n"))
|
|
469
|
+
return true;
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
while (!atLineEnd()) {
|
|
473
|
+
if (peek() === ":" && /[ \t\r\n]/.test(peek(1) || "\n"))
|
|
474
|
+
return true;
|
|
475
|
+
if (peek() === "#" && /[ \t]/.test(src[i - 1] ?? ""))
|
|
476
|
+
return false;
|
|
477
|
+
i++;
|
|
478
|
+
}
|
|
479
|
+
return false;
|
|
480
|
+
}
|
|
481
|
+
spaces();
|
|
482
|
+
return peek() === ":" && /[ \t\r\n]/.test(peek(1) || "\n");
|
|
483
|
+
}
|
|
484
|
+
catch {
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
finally {
|
|
488
|
+
i = save;
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
/** A value on the line of its key or dash, or below it: `parent` is the indentation of that key or dash. */
|
|
492
|
+
const valueAfter = (parent, depth, seqAtParent) => {
|
|
493
|
+
spaces();
|
|
494
|
+
if (peek() === "#" || atLineEnd()) {
|
|
495
|
+
restOfLine();
|
|
496
|
+
toContent();
|
|
497
|
+
if (peek() === "" || marker("---") || marker("..."))
|
|
498
|
+
return null;
|
|
499
|
+
const col = column();
|
|
500
|
+
if (col > parent)
|
|
501
|
+
return blockNode(col, depth + 1);
|
|
502
|
+
if (seqAtParent && col === parent && peek() === "-" && /[ \t\r\n]/.test(peek(1) || "\n"))
|
|
503
|
+
return sequence(col, depth + 1);
|
|
504
|
+
return null;
|
|
505
|
+
}
|
|
506
|
+
const c = peek();
|
|
507
|
+
let v;
|
|
508
|
+
if (c === "|" || c === ">")
|
|
509
|
+
return blockScalar(parent);
|
|
510
|
+
if (c === "[" || c === "{")
|
|
511
|
+
v = flowNode(depth + 1);
|
|
512
|
+
else if (c === '"')
|
|
513
|
+
v = doubleQuoted();
|
|
514
|
+
else if (c === "'")
|
|
515
|
+
v = singleQuoted();
|
|
516
|
+
else {
|
|
517
|
+
if (startsKey())
|
|
518
|
+
fail("a value, not another key on the same line");
|
|
519
|
+
v = scalarOf(plain(parent, false));
|
|
520
|
+
}
|
|
521
|
+
restOfLine();
|
|
522
|
+
return v;
|
|
523
|
+
};
|
|
524
|
+
const sequence = (col, depth) => {
|
|
525
|
+
const out = [];
|
|
526
|
+
for (;;) {
|
|
527
|
+
i++;
|
|
528
|
+
if (peek() === " " || peek() === "\t") {
|
|
529
|
+
spaces();
|
|
530
|
+
if (!(peek() === "#" || atLineEnd())) {
|
|
531
|
+
const inner = column();
|
|
532
|
+
if (peek() === "-" && /[ \t\r\n]/.test(peek(1) || "\n"))
|
|
533
|
+
out.push(sequence(inner, depth + 1));
|
|
534
|
+
else if (startsKey())
|
|
535
|
+
out.push(mapping(inner, depth + 1));
|
|
536
|
+
else
|
|
537
|
+
out.push(valueAfter(col, depth, false));
|
|
538
|
+
}
|
|
539
|
+
else
|
|
540
|
+
out.push(valueAfter(col, depth, false));
|
|
541
|
+
}
|
|
542
|
+
else
|
|
543
|
+
out.push(valueAfter(col, depth, false));
|
|
544
|
+
toContent();
|
|
545
|
+
if (peek() === "" || marker("---") || marker("..."))
|
|
546
|
+
return out;
|
|
547
|
+
const next = column();
|
|
548
|
+
if (next > col)
|
|
549
|
+
fail("the indentation of the sequence");
|
|
550
|
+
if (next < col || !(peek() === "-" && /[ \t\r\n]/.test(peek(1) || "\n")))
|
|
551
|
+
return out;
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
const mapping = (col, depth) => {
|
|
555
|
+
const out = {};
|
|
556
|
+
for (;;) {
|
|
557
|
+
refuseSpecial();
|
|
558
|
+
const at = i;
|
|
559
|
+
const c = peek();
|
|
560
|
+
const k = c === '"' ? doubleQuoted() : c === "'" ? singleQuoted() : keyText(scalarOf(plain(col, false)));
|
|
561
|
+
spaces();
|
|
562
|
+
if (peek() !== ":")
|
|
563
|
+
fail("a colon after the key");
|
|
564
|
+
i++;
|
|
565
|
+
define(out, k, valueAfter(col, depth, true), at);
|
|
566
|
+
toContent();
|
|
567
|
+
if (peek() === "" || marker("---") || marker("..."))
|
|
568
|
+
return out;
|
|
569
|
+
const next = column();
|
|
570
|
+
if (next > col)
|
|
571
|
+
fail("the indentation of the mapping");
|
|
572
|
+
if (next < col)
|
|
573
|
+
return out;
|
|
574
|
+
if (peek() === "-" && /[ \t\r\n]/.test(peek(1) || "\n"))
|
|
575
|
+
return out;
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
const blockNode = (col, depth) => {
|
|
579
|
+
if (depth > 256)
|
|
580
|
+
fail("less nesting (more than 256 levels)");
|
|
581
|
+
refuseSpecial();
|
|
582
|
+
if (peek() === "-" && /[ \t\r\n]/.test(peek(1) || "\n"))
|
|
583
|
+
return sequence(col, depth);
|
|
584
|
+
if (startsKey())
|
|
585
|
+
return mapping(col, depth);
|
|
586
|
+
const c = peek();
|
|
587
|
+
let v;
|
|
588
|
+
if (c === "[" || c === "{")
|
|
589
|
+
v = flowNode(depth + 1);
|
|
590
|
+
else if (c === '"')
|
|
591
|
+
v = doubleQuoted();
|
|
592
|
+
else if (c === "'")
|
|
593
|
+
v = singleQuoted();
|
|
594
|
+
else if (c === "|" || c === ">")
|
|
595
|
+
return blockScalar(col - 1);
|
|
596
|
+
else
|
|
597
|
+
v = scalarOf(plain(col - 1, false));
|
|
598
|
+
restOfLine();
|
|
599
|
+
return v;
|
|
600
|
+
};
|
|
601
|
+
try {
|
|
602
|
+
toContent();
|
|
603
|
+
if (peek() === "%")
|
|
604
|
+
fail("no directive");
|
|
605
|
+
if (marker("---")) {
|
|
606
|
+
i += 3;
|
|
607
|
+
spaces();
|
|
608
|
+
if (atLineEnd() || peek() === "#") {
|
|
609
|
+
restOfLine();
|
|
610
|
+
toContent();
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
let value = null;
|
|
614
|
+
if (!(peek() === "" || marker("...") || marker("---")))
|
|
615
|
+
value = blockNode(column(), 0);
|
|
616
|
+
toContent();
|
|
617
|
+
if (marker("...")) {
|
|
618
|
+
i += 3;
|
|
619
|
+
restOfLine();
|
|
620
|
+
toContent();
|
|
621
|
+
}
|
|
622
|
+
if (marker("---"))
|
|
623
|
+
fail("one document (more than one is not read)");
|
|
624
|
+
if (peek() !== "")
|
|
625
|
+
fail("the end of the document");
|
|
626
|
+
return ok(value);
|
|
627
|
+
}
|
|
628
|
+
catch (e) {
|
|
629
|
+
if (!(e instanceof Stop))
|
|
630
|
+
throw e;
|
|
631
|
+
const before = src.slice(0, e.at).split(/\r?\n/);
|
|
632
|
+
return err({ kind: "yaml", line: before.length, column: (before.at(-1)?.length ?? 0) + 1, message: `expected ${e.message}` });
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
636
|
+
/** Whether text can be written as a plain scalar and read back as the same text by the core schema. */
|
|
637
|
+
export function isPlainSafe(s) {
|
|
638
|
+
if (s === "" || s !== s.trim() || /[\n\r\t]/.test(s))
|
|
639
|
+
return false;
|
|
640
|
+
if (typeof resolveCore(s) !== "string")
|
|
641
|
+
return false;
|
|
642
|
+
if (/^[-?:,[\]{}#&*!|>'"%@`]/.test(s))
|
|
643
|
+
return false;
|
|
644
|
+
if (/: |:$| #/.test(s))
|
|
645
|
+
return false;
|
|
646
|
+
if (s === "---" || s === "...")
|
|
647
|
+
return false;
|
|
648
|
+
return /^[\x20-\x7e\u00a0-\ud7ff\ue000-\ufffd]*$/.test(s);
|
|
649
|
+
}
|
|
650
|
+
function scalar(v) {
|
|
651
|
+
if (v === null)
|
|
652
|
+
return "null";
|
|
653
|
+
if (typeof v === "boolean")
|
|
654
|
+
return String(v);
|
|
655
|
+
if (typeof v === "number") {
|
|
656
|
+
if (Number.isNaN(v))
|
|
657
|
+
return ".nan";
|
|
658
|
+
if (!Number.isFinite(v))
|
|
659
|
+
return v > 0 ? ".inf" : "-.inf";
|
|
660
|
+
if (Object.is(v, -0))
|
|
661
|
+
return "-0.0";
|
|
662
|
+
return String(v);
|
|
663
|
+
}
|
|
664
|
+
if (typeof v === "string")
|
|
665
|
+
return isPlainSafe(v) ? v : JSON.stringify(v);
|
|
666
|
+
return Array.isArray(v) ? "[]" : "{}";
|
|
667
|
+
}
|
|
668
|
+
const empty = (v) => (Array.isArray(v) ? v.length === 0 : isObject(v) ? Object.keys(v).length === 0 : false);
|
|
669
|
+
function block(v, indent, out) {
|
|
670
|
+
const pad = " ".repeat(indent);
|
|
671
|
+
if (Array.isArray(v)) {
|
|
672
|
+
for (const item of v) {
|
|
673
|
+
if ((Array.isArray(item) || isObject(item)) && !empty(item)) {
|
|
674
|
+
const inner = [];
|
|
675
|
+
block(item, 0, inner);
|
|
676
|
+
out.push(`${pad}- ${(inner[0] ?? "").trimStart()}`);
|
|
677
|
+
for (const line of inner.slice(1))
|
|
678
|
+
out.push(`${pad} ${line}`);
|
|
679
|
+
}
|
|
680
|
+
else
|
|
681
|
+
out.push(`${pad}- ${scalar(item)}`);
|
|
682
|
+
}
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
if (isObject(v)) {
|
|
686
|
+
for (const [k, item] of Object.entries(v)) {
|
|
687
|
+
const key = isPlainSafe(k) ? k : JSON.stringify(k);
|
|
688
|
+
if ((Array.isArray(item) || isObject(item)) && !empty(item)) {
|
|
689
|
+
out.push(`${pad}${key}:`);
|
|
690
|
+
block(item, indent + 1, out);
|
|
691
|
+
}
|
|
692
|
+
else
|
|
693
|
+
out.push(`${pad}${key}: ${scalar(item)}`);
|
|
694
|
+
}
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
out.push(`${pad}${scalar(v)}`);
|
|
698
|
+
}
|
|
699
|
+
/** A value as block YAML, ending in a line break: mappings and sequences by indentation, text plain where it reads back the same, else double-quoted. */
|
|
700
|
+
export function toYaml(value) {
|
|
701
|
+
if ((Array.isArray(value) || isObject(value)) && empty(value))
|
|
702
|
+
return `${scalar(value)}\n`;
|
|
703
|
+
const out = [];
|
|
704
|
+
block(value, 0, out);
|
|
705
|
+
return `${out.join("\n")}\n`;
|
|
706
|
+
}
|