import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _get from "@babel/runtime/helpers/get"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import { Char, Type } from '../constants'; import { YAMLSemanticError, YAMLSyntaxError } from '../errors'; import BlankLine from './BlankLine'; import { grabCollectionEndComments } from './Collection'; import Comment from './Comment'; import Directive from './Directive'; import Node from './Node'; import Range from './Range'; var Document = /*#__PURE__*/function (_Node) { _inherits(Document, _Node); _createClass(Document, null, [{ key: "startCommentOrEndBlankLine", value: function startCommentOrEndBlankLine(src, start) { var offset = Node.endOfWhiteSpace(src, start); var ch = src[offset]; return ch === '#' || ch === '\n' ? offset : start; } }]); function Document() { var _this; _classCallCheck(this, Document); _this = _possibleConstructorReturn(this, _getPrototypeOf(Document).call(this, Type.DOCUMENT)); _this.directives = null; _this.contents = null; _this.directivesEndMarker = null; _this.documentEndMarker = null; return _this; } _createClass(Document, [{ key: "parseDirectives", value: function parseDirectives(start) { var src = this.context.src; this.directives = []; var atLineStart = true; var hasDirectives = false; var offset = start; while (!Node.atDocumentBoundary(src, offset, Char.DIRECTIVES_END)) { offset = Document.startCommentOrEndBlankLine(src, offset); switch (src[offset]) { case '\n': if (atLineStart) { var blankLine = new BlankLine(); offset = blankLine.parse({ src: src }, offset); if (offset < src.length) { this.directives.push(blankLine); } } else { offset += 1; atLineStart = true; } break; case '#': { var comment = new Comment(); offset = comment.parse({ src: src }, offset); this.directives.push(comment); atLineStart = false; } break; case '%': { var directive = new Directive(); offset = directive.parse({ parent: this, src: src }, offset); this.directives.push(directive); hasDirectives = true; atLineStart = false; } break; default: if (hasDirectives) { this.error = new YAMLSemanticError(this, 'Missing directives-end indicator line'); } else if (this.directives.length > 0) { this.contents = this.directives; this.directives = []; } return offset; } } if (src[offset]) { this.directivesEndMarker = new Range(offset, offset + 3); return offset + 3; } if (hasDirectives) { this.error = new YAMLSemanticError(this, 'Missing directives-end indicator line'); } else if (this.directives.length > 0) { this.contents = this.directives; this.directives = []; } return offset; } }, { key: "parseContents", value: function parseContents(start) { var _this$context = this.context, parseNode = _this$context.parseNode, src = _this$context.src; if (!this.contents) this.contents = []; var lineStart = start; while (src[lineStart - 1] === '-') { lineStart -= 1; } var offset = Node.endOfWhiteSpace(src, start); var atLineStart = lineStart === start; this.valueRange = new Range(offset); while (!Node.atDocumentBoundary(src, offset, Char.DOCUMENT_END)) { switch (src[offset]) { case '\n': if (atLineStart) { var blankLine = new BlankLine(); offset = blankLine.parse({ src: src }, offset); if (offset < src.length) { this.contents.push(blankLine); } } else { offset += 1; atLineStart = true; } lineStart = offset; break; case '#': { var comment = new Comment(); offset = comment.parse({ src: src }, offset); this.contents.push(comment); atLineStart = false; } break; default: { var iEnd = Node.endOfIndent(src, offset); var context = { atLineStart: atLineStart, indent: -1, inFlow: false, inCollection: false, lineStart: lineStart, parent: this }; var node = parseNode(context, iEnd); if (!node) return this.valueRange.end = iEnd; // at next document start this.contents.push(node); offset = node.range.end; atLineStart = false; var ec = grabCollectionEndComments(node); if (ec) Array.prototype.push.apply(this.contents, ec); } } offset = Document.startCommentOrEndBlankLine(src, offset); } this.valueRange.end = offset; if (src[offset]) { this.documentEndMarker = new Range(offset, offset + 3); offset += 3; if (src[offset]) { offset = Node.endOfWhiteSpace(src, offset); if (src[offset] === '#') { var _comment = new Comment(); offset = _comment.parse({ src: src }, offset); this.contents.push(_comment); } switch (src[offset]) { case '\n': offset += 1; break; case undefined: break; default: this.error = new YAMLSyntaxError(this, 'Document end marker line cannot have a non-comment suffix'); } } } return offset; } /** * @param {ParseContext} context * @param {number} start - Index of first character * @returns {number} - Index of the character after this */ }, { key: "parse", value: function parse(context, start) { context.root = this; this.context = context; var src = context.src; var offset = src.charCodeAt(start) === 0xfeff ? start + 1 : start; // skip BOM offset = this.parseDirectives(offset); offset = this.parseContents(offset); return offset; } }, { key: "setOrigRanges", value: function setOrigRanges(cr, offset) { offset = _get(_getPrototypeOf(Document.prototype), "setOrigRanges", this).call(this, cr, offset); this.directives.forEach(function (node) { offset = node.setOrigRanges(cr, offset); }); if (this.directivesEndMarker) offset = this.directivesEndMarker.setOrigRange(cr, offset); this.contents.forEach(function (node) { offset = node.setOrigRanges(cr, offset); }); if (this.documentEndMarker) offset = this.documentEndMarker.setOrigRange(cr, offset); return offset; } }, { key: "toString", value: function toString() { var contents = this.contents, directives = this.directives, value = this.value; if (value != null) return value; var str = directives.join(''); if (contents.length > 0) { if (directives.length > 0 || contents[0].type === Type.COMMENT) str += '---\n'; str += contents.join(''); } if (str[str.length - 1] !== '\n') str += '\n'; return str; } }]); return Document; }(Node); export { Document as default };