@poe-platform/safe-bash 0.1.508 → 0.1.509

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.
Files changed (45) hide show
  1. package/dist/safe-bash/chunks/chunk-4OKU6JXB.js +5279 -0
  2. package/dist/safe-bash/chunks/chunk-4OKU6JXB.js.map +7 -0
  3. package/dist/safe-bash/chunks/chunk-5HT35GTV.js +520 -0
  4. package/dist/safe-bash/chunks/chunk-5HT35GTV.js.map +7 -0
  5. package/dist/safe-bash/chunks/{chunk-3ISDVRIM.js → chunk-B637PJBS.js} +19 -523
  6. package/dist/safe-bash/chunks/chunk-B637PJBS.js.map +7 -0
  7. package/dist/safe-bash/commands/structured/query-core.d.ts +35 -0
  8. package/dist/safe-bash/commands/structured/query-core.d.ts.map +1 -0
  9. package/dist/safe-bash/commands/structured/query-core.js +588 -0
  10. package/dist/safe-bash/commands/structured/query-core.js.map +1 -0
  11. package/dist/safe-bash/commands/xml/index.browser.js +2 -1
  12. package/dist/safe-bash/commands/yq/accounting.d.ts +64 -0
  13. package/dist/safe-bash/commands/yq/accounting.d.ts.map +1 -0
  14. package/dist/safe-bash/commands/yq/accounting.js +257 -0
  15. package/dist/safe-bash/commands/yq/accounting.js.map +1 -0
  16. package/dist/safe-bash/commands/yq/encoder.d.ts +6 -0
  17. package/dist/safe-bash/commands/yq/encoder.d.ts.map +1 -0
  18. package/dist/safe-bash/commands/yq/encoder.js +240 -0
  19. package/dist/safe-bash/commands/yq/encoder.js.map +1 -0
  20. package/dist/safe-bash/commands/yq/errors.d.ts +15 -0
  21. package/dist/safe-bash/commands/yq/errors.d.ts.map +1 -0
  22. package/dist/safe-bash/commands/yq/errors.js +37 -0
  23. package/dist/safe-bash/commands/yq/errors.js.map +1 -0
  24. package/dist/safe-bash/commands/yq/index.browser.js +12 -0
  25. package/dist/safe-bash/commands/yq/index.browser.js.map +7 -0
  26. package/dist/safe-bash/commands/yq/index.d.ts +9 -0
  27. package/dist/safe-bash/commands/yq/index.d.ts.map +1 -0
  28. package/dist/safe-bash/commands/yq/index.js +719 -0
  29. package/dist/safe-bash/commands/yq/index.js.map +1 -0
  30. package/dist/safe-bash/commands/yq/parser.d.ts +5 -0
  31. package/dist/safe-bash/commands/yq/parser.d.ts.map +1 -0
  32. package/dist/safe-bash/commands/yq/parser.js +1326 -0
  33. package/dist/safe-bash/commands/yq/parser.js.map +1 -0
  34. package/dist/safe-bash/commands/yq/toml.d.ts +5 -0
  35. package/dist/safe-bash/commands/yq/toml.d.ts.map +1 -0
  36. package/dist/safe-bash/commands/yq/toml.js +690 -0
  37. package/dist/safe-bash/commands/yq/toml.js.map +1 -0
  38. package/dist/safe-bash/core.browser.js +221 -2171
  39. package/dist/safe-bash/core.browser.js.map +4 -4
  40. package/dist/safe-bash/core.d.ts +1 -0
  41. package/dist/safe-bash/core.d.ts.map +1 -1
  42. package/dist/safe-bash/core.js +1 -0
  43. package/dist/safe-bash/core.js.map +1 -1
  44. package/package.json +8 -2
  45. package/dist/safe-bash/chunks/chunk-3ISDVRIM.js.map +0 -7
