forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.ts
1760 lines (1504 loc) · 44.5 KB
/
loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { YamlError } from "../_error.ts";
import { Mark } from "../_mark.ts";
import type { Type } from "../_type.ts";
import * as common from "../_utils.ts";
import {
LoaderState,
type LoaderStateOptions,
type ResultType,
} from "./loader_state.ts";
type Any = common.Any;
type ArrayObject<T = Any> = common.ArrayObject<T>;
const { hasOwn } = Object;
const CONTEXT_FLOW_IN = 1;
const CONTEXT_FLOW_OUT = 2;
const CONTEXT_BLOCK_IN = 3;
const CONTEXT_BLOCK_OUT = 4;
const CHOMPING_CLIP = 1;
const CHOMPING_STRIP = 2;
const CHOMPING_KEEP = 3;
const PATTERN_NON_PRINTABLE =
// deno-lint-ignore no-control-regex
/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;
const PATTERN_NON_ASCII_LINE_BREAKS = /[\x85\u2028\u2029]/;
const PATTERN_FLOW_INDICATORS = /[,\[\]\{\}]/;
const PATTERN_TAG_HANDLE = /^(?:!|!!|![a-z\-]+!)$/i;
const PATTERN_TAG_URI =
/^(?:!|[^,\[\]\{\}])(?:%[0-9a-f]{2}|[0-9a-z\-#;\/\?:@&=\+\$,_\.!~\*'\(\)\[\]])*$/i;
function _class(obj: unknown): string {
return Object.prototype.toString.call(obj);
}
function isEOL(c: number): boolean {
return c === 0x0a || /* LF */ c === 0x0d /* CR */;
}
function isWhiteSpace(c: number): boolean {
return c === 0x09 || /* Tab */ c === 0x20 /* Space */;
}
function isWsOrEol(c: number): boolean {
return (
c === 0x09 /* Tab */ ||
c === 0x20 /* Space */ ||
c === 0x0a /* LF */ ||
c === 0x0d /* CR */
);
}
function isFlowIndicator(c: number): boolean {
return (
c === 0x2c /* , */ ||
c === 0x5b /* [ */ ||
c === 0x5d /* ] */ ||
c === 0x7b /* { */ ||
c === 0x7d /* } */
);
}
function fromHexCode(c: number): number {
if (0x30 <= /* 0 */ c && c <= 0x39 /* 9 */) {
return c - 0x30;
}
const lc = c | 0x20;
if (0x61 <= /* a */ lc && lc <= 0x66 /* f */) {
return lc - 0x61 + 10;
}
return -1;
}
function escapedHexLen(c: number): number {
if (c === 0x78 /* x */) {
return 2;
}
if (c === 0x75 /* u */) {
return 4;
}
if (c === 0x55 /* U */) {
return 8;
}
return 0;
}
function fromDecimalCode(c: number): number {
if (0x30 <= /* 0 */ c && c <= 0x39 /* 9 */) {
return c - 0x30;
}
return -1;
}
function simpleEscapeSequence(c: number): string {
return c === 0x30 /* 0 */
? "\x00"
: c === 0x61 /* a */
? "\x07"
: c === 0x62 /* b */
? "\x08"
: c === 0x74 /* t */
? "\x09"
: c === 0x09 /* Tab */
? "\x09"
: c === 0x6e /* n */
? "\x0A"
: c === 0x76 /* v */
? "\x0B"
: c === 0x66 /* f */
? "\x0C"
: c === 0x72 /* r */
? "\x0D"
: c === 0x65 /* e */
? "\x1B"
: c === 0x20 /* Space */
? " "
: c === 0x22 /* " */
? "\x22"
: c === 0x2f /* / */
? "/"
: c === 0x5c /* \ */
? "\x5C"
: c === 0x4e /* N */
? "\x85"
: c === 0x5f /* _ */
? "\xA0"
: c === 0x4c /* L */
? "\u2028"
: c === 0x50 /* P */
? "\u2029"
: "";
}
function charFromCodepoint(c: number): string {
if (c <= 0xffff) {
return String.fromCharCode(c);
}
// Encode UTF-16 surrogate pair
// https://en.wikipedia.org/wiki/UTF-16#Code_points_U.2B010000_to_U.2B10FFFF
return String.fromCharCode(
((c - 0x010000) >> 10) + 0xd800,
((c - 0x010000) & 0x03ff) + 0xdc00,
);
}
const simpleEscapeCheck = Array.from<number>({ length: 256 }); // integer, for fast access
const simpleEscapeMap = Array.from<string>({ length: 256 });
for (let i = 0; i < 256; i++) {
simpleEscapeCheck[i] = simpleEscapeSequence(i) ? 1 : 0;
simpleEscapeMap[i] = simpleEscapeSequence(i);
}
function generateError(state: LoaderState, message: string): YamlError {
return new YamlError(
message,
new Mark(
state.filename as string,
state.input,
state.position,
state.line,
state.position - state.lineStart,
),
);
}
function throwError(state: LoaderState, message: string): never {
throw generateError(state, message);
}
function throwWarning(state: LoaderState, message: string) {
if (state.onWarning) {
state.onWarning.call(null, generateError(state, message));
}
}
interface DirectiveHandlers {
[directive: string]: (
state: LoaderState,
name: string,
...args: string[]
) => void;
}
const directiveHandlers: DirectiveHandlers = {
YAML(state, _name, ...args: string[]) {
if (state.version !== null) {
return throwError(state, "duplication of %YAML directive");
}
if (args.length !== 1) {
return throwError(state, "YAML directive accepts exactly one argument");
}
const match = /^([0-9]+)\.([0-9]+)$/.exec(args[0]!);
if (match === null) {
return throwError(state, "ill-formed argument of the YAML directive");
}
const major = parseInt(match[1]!, 10);
const minor = parseInt(match[2]!, 10);
if (major !== 1) {
return throwError(state, "unacceptable YAML version of the document");
}
state.version = args[0];
state.checkLineBreaks = minor < 2;
if (minor !== 1 && minor !== 2) {
return throwWarning(state, "unsupported YAML version of the document");
}
},
TAG(state, _name, ...args: string[]) {
if (args.length !== 2) {
return throwError(state, "TAG directive accepts exactly two arguments");
}
const handle = args[0]!;
const prefix = args[1]!;
if (!PATTERN_TAG_HANDLE.test(handle)) {
return throwError(
state,
"ill-formed tag handle (first argument) of the TAG directive",
);
}
if (hasOwn(state.tagMap, handle)) {
return throwError(
state,
`there is a previously declared suffix for "${handle}" tag handle`,
);
}
if (!PATTERN_TAG_URI.test(prefix)) {
return throwError(
state,
"ill-formed tag prefix (second argument) of the TAG directive",
);
}
state.tagMap[handle] = prefix;
},
};
function captureSegment(
state: LoaderState,
start: number,
end: number,
checkJson: boolean,
) {
let result: string;
if (start < end) {
result = state.input.slice(start, end);
if (checkJson) {
for (
let position = 0;
position < result.length;
position++
) {
const character = result.charCodeAt(position);
if (
!(character === 0x09 || (0x20 <= character && character <= 0x10ffff))
) {
return throwError(state, "expected valid JSON character");
}
}
} else if (PATTERN_NON_PRINTABLE.test(result)) {
return throwError(state, "the stream contains non-printable characters");
}
state.result += result;
}
}
function mergeMappings(
state: LoaderState,
destination: ArrayObject,
source: ArrayObject,
overridableKeys: ArrayObject<boolean>,
) {
if (!common.isObject(source)) {
return throwError(
state,
"cannot merge mappings; the provided source object is unacceptable",
);
}
for (const key of Object.keys(source)) {
if (!hasOwn(destination, key)) {
Object.defineProperty(destination, key, {
value: source[key],
writable: true,
enumerable: true,
configurable: true,
});
overridableKeys[key] = true;
}
}
}
function storeMappingPair(
state: LoaderState,
result: ArrayObject | null,
overridableKeys: ArrayObject<boolean>,
keyTag: string | null,
keyNode: Any,
valueNode: unknown,
startLine?: number,
startPos?: number,
): ArrayObject {
// The output is a plain object here, so keys can only be strings.
// We need to convert keyNode to a string, but doing so can hang the process
// (deeply nested arrays that explode exponentially using aliases).
if (Array.isArray(keyNode)) {
keyNode = Array.prototype.slice.call(keyNode);
for (let index = 0; index < keyNode.length; index++) {
if (Array.isArray(keyNode[index])) {
return throwError(state, "nested arrays are not supported inside keys");
}
if (
typeof keyNode === "object" &&
_class(keyNode[index]) === "[object Object]"
) {
keyNode[index] = "[object Object]";
}
}
}
// Avoid code execution in load() via toString property
// (still use its own toString for arrays, timestamps,
// and whatever user schema extensions happen to have @@toStringTag)
if (typeof keyNode === "object" && _class(keyNode) === "[object Object]") {
keyNode = "[object Object]";
}
keyNode = String(keyNode);
if (result === null) {
result = {};
}
if (keyTag === "tag:yaml.org,2002:merge") {
if (Array.isArray(valueNode)) {
for (
let index = 0;
index < valueNode.length;
index++
) {
mergeMappings(state, result, valueNode[index], overridableKeys);
}
} else {
mergeMappings(state, result, valueNode as ArrayObject, overridableKeys);
}
} else {
if (
!state.json &&
!hasOwn(overridableKeys, keyNode) &&
hasOwn(result, keyNode)
) {
state.line = startLine || state.line;
state.position = startPos || state.position;
return throwError(state, "duplicated mapping key");
}
Object.defineProperty(result, keyNode, {
value: valueNode,
writable: true,
enumerable: true,
configurable: true,
});
delete overridableKeys[keyNode];
}
return result;
}
function readLineBreak(state: LoaderState) {
const ch = state.input.charCodeAt(state.position);
if (ch === 0x0a /* LF */) {
state.position++;
} else if (ch === 0x0d /* CR */) {
state.position++;
if (state.input.charCodeAt(state.position) === 0x0a /* LF */) {
state.position++;
}
} else {
return throwError(state, "a line break is expected");
}
state.line += 1;
state.lineStart = state.position;
}
function skipSeparationSpace(
state: LoaderState,
allowComments: boolean,
checkIndent: number,
): number {
let lineBreaks = 0;
let ch = state.input.charCodeAt(state.position);
while (ch !== 0) {
while (isWhiteSpace(ch)) {
ch = state.input.charCodeAt(++state.position);
}
if (allowComments && ch === 0x23 /* # */) {
do {
ch = state.input.charCodeAt(++state.position);
} while (ch !== 0x0a && /* LF */ ch !== 0x0d && /* CR */ ch !== 0);
}
if (isEOL(ch)) {
readLineBreak(state);
ch = state.input.charCodeAt(state.position);
lineBreaks++;
state.lineIndent = 0;
while (ch === 0x20 /* Space */) {
state.lineIndent++;
ch = state.input.charCodeAt(++state.position);
}
} else {
break;
}
}
if (
checkIndent !== -1 &&
lineBreaks !== 0 &&
state.lineIndent < checkIndent
) {
throwWarning(state, "deficient indentation");
}
return lineBreaks;
}
function testDocumentSeparator(state: LoaderState): boolean {
let _position = state.position;
let ch = state.input.charCodeAt(_position);
// Condition state.position === state.lineStart is tested
// in parent on each call, for efficiency. No needs to test here again.
if (
(ch === 0x2d || /* - */ ch === 0x2e) /* . */ &&
ch === state.input.charCodeAt(_position + 1) &&
ch === state.input.charCodeAt(_position + 2)
) {
_position += 3;
ch = state.input.charCodeAt(_position);
if (ch === 0 || isWsOrEol(ch)) {
return true;
}
}
return false;
}
function writeFoldedLines(state: LoaderState, count: number) {
if (count === 1) {
state.result += " ";
} else if (count > 1) {
state.result += common.repeat("\n", count - 1);
}
}
function readPlainScalar(
state: LoaderState,
nodeIndent: number,
withinFlowCollection: boolean,
): boolean {
const kind = state.kind;
const result = state.result;
let ch = state.input.charCodeAt(state.position);
if (
isWsOrEol(ch) ||
isFlowIndicator(ch) ||
ch === 0x23 /* # */ ||
ch === 0x26 /* & */ ||
ch === 0x2a /* * */ ||
ch === 0x21 /* ! */ ||
ch === 0x7c /* | */ ||
ch === 0x3e /* > */ ||
ch === 0x27 /* ' */ ||
ch === 0x22 /* " */ ||
ch === 0x25 /* % */ ||
ch === 0x40 /* @ */ ||
ch === 0x60 /* ` */
) {
return false;
}
let following: number;
if (ch === 0x3f || /* ? */ ch === 0x2d /* - */) {
following = state.input.charCodeAt(state.position + 1);
if (
isWsOrEol(following) ||
(withinFlowCollection && isFlowIndicator(following))
) {
return false;
}
}
state.kind = "scalar";
state.result = "";
let captureEnd = state.position;
let captureStart = state.position;
let hasPendingContent = false;
let line = 0;
while (ch !== 0) {
if (ch === 0x3a /* : */) {
following = state.input.charCodeAt(state.position + 1);
if (
isWsOrEol(following) ||
(withinFlowCollection && isFlowIndicator(following))
) {
break;
}
} else if (ch === 0x23 /* # */) {
const preceding = state.input.charCodeAt(state.position - 1);
if (isWsOrEol(preceding)) {
break;
}
} else if (
(state.position === state.lineStart && testDocumentSeparator(state)) ||
(withinFlowCollection && isFlowIndicator(ch))
) {
break;
} else if (isEOL(ch)) {
line = state.line;
const lineStart = state.lineStart;
const lineIndent = state.lineIndent;
skipSeparationSpace(state, false, -1);
if (state.lineIndent >= nodeIndent) {
hasPendingContent = true;
ch = state.input.charCodeAt(state.position);
continue;
} else {
state.position = captureEnd;
state.line = line;
state.lineStart = lineStart;
state.lineIndent = lineIndent;
break;
}
}
if (hasPendingContent) {
captureSegment(state, captureStart, captureEnd, false);
writeFoldedLines(state, state.line - line);
captureStart = captureEnd = state.position;
hasPendingContent = false;
}
if (!isWhiteSpace(ch)) {
captureEnd = state.position + 1;
}
ch = state.input.charCodeAt(++state.position);
}
captureSegment(state, captureStart, captureEnd, false);
if (state.result) {
return true;
}
state.kind = kind;
state.result = result;
return false;
}
function readSingleQuotedScalar(
state: LoaderState,
nodeIndent: number,
): boolean {
let ch;
let captureStart;
let captureEnd;
ch = state.input.charCodeAt(state.position);
if (ch !== 0x27 /* ' */) {
return false;
}
state.kind = "scalar";
state.result = "";
state.position++;
captureStart = captureEnd = state.position;
while ((ch = state.input.charCodeAt(state.position)) !== 0) {
if (ch === 0x27 /* ' */) {
captureSegment(state, captureStart, state.position, true);
ch = state.input.charCodeAt(++state.position);
if (ch === 0x27 /* ' */) {
captureStart = state.position;
state.position++;
captureEnd = state.position;
} else {
return true;
}
} else if (isEOL(ch)) {
captureSegment(state, captureStart, captureEnd, true);
writeFoldedLines(state, skipSeparationSpace(state, false, nodeIndent));
captureStart = captureEnd = state.position;
} else if (
state.position === state.lineStart &&
testDocumentSeparator(state)
) {
return throwError(
state,
"unexpected end of the document within a single quoted scalar",
);
} else {
state.position++;
captureEnd = state.position;
}
}
return throwError(
state,
"unexpected end of the stream within a single quoted scalar",
);
}
function readDoubleQuotedScalar(
state: LoaderState,
nodeIndent: number,
): boolean {
let ch = state.input.charCodeAt(state.position);
if (ch !== 0x22 /* " */) {
return false;
}
state.kind = "scalar";
state.result = "";
state.position++;
let captureEnd = state.position;
let captureStart = state.position;
let tmp: number;
while ((ch = state.input.charCodeAt(state.position)) !== 0) {
if (ch === 0x22 /* " */) {
captureSegment(state, captureStart, state.position, true);
state.position++;
return true;
}
if (ch === 0x5c /* \ */) {
captureSegment(state, captureStart, state.position, true);
ch = state.input.charCodeAt(++state.position);
if (isEOL(ch)) {
skipSeparationSpace(state, false, nodeIndent);
// TODO(bartlomieju): rework to inline fn with no type cast?
} else if (ch < 256 && simpleEscapeCheck[ch]) {
state.result += simpleEscapeMap[ch];
state.position++;
} else if ((tmp = escapedHexLen(ch)) > 0) {
let hexLength = tmp;
let hexResult = 0;
for (; hexLength > 0; hexLength--) {
ch = state.input.charCodeAt(++state.position);
if ((tmp = fromHexCode(ch)) >= 0) {
hexResult = (hexResult << 4) + tmp;
} else {
return throwError(state, "expected hexadecimal character");
}
}
state.result += charFromCodepoint(hexResult);
state.position++;
} else {
return throwError(state, "unknown escape sequence");
}
captureStart = captureEnd = state.position;
} else if (isEOL(ch)) {
captureSegment(state, captureStart, captureEnd, true);
writeFoldedLines(state, skipSeparationSpace(state, false, nodeIndent));
captureStart = captureEnd = state.position;
} else if (
state.position === state.lineStart &&
testDocumentSeparator(state)
) {
return throwError(
state,
"unexpected end of the document within a double quoted scalar",
);
} else {
state.position++;
captureEnd = state.position;
}
}
return throwError(
state,
"unexpected end of the stream within a double quoted scalar",
);
}
function readFlowCollection(state: LoaderState, nodeIndent: number): boolean {
let ch = state.input.charCodeAt(state.position);
let terminator: number;
let isMapping = true;
let result: ResultType = {};
if (ch === 0x5b /* [ */) {
terminator = 0x5d; /* ] */
isMapping = false;
result = [];
} else if (ch === 0x7b /* { */) {
terminator = 0x7d; /* } */
} else {
return false;
}
if (state.anchor !== null && typeof state.anchor !== "undefined") {
state.anchorMap[state.anchor] = result;
}
ch = state.input.charCodeAt(++state.position);
const tag = state.tag;
const anchor = state.anchor;
let readNext = true;
let valueNode = null;
let keyNode = null;
let keyTag: string | null = null;
let isExplicitPair = false;
let isPair = false;
let following = 0;
let line = 0;
const overridableKeys: ArrayObject<boolean> = Object.create(null);
while (ch !== 0) {
skipSeparationSpace(state, true, nodeIndent);
ch = state.input.charCodeAt(state.position);
if (ch === terminator) {
state.position++;
state.tag = tag;
state.anchor = anchor;
state.kind = isMapping ? "mapping" : "sequence";
state.result = result;
return true;
}
if (!readNext) {
return throwError(state, "missed comma between flow collection entries");
}
keyTag = keyNode = valueNode = null;
isPair = isExplicitPair = false;
if (ch === 0x3f /* ? */) {
following = state.input.charCodeAt(state.position + 1);
if (isWsOrEol(following)) {
isPair = isExplicitPair = true;
state.position++;
skipSeparationSpace(state, true, nodeIndent);
}
}
line = state.line;
composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true);
keyTag = state.tag || null;
keyNode = state.result;
skipSeparationSpace(state, true, nodeIndent);
ch = state.input.charCodeAt(state.position);
if ((isExplicitPair || state.line === line) && ch === 0x3a /* : */) {
isPair = true;
ch = state.input.charCodeAt(++state.position);
skipSeparationSpace(state, true, nodeIndent);
composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true);
valueNode = state.result;
}
if (isMapping) {
storeMappingPair(
state,
result,
overridableKeys,
keyTag,
keyNode,
valueNode,
);
} else if (isPair) {
(result as ArrayObject[]).push(
storeMappingPair(
state,
null,
overridableKeys,
keyTag,
keyNode,
valueNode,
),
);
} else {
(result as ResultType[]).push(keyNode as ResultType);
}
skipSeparationSpace(state, true, nodeIndent);
ch = state.input.charCodeAt(state.position);
if (ch === 0x2c /* , */) {
readNext = true;
ch = state.input.charCodeAt(++state.position);
} else {
readNext = false;
}
}
return throwError(
state,
"unexpected end of the stream within a flow collection",
);
}
function readBlockScalar(state: LoaderState, nodeIndent: number): boolean {
let chomping = CHOMPING_CLIP;
let didReadContent = false;
let detectedIndent = false;
let textIndent = nodeIndent;
let emptyLines = 0;
let atMoreIndented = false;
let ch = state.input.charCodeAt(state.position);
let folding = false;
if (ch === 0x7c /* | */) {
folding = false;
} else if (ch === 0x3e /* > */) {
folding = true;
} else {
return false;
}
state.kind = "scalar";
state.result = "";
let tmp = 0;
while (ch !== 0) {
ch = state.input.charCodeAt(++state.position);
if (ch === 0x2b || /* + */ ch === 0x2d /* - */) {
if (CHOMPING_CLIP === chomping) {
chomping = ch === 0x2b /* + */ ? CHOMPING_KEEP : CHOMPING_STRIP;
} else {
return throwError(state, "repeat of a chomping mode identifier");
}
} else if ((tmp = fromDecimalCode(ch)) >= 0) {
if (tmp === 0) {
return throwError(
state,
"bad explicit indentation width of a block scalar; it cannot be less than one",
);
} else if (!detectedIndent) {
textIndent = nodeIndent + tmp - 1;
detectedIndent = true;
} else {
return throwError(state, "repeat of an indentation width identifier");
}
} else {
break;
}
}
if (isWhiteSpace(ch)) {
do {
ch = state.input.charCodeAt(++state.position);
} while (isWhiteSpace(ch));
if (ch === 0x23 /* # */) {
do {
ch = state.input.charCodeAt(++state.position);
} while (!isEOL(ch) && ch !== 0);
}
}
while (ch !== 0) {
readLineBreak(state);
state.lineIndent = 0;
ch = state.input.charCodeAt(state.position);
while (
(!detectedIndent || state.lineIndent < textIndent) &&
ch === 0x20 /* Space */
) {
state.lineIndent++;
ch = state.input.charCodeAt(++state.position);
}
if (!detectedIndent && state.lineIndent > textIndent) {
textIndent = state.lineIndent;
}
if (isEOL(ch)) {
emptyLines++;
continue;
}
// End of the scalar.
if (state.lineIndent < textIndent) {
// Perform the chomping.
if (chomping === CHOMPING_KEEP) {
state.result += common.repeat(
"\n",
didReadContent ? 1 + emptyLines : emptyLines,
);
} else if (chomping === CHOMPING_CLIP) {
if (didReadContent) {
// i.e. only if the scalar is not empty.
state.result += "\n";
}
}
// Break this `while` cycle and go to the function's epilogue.
break;
}
// Folded style: use fancy rules to handle line breaks.
if (folding) {
// Lines starting with white space characters (more-indented lines) are not folded.
if (isWhiteSpace(ch)) {
atMoreIndented = true;
// except for the first content line (cf. Example 8.1)
state.result += common.repeat(
"\n",
didReadContent ? 1 + emptyLines : emptyLines,
);
// End of more-indented block.
} else if (atMoreIndented) {
atMoreIndented = false;
state.result += common.repeat("\n", emptyLines + 1);
// Just one line break - perceive as the same line.
} else if (emptyLines === 0) {
if (didReadContent) {
// i.e. only if we have already read some scalar content.
state.result += " ";
}
// Several line breaks - perceive as different lines.
} else {
state.result += common.repeat("\n", emptyLines);
}
// Literal style: just add exact number of line breaks between content lines.
} else {
// Keep all line breaks except the header line break.
state.result += common.repeat(
"\n",
didReadContent ? 1 + emptyLines : emptyLines,
);
}
didReadContent = true;
detectedIndent = true;
emptyLines = 0;
const captureStart = state.position;
while (!isEOL(ch) && ch !== 0) {
ch = state.input.charCodeAt(++state.position);
}
captureSegment(state, captureStart, state.position, false);
}