/ cloudformation-templates / node_modules / aws-cdk / node_modules / yaml / browser / dist / parse-cst.js
parse-cst.js
1 import { j as _inherits, k as _createSuper, c as _classCallCheck, T as Type, b as _createClass, R as Range, N as Node, g as YAMLSemanticError, l as _get, m as _getPrototypeOf, Y as YAMLSyntaxError, C as Char, e as _defineProperty, P as PlainValue } from './PlainValue-b8036b75.js'; 2 3 var BlankLine = /*#__PURE__*/function (_Node) { 4 _inherits(BlankLine, _Node); 5 6 var _super = _createSuper(BlankLine); 7 8 function BlankLine() { 9 _classCallCheck(this, BlankLine); 10 11 return _super.call(this, Type.BLANK_LINE); 12 } 13 /* istanbul ignore next */ 14 15 16 _createClass(BlankLine, [{ 17 key: "includesTrailingLines", 18 get: function get() { 19 // This is never called from anywhere, but if it were, 20 // this is the value it should return. 21 return true; 22 } 23 /** 24 * Parses a blank line from the source 25 * 26 * @param {ParseContext} context 27 * @param {number} start - Index of first \n character 28 * @returns {number} - Index of the character after this 29 */ 30 31 }, { 32 key: "parse", 33 value: function parse(context, start) { 34 this.context = context; 35 this.range = new Range(start, start + 1); 36 return start + 1; 37 } 38 }]); 39 40 return BlankLine; 41 }(Node); 42 43 var CollectionItem = /*#__PURE__*/function (_Node) { 44 _inherits(CollectionItem, _Node); 45 46 var _super = _createSuper(CollectionItem); 47 48 function CollectionItem(type, props) { 49 var _this; 50 51 _classCallCheck(this, CollectionItem); 52 53 _this = _super.call(this, type, props); 54 _this.node = null; 55 return _this; 56 } 57 58 _createClass(CollectionItem, [{ 59 key: "includesTrailingLines", 60 get: function get() { 61 return !!this.node && this.node.includesTrailingLines; 62 } 63 /** 64 * @param {ParseContext} context 65 * @param {number} start - Index of first character 66 * @returns {number} - Index of the character after this 67 */ 68 69 }, { 70 key: "parse", 71 value: function parse(context, start) { 72 this.context = context; 73 var parseNode = context.parseNode, 74 src = context.src; 75 var atLineStart = context.atLineStart, 76 lineStart = context.lineStart; 77 if (!atLineStart && this.type === Type.SEQ_ITEM) this.error = new YAMLSemanticError(this, 'Sequence items must not have preceding content on the same line'); 78 var indent = atLineStart ? start - lineStart : context.indent; 79 var offset = Node.endOfWhiteSpace(src, start + 1); 80 var ch = src[offset]; 81 var inlineComment = ch === '#'; 82 var comments = []; 83 var blankLine = null; 84 85 while (ch === '\n' || ch === '#') { 86 if (ch === '#') { 87 var _end = Node.endOfLine(src, offset + 1); 88 89 comments.push(new Range(offset, _end)); 90 offset = _end; 91 } else { 92 atLineStart = true; 93 lineStart = offset + 1; 94 var wsEnd = Node.endOfWhiteSpace(src, lineStart); 95 96 if (src[wsEnd] === '\n' && comments.length === 0) { 97 blankLine = new BlankLine(); 98 lineStart = blankLine.parse({ 99 src: src 100 }, lineStart); 101 } 102 103 offset = Node.endOfIndent(src, lineStart); 104 } 105 106 ch = src[offset]; 107 } 108 109 if (Node.nextNodeIsIndented(ch, offset - (lineStart + indent), this.type !== Type.SEQ_ITEM)) { 110 this.node = parseNode({ 111 atLineStart: atLineStart, 112 inCollection: false, 113 indent: indent, 114 lineStart: lineStart, 115 parent: this 116 }, offset); 117 } else if (ch && lineStart > start + 1) { 118 offset = lineStart - 1; 119 } 120 121 if (this.node) { 122 if (blankLine) { 123 // Only blank lines preceding non-empty nodes are captured. Note that 124 // this means that collection item range start indices do not always 125 // increase monotonically. -- eemeli/yaml#126 126 var items = context.parent.items || context.parent.contents; 127 if (items) items.push(blankLine); 128 } 129 130 if (comments.length) Array.prototype.push.apply(this.props, comments); 131 offset = this.node.range.end; 132 } else { 133 if (inlineComment) { 134 var c = comments[0]; 135 this.props.push(c); 136 offset = c.end; 137 } else { 138 offset = Node.endOfLine(src, start + 1); 139 } 140 } 141 142 var end = this.node ? this.node.valueRange.end : offset; 143 this.valueRange = new Range(start, end); 144 return offset; 145 } 146 }, { 147 key: "setOrigRanges", 148 value: function setOrigRanges(cr, offset) { 149 offset = _get(_getPrototypeOf(CollectionItem.prototype), "setOrigRanges", this).call(this, cr, offset); 150 return this.node ? this.node.setOrigRanges(cr, offset) : offset; 151 } 152 }, { 153 key: "toString", 154 value: function toString() { 155 var src = this.context.src, 156 node = this.node, 157 range = this.range, 158 value = this.value; 159 if (value != null) return value; 160 var str = node ? src.slice(range.start, node.range.start) + String(node) : src.slice(range.start, range.end); 161 return Node.addStringTerminator(src, range.end, str); 162 } 163 }]); 164 165 return CollectionItem; 166 }(Node); 167 168 var Comment = /*#__PURE__*/function (_Node) { 169 _inherits(Comment, _Node); 170 171 var _super = _createSuper(Comment); 172 173 function Comment() { 174 _classCallCheck(this, Comment); 175 176 return _super.call(this, Type.COMMENT); 177 } 178 /** 179 * Parses a comment line from the source 180 * 181 * @param {ParseContext} context 182 * @param {number} start - Index of first character 183 * @returns {number} - Index of the character after this scalar 184 */ 185 186 187 _createClass(Comment, [{ 188 key: "parse", 189 value: function parse(context, start) { 190 this.context = context; 191 var offset = this.parseComment(start); 192 this.range = new Range(start, offset); 193 return offset; 194 } 195 }]); 196 197 return Comment; 198 }(Node); 199 200 function grabCollectionEndComments(node) { 201 var cnode = node; 202 203 while (cnode instanceof CollectionItem) { 204 cnode = cnode.node; 205 } 206 207 if (!(cnode instanceof Collection)) return null; 208 var len = cnode.items.length; 209 var ci = -1; 210 211 for (var i = len - 1; i >= 0; --i) { 212 var n = cnode.items[i]; 213 214 if (n.type === Type.COMMENT) { 215 // Keep sufficiently indented comments with preceding node 216 var _n$context = n.context, 217 indent = _n$context.indent, 218 lineStart = _n$context.lineStart; 219 if (indent > 0 && n.range.start >= lineStart + indent) break; 220 ci = i; 221 } else if (n.type === Type.BLANK_LINE) ci = i;else break; 222 } 223 224 if (ci === -1) return null; 225 var ca = cnode.items.splice(ci, len - ci); 226 var prevEnd = ca[0].range.start; 227 228 while (true) { 229 cnode.range.end = prevEnd; 230 if (cnode.valueRange && cnode.valueRange.end > prevEnd) cnode.valueRange.end = prevEnd; 231 if (cnode === node) break; 232 cnode = cnode.context.parent; 233 } 234 235 return ca; 236 } 237 var Collection = /*#__PURE__*/function (_Node) { 238 _inherits(Collection, _Node); 239 240 var _super = _createSuper(Collection); 241 242 function Collection(firstItem) { 243 var _this; 244 245 _classCallCheck(this, Collection); 246 247 _this = _super.call(this, firstItem.type === Type.SEQ_ITEM ? Type.SEQ : Type.MAP); 248 249 for (var i = firstItem.props.length - 1; i >= 0; --i) { 250 if (firstItem.props[i].start < firstItem.context.lineStart) { 251 // props on previous line are assumed by the collection 252 _this.props = firstItem.props.slice(0, i + 1); 253 firstItem.props = firstItem.props.slice(i + 1); 254 var itemRange = firstItem.props[0] || firstItem.valueRange; 255 firstItem.range.start = itemRange.start; 256 break; 257 } 258 } 259 260 _this.items = [firstItem]; 261 var ec = grabCollectionEndComments(firstItem); 262 if (ec) Array.prototype.push.apply(_this.items, ec); 263 return _this; 264 } 265 266 _createClass(Collection, [{ 267 key: "includesTrailingLines", 268 get: function get() { 269 return this.items.length > 0; 270 } 271 /** 272 * @param {ParseContext} context 273 * @param {number} start - Index of first character 274 * @returns {number} - Index of the character after this 275 */ 276 277 }, { 278 key: "parse", 279 value: function parse(context, start) { 280 this.context = context; 281 var parseNode = context.parseNode, 282 src = context.src; // It's easier to recalculate lineStart here rather than tracking down the 283 // last context from which to read it -- eemeli/yaml#2 284 285 var lineStart = Node.startOfLine(src, start); 286 var firstItem = this.items[0]; // First-item context needs to be correct for later comment handling 287 // -- eemeli/yaml#17 288 289 firstItem.context.parent = this; 290 this.valueRange = Range.copy(firstItem.valueRange); 291 var indent = firstItem.range.start - firstItem.context.lineStart; 292 var offset = start; 293 offset = Node.normalizeOffset(src, offset); 294 var ch = src[offset]; 295 var atLineStart = Node.endOfWhiteSpace(src, lineStart) === offset; 296 var prevIncludesTrailingLines = false; 297 298 while (ch) { 299 while (ch === '\n' || ch === '#') { 300 if (atLineStart && ch === '\n' && !prevIncludesTrailingLines) { 301 var blankLine = new BlankLine(); 302 offset = blankLine.parse({ 303 src: src 304 }, offset); 305 this.valueRange.end = offset; 306 307 if (offset >= src.length) { 308 ch = null; 309 break; 310 } 311 312 this.items.push(blankLine); 313 offset -= 1; // blankLine.parse() consumes terminal newline 314 } else if (ch === '#') { 315 if (offset < lineStart + indent && !Collection.nextContentHasIndent(src, offset, indent)) { 316 return offset; 317 } 318 319 var comment = new Comment(); 320 offset = comment.parse({ 321 indent: indent, 322 lineStart: lineStart, 323 src: src 324 }, offset); 325 this.items.push(comment); 326 this.valueRange.end = offset; 327 328 if (offset >= src.length) { 329 ch = null; 330 break; 331 } 332 } 333 334 lineStart = offset + 1; 335 offset = Node.endOfIndent(src, lineStart); 336 337 if (Node.atBlank(src, offset)) { 338 var wsEnd = Node.endOfWhiteSpace(src, offset); 339 var next = src[wsEnd]; 340 341 if (!next || next === '\n' || next === '#') { 342 offset = wsEnd; 343 } 344 } 345 346 ch = src[offset]; 347 atLineStart = true; 348 } 349 350 if (!ch) { 351 break; 352 } 353 354 if (offset !== lineStart + indent && (atLineStart || ch !== ':')) { 355 if (offset < lineStart + indent) { 356 if (lineStart > start) offset = lineStart; 357 break; 358 } else if (!this.error) { 359 var msg = 'All collection items must start at the same column'; 360 this.error = new YAMLSyntaxError(this, msg); 361 } 362 } 363 364 if (firstItem.type === Type.SEQ_ITEM) { 365 if (ch !== '-') { 366 if (lineStart > start) offset = lineStart; 367 break; 368 } 369 } else if (ch === '-' && !this.error) { 370 // map key may start with -, as long as it's followed by a non-whitespace char 371 var _next = src[offset + 1]; 372 373 if (!_next || _next === '\n' || _next === '\t' || _next === ' ') { 374 var _msg = 'A collection cannot be both a mapping and a sequence'; 375 this.error = new YAMLSyntaxError(this, _msg); 376 } 377 } 378 379 var node = parseNode({ 380 atLineStart: atLineStart, 381 inCollection: true, 382 indent: indent, 383 lineStart: lineStart, 384 parent: this 385 }, offset); 386 if (!node) return offset; // at next document start 387 388 this.items.push(node); 389 this.valueRange.end = node.valueRange.end; 390 offset = Node.normalizeOffset(src, node.range.end); 391 ch = src[offset]; 392 atLineStart = false; 393 prevIncludesTrailingLines = node.includesTrailingLines; // Need to reset lineStart and atLineStart here if preceding node's range 394 // has advanced to check the current line's indentation level 395 // -- eemeli/yaml#10 & eemeli/yaml#38 396 397 if (ch) { 398 var ls = offset - 1; 399 var prev = src[ls]; 400 401 while (prev === ' ' || prev === '\t') { 402 prev = src[--ls]; 403 } 404 405 if (prev === '\n') { 406 lineStart = ls + 1; 407 atLineStart = true; 408 } 409 } 410 411 var ec = grabCollectionEndComments(node); 412 if (ec) Array.prototype.push.apply(this.items, ec); 413 } 414 415 return offset; 416 } 417 }, { 418 key: "setOrigRanges", 419 value: function setOrigRanges(cr, offset) { 420 offset = _get(_getPrototypeOf(Collection.prototype), "setOrigRanges", this).call(this, cr, offset); 421 this.items.forEach(function (node) { 422 offset = node.setOrigRanges(cr, offset); 423 }); 424 return offset; 425 } 426 }, { 427 key: "toString", 428 value: function toString() { 429 var src = this.context.src, 430 items = this.items, 431 range = this.range, 432 value = this.value; 433 if (value != null) return value; 434 var str = src.slice(range.start, items[0].range.start) + String(items[0]); 435 436 for (var i = 1; i < items.length; ++i) { 437 var item = items[i]; 438 var _item$context = item.context, 439 atLineStart = _item$context.atLineStart, 440 indent = _item$context.indent; 441 if (atLineStart) for (var _i = 0; _i < indent; ++_i) { 442 str += ' '; 443 } 444 str += String(item); 445 } 446 447 return Node.addStringTerminator(src, range.end, str); 448 } 449 }], [{ 450 key: "nextContentHasIndent", 451 value: function nextContentHasIndent(src, offset, indent) { 452 var lineStart = Node.endOfLine(src, offset) + 1; 453 offset = Node.endOfWhiteSpace(src, lineStart); 454 var ch = src[offset]; 455 if (!ch) return false; 456 if (offset >= lineStart + indent) return true; 457 if (ch !== '#' && ch !== '\n') return false; 458 return Collection.nextContentHasIndent(src, offset, indent); 459 } 460 }]); 461 462 return Collection; 463 }(Node); 464 465 var Directive = /*#__PURE__*/function (_Node) { 466 _inherits(Directive, _Node); 467 468 var _super = _createSuper(Directive); 469 470 function Directive() { 471 var _this; 472 473 _classCallCheck(this, Directive); 474 475 _this = _super.call(this, Type.DIRECTIVE); 476 _this.name = null; 477 return _this; 478 } 479 480 _createClass(Directive, [{ 481 key: "parameters", 482 get: function get() { 483 var raw = this.rawValue; 484 return raw ? raw.trim().split(/[ \t]+/) : []; 485 } 486 }, { 487 key: "parseName", 488 value: function parseName(start) { 489 var src = this.context.src; 490 var offset = start; 491 var ch = src[offset]; 492 493 while (ch && ch !== '\n' && ch !== '\t' && ch !== ' ') { 494 ch = src[offset += 1]; 495 } 496 497 this.name = src.slice(start, offset); 498 return offset; 499 } 500 }, { 501 key: "parseParameters", 502 value: function parseParameters(start) { 503 var src = this.context.src; 504 var offset = start; 505 var ch = src[offset]; 506 507 while (ch && ch !== '\n' && ch !== '#') { 508 ch = src[offset += 1]; 509 } 510 511 this.valueRange = new Range(start, offset); 512 return offset; 513 } 514 }, { 515 key: "parse", 516 value: function parse(context, start) { 517 this.context = context; 518 var offset = this.parseName(start + 1); 519 offset = this.parseParameters(offset); 520 offset = this.parseComment(offset); 521 this.range = new Range(start, offset); 522 return offset; 523 } 524 }]); 525 526 return Directive; 527 }(Node); 528 529 var Document = /*#__PURE__*/function (_Node) { 530 _inherits(Document, _Node); 531 532 var _super = _createSuper(Document); 533 534 function Document() { 535 var _this; 536 537 _classCallCheck(this, Document); 538 539 _this = _super.call(this, Type.DOCUMENT); 540 _this.directives = null; 541 _this.contents = null; 542 _this.directivesEndMarker = null; 543 _this.documentEndMarker = null; 544 return _this; 545 } 546 547 _createClass(Document, [{ 548 key: "parseDirectives", 549 value: function parseDirectives(start) { 550 var src = this.context.src; 551 this.directives = []; 552 var atLineStart = true; 553 var hasDirectives = false; 554 var offset = start; 555 556 while (!Node.atDocumentBoundary(src, offset, Char.DIRECTIVES_END)) { 557 offset = Document.startCommentOrEndBlankLine(src, offset); 558 559 switch (src[offset]) { 560 case '\n': 561 if (atLineStart) { 562 var blankLine = new BlankLine(); 563 offset = blankLine.parse({ 564 src: src 565 }, offset); 566 567 if (offset < src.length) { 568 this.directives.push(blankLine); 569 } 570 } else { 571 offset += 1; 572 atLineStart = true; 573 } 574 575 break; 576 577 case '#': 578 { 579 var comment = new Comment(); 580 offset = comment.parse({ 581 src: src 582 }, offset); 583 this.directives.push(comment); 584 atLineStart = false; 585 } 586 break; 587 588 case '%': 589 { 590 var directive = new Directive(); 591 offset = directive.parse({ 592 parent: this, 593 src: src 594 }, offset); 595 this.directives.push(directive); 596 hasDirectives = true; 597 atLineStart = false; 598 } 599 break; 600 601 default: 602 if (hasDirectives) { 603 this.error = new YAMLSemanticError(this, 'Missing directives-end indicator line'); 604 } else if (this.directives.length > 0) { 605 this.contents = this.directives; 606 this.directives = []; 607 } 608 609 return offset; 610 } 611 } 612 613 if (src[offset]) { 614 this.directivesEndMarker = new Range(offset, offset + 3); 615 return offset + 3; 616 } 617 618 if (hasDirectives) { 619 this.error = new YAMLSemanticError(this, 'Missing directives-end indicator line'); 620 } else if (this.directives.length > 0) { 621 this.contents = this.directives; 622 this.directives = []; 623 } 624 625 return offset; 626 } 627 }, { 628 key: "parseContents", 629 value: function parseContents(start) { 630 var _this$context = this.context, 631 parseNode = _this$context.parseNode, 632 src = _this$context.src; 633 if (!this.contents) this.contents = []; 634 var lineStart = start; 635 636 while (src[lineStart - 1] === '-') { 637 lineStart -= 1; 638 } 639 640 var offset = Node.endOfWhiteSpace(src, start); 641 var atLineStart = lineStart === start; 642 this.valueRange = new Range(offset); 643 644 while (!Node.atDocumentBoundary(src, offset, Char.DOCUMENT_END)) { 645 switch (src[offset]) { 646 case '\n': 647 if (atLineStart) { 648 var blankLine = new BlankLine(); 649 offset = blankLine.parse({ 650 src: src 651 }, offset); 652 653 if (offset < src.length) { 654 this.contents.push(blankLine); 655 } 656 } else { 657 offset += 1; 658 atLineStart = true; 659 } 660 661 lineStart = offset; 662 break; 663 664 case '#': 665 { 666 var comment = new Comment(); 667 offset = comment.parse({ 668 src: src 669 }, offset); 670 this.contents.push(comment); 671 atLineStart = false; 672 } 673 break; 674 675 default: 676 { 677 var iEnd = Node.endOfIndent(src, offset); 678 var context = { 679 atLineStart: atLineStart, 680 indent: -1, 681 inFlow: false, 682 inCollection: false, 683 lineStart: lineStart, 684 parent: this 685 }; 686 var node = parseNode(context, iEnd); 687 if (!node) return this.valueRange.end = iEnd; // at next document start 688 689 this.contents.push(node); 690 offset = node.range.end; 691 atLineStart = false; 692 var ec = grabCollectionEndComments(node); 693 if (ec) Array.prototype.push.apply(this.contents, ec); 694 } 695 } 696 697 offset = Document.startCommentOrEndBlankLine(src, offset); 698 } 699 700 this.valueRange.end = offset; 701 702 if (src[offset]) { 703 this.documentEndMarker = new Range(offset, offset + 3); 704 offset += 3; 705 706 if (src[offset]) { 707 offset = Node.endOfWhiteSpace(src, offset); 708 709 if (src[offset] === '#') { 710 var _comment = new Comment(); 711 712 offset = _comment.parse({ 713 src: src 714 }, offset); 715 this.contents.push(_comment); 716 } 717 718 switch (src[offset]) { 719 case '\n': 720 offset += 1; 721 break; 722 723 case undefined: 724 break; 725 726 default: 727 this.error = new YAMLSyntaxError(this, 'Document end marker line cannot have a non-comment suffix'); 728 } 729 } 730 } 731 732 return offset; 733 } 734 /** 735 * @param {ParseContext} context 736 * @param {number} start - Index of first character 737 * @returns {number} - Index of the character after this 738 */ 739 740 }, { 741 key: "parse", 742 value: function parse(context, start) { 743 context.root = this; 744 this.context = context; 745 var src = context.src; 746 var offset = src.charCodeAt(start) === 0xfeff ? start + 1 : start; // skip BOM 747 748 offset = this.parseDirectives(offset); 749 offset = this.parseContents(offset); 750 return offset; 751 } 752 }, { 753 key: "setOrigRanges", 754 value: function setOrigRanges(cr, offset) { 755 offset = _get(_getPrototypeOf(Document.prototype), "setOrigRanges", this).call(this, cr, offset); 756 this.directives.forEach(function (node) { 757 offset = node.setOrigRanges(cr, offset); 758 }); 759 if (this.directivesEndMarker) offset = this.directivesEndMarker.setOrigRange(cr, offset); 760 this.contents.forEach(function (node) { 761 offset = node.setOrigRanges(cr, offset); 762 }); 763 if (this.documentEndMarker) offset = this.documentEndMarker.setOrigRange(cr, offset); 764 return offset; 765 } 766 }, { 767 key: "toString", 768 value: function toString() { 769 var contents = this.contents, 770 directives = this.directives, 771 value = this.value; 772 if (value != null) return value; 773 var str = directives.join(''); 774 775 if (contents.length > 0) { 776 if (directives.length > 0 || contents[0].type === Type.COMMENT) str += '---\n'; 777 str += contents.join(''); 778 } 779 780 if (str[str.length - 1] !== '\n') str += '\n'; 781 return str; 782 } 783 }], [{ 784 key: "startCommentOrEndBlankLine", 785 value: function startCommentOrEndBlankLine(src, start) { 786 var offset = Node.endOfWhiteSpace(src, start); 787 var ch = src[offset]; 788 return ch === '#' || ch === '\n' ? offset : start; 789 } 790 }]); 791 792 return Document; 793 }(Node); 794 795 var Alias = /*#__PURE__*/function (_Node) { 796 _inherits(Alias, _Node); 797 798 var _super = _createSuper(Alias); 799 800 function Alias() { 801 _classCallCheck(this, Alias); 802 803 return _super.apply(this, arguments); 804 } 805 806 _createClass(Alias, [{ 807 key: "parse", 808 value: 809 /** 810 * Parses an *alias from the source 811 * 812 * @param {ParseContext} context 813 * @param {number} start - Index of first character 814 * @returns {number} - Index of the character after this scalar 815 */ 816 function parse(context, start) { 817 this.context = context; 818 var src = context.src; 819 var offset = Node.endOfIdentifier(src, start + 1); 820 this.valueRange = new Range(start + 1, offset); 821 offset = Node.endOfWhiteSpace(src, offset); 822 offset = this.parseComment(offset); 823 return offset; 824 } 825 }]); 826 827 return Alias; 828 }(Node); 829 830 var Chomp = { 831 CLIP: 'CLIP', 832 KEEP: 'KEEP', 833 STRIP: 'STRIP' 834 }; 835 var BlockValue = /*#__PURE__*/function (_Node) { 836 _inherits(BlockValue, _Node); 837 838 var _super = _createSuper(BlockValue); 839 840 function BlockValue(type, props) { 841 var _this; 842 843 _classCallCheck(this, BlockValue); 844 845 _this = _super.call(this, type, props); 846 _this.blockIndent = null; 847 _this.chomping = Chomp.CLIP; 848 _this.header = null; 849 return _this; 850 } 851 852 _createClass(BlockValue, [{ 853 key: "includesTrailingLines", 854 get: function get() { 855 return this.chomping === Chomp.KEEP; 856 } 857 }, { 858 key: "strValue", 859 get: function get() { 860 if (!this.valueRange || !this.context) return null; 861 var _this$valueRange = this.valueRange, 862 start = _this$valueRange.start, 863 end = _this$valueRange.end; 864 var _this$context = this.context, 865 indent = _this$context.indent, 866 src = _this$context.src; 867 if (this.valueRange.isEmpty()) return ''; 868 var lastNewLine = null; 869 var ch = src[end - 1]; 870 871 while (ch === '\n' || ch === '\t' || ch === ' ') { 872 end -= 1; 873 874 if (end <= start) { 875 if (this.chomping === Chomp.KEEP) break;else return ''; // probably never happens 876 } 877 878 if (ch === '\n') lastNewLine = end; 879 ch = src[end - 1]; 880 } 881 882 var keepStart = end + 1; 883 884 if (lastNewLine) { 885 if (this.chomping === Chomp.KEEP) { 886 keepStart = lastNewLine; 887 end = this.valueRange.end; 888 } else { 889 end = lastNewLine; 890 } 891 } 892 893 var bi = indent + this.blockIndent; 894 var folded = this.type === Type.BLOCK_FOLDED; 895 var atStart = true; 896 var str = ''; 897 var sep = ''; 898 var prevMoreIndented = false; 899 900 for (var i = start; i < end; ++i) { 901 for (var j = 0; j < bi; ++j) { 902 if (src[i] !== ' ') break; 903 i += 1; 904 } 905 906 var _ch = src[i]; 907 908 if (_ch === '\n') { 909 if (sep === '\n') str += '\n';else sep = '\n'; 910 } else { 911 var lineEnd = Node.endOfLine(src, i); 912 var line = src.slice(i, lineEnd); 913 i = lineEnd; 914 915 if (folded && (_ch === ' ' || _ch === '\t') && i < keepStart) { 916 if (sep === ' ') sep = '\n';else if (!prevMoreIndented && !atStart && sep === '\n') sep = '\n\n'; 917 str += sep + line; //+ ((lineEnd < end && src[lineEnd]) || '') 918 919 sep = lineEnd < end && src[lineEnd] || ''; 920 prevMoreIndented = true; 921 } else { 922 str += sep + line; 923 sep = folded && i < keepStart ? ' ' : '\n'; 924 prevMoreIndented = false; 925 } 926 927 if (atStart && line !== '') atStart = false; 928 } 929 } 930 931 return this.chomping === Chomp.STRIP ? str : str + '\n'; 932 } 933 }, { 934 key: "parseBlockHeader", 935 value: function parseBlockHeader(start) { 936 var src = this.context.src; 937 var offset = start + 1; 938 var bi = ''; 939 940 while (true) { 941 var ch = src[offset]; 942 943 switch (ch) { 944 case '-': 945 this.chomping = Chomp.STRIP; 946 break; 947 948 case '+': 949 this.chomping = Chomp.KEEP; 950 break; 951 952 case '0': 953 case '1': 954 case '2': 955 case '3': 956 case '4': 957 case '5': 958 case '6': 959 case '7': 960 case '8': 961 case '9': 962 bi += ch; 963 break; 964 965 default: 966 this.blockIndent = Number(bi) || null; 967 this.header = new Range(start, offset); 968 return offset; 969 } 970 971 offset += 1; 972 } 973 } 974 }, { 975 key: "parseBlockValue", 976 value: function parseBlockValue(start) { 977 var _this$context2 = this.context, 978 indent = _this$context2.indent, 979 src = _this$context2.src; 980 var explicit = !!this.blockIndent; 981 var offset = start; 982 var valueEnd = start; 983 var minBlockIndent = 1; 984 985 for (var ch = src[offset]; ch === '\n'; ch = src[offset]) { 986 offset += 1; 987 if (Node.atDocumentBoundary(src, offset)) break; 988 var end = Node.endOfBlockIndent(src, indent, offset); // should not include tab? 989 990 if (end === null) break; 991 var _ch2 = src[end]; 992 var lineIndent = end - (offset + indent); 993 994 if (!this.blockIndent) { 995 // no explicit block indent, none yet detected 996 if (src[end] !== '\n') { 997 // first line with non-whitespace content 998 if (lineIndent < minBlockIndent) { 999 var msg = 'Block scalars with more-indented leading empty lines must use an explicit indentation indicator'; 1000 this.error = new YAMLSemanticError(this, msg); 1001 } 1002 1003 this.blockIndent = lineIndent; 1004 } else if (lineIndent > minBlockIndent) { 1005 // empty line with more whitespace 1006 minBlockIndent = lineIndent; 1007 } 1008 } else if (_ch2 && _ch2 !== '\n' && lineIndent < this.blockIndent) { 1009 if (src[end] === '#') break; 1010 1011 if (!this.error) { 1012 var _src = explicit ? 'explicit indentation indicator' : 'first line'; 1013 1014 var _msg = "Block scalars must not be less indented than their ".concat(_src); 1015 1016 this.error = new YAMLSemanticError(this, _msg); 1017 } 1018 } 1019 1020 if (src[end] === '\n') { 1021 offset = end; 1022 } else { 1023 offset = valueEnd = Node.endOfLine(src, end); 1024 } 1025 } 1026 1027 if (this.chomping !== Chomp.KEEP) { 1028 offset = src[valueEnd] ? valueEnd + 1 : valueEnd; 1029 } 1030 1031 this.valueRange = new Range(start + 1, offset); 1032 return offset; 1033 } 1034 /** 1035 * Parses a block value from the source 1036 * 1037 * Accepted forms are: 1038 * ``` 1039 * BS 1040 * block 1041 * lines 1042 * 1043 * BS #comment 1044 * block 1045 * lines 1046 * ``` 1047 * where the block style BS matches the regexp `[|>][-+1-9]*` and block lines 1048 * are empty or have an indent level greater than `indent`. 1049 * 1050 * @param {ParseContext} context 1051 * @param {number} start - Index of first character 1052 * @returns {number} - Index of the character after this block 1053 */ 1054 1055 }, { 1056 key: "parse", 1057 value: function parse(context, start) { 1058 this.context = context; 1059 var src = context.src; 1060 var offset = this.parseBlockHeader(start); 1061 offset = Node.endOfWhiteSpace(src, offset); 1062 offset = this.parseComment(offset); 1063 offset = this.parseBlockValue(offset); 1064 return offset; 1065 } 1066 }, { 1067 key: "setOrigRanges", 1068 value: function setOrigRanges(cr, offset) { 1069 offset = _get(_getPrototypeOf(BlockValue.prototype), "setOrigRanges", this).call(this, cr, offset); 1070 return this.header ? this.header.setOrigRange(cr, offset) : offset; 1071 } 1072 }]); 1073 1074 return BlockValue; 1075 }(Node); 1076 1077 var FlowCollection = /*#__PURE__*/function (_Node) { 1078 _inherits(FlowCollection, _Node); 1079 1080 var _super = _createSuper(FlowCollection); 1081 1082 function FlowCollection(type, props) { 1083 var _this; 1084 1085 _classCallCheck(this, FlowCollection); 1086 1087 _this = _super.call(this, type, props); 1088 _this.items = null; 1089 return _this; 1090 } 1091 1092 _createClass(FlowCollection, [{ 1093 key: "prevNodeIsJsonLike", 1094 value: function prevNodeIsJsonLike() { 1095 var idx = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.items.length; 1096 var node = this.items[idx - 1]; 1097 return !!node && (node.jsonLike || node.type === Type.COMMENT && this.prevNodeIsJsonLike(idx - 1)); 1098 } 1099 /** 1100 * @param {ParseContext} context 1101 * @param {number} start - Index of first character 1102 * @returns {number} - Index of the character after this 1103 */ 1104 1105 }, { 1106 key: "parse", 1107 value: function parse(context, start) { 1108 this.context = context; 1109 var parseNode = context.parseNode, 1110 src = context.src; 1111 var indent = context.indent, 1112 lineStart = context.lineStart; 1113 var char = src[start]; // { or [ 1114 1115 this.items = [{ 1116 char: char, 1117 offset: start 1118 }]; 1119 var offset = Node.endOfWhiteSpace(src, start + 1); 1120 char = src[offset]; 1121 1122 while (char && char !== ']' && char !== '}') { 1123 switch (char) { 1124 case '\n': 1125 { 1126 lineStart = offset + 1; 1127 var wsEnd = Node.endOfWhiteSpace(src, lineStart); 1128 1129 if (src[wsEnd] === '\n') { 1130 var blankLine = new BlankLine(); 1131 lineStart = blankLine.parse({ 1132 src: src 1133 }, lineStart); 1134 this.items.push(blankLine); 1135 } 1136 1137 offset = Node.endOfIndent(src, lineStart); 1138 1139 if (offset <= lineStart + indent) { 1140 char = src[offset]; 1141 1142 if (offset < lineStart + indent || char !== ']' && char !== '}') { 1143 var msg = 'Insufficient indentation in flow collection'; 1144 this.error = new YAMLSemanticError(this, msg); 1145 } 1146 } 1147 } 1148 break; 1149 1150 case ',': 1151 { 1152 this.items.push({ 1153 char: char, 1154 offset: offset 1155 }); 1156 offset += 1; 1157 } 1158 break; 1159 1160 case '#': 1161 { 1162 var comment = new Comment(); 1163 offset = comment.parse({ 1164 src: src 1165 }, offset); 1166 this.items.push(comment); 1167 } 1168 break; 1169 1170 case '?': 1171 case ':': 1172 { 1173 var next = src[offset + 1]; 1174 1175 if (next === '\n' || next === '\t' || next === ' ' || next === ',' || // in-flow : after JSON-like key does not need to be followed by whitespace 1176 char === ':' && this.prevNodeIsJsonLike()) { 1177 this.items.push({ 1178 char: char, 1179 offset: offset 1180 }); 1181 offset += 1; 1182 break; 1183 } 1184 } 1185 // fallthrough 1186 1187 default: 1188 { 1189 var node = parseNode({ 1190 atLineStart: false, 1191 inCollection: false, 1192 inFlow: true, 1193 indent: -1, 1194 lineStart: lineStart, 1195 parent: this 1196 }, offset); 1197 1198 if (!node) { 1199 // at next document start 1200 this.valueRange = new Range(start, offset); 1201 return offset; 1202 } 1203 1204 this.items.push(node); 1205 offset = Node.normalizeOffset(src, node.range.end); 1206 } 1207 } 1208 1209 offset = Node.endOfWhiteSpace(src, offset); 1210 char = src[offset]; 1211 } 1212 1213 this.valueRange = new Range(start, offset + 1); 1214 1215 if (char) { 1216 this.items.push({ 1217 char: char, 1218 offset: offset 1219 }); 1220 offset = Node.endOfWhiteSpace(src, offset + 1); 1221 offset = this.parseComment(offset); 1222 } 1223 1224 return offset; 1225 } 1226 }, { 1227 key: "setOrigRanges", 1228 value: function setOrigRanges(cr, offset) { 1229 offset = _get(_getPrototypeOf(FlowCollection.prototype), "setOrigRanges", this).call(this, cr, offset); 1230 this.items.forEach(function (node) { 1231 if (node instanceof Node) { 1232 offset = node.setOrigRanges(cr, offset); 1233 } else if (cr.length === 0) { 1234 node.origOffset = node.offset; 1235 } else { 1236 var i = offset; 1237 1238 while (i < cr.length) { 1239 if (cr[i] > node.offset) break;else ++i; 1240 } 1241 1242 node.origOffset = node.offset + i; 1243 offset = i; 1244 } 1245 }); 1246 return offset; 1247 } 1248 }, { 1249 key: "toString", 1250 value: function toString() { 1251 var src = this.context.src, 1252 items = this.items, 1253 range = this.range, 1254 value = this.value; 1255 if (value != null) return value; 1256 var nodes = items.filter(function (item) { 1257 return item instanceof Node; 1258 }); 1259 var str = ''; 1260 var prevEnd = range.start; 1261 nodes.forEach(function (node) { 1262 var prefix = src.slice(prevEnd, node.range.start); 1263 prevEnd = node.range.end; 1264 str += prefix + String(node); 1265 1266 if (str[str.length - 1] === '\n' && src[prevEnd - 1] !== '\n' && src[prevEnd] === '\n') { 1267 // Comment range does not include the terminal newline, but its 1268 // stringified value does. Without this fix, newlines at comment ends 1269 // get duplicated. 1270 prevEnd += 1; 1271 } 1272 }); 1273 str += src.slice(prevEnd, range.end); 1274 return Node.addStringTerminator(src, range.end, str); 1275 } 1276 }]); 1277 1278 return FlowCollection; 1279 }(Node); 1280 1281 var QuoteDouble = /*#__PURE__*/function (_Node) { 1282 _inherits(QuoteDouble, _Node); 1283 1284 var _super = _createSuper(QuoteDouble); 1285 1286 function QuoteDouble() { 1287 _classCallCheck(this, QuoteDouble); 1288 1289 return _super.apply(this, arguments); 1290 } 1291 1292 _createClass(QuoteDouble, [{ 1293 key: "strValue", 1294 get: 1295 /** 1296 * @returns {string | { str: string, errors: YAMLSyntaxError[] }} 1297 */ 1298 function get() { 1299 if (!this.valueRange || !this.context) return null; 1300 var errors = []; 1301 var _this$valueRange = this.valueRange, 1302 start = _this$valueRange.start, 1303 end = _this$valueRange.end; 1304 var _this$context = this.context, 1305 indent = _this$context.indent, 1306 src = _this$context.src; 1307 if (src[end - 1] !== '"') errors.push(new YAMLSyntaxError(this, 'Missing closing "quote')); // Using String#replace is too painful with escaped newlines preceded by 1308 // escaped backslashes; also, this should be faster. 1309 1310 var str = ''; 1311 1312 for (var i = start + 1; i < end - 1; ++i) { 1313 var ch = src[i]; 1314 1315 if (ch === '\n') { 1316 if (Node.atDocumentBoundary(src, i + 1)) errors.push(new YAMLSemanticError(this, 'Document boundary indicators are not allowed within string values')); 1317 1318 var _Node$foldNewline = Node.foldNewline(src, i, indent), 1319 fold = _Node$foldNewline.fold, 1320 offset = _Node$foldNewline.offset, 1321 error = _Node$foldNewline.error; 1322 1323 str += fold; 1324 i = offset; 1325 if (error) errors.push(new YAMLSemanticError(this, 'Multi-line double-quoted string needs to be sufficiently indented')); 1326 } else if (ch === '\\') { 1327 i += 1; 1328 1329 switch (src[i]) { 1330 case '0': 1331 str += '\0'; 1332 break; 1333 // null character 1334 1335 case 'a': 1336 str += '\x07'; 1337 break; 1338 // bell character 1339 1340 case 'b': 1341 str += '\b'; 1342 break; 1343 // backspace 1344 1345 case 'e': 1346 str += '\x1b'; 1347 break; 1348 // escape character 1349 1350 case 'f': 1351 str += '\f'; 1352 break; 1353 // form feed 1354 1355 case 'n': 1356 str += '\n'; 1357 break; 1358 // line feed 1359 1360 case 'r': 1361 str += '\r'; 1362 break; 1363 // carriage return 1364 1365 case 't': 1366 str += '\t'; 1367 break; 1368 // horizontal tab 1369 1370 case 'v': 1371 str += '\v'; 1372 break; 1373 // vertical tab 1374 1375 case 'N': 1376 str += "\x85"; 1377 break; 1378 // Unicode next line 1379 1380 case '_': 1381 str += "\xA0"; 1382 break; 1383 // Unicode non-breaking space 1384 1385 case 'L': 1386 str += "\u2028"; 1387 break; 1388 // Unicode line separator 1389 1390 case 'P': 1391 str += "\u2029"; 1392 break; 1393 // Unicode paragraph separator 1394 1395 case ' ': 1396 str += ' '; 1397 break; 1398 1399 case '"': 1400 str += '"'; 1401 break; 1402 1403 case '/': 1404 str += '/'; 1405 break; 1406 1407 case '\\': 1408 str += '\\'; 1409 break; 1410 1411 case '\t': 1412 str += '\t'; 1413 break; 1414 1415 case 'x': 1416 str += this.parseCharCode(i + 1, 2, errors); 1417 i += 2; 1418 break; 1419 1420 case 'u': 1421 str += this.parseCharCode(i + 1, 4, errors); 1422 i += 4; 1423 break; 1424 1425 case 'U': 1426 str += this.parseCharCode(i + 1, 8, errors); 1427 i += 8; 1428 break; 1429 1430 case '\n': 1431 // skip escaped newlines, but still trim the following line 1432 while (src[i + 1] === ' ' || src[i + 1] === '\t') { 1433 i += 1; 1434 } 1435 1436 break; 1437 1438 default: 1439 errors.push(new YAMLSyntaxError(this, "Invalid escape sequence ".concat(src.substr(i - 1, 2)))); 1440 str += '\\' + src[i]; 1441 } 1442 } else if (ch === ' ' || ch === '\t') { 1443 // trim trailing whitespace 1444 var wsStart = i; 1445 var next = src[i + 1]; 1446 1447 while (next === ' ' || next === '\t') { 1448 i += 1; 1449 next = src[i + 1]; 1450 } 1451 1452 if (next !== '\n') str += i > wsStart ? src.slice(wsStart, i + 1) : ch; 1453 } else { 1454 str += ch; 1455 } 1456 } 1457 1458 return errors.length > 0 ? { 1459 errors: errors, 1460 str: str 1461 } : str; 1462 } 1463 }, { 1464 key: "parseCharCode", 1465 value: function parseCharCode(offset, length, errors) { 1466 var src = this.context.src; 1467 var cc = src.substr(offset, length); 1468 var ok = cc.length === length && /^[0-9a-fA-F]+$/.test(cc); 1469 var code = ok ? parseInt(cc, 16) : NaN; 1470 1471 if (isNaN(code)) { 1472 errors.push(new YAMLSyntaxError(this, "Invalid escape sequence ".concat(src.substr(offset - 2, length + 2)))); 1473 return src.substr(offset - 2, length + 2); 1474 } 1475 1476 return String.fromCodePoint(code); 1477 } 1478 /** 1479 * Parses a "double quoted" value from the source 1480 * 1481 * @param {ParseContext} context 1482 * @param {number} start - Index of first character 1483 * @returns {number} - Index of the character after this scalar 1484 */ 1485 1486 }, { 1487 key: "parse", 1488 value: function parse(context, start) { 1489 this.context = context; 1490 var src = context.src; 1491 var offset = QuoteDouble.endOfQuote(src, start + 1); 1492 this.valueRange = new Range(start, offset); 1493 offset = Node.endOfWhiteSpace(src, offset); 1494 offset = this.parseComment(offset); 1495 return offset; 1496 } 1497 }], [{ 1498 key: "endOfQuote", 1499 value: function endOfQuote(src, offset) { 1500 var ch = src[offset]; 1501 1502 while (ch && ch !== '"') { 1503 offset += ch === '\\' ? 2 : 1; 1504 ch = src[offset]; 1505 } 1506 1507 return offset + 1; 1508 } 1509 }]); 1510 1511 return QuoteDouble; 1512 }(Node); 1513 1514 var QuoteSingle = /*#__PURE__*/function (_Node) { 1515 _inherits(QuoteSingle, _Node); 1516 1517 var _super = _createSuper(QuoteSingle); 1518 1519 function QuoteSingle() { 1520 _classCallCheck(this, QuoteSingle); 1521 1522 return _super.apply(this, arguments); 1523 } 1524 1525 _createClass(QuoteSingle, [{ 1526 key: "strValue", 1527 get: 1528 /** 1529 * @returns {string | { str: string, errors: YAMLSyntaxError[] }} 1530 */ 1531 function get() { 1532 if (!this.valueRange || !this.context) return null; 1533 var errors = []; 1534 var _this$valueRange = this.valueRange, 1535 start = _this$valueRange.start, 1536 end = _this$valueRange.end; 1537 var _this$context = this.context, 1538 indent = _this$context.indent, 1539 src = _this$context.src; 1540 if (src[end - 1] !== "'") errors.push(new YAMLSyntaxError(this, "Missing closing 'quote")); 1541 var str = ''; 1542 1543 for (var i = start + 1; i < end - 1; ++i) { 1544 var ch = src[i]; 1545 1546 if (ch === '\n') { 1547 if (Node.atDocumentBoundary(src, i + 1)) errors.push(new YAMLSemanticError(this, 'Document boundary indicators are not allowed within string values')); 1548 1549 var _Node$foldNewline = Node.foldNewline(src, i, indent), 1550 fold = _Node$foldNewline.fold, 1551 offset = _Node$foldNewline.offset, 1552 error = _Node$foldNewline.error; 1553 1554 str += fold; 1555 i = offset; 1556 if (error) errors.push(new YAMLSemanticError(this, 'Multi-line single-quoted string needs to be sufficiently indented')); 1557 } else if (ch === "'") { 1558 str += ch; 1559 i += 1; 1560 if (src[i] !== "'") errors.push(new YAMLSyntaxError(this, 'Unescaped single quote? This should not happen.')); 1561 } else if (ch === ' ' || ch === '\t') { 1562 // trim trailing whitespace 1563 var wsStart = i; 1564 var next = src[i + 1]; 1565 1566 while (next === ' ' || next === '\t') { 1567 i += 1; 1568 next = src[i + 1]; 1569 } 1570 1571 if (next !== '\n') str += i > wsStart ? src.slice(wsStart, i + 1) : ch; 1572 } else { 1573 str += ch; 1574 } 1575 } 1576 1577 return errors.length > 0 ? { 1578 errors: errors, 1579 str: str 1580 } : str; 1581 } 1582 /** 1583 * Parses a 'single quoted' value from the source 1584 * 1585 * @param {ParseContext} context 1586 * @param {number} start - Index of first character 1587 * @returns {number} - Index of the character after this scalar 1588 */ 1589 1590 }, { 1591 key: "parse", 1592 value: function parse(context, start) { 1593 this.context = context; 1594 var src = context.src; 1595 var offset = QuoteSingle.endOfQuote(src, start + 1); 1596 this.valueRange = new Range(start, offset); 1597 offset = Node.endOfWhiteSpace(src, offset); 1598 offset = this.parseComment(offset); 1599 return offset; 1600 } 1601 }], [{ 1602 key: "endOfQuote", 1603 value: function endOfQuote(src, offset) { 1604 var ch = src[offset]; 1605 1606 while (ch) { 1607 if (ch === "'") { 1608 if (src[offset + 1] !== "'") break; 1609 ch = src[offset += 2]; 1610 } else { 1611 ch = src[offset += 1]; 1612 } 1613 } 1614 1615 return offset + 1; 1616 } 1617 }]); 1618 1619 return QuoteSingle; 1620 }(Node); 1621 1622 function createNewNode(type, props) { 1623 switch (type) { 1624 case Type.ALIAS: 1625 return new Alias(type, props); 1626 1627 case Type.BLOCK_FOLDED: 1628 case Type.BLOCK_LITERAL: 1629 return new BlockValue(type, props); 1630 1631 case Type.FLOW_MAP: 1632 case Type.FLOW_SEQ: 1633 return new FlowCollection(type, props); 1634 1635 case Type.MAP_KEY: 1636 case Type.MAP_VALUE: 1637 case Type.SEQ_ITEM: 1638 return new CollectionItem(type, props); 1639 1640 case Type.COMMENT: 1641 case Type.PLAIN: 1642 return new PlainValue(type, props); 1643 1644 case Type.QUOTE_DOUBLE: 1645 return new QuoteDouble(type, props); 1646 1647 case Type.QUOTE_SINGLE: 1648 return new QuoteSingle(type, props); 1649 1650 /* istanbul ignore next */ 1651 1652 default: 1653 return null; 1654 // should never happen 1655 } 1656 } 1657 /** 1658 * @param {boolean} atLineStart - Node starts at beginning of line 1659 * @param {boolean} inFlow - true if currently in a flow context 1660 * @param {boolean} inCollection - true if currently in a collection context 1661 * @param {number} indent - Current level of indentation 1662 * @param {number} lineStart - Start of the current line 1663 * @param {Node} parent - The parent of the node 1664 * @param {string} src - Source of the YAML document 1665 */ 1666 1667 1668 var ParseContext = /*#__PURE__*/function () { 1669 function ParseContext() { 1670 var _this = this; 1671 1672 var orig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; 1673 1674 var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, 1675 atLineStart = _ref.atLineStart, 1676 inCollection = _ref.inCollection, 1677 inFlow = _ref.inFlow, 1678 indent = _ref.indent, 1679 lineStart = _ref.lineStart, 1680 parent = _ref.parent; 1681 1682 _classCallCheck(this, ParseContext); 1683 1684 _defineProperty(this, "parseNode", function (overlay, start) { 1685 if (Node.atDocumentBoundary(_this.src, start)) return null; 1686 var context = new ParseContext(_this, overlay); 1687 1688 var _context$parseProps = context.parseProps(start), 1689 props = _context$parseProps.props, 1690 type = _context$parseProps.type, 1691 valueStart = _context$parseProps.valueStart; 1692 1693 var node = createNewNode(type, props); 1694 var offset = node.parse(context, valueStart); 1695 node.range = new Range(start, offset); 1696 /* istanbul ignore if */ 1697 1698 if (offset <= start) { 1699 // This should never happen, but if it does, let's make sure to at least 1700 // step one character forward to avoid a busy loop. 1701 node.error = new Error("Node#parse consumed no characters"); 1702 node.error.parseEnd = offset; 1703 node.error.source = node; 1704 node.range.end = start + 1; 1705 } 1706 1707 if (context.nodeStartsCollection(node)) { 1708 if (!node.error && !context.atLineStart && context.parent.type === Type.DOCUMENT) { 1709 node.error = new YAMLSyntaxError(node, 'Block collection must not have preceding content here (e.g. directives-end indicator)'); 1710 } 1711 1712 var collection = new Collection(node); 1713 offset = collection.parse(new ParseContext(context), offset); 1714 collection.range = new Range(start, offset); 1715 return collection; 1716 } 1717 1718 return node; 1719 }); 1720 1721 this.atLineStart = atLineStart != null ? atLineStart : orig.atLineStart || false; 1722 this.inCollection = inCollection != null ? inCollection : orig.inCollection || false; 1723 this.inFlow = inFlow != null ? inFlow : orig.inFlow || false; 1724 this.indent = indent != null ? indent : orig.indent; 1725 this.lineStart = lineStart != null ? lineStart : orig.lineStart; 1726 this.parent = parent != null ? parent : orig.parent || {}; 1727 this.root = orig.root; 1728 this.src = orig.src; 1729 } 1730 1731 _createClass(ParseContext, [{ 1732 key: "nodeStartsCollection", 1733 value: function nodeStartsCollection(node) { 1734 var inCollection = this.inCollection, 1735 inFlow = this.inFlow, 1736 src = this.src; 1737 if (inCollection || inFlow) return false; 1738 if (node instanceof CollectionItem) return true; // check for implicit key 1739 1740 var offset = node.range.end; 1741 if (src[offset] === '\n' || src[offset - 1] === '\n') return false; 1742 offset = Node.endOfWhiteSpace(src, offset); 1743 return src[offset] === ':'; 1744 } // Anchor and tag are before type, which determines the node implementation 1745 // class; hence this intermediate step. 1746 1747 }, { 1748 key: "parseProps", 1749 value: function parseProps(offset) { 1750 var inFlow = this.inFlow, 1751 parent = this.parent, 1752 src = this.src; 1753 var props = []; 1754 var lineHasProps = false; 1755 offset = this.atLineStart ? Node.endOfIndent(src, offset) : Node.endOfWhiteSpace(src, offset); 1756 var ch = src[offset]; 1757 1758 while (ch === Char.ANCHOR || ch === Char.COMMENT || ch === Char.TAG || ch === '\n') { 1759 if (ch === '\n') { 1760 var inEnd = offset; 1761 var lineStart = void 0; 1762 1763 do { 1764 lineStart = inEnd + 1; 1765 inEnd = Node.endOfIndent(src, lineStart); 1766 } while (src[inEnd] === '\n'); 1767 1768 var indentDiff = inEnd - (lineStart + this.indent); 1769 var noIndicatorAsIndent = parent.type === Type.SEQ_ITEM && parent.context.atLineStart; 1770 if (src[inEnd] !== '#' && !Node.nextNodeIsIndented(src[inEnd], indentDiff, !noIndicatorAsIndent)) break; 1771 this.atLineStart = true; 1772 this.lineStart = lineStart; 1773 lineHasProps = false; 1774 offset = inEnd; 1775 } else if (ch === Char.COMMENT) { 1776 var end = Node.endOfLine(src, offset + 1); 1777 props.push(new Range(offset, end)); 1778 offset = end; 1779 } else { 1780 var _end = Node.endOfIdentifier(src, offset + 1); 1781 1782 if (ch === Char.TAG && src[_end] === ',' && /^[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+,\d\d\d\d(-\d\d){0,2}\/\S/.test(src.slice(offset + 1, _end + 13))) { 1783 // Let's presume we're dealing with a YAML 1.0 domain tag here, rather 1784 // than an empty but 'foo.bar' private-tagged node in a flow collection 1785 // followed without whitespace by a plain string starting with a year 1786 // or date divided by something. 1787 _end = Node.endOfIdentifier(src, _end + 5); 1788 } 1789 1790 props.push(new Range(offset, _end)); 1791 lineHasProps = true; 1792 offset = Node.endOfWhiteSpace(src, _end); 1793 } 1794 1795 ch = src[offset]; 1796 } // '- &a : b' has an anchor on an empty node 1797 1798 1799 if (lineHasProps && ch === ':' && Node.atBlank(src, offset + 1, true)) offset -= 1; 1800 var type = ParseContext.parseType(src, offset, inFlow); 1801 return { 1802 props: props, 1803 type: type, 1804 valueStart: offset 1805 }; 1806 } 1807 /** 1808 * Parses a node from the source 1809 * @param {ParseContext} overlay 1810 * @param {number} start - Index of first non-whitespace character for the node 1811 * @returns {?Node} - null if at a document boundary 1812 */ 1813 1814 }], [{ 1815 key: "parseType", 1816 value: function parseType(src, offset, inFlow) { 1817 switch (src[offset]) { 1818 case '*': 1819 return Type.ALIAS; 1820 1821 case '>': 1822 return Type.BLOCK_FOLDED; 1823 1824 case '|': 1825 return Type.BLOCK_LITERAL; 1826 1827 case '{': 1828 return Type.FLOW_MAP; 1829 1830 case '[': 1831 return Type.FLOW_SEQ; 1832 1833 case '?': 1834 return !inFlow && Node.atBlank(src, offset + 1, true) ? Type.MAP_KEY : Type.PLAIN; 1835 1836 case ':': 1837 return !inFlow && Node.atBlank(src, offset + 1, true) ? Type.MAP_VALUE : Type.PLAIN; 1838 1839 case '-': 1840 return !inFlow && Node.atBlank(src, offset + 1, true) ? Type.SEQ_ITEM : Type.PLAIN; 1841 1842 case '"': 1843 return Type.QUOTE_DOUBLE; 1844 1845 case "'": 1846 return Type.QUOTE_SINGLE; 1847 1848 default: 1849 return Type.PLAIN; 1850 } 1851 } 1852 }]); 1853 1854 return ParseContext; 1855 }(); 1856 1857 // Published as 'yaml/parse-cst' 1858 function parse(src) { 1859 var cr = []; 1860 1861 if (src.indexOf('\r') !== -1) { 1862 src = src.replace(/\r\n?/g, function (match, offset) { 1863 if (match.length > 1) cr.push(offset); 1864 return '\n'; 1865 }); 1866 } 1867 1868 var documents = []; 1869 var offset = 0; 1870 1871 do { 1872 var doc = new Document(); 1873 var context = new ParseContext({ 1874 src: src 1875 }); 1876 offset = doc.parse(context, offset); 1877 documents.push(doc); 1878 } while (offset < src.length); 1879 1880 documents.setOrigRanges = function () { 1881 if (cr.length === 0) return false; 1882 1883 for (var i = 1; i < cr.length; ++i) { 1884 cr[i] -= i; 1885 } 1886 1887 var crOffset = 0; 1888 1889 for (var _i = 0; _i < documents.length; ++_i) { 1890 crOffset = documents[_i].setOrigRanges(cr, crOffset); 1891 } 1892 1893 cr.splice(0, cr.length); 1894 return true; 1895 }; 1896 1897 documents.toString = function () { 1898 return documents.join('...\n'); 1899 }; 1900 1901 return documents; 1902 } 1903 1904 export { parse };