@@ -0,0 +1,1326 @@
1
+ import { object, objectKeys, put, wellFormed } from "../structured/limits.js";
2
+ import { Decimal, isNumber, numberText } from "../structured/numbers.js";
3
+ import { aliasFailure, copyAlias, YqLedger, yqCaps } from "./accounting.js";
4
+ import { YqError, limit } from "./errors.js";
5
+ const integerPattern = /^(?:[+-]?[0-9]+|0o[0-7]+|0x[0-9a-fA-F]+)$/u;
6
+ const floatPattern = /^[+-]?(?:(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?|\.(?:inf|Inf|INF|nan|NaN|NAN))$/u;
7
+ function syntax(line, column) {
8
+ return new YqError("input", "INPUT_YAML_SYNTAX", 5, undefined, line, column);
9
+ }
10
+ function structure(line, column) {
11
+ return new YqError("input", "INPUT_DOCUMENT_STRUCTURE", 5, undefined, line, column);
12
+ }
13
+ function schema(code, line, column) {
14
+ return new YqError("schema", code, 5, undefined, line, column);
15
+ }
16
+ function countCodePoints(text) {
17
+ let count = 0;
18
+ for (const unused of text) {
19
+ void unused;
20
+ count++;
21
+ }
22
+ return count;
23
+ }
24
+ function utf8Width(codePoint) {
25
+ return codePoint <= 0x7f ? 1 : codePoint <= 0x7ff ? 2 : codePoint <= 0xffff ? 3 : 4;
26
+ }
27
+ function compactStringBytes(text) {
28
+ let bytes = 2;
29
+ for (const character of text) {
30
+ const point = character.codePointAt(0);
31
+ if (point === 0x22 || point === 0x5c || point === 0x08 || point === 0x0c || point === 0x0a || point === 0x0d || point === 0x09)
32
+ bytes += 2;
33
+ else if (point < 0x20)
34
+ bytes += 6;
35
+ else
36
+ bytes += Buffer.byteLength(character);
37
+ }
38
+ return bytes;
39
+ }
40
+ function compactScalarBytes(value) {
41
+ if (typeof value === "string")
42
+ return compactStringBytes(value);
43
+ if (isNumber(value))
44
+ return Buffer.byteLength(numberText(value));
45
+ return Buffer.byteLength(JSON.stringify(value));
46
+ }
47
+ function indentation(line) {
48
+ let index = 0;
49
+ while (line[index] === " ")
50
+ index++;
51
+ if (line[index] === "\t")
52
+ throw syntax();
53
+ return index;
54
+ }
55
+ function stripComment(text) {
56
+ let single = false;
57
+ let double = false;
58
+ let escaped = false;
59
+ let flowDepth = 0;
60
+ for (let index = 0; index < text.length; index++) {
61
+ const character = text[index];
62
+ if (double) {
63
+ if (escaped)
64
+ escaped = false;
65
+ else if (character === "\\")
66
+ escaped = true;
67
+ else if (character === '"')
68
+ double = false;
69
+ continue;
70
+ }
71
+ if (single) {
72
+ if (character === "'" && text[index + 1] === "'")
73
+ index++;
74
+ else if (character === "'")
75
+ single = false;
76
+ continue;
77
+ }
78
+ if (character === '"')
79
+ double = true;
80
+ else if (character === "'")
81
+ single = true;
82
+ else if (character === "[" || character === "{")
83
+ flowDepth++;
84
+ else if (character === "]" || character === "}")
85
+ flowDepth--;
86
+ else if (character === "#" && (index === 0 || /[ \t\r\n]/u.test(text[index - 1])))
87
+ return text.slice(0, index).trimEnd();
88
+ }
89
+ void flowDepth;
90
+ return text.trimEnd();
91
+ }
92
+ function mappingColon(text, flow = false) {
93
+ let single = false;
94
+ let double = false;
95
+ let escaped = false;
96
+ let depth = 0;
97
+ for (let index = 0; index < text.length; index++) {
98
+ const character = text[index];
99
+ if (double) {
100
+ if (escaped)
101
+ escaped = false;
102
+ else if (character === "\\")
103
+ escaped = true;
104
+ else if (character === '"')
105
+ double = false;
106
+ continue;
107
+ }
108
+ if (single) {
109
+ if (character === "'" && text[index + 1] === "'")
110
+ index++;
111
+ else if (character === "'")
112
+ single = false;
113
+ continue;
114
+ }
115
+ if (character === '"')
116
+ double = true;
117
+ else if (character === "'")
118
+ single = true;
119
+ else if (character === "[" || character === "{")
120
+ depth++;
121
+ else if (character === "]" || character === "}")
122
+ depth--;
123
+ else if (character === ":" && depth === 0 && (flow || index + 1 === text.length || /[ \t]/u.test(text[index + 1])))
124
+ return index;
125
+ }
126
+ return -1;
127
+ }
128
+ function checkedExponent(text) {
129
+ let index = 0;
130
+ let negative = false;
131
+ if (text[index] === "+" || text[index] === "-") {
132
+ negative = text[index] === "-";
133
+ index++;
134
+ }
135
+ let value = 0;
136
+ for (; index < text.length; index++) {
137
+ const digit = text.charCodeAt(index) - 48;
138
+ if (digit < 0 || digit > 9)
139
+ throw syntax();
140
+ value = value > 2_000_000_000 ? 2_000_000_001 : value * 10 + digit;
141
+ }
142
+ return negative ? -value : value;
143
+ }
144
+ function decimalText(digits, exponent, negative) {
145
+ const sign = negative ? "-" : "";
146
+ const adjusted = exponent + digits.length - 1;
147
+ if (exponent > 0 || adjusted < -6) {
148
+ return `${sign}${digits[0]}${digits.length > 1 ? `.${digits.slice(1)}` : ""}E${adjusted >= 0 ? "+" : ""}${adjusted}`;
149
+ }
150
+ const point = digits.length + exponent;
151
+ if (point <= 0)
152
+ return `${sign}0.${"0".repeat(-point)}${digits}`;
153
+ return `${sign}${digits.slice(0, point)}${point < digits.length ? `.${digits.slice(point)}` : ""}`;
154
+ }
155
+ function exactSafeIntegral(digits, exponent) {
156
+ if (digits === "0" || exponent < 0)
157
+ return true;
158
+ const length = digits.length + exponent;
159
+ if (length < 16)
160
+ return true;
161
+ if (length > 16)
162
+ return false;
163
+ const boundary = "9007199254740991";
164
+ for (let index = 0; index < digits.length; index++) {
165
+ if (digits[index] < boundary[index])
166
+ return true;
167
+ if (digits[index] > boundary[index])
168
+ return false;
169
+ }
170
+ for (let index = digits.length; index < boundary.length; index++)
171
+ if (boundary[index] !== "0")
172
+ return true;
173
+ return true;
174
+ }
175
+ function parseDecimal(raw) {
176
+ const negative = raw[0] === "-";
177
+ const unsigned = raw[0] === "+" || raw[0] === "-" ? raw.slice(1) : raw;
178
+ const exponentMarker = unsigned.search(/[eE]/u);
179
+ const coefficient = exponentMarker < 0 ? unsigned : unsigned.slice(0, exponentMarker);
180
+ const explicit = exponentMarker < 0 ? 0 : checkedExponent(unsigned.slice(exponentMarker + 1));
181
+ const point = coefficient.indexOf(".");
182
+ const fractional = point < 0 ? 0 : coefficient.length - point - 1;
183
+ let exponent = explicit - fractional;
184
+ let digits = coefficient.replace(".", "").replace(/^0+/u, "") || "0";
185
+ if (digits !== "0") {
186
+ let trailing = 0;
187
+ while (digits.length - trailing > 1 && digits[digits.length - trailing - 1] === "0")
188
+ trailing++;
189
+ if (trailing > 0) {
190
+ digits = digits.slice(0, -trailing);
191
+ exponent += trailing;
192
+ }
193
+ }
194
+ if (exponent < -1_147_483_646 || exponent > 999_999_999)
195
+ throw schema("SCHEMA_DECIMAL_RANGE");
196
+ if (!exactSafeIntegral(digits, exponent))
197
+ throw schema("SCHEMA_UNSAFE_INTEGER");
198
+ const text = decimalText(digits, exponent, negative);
199
+ const double = Number(`${negative ? "-" : ""}${digits}e${exponent}`);
200
+ if (!Number.isFinite(double))
201
+ throw schema("SCHEMA_NONFINITE_NUMBER");
202
+ if (Number.isInteger(double) && !Number.isSafeInteger(double))
203
+ throw schema("SCHEMA_UNSAFE_INTEGER");
204
+ return new Decimal(digits, exponent, negative, text, double);
205
+ }
206
+ function parseBasedInteger(raw) {
207
+ const negative = raw[0] === "-";
208
+ const unsigned = raw[0] === "+" || raw[0] === "-" ? raw.slice(1) : raw;
209
+ const base = unsigned.startsWith("0x") ? 16 : unsigned.startsWith("0o") ? 8 : 10;
210
+ const digits = base === 10 ? unsigned : unsigned.slice(2);
211
+ let value = 0;
212
+ for (const character of digits) {
213
+ const digit = Number.parseInt(character, base);
214
+ if (value > Math.floor((Number.MAX_SAFE_INTEGER - digit) / base))
215
+ throw schema("SCHEMA_UNSAFE_INTEGER");
216
+ value = value * base + digit;
217
+ }
218
+ return negative ? -value : value;
219
+ }
220
+ function scalarFromPlain(raw, explicitTag) {
221
+ if (explicitTag === "str")
222
+ return raw;
223
+ const nullMatch = /^(?:~|null|Null|NULL)$/u.test(raw);
224
+ const boolMatch = /^(?:true|True|TRUE|false|False|FALSE)$/u.test(raw);
225
+ const intMatch = integerPattern.test(raw);
226
+ const floatMatch = floatPattern.test(raw);
227
+ if (explicitTag === "null") {
228
+ if (!nullMatch)
229
+ throw schema("SCHEMA_TAG_LEXEME_MISMATCH");
230
+ return null;
231
+ }
232
+ if (explicitTag === "bool") {
233
+ if (!boolMatch)
234
+ throw schema("SCHEMA_TAG_LEXEME_MISMATCH");
235
+ return /^true$/iu.test(raw);
236
+ }
237
+ if (explicitTag === "int") {
238
+ if (!intMatch)
239
+ throw schema("SCHEMA_TAG_LEXEME_MISMATCH");
240
+ return parseBasedInteger(raw);
241
+ }
242
+ if (explicitTag === "float") {
243
+ if (!(floatMatch || intMatch && /^[+-]?[0-9]+$/u.test(raw)))
244
+ throw schema("SCHEMA_TAG_LEXEME_MISMATCH");
245
+ if (/^[+-]?\.(?:inf|Inf|INF|nan|NaN|NAN)$/u.test(raw))
246
+ throw schema("SCHEMA_NONFINITE_NUMBER");
247
+ return parseDecimal(raw);
248
+ }
249
+ if (nullMatch)
250
+ return null;
251
+ if (boolMatch)
252
+ return /^true$/iu.test(raw);
253
+ if (intMatch)
254
+ return parseBasedInteger(raw);
255
+ if (floatMatch) {
256
+ if (/^[+-]?\.(?:inf|Inf|INF|nan|NaN|NAN)$/u.test(raw))
257
+ throw schema("SCHEMA_NONFINITE_NUMBER");
258
+ return parseDecimal(raw);
259
+ }
260
+ return raw;
261
+ }
262
+ function normalizeTag(raw) {
263
+ const expanded = raw.startsWith("!!") ? `tag:yaml.org,2002:${raw.slice(2)}` : raw.startsWith("!<") && raw.endsWith(">") ? raw.slice(2, -1) : raw;
264
+ if (expanded === "!")
265
+ throw schema("SCHEMA_UNSUPPORTED_TAG");
266
+ const prefix = "tag:yaml.org,2002:";
267
+ if (!expanded.startsWith(prefix))
268
+ throw schema("SCHEMA_UNSUPPORTED_TAG");
269
+ const name = expanded.slice(prefix.length);
270
+ if (!["map", "seq", "str", "null", "bool", "int", "float"].includes(name))
271
+ throw schema("SCHEMA_UNSUPPORTED_TAG");
272
+ return name;
273
+ }
274
+ function decodeDouble(raw) {
275
+ let result = "";
276
+ for (let index = 1; index < raw.length - 1; index++) {
277
+ const character = raw[index];
278
+ if (character === "\n" || character === "\r") {
279
+ if (character === "\r" && raw[index + 1] === "\n")
280
+ index++;
281
+ while (raw[index + 1] === " " || raw[index + 1] === "\t")
282
+ index++;
283
+ result += " ";
284
+ continue;
285
+ }
286
+ if (character !== "\\") {
287
+ const codePoint = character.codePointAt(0);
288
+ if (codePoint === 0)
289
+ throw syntax();
290
+ result += character;
291
+ if (codePoint > 0xffff)
292
+ index++;
293
+ continue;
294
+ }
295
+ const escape = raw[++index];
296
+ if (escape === undefined)
297
+ throw syntax();
298
+ const simple = {
299
+ "0": "\0", a: "\x07", b: "\b", t: "\t", "\t": "\t", n: "\n", v: "\x0b", f: "\f", r: "\r",
300
+ e: "\x1b", " ": " ", '"': '"', "/": "/", "\\": "\\", N: "\u0085", _: "\u00a0", L: "\u2028", P: "\u2029",
301
+ };
302
+ if (Object.hasOwn(simple, escape)) {
303
+ result += simple[escape];
304
+ continue;
305
+ }
306
+ if (escape === "\n" || escape === "\r") {
307
+ if (escape === "\r" && raw[index + 1] === "\n")
308
+ index++;
309
+ while (raw[index + 1] === " " || raw[index + 1] === "\t")
310
+ index++;
311
+ continue;
312
+ }
313
+ const digits = escape === "x" ? 2 : escape === "u" ? 4 : escape === "U" ? 8 : 0;
314
+ if (digits === 0)
315
+ throw syntax();
316
+ const hex = raw.slice(index + 1, index + 1 + digits);
317
+ if (hex.length !== digits || !/^[0-9a-fA-F]+$/u.test(hex))
318
+ throw syntax();
319
+ let codePoint = Number.parseInt(hex, 16);
320
+ index += digits;
321
+ if (escape === "u" && codePoint >= 0xd800 && codePoint <= 0xdbff) {
322
+ const next = raw.slice(index + 1, index + 7);
323
+ if (!/^\\u[0-9a-fA-F]{4}$/u.test(next))
324
+ throw syntax();
325
+ const low = Number.parseInt(next.slice(2), 16);
326
+ if (low < 0xdc00 || low > 0xdfff)
327
+ throw syntax();
328
+ codePoint = 0x10000 + (codePoint - 0xd800) * 0x400 + low - 0xdc00;
329
+ index += 6;
330
+ }
331
+ else if (codePoint >= 0xd800 && codePoint <= 0xdfff)
332
+ throw syntax();
333
+ if (codePoint > 0x10ffff)
334
+ throw syntax();
335
+ result += String.fromCodePoint(codePoint);
336
+ }
337
+ if (!wellFormed(result))
338
+ throw syntax();
339
+ return result;
340
+ }
341
+ function projectDoubleBytes(raw, start = 0, end = raw.length) {
342
+ let bytes = 0;
343
+ for (let index = start + 1; index < end - 1; index++) {
344
+ const character = raw[index];
345
+ if (character === "\n" || character === "\r") {
346
+ if (character === "\r" && raw[index + 1] === "\n")
347
+ index++;
348
+ while (raw[index + 1] === " " || raw[index + 1] === "\t")
349
+ index++;
350
+ bytes++;
351
+ continue;
352
+ }
353
+ if (character !== "\\") {
354
+ const codePoint = character.codePointAt(0);
355
+ if (codePoint === 0)
356
+ throw syntax();
357
+ bytes += utf8Width(codePoint);
358
+ if (codePoint > 0xffff)
359
+ index++;
360
+ continue;
361
+ }
362
+ const escape = raw[++index];
363
+ if (escape === undefined)
364
+ throw syntax();
365
+ const simpleWidths = {
366
+ "0": 1, a: 1, b: 1, t: 1, "\t": 1, n: 1, v: 1, f: 1, r: 1,
367
+ e: 1, " ": 1, '"': 1, "/": 1, "\\": 1, N: 2, _: 2, L: 3, P: 3,
368
+ };
369
+ if (Object.hasOwn(simpleWidths, escape)) {
370
+ bytes += simpleWidths[escape];
371
+ continue;
372
+ }
373
+ if (escape === "\n" || escape === "\r") {
374
+ if (escape === "\r" && raw[index + 1] === "\n")
375
+ index++;
376
+ while (raw[index + 1] === " " || raw[index + 1] === "\t")
377
+ index++;
378
+ continue;
379
+ }
380
+ const digits = escape === "x" ? 2 : escape === "u" ? 4 : escape === "U" ? 8 : 0;
381
+ if (digits === 0)
382
+ throw syntax();
383
+ const hex = raw.slice(index + 1, index + 1 + digits);
384
+ if (hex.length !== digits || !/^[0-9a-fA-F]+$/u.test(hex))
385
+ throw syntax();
386
+ let codePoint = Number.parseInt(hex, 16);
387
+ index += digits;
388
+ if (escape === "u" && codePoint >= 0xd800 && codePoint <= 0xdbff) {
389
+ const next = raw.slice(index + 1, index + 7);
390
+ if (!/^\\u[0-9a-fA-F]{4}$/u.test(next))
391
+ throw syntax();
392
+ const low = Number.parseInt(next.slice(2), 16);
393
+ if (low < 0xdc00 || low > 0xdfff)
394
+ throw syntax();
395
+ codePoint = 0x10000 + (codePoint - 0xd800) * 0x400 + low - 0xdc00;
396
+ index += 6;
397
+ }
398
+ else if (codePoint >= 0xd800 && codePoint <= 0xdfff)
399
+ throw syntax();
400
+ if (codePoint > 0x10ffff)
401
+ throw syntax();
402
+ bytes += utf8Width(codePoint);
403
+ }
404
+ return bytes;
405
+ }
406
+ function decodeSingle(raw) {
407
+ const inner = raw.slice(1, -1);
408
+ return inner.replace(/''/gu, "'").replace(/\r\n|\r|\n[ \t]*/gu, " ");
409
+ }
410
+ function projectSingleBytes(raw, start = 0, end = raw.length) {
411
+ let bytes = 0;
412
+ for (let index = start + 1; index < end - 1; index++) {
413
+ const character = raw[index];
414
+ if (character === "'" && raw[index + 1] === "'") {
415
+ bytes++;
416
+ index++;
417
+ }
418
+ else if (character === "\r" || character === "\n") {
419
+ if (character === "\r" && raw[index + 1] === "\n")
420
+ index++;
421
+ if (character === "\n" || raw[index] === "\n")
422
+ while (raw[index + 1] === " " || raw[index + 1] === "\t")
423
+ index++;
424
+ bytes++;
425
+ }
426
+ else {
427
+ const codePoint = character.codePointAt(0);
428
+ bytes += utf8Width(codePoint);
429
+ if (codePoint > 0xffff)
430
+ index++;
431
+ }
432
+ }
433
+ return bytes;
434
+ }
435
+ function plainBounds(source, start, end) {
436
+ while (start < end && /\s/u.test(source[start]))
437
+ start++;
438
+ while (end > start && /\s/u.test(source[end - 1]))
439
+ end--;
440
+ return [start, end];
441
+ }
442
+ function projectPlainBytes(source, start, end) {
443
+ [start, end] = plainBounds(source, start, end);
444
+ let bytes = 0;
445
+ for (let index = start; index < end;) {
446
+ if (source[index] === "\n") {
447
+ index++;
448
+ while (index < end && (source[index] === " " || source[index] === "\t"))
449
+ index++;
450
+ bytes++;
451
+ continue;
452
+ }
453
+ const codePoint = source.codePointAt(index);
454
+ bytes += utf8Width(codePoint);
455
+ index += codePoint > 0xffff ? 2 : 1;
456
+ }
457
+ return bytes;
458
+ }
459
+ function blockPartText(part) {
460
+ return part.empty ? "" : part.lineText.slice(part.contentIndent);
461
+ }
462
+ function blockPartBytes(part) {
463
+ if (part.empty)
464
+ return 0;
465
+ let bytes = 0;
466
+ for (let index = part.contentIndent; index < part.lineText.length;) {
467
+ const codePoint = part.lineText.codePointAt(index);
468
+ bytes += utf8Width(codePoint);
469
+ index += codePoint > 0xffff ? 2 : 1;
470
+ }
471
+ return bytes;
472
+ }
473
+ function blockScalarUnchomped(parts, style) {
474
+ if (style === "literal") {
475
+ let value = parts.map(blockPartText).join("\n");
476
+ if (parts.at(-1)?.hadBreak)
477
+ value += "\n";
478
+ return value;
479
+ }
480
+ let value = "";
481
+ for (let index = 0; index < parts.length; index++) {
482
+ const current = parts[index];
483
+ value += blockPartText(current);
484
+ if (index < parts.length - 1) {
485
+ const next = parts[index + 1];
486
+ if (current.empty)
487
+ value += next.empty ? "\n" : "";
488
+ else
489
+ value += next.empty || current.moreIndented || next.moreIndented ? "\n" : " ";
490
+ }
491
+ else if (current.hadBreak)
492
+ value += "\n";
493
+ }
494
+ return value;
495
+ }
496
+ function projectBlockScalarBytes(parts, style, chomping) {
497
+ let bytes = 0;
498
+ let trailingBreaks = 0;
499
+ const addPart = (part) => {
500
+ bytes += blockPartBytes(part);
501
+ if (!part.empty)
502
+ trailingBreaks = 0;
503
+ };
504
+ const addBreak = () => {
505
+ bytes++;
506
+ trailingBreaks++;
507
+ };
508
+ if (style === "literal") {
509
+ for (let index = 0; index < parts.length; index++) {
510
+ if (index > 0)
511
+ addBreak();
512
+ addPart(parts[index]);
513
+ }
514
+ if (parts.at(-1)?.hadBreak)
515
+ addBreak();
516
+ }
517
+ else {
518
+ for (let index = 0; index < parts.length; index++) {
519
+ const current = parts[index];
520
+ addPart(current);
521
+ if (index < parts.length - 1) {
522
+ const next = parts[index + 1];
523
+ if (current.empty && next.empty || !current.empty && (next.empty || current.moreIndented || next.moreIndented))
524
+ addBreak();
525
+ else if (!current.empty) {
526
+ bytes++;
527
+ trailingBreaks = 0;
528
+ }
529
+ }
530
+ else if (current.hadBreak)
531
+ addBreak();
532
+ }
533
+ }
534
+ if (chomping === "+")
535
+ return bytes;
536
+ const hasContent = parts.some(item => !item.empty);
537
+ return bytes - trailingBreaks + (chomping === "clip" && hasContent && parts.at(-1)?.hadBreak ? 1 : 0);
538
+ }
539
+ function buildBlockScalar(parts, style, chomping) {
540
+ let value = blockScalarUnchomped(parts, style);
541
+ const hasContent = parts.some(item => !item.empty);
542
+ if (chomping === "clip")
543
+ value = hasContent && parts.at(-1)?.hadBreak ? `${value.replace(/\n+$/u, "")}\n` : value.replace(/\n+$/u, "");
544
+ if (chomping === "-")
545
+ value = value.replace(/\n+$/u, "");
546
+ return value;
547
+ }
548
+ class FlowParser {
549
+ source;
550
+ composer;
551
+ line;
552
+ #position = 0;
553
+ constructor(source, composer, line) {
554
+ this.source = source;
555
+ this.composer = composer;
556
+ this.line = line;
557
+ }
558
+ async parse() {
559
+ const node = await this.#node();
560
+ this.#space();
561
+ if (this.#position !== this.source.length)
562
+ throw syntax(this.line, this.#position + 1);
563
+ return node;
564
+ }
565
+ async #node(keyMode = false) {
566
+ this.#space();
567
+ let tag;
568
+ let anchor;
569
+ while (true) {
570
+ if (this.source.startsWith("!!", this.#position)) {
571
+ if (tag !== undefined)
572
+ throw syntax(this.line, this.#position + 1);
573
+ const match = /^!![^\s,\[\]{}]+/u.exec(this.source.slice(this.#position));
574
+ if (!match)
575
+ throw syntax(this.line, this.#position + 1);
576
+ tag = normalizeTag(match[0]);
577
+ this.#position += match[0].length;
578
+ this.#space();
579
+ }
580
+ else if (this.source.startsWith("!<", this.#position)) {
581
+ if (tag !== undefined)
582
+ throw syntax(this.line, this.#position + 1);
583
+ const end = this.source.indexOf(">", this.#position + 2);
584
+ if (end < 0)
585
+ throw syntax(this.line, this.#position + 1);
586
+ tag = normalizeTag(this.source.slice(this.#position, end + 1));
587
+ this.#position = end + 1;
588
+ this.#space();
589
+ }
590
+ else if (this.source[this.#position] === "!") {
591
+ throw schema("SCHEMA_UNSUPPORTED_TAG", this.line, this.#position + 1);
592
+ }
593
+ else if (this.source[this.#position] === "&") {
594
+ if (anchor !== undefined)
595
+ throw syntax(this.line, this.#position + 1);
596
+ const match = /^&([^\s,\[\]{}]+)/u.exec(this.source.slice(this.#position));
597
+ if (!match)
598
+ throw syntax(this.line, this.#position + 1);
599
+ anchor = match[1];
600
+ this.#position += match[0].length;
601
+ this.#space();
602
+ }
603
+ else
604
+ break;
605
+ }
606
+ const record = anchor === undefined ? undefined : this.composer.beginAnchor(anchor);
607
+ let parsed;
608
+ const character = this.source[this.#position];
609
+ if (character === "[")
610
+ parsed = await this.#sequence();
611
+ else if (character === "{")
612
+ parsed = await this.#mapping();
613
+ else if (character === '"' || character === "'")
614
+ parsed = await this.#quoted();
615
+ else if (character === "*")
616
+ parsed = await this.#alias();
617
+ else if (character === undefined && tag !== undefined) {
618
+ await this.composer.scalar("", null);
619
+ parsed = { value: null, style: "plain", raw: "" };
620
+ }
621
+ else
622
+ parsed = await this.#plain(keyMode);
623
+ parsed = await this.composer.applyTag(parsed, tag);
624
+ if (record)
625
+ this.composer.completeAnchor(record, parsed.value);
626
+ return { ...parsed, ...(tag === undefined ? {} : { explicitTag: tag }) };
627
+ }
628
+ async #sequence() {
629
+ this.composer.enterCollection();
630
+ try {
631
+ this.#position++;
632
+ await this.composer.node();
633
+ this.composer.collection();
634
+ const result = [];
635
+ this.#space();
636
+ if (this.source[this.#position] === "]") {
637
+ this.#position++;
638
+ return { value: result };
639
+ }
640
+ while (true) {
641
+ this.composer.member(result.length + 1);
642
+ this.composer.member(1);
643
+ const start = this.#position;
644
+ let value = await this.#node();
645
+ this.#space();
646
+ if (this.source[this.#position] === ":") {
647
+ if (this.source.slice(start, this.#position).includes("\n"))
648
+ throw syntax(this.line, this.#position + 1);
649
+ this.#position++;
650
+ this.composer.enterCollection();
651
+ try {
652
+ const mapped = await this.#node();
653
+ await this.composer.node();
654
+ this.composer.collection();
655
+ const pair = object();
656
+ this.composer.mappingEntry(pair, value, mapped.value, true, true);
657
+ value = { value: pair };
658
+ }
659
+ finally {
660
+ this.composer.leaveCollection();
661
+ }
662
+ }
663
+ result.push(value.value);
664
+ this.#space();
665
+ if (this.source[this.#position] === "]") {
666
+ this.#position++;
667
+ return { value: result };
668
+ }
669
+ if (this.source[this.#position] !== ",")
670
+ throw syntax(this.line, this.#position + 1);
671
+ this.#position++;
672
+ this.#space();
673
+ if (this.source[this.#position] === "]") {
674
+ this.#position++;
675
+ return { value: result };
676
+ }
677
+ }
678
+ }
679
+ finally {
680
+ this.composer.leaveCollection();
681
+ }
682
+ }
683
+ async #mapping() {
684
+ this.composer.enterCollection();
685
+ try {
686
+ this.#position++;
687
+ await this.composer.node();
688
+ this.composer.collection();
689
+ const result = object();
690
+ let members = 0;
691
+ this.#space();
692
+ if (this.source[this.#position] === "}") {
693
+ this.#position++;
694
+ return { value: result };
695
+ }
696
+ while (true) {
697
+ this.composer.member(++members);
698
+ const key = await this.#node(true);
699
+ this.#space();
700
+ if (this.source[this.#position] !== ":")
701
+ throw syntax(this.line, this.#position + 1);
702
+ this.#position++;
703
+ const value = await this.#node();
704
+ this.composer.mappingEntry(result, key, value.value, true, true);
705
+ this.#space();
706
+ if (this.source[this.#position] === "}") {
707
+ this.#position++;
708
+ return { value: result };
709
+ }
710
+ if (this.source[this.#position] !== ",")
711
+ throw syntax(this.line, this.#position + 1);
712
+ this.#position++;
713
+ this.#space();
714
+ if (this.source[this.#position] === "}") {
715
+ this.#position++;
716
+ return { value: result };
717
+ }
718
+ }
719
+ }
720
+ finally {
721
+ this.composer.leaveCollection();
722
+ }
723
+ }
724
+ async #quoted() {
725
+ const quote = this.source[this.#position];
726
+ const start = this.#position++;
727
+ let escaped = false;
728
+ while (this.#position < this.source.length) {
729
+ const character = this.source[this.#position++];
730
+ if (quote === '"') {
731
+ if (!escaped && character === '"')
732
+ break;
733
+ if (!escaped && character === "\\")
734
+ escaped = true;
735
+ else
736
+ escaped = false;
737
+ }
738
+ else if (character === "'" && this.source[this.#position] === "'")
739
+ this.#position++;
740
+ else if (character === "'")
741
+ break;
742
+ }
743
+ if (this.source[this.#position - 1] !== quote)
744
+ throw syntax(this.line, start + 1);
745
+ const projectedBytes = quote === '"'
746
+ ? projectDoubleBytes(this.source, start, this.#position)
747
+ : projectSingleBytes(this.source, start, this.#position);
748
+ this.composer.admitScalar(projectedBytes);
749
+ const raw = this.source.slice(start, this.#position);
750
+ const value = quote === '"' ? decodeDouble(raw) : decodeSingle(raw);
751
+ await this.composer.scalar(value, value, true);
752
+ return { value, style: quote === '"' ? "double" : "single" };
753
+ }
754
+ async #alias() {
755
+ const match = /^\*([^\s,\[\]{}]+)/u.exec(this.source.slice(this.#position));
756
+ if (!match)
757
+ throw syntax(this.line, this.#position + 1);
758
+ this.#position += match[0].length;
759
+ return { value: await this.composer.alias(match[1]) };
760
+ }
761
+ async #plain(keyMode) {
762
+ const start = this.#position;
763
+ let depth = 0;
764
+ while (this.#position < this.source.length) {
765
+ const character = this.source[this.#position];
766
+ if (character === "[" || character === "{")
767
+ depth++;
768
+ if (character === "]" || character === "}") {
769
+ if (depth === 0)
770
+ break;
771
+ depth--;
772
+ }
773
+ if (depth === 0 && (character === "," || character === "]" || character === "}"))
774
+ break;
775
+ if (depth === 0 && character === ":" && (keyMode || /[\s,\]}]/u.test(this.source[this.#position + 1] ?? "")))
776
+ break;
777
+ this.#position++;
778
+ }
779
+ const projectedBytes = projectPlainBytes(this.source, start, this.#position);
780
+ this.composer.admitScalar(projectedBytes);
781
+ const raw = this.source.slice(start, this.#position).trim().replace(/\r?\n[ \t]*/gu, " ");
782
+ if (raw.length === 0)
783
+ throw syntax(this.line, start + 1);
784
+ const value = scalarFromPlain(raw);
785
+ await this.composer.scalar(typeof value === "string" ? value : raw, value, true);
786
+ return { value, style: "plain", raw };
787
+ }
788
+ #space() {
789
+ while (/[ \t\r\n]/u.test(this.source[this.#position] ?? ""))
790
+ this.#position++;
791
+ }
792
+ }
793
+ class Composer {
794
+ work;
795
+ ledger;
796
+ #anchors = new Map();
797
+ #futureAnchors;
798
+ #collectionDepth = 0;
799
+ constructor(work, ledger, futureAnchors) {
800
+ this.work = work;
801
+ this.ledger = ledger;
802
+ this.#futureAnchors = new Set(futureAnchors);
803
+ }
804
+ async node() {
805
+ await this.work.charge(1);
806
+ this.work.assertOpen();
807
+ this.ledger.admitNode();
808
+ }
809
+ enterCollection() {
810
+ this.work.assertOpen();
811
+ if (this.#collectionDepth >= yqCaps.maxDepth)
812
+ throw limit("LIMIT_MAX_DEPTH");
813
+ this.#collectionDepth++;
814
+ }
815
+ leaveCollection() {
816
+ this.#collectionDepth--;
817
+ }
818
+ admitScalar(bytes) {
819
+ this.ledger.admitScalar(bytes);
820
+ }
821
+ async scalar(text, value = text, admitted = false) {
822
+ await this.node();
823
+ const bytes = Buffer.byteLength(text);
824
+ if (!admitted)
825
+ this.ledger.admitScalar(bytes);
826
+ let codePoints = 0;
827
+ for (const unused of text) {
828
+ void unused;
829
+ codePoints++;
830
+ if (codePoints === 256) {
831
+ await this.work.charge(codePoints);
832
+ this.work.assertOpen();
833
+ codePoints = 0;
834
+ }
835
+ }
836
+ if (codePoints > 0)
837
+ await this.work.charge(codePoints);
838
+ this.work.assertOpen();
839
+ if (bytes > 0)
840
+ await this.work.charge(Math.ceil(bytes / 1024));
841
+ this.work.assertOpen();
842
+ this.ledger.admitValueBytes(compactScalarBytes(value));
843
+ }
844
+ collection() {
845
+ this.ledger.admitValueBytes(2);
846
+ }
847
+ member(size) {
848
+ if (size > yqCaps.maxCollectionSize)
849
+ throw limit("LIMIT_MAX_COLLECTION_SIZE");
850
+ if (size > 1)
851
+ this.ledger.admitValueBytes(1);
852
+ }
853
+ mappingEntry(target, key, value, implicit, memberAdmitted = false) {
854
+ if (typeof key.value !== "string")
855
+ throw schema("SCHEMA_NONSTRING_KEY");
856
+ if (implicit && countCodePoints(key.value) > 1024)
857
+ throw syntax();
858
+ if (key.style === "plain" && key.explicitTag === undefined && key.value === "<<")
859
+ throw schema("SCHEMA_PLAIN_MERGE_KEY");
860
+ if (Object.hasOwn(target, key.value))
861
+ throw schema("SCHEMA_DUPLICATE_KEY");
862
+ if (!memberAdmitted)
863
+ this.member(objectKeys(target).length + 1);
864
+ this.ledger.admitValueBytes(1);
865
+ put(target, key.value, value);
866
+ }
867
+ beginAnchor(name) {
868
+ this.ledger.admitAnchor();
869
+ this.#futureAnchors.delete(name);
870
+ const record = { name, pending: true };
871
+ this.#anchors.set(name, record);
872
+ return record;
873
+ }
874
+ completeAnchor(record, value) {
875
+ record.pending = false;
876
+ record.value = value;
877
+ }
878
+ async alias(name) {
879
+ const record = this.#anchors.get(name);
880
+ if (!record)
881
+ throw aliasFailure(this.#futureAnchors.has(name) ? "ALIAS_FORWARD" : "ALIAS_UNDEFINED");
882
+ if (record.pending || record.value === undefined)
883
+ throw aliasFailure("ALIAS_CURRENT_NODE");
884
+ return copyAlias(record.value, this.ledger, this.work);
885
+ }
886
+ async applyTag(node, tag) {
887
+ if (tag === undefined)
888
+ return node;
889
+ const collection = Array.isArray(node.value) ? "seq" : node.value !== null && typeof node.value === "object" && !(node.value instanceof Decimal) ? "map" : "scalar";
890
+ if (tag === "map" || tag === "seq") {
891
+ if (collection !== tag)
892
+ throw schema("SCHEMA_TAG_KIND_MISMATCH");
893
+ return node;
894
+ }
895
+ if (collection !== "scalar")
896
+ throw schema("SCHEMA_TAG_KIND_MISMATCH");
897
+ const raw = node.raw ?? (typeof node.value === "string" ? node.value : node.value instanceof Decimal ? node.value.text : String(node.value));
898
+ const value = scalarFromPlain(raw, tag);
899
+ return { value, ...(node.style === undefined ? {} : { style: node.style }), explicitTag: tag, ...(node.raw === undefined ? {} : { raw: node.raw }) };
900
+ }
901
+ }
902
+ class BlockParser {
903
+ lines;
904
+ composer;
905
+ #index = 0;
906
+ constructor(lines, composer) {
907
+ this.lines = lines;
908
+ this.composer = composer;
909
+ }
910
+ async parse() {
911
+ this.#skip();
912
+ if (this.#index >= this.lines.length) {
913
+ await this.composer.scalar("", null);
914
+ return null;
915
+ }
916
+ const indent = indentation(this.lines[this.#index].text);
917
+ const node = await this.#node(indent);
918
+ this.#skip();
919
+ if (this.#index < this.lines.length)
920
+ throw syntax(this.lines[this.#index].number, 1);
921
+ return node.value;
922
+ }
923
+ async #node(indent) {
924
+ this.#skip();
925
+ const line = this.lines[this.#index];
926
+ if (!line) {
927
+ await this.composer.scalar("", null);
928
+ return { value: null, style: "plain" };
929
+ }
930
+ const actual = indentation(line.text);
931
+ if (actual !== indent)
932
+ throw syntax(line.number, actual + 1);
933
+ const content = stripComment(line.text.slice(indent));
934
+ if (/^-(?:[ \t]|$)/u.test(content))
935
+ return this.#sequence(indent);
936
+ if (content.startsWith("? ") || mappingColon(content) >= 0)
937
+ return this.#mapping(indent);
938
+ this.#index++;
939
+ return this.#inlineOrBlock(content, indent, line.number);
940
+ }
941
+ async #sequence(indent) {
942
+ this.composer.enterCollection();
943
+ try {
944
+ await this.composer.node();
945
+ this.composer.collection();
946
+ const result = [];
947
+ while (this.#index < this.lines.length) {
948
+ this.#skip();
949
+ const line = this.lines[this.#index];
950
+ if (!line || indentation(line.text) !== indent)
951
+ break;
952
+ const content = stripComment(line.text.slice(indent));
953
+ if (!/^-(?:[ \t]|$)/u.test(content))
954
+ break;
955
+ this.composer.member(result.length + 1);
956
+ this.#index++;
957
+ const rest = content.slice(1).trimStart();
958
+ let item;
959
+ if (rest.length === 0) {
960
+ this.#skip();
961
+ const next = this.lines[this.#index];
962
+ item = next && indentation(next.text) > indent ? await this.#node(indentation(next.text)) : { value: null, style: "plain" };
963
+ if (!next || indentation(next.text) <= indent)
964
+ await this.composer.scalar("", null);
965
+ }
966
+ else if (mappingColon(rest) >= 0) {
967
+ item = await this.#inlineMappingItem(rest, indent + 2, line.number);
968
+ }
969
+ else
970
+ item = await this.#inlineOrBlock(rest, indent, line.number);
971
+ result.push(item.value);
972
+ }
973
+ return { value: result };
974
+ }
975
+ finally {
976
+ this.composer.leaveCollection();
977
+ }
978
+ }
979
+ async #inlineMappingItem(first, indent, lineNumber) {
980
+ this.composer.enterCollection();
981
+ try {
982
+ await this.composer.node();
983
+ this.composer.collection();
984
+ const result = object();
985
+ let members = 1;
986
+ this.composer.member(members);
987
+ await this.#mappingLine(result, first, indent, lineNumber, true);
988
+ while (this.#index < this.lines.length) {
989
+ this.#skip();
990
+ const line = this.lines[this.#index];
991
+ if (!line || indentation(line.text) !== indent)
992
+ break;
993
+ const content = stripComment(line.text.slice(indent));
994
+ if (mappingColon(content) < 0)
995
+ break;
996
+ this.composer.member(++members);
997
+ this.#index++;
998
+ await this.#mappingLine(result, content, indent, line.number, true);
999
+ }
1000
+ return { value: result };
1001
+ }
1002
+ finally {
1003
+ this.composer.leaveCollection();
1004
+ }
1005
+ }
1006
+ async #mapping(indent) {
1007
+ this.composer.enterCollection();
1008
+ try {
1009
+ await this.composer.node();
1010
+ this.composer.collection();
1011
+ const result = object();
1012
+ let members = 0;
1013
+ while (this.#index < this.lines.length) {
1014
+ this.#skip();
1015
+ const line = this.lines[this.#index];
1016
+ if (!line || indentation(line.text) !== indent)
1017
+ break;
1018
+ const content = stripComment(line.text.slice(indent));
1019
+ if (content.startsWith("? ")) {
1020
+ this.composer.member(++members);
1021
+ this.#index++;
1022
+ const key = await this.#inlineOrBlock(content.slice(2).trimStart(), indent, line.number);
1023
+ this.#skip();
1024
+ const valueLine = this.lines[this.#index];
1025
+ if (!valueLine || indentation(valueLine.text) !== indent || !stripComment(valueLine.text.slice(indent)).startsWith(":"))
1026
+ throw syntax(line.number, 1);
1027
+ this.#index++;
1028
+ const valueText = stripComment(valueLine.text.slice(indent)).slice(1).trimStart();
1029
+ const value = valueText.length > 0 ? await this.#inlineOrBlock(valueText, indent, valueLine.number) : await this.#nestedOrNull(indent);
1030
+ this.composer.mappingEntry(result, key, value.value, false, true);
1031
+ continue;
1032
+ }
1033
+ const colon = mappingColon(content);
1034
+ if (colon < 0)
1035
+ break;
1036
+ this.composer.member(++members);
1037
+ this.#index++;
1038
+ await this.#mappingLine(result, content, indent, line.number, true);
1039
+ }
1040
+ return { value: result };
1041
+ }
1042
+ finally {
1043
+ this.composer.leaveCollection();
1044
+ }
1045
+ }
1046
+ async #mappingLine(target, content, indent, lineNumber, memberAdmitted) {
1047
+ const colon = mappingColon(content);
1048
+ if (colon < 0)
1049
+ throw syntax(lineNumber, 1);
1050
+ const keyText = content.slice(0, colon).trimEnd();
1051
+ const valueText = content.slice(colon + 1).trimStart();
1052
+ const key = await this.#inlineOrBlock(keyText, indent, lineNumber);
1053
+ const value = valueText.length > 0 ? await this.#inlineOrBlock(valueText, indent, lineNumber) : await this.#nestedOrNull(indent);
1054
+ this.composer.mappingEntry(target, key, value.value, true, memberAdmitted);
1055
+ }
1056
+ async #nestedOrNull(parentIndent) {
1057
+ this.#skip();
1058
+ const next = this.lines[this.#index];
1059
+ if (next && indentation(next.text) > parentIndent)
1060
+ return this.#node(indentation(next.text));
1061
+ if (next && indentation(next.text) === parentIndent && /^-(?:[ \t]|$)/u.test(stripComment(next.text.slice(parentIndent)))) {
1062
+ return this.#node(parentIndent);
1063
+ }
1064
+ await this.composer.scalar("", null);
1065
+ return { value: null, style: "plain" };
1066
+ }
1067
+ async #inlineOrBlock(content, parentIndent, lineNumber) {
1068
+ const property = /^(?:(!![^\s]+|!<[^>]+>|!)\s+)?(?:&([^\s]+)\s+)?([|>])([1-9]?[+-]?|[+-]?[1-9]?)$/u.exec(content);
1069
+ if (property)
1070
+ return this.#blockScalar(property, parentIndent, lineNumber);
1071
+ if (/^[|>]/u.test(content))
1072
+ throw syntax(lineNumber, 1);
1073
+ const fragments = [content];
1074
+ let fragment = content;
1075
+ let single = false;
1076
+ let double = false;
1077
+ let escaped = false;
1078
+ let depth = 0;
1079
+ let negativeDepth = false;
1080
+ let balanced = false;
1081
+ while (true) {
1082
+ for (let start = 0; start < fragment.length; start += 256) {
1083
+ const end = Math.min(start + 256, fragment.length);
1084
+ await this.composer.work.charge(end - start);
1085
+ this.composer.work.assertOpen();
1086
+ for (let index = start; index < end; index++) {
1087
+ const character = fragment[index];
1088
+ if (double) {
1089
+ if (escaped)
1090
+ escaped = false;
1091
+ else if (character === "\\")
1092
+ escaped = true;
1093
+ else if (character === '"')
1094
+ double = false;
1095
+ }
1096
+ else if (single) {
1097
+ if (character === "'")
1098
+ single = false;
1099
+ }
1100
+ else if (character === '"')
1101
+ double = true;
1102
+ else if (character === "'")
1103
+ single = true;
1104
+ else if (character === "[" || character === "{")
1105
+ depth++;
1106
+ else if (character === "]" || character === "}") {
1107
+ depth--;
1108
+ if (depth < 0)
1109
+ negativeDepth = true;
1110
+ }
1111
+ }
1112
+ }
1113
+ balanced = !single && !double && (negativeDepth || depth === 0);
1114
+ if (balanced || this.#index >= this.lines.length)
1115
+ break;
1116
+ fragment = `\n${this.lines[this.#index++].text.trimStart()}`;
1117
+ fragments.push(fragment);
1118
+ }
1119
+ if (!balanced)
1120
+ throw syntax(lineNumber, 1);
1121
+ return new FlowParser(fragments.join(""), this.composer, lineNumber).parse();
1122
+ }
1123
+ async #blockScalar(match, parentIndent, lineNumber) {
1124
+ const tag = match[1] === undefined ? undefined : normalizeTag(match[1]);
1125
+ const anchorName = match[2];
1126
+ const style = match[3] === "|" ? "literal" : "folded";
1127
+ const header = match[4] ?? "";
1128
+ const digitMatch = /[1-9]/u.exec(header);
1129
+ const chomping = header.includes("+") ? "+" : header.includes("-") ? "-" : "clip";
1130
+ let contentIndent = digitMatch ? parentIndent + Number(digitMatch[0]) : -1;
1131
+ if (contentIndent < 0) {
1132
+ for (let cursor = this.#index; cursor < this.lines.length; cursor++) {
1133
+ if (this.lines[cursor].text.trim().length > 0) {
1134
+ contentIndent = indentation(this.lines[cursor].text);
1135
+ break;
1136
+ }
1137
+ }
1138
+ if (contentIndent < 0)
1139
+ contentIndent = parentIndent + 1;
1140
+ }
1141
+ const values = [];
1142
+ while (this.#index < this.lines.length) {
1143
+ const line = this.lines[this.#index];
1144
+ const actual = indentation(line.text);
1145
+ if (line.text.trim().length > 0 && actual < contentIndent)
1146
+ break;
1147
+ this.#index++;
1148
+ const empty = line.text.trim().length === 0;
1149
+ values.push({ lineText: line.text, contentIndent, empty, moreIndented: !empty && actual > contentIndent, hadBreak: line.hadBreak });
1150
+ }
1151
+ const projectedBytes = projectBlockScalarBytes(values, style, chomping);
1152
+ this.composer.admitScalar(projectedBytes);
1153
+ const value = buildBlockScalar(values, style, chomping);
1154
+ const record = anchorName === undefined ? undefined : this.composer.beginAnchor(anchorName);
1155
+ await this.composer.scalar(value, value, true);
1156
+ let node = { value, style };
1157
+ node = await this.composer.applyTag(node, tag);
1158
+ if (record)
1159
+ this.composer.completeAnchor(record, node.value);
1160
+ void lineNumber;
1161
+ return node;
1162
+ }
1163
+ #skip() {
1164
+ while (this.#index < this.lines.length) {
1165
+ const line = this.lines[this.#index];
1166
+ const indent = indentation(line.text);
1167
+ const content = line.text.slice(indent);
1168
+ if (content.trim().length > 0 && !content.startsWith("#"))
1169
+ return;
1170
+ this.#index++;
1171
+ }
1172
+ }
1173
+ }
1174
+ function rawLines(text, lineOffset = 0) {
1175
+ const normalized = text.replace(/\r\n|\r/gu, "\n");
1176
+ const finalBreak = normalized.endsWith("\n");
1177
+ const pieces = normalized.split("\n");
1178
+ if (pieces[pieces.length - 1] === "")
1179
+ pieces.pop();
1180
+ return pieces.map((line, index) => {
1181
+ const hadBreak = index < pieces.length - 1 || finalBreak;
1182
+ return { text: line, number: lineOffset + index + 1, rawBytes: Buffer.byteLength(line) + (hadBreak ? 1 : 0), hadBreak };
1183
+ });
1184
+ }
1185
+ function* documents(text, lineOffset = 0) {
1186
+ const lines = rawLines(text, lineOffset);
1187
+ let current = [];
1188
+ let explicit = false;
1189
+ let ended = false;
1190
+ let directives = 0;
1191
+ let sawContent = false;
1192
+ const take = (force) => {
1193
+ const document = force || sawContent || explicit
1194
+ ? { lines: current, explicit, rawBytes: current.reduce((sum, line) => sum + line.rawBytes, 0) }
1195
+ : undefined;
1196
+ current = [];
1197
+ explicit = false;
1198
+ sawContent = false;
1199
+ directives = 0;
1200
+ return document;
1201
+ };
1202
+ for (const original of lines) {
1203
+ let line = original;
1204
+ if (line.number === 1 && line.text.startsWith("\ufeff"))
1205
+ line = { ...line, text: line.text.slice(1) };
1206
+ if (ended && line.text.startsWith("\ufeff"))
1207
+ line = { ...line, text: line.text.slice(1) };
1208
+ const trimmed = stripComment(line.text).trim();
1209
+ if (line.text.startsWith("%")) {
1210
+ if (sawContent || explicit || ended)
1211
+ throw structure(line.number, 1);
1212
+ if (trimmed !== "%YAML 1.2" || directives > 0)
1213
+ throw schema("SCHEMA_UNSUPPORTED_DIRECTIVE", line.number, 1);
1214
+ directives++;
1215
+ continue;
1216
+ }
1217
+ if (/^---(?:[ \t]+#.*)?$/u.test(line.text)) {
1218
+ if (sawContent || explicit) {
1219
+ const document = take(false);
1220
+ if (document)
1221
+ yield document;
1222
+ }
1223
+ explicit = true;
1224
+ ended = false;
1225
+ continue;
1226
+ }
1227
+ if (/^\.\.\.(?:[ \t]+#.*)?$/u.test(line.text)) {
1228
+ if (!sawContent && !explicit) {
1229
+ ended = true;
1230
+ continue;
1231
+ }
1232
+ const document = take(true);
1233
+ if (document)
1234
+ yield document;
1235
+ ended = true;
1236
+ continue;
1237
+ }
1238
+ if (ended) {
1239
+ if (trimmed === "")
1240
+ continue;
1241
+ ended = false;
1242
+ }
1243
+ if (directives > 0 && !explicit && trimmed !== "")
1244
+ throw structure(line.number, 1);
1245
+ current.push(line);
1246
+ if (trimmed !== "")
1247
+ sawContent = true;
1248
+ }
1249
+ const document = take(false);
1250
+ if (document)
1251
+ yield document;
1252
+ }
1253
+ function futureAnchorNames(lines) {
1254
+ const names = [];
1255
+ for (const line of lines) {
1256
+ const source = stripComment(line.text);
1257
+ const matcher = /(?:^|[\s\[,{}])&([^\s\[\],{}]+)/gu;
1258
+ for (const match of source.matchAll(matcher))
1259
+ names.push(match[1]);
1260
+ }
1261
+ return names;
1262
+ }
1263
+ export async function* parseYamlDocuments(text, work, ledger, admittedRawBytes, lineOffset = 0) {
1264
+ if (!wellFormed(text))
1265
+ throw new YqError("input", "INPUT_INVALID_UTF8", 5);
1266
+ let admitted = false;
1267
+ for (const document of documents(text, lineOffset)) {
1268
+ ledger.beginDocument(admittedRawBytes !== undefined && !admitted ? admittedRawBytes : document.rawBytes);
1269
+ admitted = true;
1270
+ let singleQuoted = false;
1271
+ let doubleQuoted = false;
1272
+ let escaped = false;
1273
+ for (const line of document.lines) {
1274
+ let codePoints = 0;
1275
+ for (let index = 0; index < line.text.length; index++) {
1276
+ const character = line.text[index];
1277
+ const point = character.codePointAt(0);
1278
+ if (doubleQuoted) {
1279
+ if (escaped)
1280
+ escaped = false;
1281
+ else if (character === "\\")
1282
+ escaped = true;
1283
+ else if (character === '"')
1284
+ doubleQuoted = false;
1285
+ if (!(point === 0x09 || point >= 0x20))
1286
+ throw syntax(line.number, index + 1);
1287
+ }
1288
+ else if (singleQuoted) {
1289
+ if (character === "'" && line.text[index + 1] === "'")
1290
+ index++;
1291
+ else if (character === "'")
1292
+ singleQuoted = false;
1293
+ if (!(point === 0x09 || point >= 0x20))
1294
+ throw syntax(line.number, index + 1);
1295
+ }
1296
+ else {
1297
+ if (character === '"')
1298
+ doubleQuoted = true;
1299
+ else if (character === "'")
1300
+ singleQuoted = true;
1301
+ const printable = point === 0x09 || point >= 0x20 && point <= 0x7e || point === 0x85 || point >= 0xa0 && point <= 0xd7ff || point >= 0xe000 && point <= 0xfffd || point >= 0x10000 && point <= 0x10ffff;
1302
+ if (!printable)
1303
+ throw syntax(line.number, index + 1);
1304
+ if (point === 0xfeff)
1305
+ throw structure(line.number, index + 1);
1306
+ }
1307
+ codePoints++;
1308
+ if (point > 0xffff)
1309
+ index++;
1310
+ if (codePoints === 256) {
1311
+ await work.charge(codePoints);
1312
+ work.assertOpen();
1313
+ codePoints = 0;
1314
+ }
1315
+ }
1316
+ if (codePoints > 0)
1317
+ await work.charge(codePoints);
1318
+ work.assertOpen();
1319
+ }
1320
+ const composer = new Composer(work, ledger, futureAnchorNames(document.lines));
1321
+ const value = await new BlockParser(document.lines, composer).parse();
1322
+ work.assertOpen();
1323
+ yield value;
1324
+ }
1325
+ }
1326
+ //# sourceMappingURL=parser.js.map