UNPKG

jupystar

Version:

Converter from Jupyter notebook (ipynb) to Starboard notebook

2,001 lines (1,671 loc) 221 kB
const Char = { ANCHOR: '&', COMMENT: '#', TAG: '!', DIRECTIVES_END: '-', DOCUMENT_END: '.' }; const LogLevel = Object.assign(['silent', 'error', 'warn', 'debug'], { SILENT: 0, ERROR: 1, WARN: 2, DEBUG: 3 }); const Type = { ALIAS: 'ALIAS', BLANK_LINE: 'BLANK_LINE', BLOCK_FOLDED: 'BLOCK_FOLDED', BLOCK_LITERAL: 'BLOCK_LITERAL', COMMENT: 'COMMENT', DIRECTIVE: 'DIRECTIVE', DOCUMENT: 'DOCUMENT', FLOW_MAP: 'FLOW_MAP', FLOW_SEQ: 'FLOW_SEQ', MAP: 'MAP', MAP_KEY: 'MAP_KEY', MAP_VALUE: 'MAP_VALUE', PLAIN: 'PLAIN', QUOTE_DOUBLE: 'QUOTE_DOUBLE', QUOTE_SINGLE: 'QUOTE_SINGLE', SEQ: 'SEQ', SEQ_ITEM: 'SEQ_ITEM' }; const defaultTagPrefix = 'tag:yaml.org,2002:'; const defaultTags = { MAP: 'tag:yaml.org,2002:map', SEQ: 'tag:yaml.org,2002:seq', STR: 'tag:yaml.org,2002:str' }; var Char_1 = Char; var LogLevel_1 = LogLevel; var Type_1 = Type; var defaultTagPrefix_1 = defaultTagPrefix; var defaultTags_1 = defaultTags; var constants = { Char: Char_1, LogLevel: LogLevel_1, Type: Type_1, defaultTagPrefix: defaultTagPrefix_1, defaultTags: defaultTags_1 }; function findLineStarts(src) { const ls = [0]; let offset = src.indexOf('\n'); while (offset !== -1) { offset += 1; ls.push(offset); offset = src.indexOf('\n', offset); } return ls; } function getSrcInfo(cst) { let lineStarts, src; if (typeof cst === 'string') { lineStarts = findLineStarts(cst); src = cst; } else { if (Array.isArray(cst)) cst = cst[0]; if (cst && cst.context) { if (!cst.lineStarts) cst.lineStarts = findLineStarts(cst.context.src); lineStarts = cst.lineStarts; src = cst.context.src; } } return { lineStarts, src }; } /** * @typedef {Object} LinePos - One-indexed position in the source * @property {number} line * @property {number} col */ /** * Determine the line/col position matching a character offset. * * Accepts a source string or a CST document as the second parameter. With * the latter, starting indices for lines are cached in the document as * `lineStarts: number[]`. * * Returns a one-indexed `{ line, col }` location if found, or * `undefined` otherwise. * * @param {number} offset * @param {string|Document|Document[]} cst * @returns {?LinePos} */ function getLinePos(offset, cst) { if (typeof offset !== 'number' || offset < 0) return null; const { lineStarts, src } = getSrcInfo(cst); if (!lineStarts || !src || offset > src.length) return null; for (let i = 0; i < lineStarts.length; ++i) { const start = lineStarts[i]; if (offset < start) { return { line: i, col: offset - lineStarts[i - 1] + 1 }; } if (offset === start) return { line: i + 1, col: 1 }; } const line = lineStarts.length; return { line, col: offset - lineStarts[line - 1] + 1 }; } /** * Get a specified line from the source. * * Accepts a source string or a CST document as the second parameter. With * the latter, starting indices for lines are cached in the document as * `lineStarts: number[]`. * * Returns the line as a string if found, or `null` otherwise. * * @param {number} line One-indexed line number * @param {string|Document|Document[]} cst * @returns {?string} */ function getLine(line, cst) { const { lineStarts, src } = getSrcInfo(cst); if (!lineStarts || !(line >= 1) || line > lineStarts.length) return null; const start = lineStarts[line - 1]; let end = lineStarts[line]; // undefined for last line; that's ok for slice() while (end && end > start && src[end - 1] === '\n') --end; return src.slice(start, end); } /** * Pretty-print the starting line from the source indicated by the range `pos` * * Trims output to `maxWidth` chars while keeping the starting column visible, * using `…` at either end to indicate dropped characters. * * Returns a two-line string (or `null`) with `\n` as separator; the second line * will hold appropriately indented `^` marks indicating the column range. * * @param {Object} pos * @param {LinePos} pos.start * @param {LinePos} [pos.end] * @param {string|Document|Document[]*} cst * @param {number} [maxWidth=80] * @returns {?string} */ function getPrettyContext({ start, end }, cst, maxWidth = 80) { let src = getLine(start.line, cst); if (!src) return null; let { col } = start; if (src.length > maxWidth) { if (col <= maxWidth - 10) { src = src.substr(0, maxWidth - 1) + '…'; } else { const halfWidth = Math.round(maxWidth / 2); if (src.length > col + halfWidth) src = src.substr(0, col + halfWidth - 1) + '…'; col -= src.length - maxWidth; src = '…' + src.substr(1 - maxWidth); } } let errLen = 1; let errEnd = ''; if (end) { if (end.line === start.line && col + (end.col - start.col) <= maxWidth + 1) { errLen = end.col - start.col; } else { errLen = Math.min(src.length + 1, maxWidth) - col; errEnd = '…'; } } const offset = col > 1 ? ' '.repeat(col - 1) : ''; const err = '^'.repeat(errLen); return `${src}\n${offset}${err}${errEnd}`; } var getLine_1 = getLine; var getLinePos_1 = getLinePos; var getPrettyContext_1 = getPrettyContext; var sourceUtils = { getLine: getLine_1, getLinePos: getLinePos_1, getPrettyContext: getPrettyContext_1 }; class Range { static copy(orig) { return new Range(orig.start, orig.end); } constructor(start, end) { this.start = start; this.end = end || start; } isEmpty() { return typeof this.start !== 'number' || !this.end || this.end <= this.start; } /** * Set `origStart` and `origEnd` to point to the original source range for * this node, which may differ due to dropped CR characters. * * @param {number[]} cr - Positions of dropped CR characters * @param {number} offset - Starting index of `cr` from the last call * @returns {number} - The next offset, matching the one found for `origStart` */ setOrigRange(cr, offset) { const { start, end } = this; if (cr.length === 0 || end <= cr[0]) { this.origStart = start; this.origEnd = end; return offset; } let i = offset; while (i < cr.length) { if (cr[i] > start) break;else ++i; } this.origStart = start + i; const nextOffset = i; while (i < cr.length) { // if end was at \n, it should now be at \r if (cr[i] >= end) break;else ++i; } this.origEnd = end + i; return nextOffset; } } var Range_2 = Range; var Range_1 = { Range: Range_2 }; /** Root class of all nodes */ class Node { static addStringTerminator(src, offset, str) { if (str[str.length - 1] === '\n') return str; const next = Node.endOfWhiteSpace(src, offset); return next >= src.length || src[next] === '\n' ? str + '\n' : str; } // ^(---|...) static atDocumentBoundary(src, offset, sep) { const ch0 = src[offset]; if (!ch0) return true; const prev = src[offset - 1]; if (prev && prev !== '\n') return false; if (sep) { if (ch0 !== sep) return false; } else { if (ch0 !== constants.Char.DIRECTIVES_END && ch0 !== constants.Char.DOCUMENT_END) return false; } const ch1 = src[offset + 1]; const ch2 = src[offset + 2]; if (ch1 !== ch0 || ch2 !== ch0) return false; const ch3 = src[offset + 3]; return !ch3 || ch3 === '\n' || ch3 === '\t' || ch3 === ' '; } static endOfIdentifier(src, offset) { let ch = src[offset]; const isVerbatim = ch === '<'; const notOk = isVerbatim ? ['\n', '\t', ' ', '>'] : ['\n', '\t', ' ', '[', ']', '{', '}', ',']; while (ch && notOk.indexOf(ch) === -1) ch = src[offset += 1]; if (isVerbatim && ch === '>') offset += 1; return offset; } static endOfIndent(src, offset) { let ch = src[offset]; while (ch === ' ') ch = src[offset += 1]; return offset; } static endOfLine(src, offset) { let ch = src[offset]; while (ch && ch !== '\n') ch = src[offset += 1]; return offset; } static endOfWhiteSpace(src, offset) { let ch = src[offset]; while (ch === '\t' || ch === ' ') ch = src[offset += 1]; return offset; } static startOfLine(src, offset) { let ch = src[offset - 1]; if (ch === '\n') return offset; while (ch && ch !== '\n') ch = src[offset -= 1]; return offset + 1; } /** * End of indentation, or null if the line's indent level is not more * than `indent` * * @param {string} src * @param {number} indent * @param {number} lineStart * @returns {?number} */ static endOfBlockIndent(src, indent, lineStart) { const inEnd = Node.endOfIndent(src, lineStart); if (inEnd > lineStart + indent) { return inEnd; } else { const wsEnd = Node.endOfWhiteSpace(src, inEnd); const ch = src[wsEnd]; if (!ch || ch === '\n') return wsEnd; } return null; } static atBlank(src, offset, endAsBlank) { const ch = src[offset]; return ch === '\n' || ch === '\t' || ch === ' ' || endAsBlank && !ch; } static nextNodeIsIndented(ch, indentDiff, indicatorAsIndent) { if (!ch || indentDiff < 0) return false; if (indentDiff > 0) return true; return indicatorAsIndent && ch === '-'; } // should be at line or string end, or at next non-whitespace char static normalizeOffset(src, offset) { const ch = src[offset]; return !ch ? offset : ch !== '\n' && src[offset - 1] === '\n' ? offset - 1 : Node.endOfWhiteSpace(src, offset); } // fold single newline into space, multiple newlines to N - 1 newlines // presumes src[offset] === '\n' static foldNewline(src, offset, indent) { let inCount = 0; let error = false; let fold = ''; let ch = src[offset + 1]; while (ch === ' ' || ch === '\t' || ch === '\n') { switch (ch) { case '\n': inCount = 0; offset += 1; fold += '\n'; break; case '\t': if (inCount <= indent) error = true; offset = Node.endOfWhiteSpace(src, offset + 2) - 1; break; case ' ': inCount += 1; offset += 1; break; } ch = src[offset + 1]; } if (!fold) fold = ' '; if (ch && inCount <= indent) error = true; return { fold, offset, error }; } constructor(type, props, context) { Object.defineProperty(this, 'context', { value: context || null, writable: true }); this.error = null; this.range = null; this.valueRange = null; this.props = props || []; this.type = type; this.value = null; } getPropValue(idx, key, skipKey) { if (!this.context) return null; const { src } = this.context; const prop = this.props[idx]; return prop && src[prop.start] === key ? src.slice(prop.start + (skipKey ? 1 : 0), prop.end) : null; } get anchor() { for (let i = 0; i < this.props.length; ++i) { const anchor = this.getPropValue(i, constants.Char.ANCHOR, true); if (anchor != null) return anchor; } return null; } get comment() { const comments = []; for (let i = 0; i < this.props.length; ++i) { const comment = this.getPropValue(i, constants.Char.COMMENT, true); if (comment != null) comments.push(comment); } return comments.length > 0 ? comments.join('\n') : null; } commentHasRequiredWhitespace(start) { const { src } = this.context; if (this.header && start === this.header.end) return false; if (!this.valueRange) return false; const { end } = this.valueRange; return start !== end || Node.atBlank(src, end - 1); } get hasComment() { if (this.context) { const { src } = this.context; for (let i = 0; i < this.props.length; ++i) { if (src[this.props[i].start] === constants.Char.COMMENT) return true; } } return false; } get hasProps() { if (this.context) { const { src } = this.context; for (let i = 0; i < this.props.length; ++i) { if (src[this.props[i].start] !== constants.Char.COMMENT) return true; } } return false; } get includesTrailingLines() { return false; } get jsonLike() { const jsonLikeTypes = [constants.Type.FLOW_MAP, constants.Type.FLOW_SEQ, constants.Type.QUOTE_DOUBLE, constants.Type.QUOTE_SINGLE]; return jsonLikeTypes.indexOf(this.type) !== -1; } get rangeAsLinePos() { if (!this.range || !this.context) return undefined; const start = sourceUtils.getLinePos(this.range.start, this.context.root); if (!start) return undefined; const end = sourceUtils.getLinePos(this.range.end, this.context.root); return { start, end }; } get rawValue() { if (!this.valueRange || !this.context) return null; const { start, end } = this.valueRange; return this.context.src.slice(start, end); } get tag() { for (let i = 0; i < this.props.length; ++i) { const tag = this.getPropValue(i, constants.Char.TAG, false); if (tag != null) { if (tag[1] === '<') { return { verbatim: tag.slice(2, -1) }; } else { // eslint-disable-next-line no-unused-vars const [_, handle, suffix] = tag.match(/^(.*!)([^!]*)$/); return { handle, suffix }; } } } return null; } get valueRangeContainsNewline() { if (!this.valueRange || !this.context) return false; const { start, end } = this.valueRange; const { src } = this.context; for (let i = start; i < end; ++i) { if (src[i] === '\n') return true; } return false; } parseComment(start) { const { src } = this.context; if (src[start] === constants.Char.COMMENT) { const end = Node.endOfLine(src, start + 1); const commentRange = new Range_1.Range(start, end); this.props.push(commentRange); return end; } return start; } /** * Populates the `origStart` and `origEnd` values of all ranges for this * node. Extended by child classes to handle descendant nodes. * * @param {number[]} cr - Positions of dropped CR characters * @param {number} offset - Starting index of `cr` from the last call * @returns {number} - The next offset, matching the one found for `origStart` */ setOrigRanges(cr, offset) { if (this.range) offset = this.range.setOrigRange(cr, offset); if (this.valueRange) this.valueRange.setOrigRange(cr, offset); this.props.forEach(prop => prop.setOrigRange(cr, offset)); return offset; } toString() { const { context: { src }, range, value } = this; if (value != null) return value; const str = src.slice(range.start, range.end); return Node.addStringTerminator(src, range.end, str); } } var Node_2 = Node; var Node_1 = { Node: Node_2 }; class YAMLError extends Error { constructor(name, source, message) { if (!message || !(source instanceof Node_1.Node)) throw new Error(`Invalid arguments for new ${name}`); super(); this.name = name; this.message = message; this.source = source; } makePretty() { if (!this.source) return; this.nodeType = this.source.type; const cst = this.source.context && this.source.context.root; if (typeof this.offset === 'number') { this.range = new Range_1.Range(this.offset, this.offset + 1); const start = cst && sourceUtils.getLinePos(this.offset, cst); if (start) { const end = { line: start.line, col: start.col + 1 }; this.linePos = { start, end }; } delete this.offset; } else { this.range = this.source.range; this.linePos = this.source.rangeAsLinePos; } if (this.linePos) { const { line, col } = this.linePos.start; this.message += ` at line ${line}, column ${col}`; const ctx = cst && sourceUtils.getPrettyContext(this.linePos, cst); if (ctx) this.message += `:\n\n${ctx}\n`; } delete this.source; } } class YAMLReferenceError extends YAMLError { constructor(source, message) { super('YAMLReferenceError', source, message); } } class YAMLSemanticError extends YAMLError { constructor(source, message) { super('YAMLSemanticError', source, message); } } class YAMLSyntaxError extends YAMLError { constructor(source, message) { super('YAMLSyntaxError', source, message); } } class YAMLWarning extends YAMLError { constructor(source, message) { super('YAMLWarning', source, message); } } var YAMLError_1 = YAMLError; var YAMLReferenceError_1 = YAMLReferenceError; var YAMLSemanticError_1 = YAMLSemanticError; var YAMLSyntaxError_1 = YAMLSyntaxError; var YAMLWarning_1 = YAMLWarning; var errors = { YAMLError: YAMLError_1, YAMLReferenceError: YAMLReferenceError_1, YAMLSemanticError: YAMLSemanticError_1, YAMLSyntaxError: YAMLSyntaxError_1, YAMLWarning: YAMLWarning_1 }; class BlankLine extends Node_1.Node { constructor() { super(constants.Type.BLANK_LINE); } /* istanbul ignore next */ get includesTrailingLines() { // This is never called from anywhere, but if it were, // this is the value it should return. return true; } /** * Parses a blank line from the source * * @param {ParseContext} context * @param {number} start - Index of first \n character * @returns {number} - Index of the character after this */ parse(context, start) { this.context = context; this.range = new Range_1.Range(start, start + 1); return start + 1; } } var BlankLine_2 = BlankLine; var BlankLine_1 = { BlankLine: BlankLine_2 }; class CollectionItem extends Node_1.Node { constructor(type, props) { super(type, props); this.node = null; } get includesTrailingLines() { return !!this.node && this.node.includesTrailingLines; } /** * @param {ParseContext} context * @param {number} start - Index of first character * @returns {number} - Index of the character after this */ parse(context, start) { this.context = context; const { parseNode, src } = context; let { atLineStart, lineStart } = context; if (!atLineStart && this.type === constants.Type.SEQ_ITEM) this.error = new errors.YAMLSemanticError(this, 'Sequence items must not have preceding content on the same line'); const indent = atLineStart ? start - lineStart : context.indent; let offset = Node_1.Node.endOfWhiteSpace(src, start + 1); let ch = src[offset]; const inlineComment = ch === '#'; const comments = []; let blankLine = null; while (ch === '\n' || ch === '#') { if (ch === '#') { const end = Node_1.Node.endOfLine(src, offset + 1); comments.push(new Range_1.Range(offset, end)); offset = end; } else { atLineStart = true; lineStart = offset + 1; const wsEnd = Node_1.Node.endOfWhiteSpace(src, lineStart); if (src[wsEnd] === '\n' && comments.length === 0) { blankLine = new BlankLine_1.BlankLine(); lineStart = blankLine.parse({ src }, lineStart); } offset = Node_1.Node.endOfIndent(src, lineStart); } ch = src[offset]; } if (Node_1.Node.nextNodeIsIndented(ch, offset - (lineStart + indent), this.type !== constants.Type.SEQ_ITEM)) { this.node = parseNode({ atLineStart, inCollection: false, indent, lineStart, parent: this }, offset); } else if (ch && lineStart > start + 1) { offset = lineStart - 1; } if (this.node) { if (blankLine) { // Only blank lines preceding non-empty nodes are captured. Note that // this means that collection item range start indices do not always // increase monotonically. -- eemeli/yaml#126 const items = context.parent.items || context.parent.contents; if (items) items.push(blankLine); } if (comments.length) Array.prototype.push.apply(this.props, comments); offset = this.node.range.end; } else { if (inlineComment) { const c = comments[0]; this.props.push(c); offset = c.end; } else { offset = Node_1.Node.endOfLine(src, start + 1); } } const end = this.node ? this.node.valueRange.end : offset; this.valueRange = new Range_1.Range(start, end); return offset; } setOrigRanges(cr, offset) { offset = super.setOrigRanges(cr, offset); return this.node ? this.node.setOrigRanges(cr, offset) : offset; } toString() { const { context: { src }, node, range, value } = this; if (value != null) return value; const str = node ? src.slice(range.start, node.range.start) + String(node) : src.slice(range.start, range.end); return Node_1.Node.addStringTerminator(src, range.end, str); } } var CollectionItem_2 = CollectionItem; var CollectionItem_1 = { CollectionItem: CollectionItem_2 }; class Comment extends Node_1.Node { constructor() { super(constants.Type.COMMENT); } /** * Parses a comment line from the source * * @param {ParseContext} context * @param {number} start - Index of first character * @returns {number} - Index of the character after this scalar */ parse(context, start) { this.context = context; const offset = this.parseComment(start); this.range = new Range_1.Range(start, offset); return offset; } } var Comment_2 = Comment; var Comment_1 = { Comment: Comment_2 }; function grabCollectionEndComments(node) { let cnode = node; while (cnode instanceof CollectionItem_1.CollectionItem) cnode = cnode.node; if (!(cnode instanceof Collection)) return null; const len = cnode.items.length; let ci = -1; for (let i = len - 1; i >= 0; --i) { const n = cnode.items[i]; if (n.type === constants.Type.COMMENT) { // Keep sufficiently indented comments with preceding node const { indent, lineStart } = n.context; if (indent > 0 && n.range.start >= lineStart + indent) break; ci = i; } else if (n.type === constants.Type.BLANK_LINE) ci = i;else break; } if (ci === -1) return null; const ca = cnode.items.splice(ci, len - ci); const prevEnd = ca[0].range.start; while (true) { cnode.range.end = prevEnd; if (cnode.valueRange && cnode.valueRange.end > prevEnd) cnode.valueRange.end = prevEnd; if (cnode === node) break; cnode = cnode.context.parent; } return ca; } class Collection extends Node_1.Node { static nextContentHasIndent(src, offset, indent) { const lineStart = Node_1.Node.endOfLine(src, offset) + 1; offset = Node_1.Node.endOfWhiteSpace(src, lineStart); const ch = src[offset]; if (!ch) return false; if (offset >= lineStart + indent) return true; if (ch !== '#' && ch !== '\n') return false; return Collection.nextContentHasIndent(src, offset, indent); } constructor(firstItem) { super(firstItem.type === constants.Type.SEQ_ITEM ? constants.Type.SEQ : constants.Type.MAP); for (let i = firstItem.props.length - 1; i >= 0; --i) { if (firstItem.props[i].start < firstItem.context.lineStart) { // props on previous line are assumed by the collection this.props = firstItem.props.slice(0, i + 1); firstItem.props = firstItem.props.slice(i + 1); const itemRange = firstItem.props[0] || firstItem.valueRange; firstItem.range.start = itemRange.start; break; } } this.items = [firstItem]; const ec = grabCollectionEndComments(firstItem); if (ec) Array.prototype.push.apply(this.items, ec); } get includesTrailingLines() { return this.items.length > 0; } /** * @param {ParseContext} context * @param {number} start - Index of first character * @returns {number} - Index of the character after this */ parse(context, start) { this.context = context; const { parseNode, src } = context; // It's easier to recalculate lineStart here rather than tracking down the // last context from which to read it -- eemeli/yaml#2 let lineStart = Node_1.Node.startOfLine(src, start); const firstItem = this.items[0]; // First-item context needs to be correct for later comment handling // -- eemeli/yaml#17 firstItem.context.parent = this; this.valueRange = Range_1.Range.copy(firstItem.valueRange); const indent = firstItem.range.start - firstItem.context.lineStart; let offset = start; offset = Node_1.Node.normalizeOffset(src, offset); let ch = src[offset]; let atLineStart = Node_1.Node.endOfWhiteSpace(src, lineStart) === offset; let prevIncludesTrailingLines = false; while (ch) { while (ch === '\n' || ch === '#') { if (atLineStart && ch === '\n' && !prevIncludesTrailingLines) { const blankLine = new BlankLine_1.BlankLine(); offset = blankLine.parse({ src }, offset); this.valueRange.end = offset; if (offset >= src.length) { ch = null; break; } this.items.push(blankLine); offset -= 1; // blankLine.parse() consumes terminal newline } else if (ch === '#') { if (offset < lineStart + indent && !Collection.nextContentHasIndent(src, offset, indent)) { return offset; } const comment = new Comment_1.Comment(); offset = comment.parse({ indent, lineStart, src }, offset); this.items.push(comment); this.valueRange.end = offset; if (offset >= src.length) { ch = null; break; } } lineStart = offset + 1; offset = Node_1.Node.endOfIndent(src, lineStart); if (Node_1.Node.atBlank(src, offset)) { const wsEnd = Node_1.Node.endOfWhiteSpace(src, offset); const next = src[wsEnd]; if (!next || next === '\n' || next === '#') { offset = wsEnd; } } ch = src[offset]; atLineStart = true; } if (!ch) { break; } if (offset !== lineStart + indent && (atLineStart || ch !== ':')) { if (offset < lineStart + indent) { if (lineStart > start) offset = lineStart; break; } else if (!this.error) { const msg = 'All collection items must start at the same column'; this.error = new errors.YAMLSyntaxError(this, msg); } } if (firstItem.type === constants.Type.SEQ_ITEM) { if (ch !== '-') { if (lineStart > start) offset = lineStart; break; } } else if (ch === '-' && !this.error) { // map key may start with -, as long as it's followed by a non-whitespace char const next = src[offset + 1]; if (!next || next === '\n' || next === '\t' || next === ' ') { const msg = 'A collection cannot be both a mapping and a sequence'; this.error = new errors.YAMLSyntaxError(this, msg); } } const node = parseNode({ atLineStart, inCollection: true, indent, lineStart, parent: this }, offset); if (!node) return offset; // at next document start this.items.push(node); this.valueRange.end = node.valueRange.end; offset = Node_1.Node.normalizeOffset(src, node.range.end); ch = src[offset]; atLineStart = false; prevIncludesTrailingLines = node.includesTrailingLines; // Need to reset lineStart and atLineStart here if preceding node's range // has advanced to check the current line's indentation level // -- eemeli/yaml#10 & eemeli/yaml#38 if (ch) { let ls = offset - 1; let prev = src[ls]; while (prev === ' ' || prev === '\t') prev = src[--ls]; if (prev === '\n') { lineStart = ls + 1; atLineStart = true; } } const ec = grabCollectionEndComments(node); if (ec) Array.prototype.push.apply(this.items, ec); } return offset; } setOrigRanges(cr, offset) { offset = super.setOrigRanges(cr, offset); this.items.forEach(node => { offset = node.setOrigRanges(cr, offset); }); return offset; } toString() { const { context: { src }, items, range, value } = this; if (value != null) return value; let str = src.slice(range.start, items[0].range.start) + String(items[0]); for (let i = 1; i < items.length; ++i) { const item = items[i]; const { atLineStart, indent } = item.context; if (atLineStart) for (let i = 0; i < indent; ++i) str += ' '; str += String(item); } return Node_1.Node.addStringTerminator(src, range.end, str); } } var Collection_2 = Collection; var grabCollectionEndComments_1 = grabCollectionEndComments; var Collection_1 = { Collection: Collection_2, grabCollectionEndComments: grabCollectionEndComments_1 }; class Directive extends Node_1.Node { constructor() { super(constants.Type.DIRECTIVE); this.name = null; } get parameters() { const raw = this.rawValue; return raw ? raw.trim().split(/[ \t]+/) : []; } parseName(start) { const { src } = this.context; let offset = start; let ch = src[offset]; while (ch && ch !== '\n' && ch !== '\t' && ch !== ' ') ch = src[offset += 1]; this.name = src.slice(start, offset); return offset; } parseParameters(start) { const { src } = this.context; let offset = start; let ch = src[offset]; while (ch && ch !== '\n' && ch !== '#') ch = src[offset += 1]; this.valueRange = new Range_1.Range(start, offset); return offset; } parse(context, start) { this.context = context; let offset = this.parseName(start + 1); offset = this.parseParameters(offset); offset = this.parseComment(offset); this.range = new Range_1.Range(start, offset); return offset; } } var Directive_2 = Directive; var Directive_1 = { Directive: Directive_2 }; class Document extends Node_1.Node { static startCommentOrEndBlankLine(src, start) { const offset = Node_1.Node.endOfWhiteSpace(src, start); const ch = src[offset]; return ch === '#' || ch === '\n' ? offset : start; } constructor() { super(constants.Type.DOCUMENT); this.directives = null; this.contents = null; this.directivesEndMarker = null; this.documentEndMarker = null; } parseDirectives(start) { const { src } = this.context; this.directives = []; let atLineStart = true; let hasDirectives = false; let offset = start; while (!Node_1.Node.atDocumentBoundary(src, offset, constants.Char.DIRECTIVES_END)) { offset = Document.startCommentOrEndBlankLine(src, offset); switch (src[offset]) { case '\n': if (atLineStart) { const blankLine = new BlankLine_1.BlankLine(); offset = blankLine.parse({ src }, offset); if (offset < src.length) { this.directives.push(blankLine); } } else { offset += 1; atLineStart = true; } break; case '#': { const comment = new Comment_1.Comment(); offset = comment.parse({ src }, offset); this.directives.push(comment); atLineStart = false; } break; case '%': { const directive = new Directive_1.Directive(); offset = directive.parse({ parent: this, src }, offset); this.directives.push(directive); hasDirectives = true; atLineStart = false; } break; default: if (hasDirectives) { this.error = new errors.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_1.Range(offset, offset + 3); return offset + 3; } if (hasDirectives) { this.error = new errors.YAMLSemanticError(this, 'Missing directives-end indicator line'); } else if (this.directives.length > 0) { this.contents = this.directives; this.directives = []; } return offset; } parseContents(start) { const { parseNode, src } = this.context; if (!this.contents) this.contents = []; let lineStart = start; while (src[lineStart - 1] === '-') lineStart -= 1; let offset = Node_1.Node.endOfWhiteSpace(src, start); let atLineStart = lineStart === start; this.valueRange = new Range_1.Range(offset); while (!Node_1.Node.atDocumentBoundary(src, offset, constants.Char.DOCUMENT_END)) { switch (src[offset]) { case '\n': if (atLineStart) { const blankLine = new BlankLine_1.BlankLine(); offset = blankLine.parse({ src }, offset); if (offset < src.length) { this.contents.push(blankLine); } } else { offset += 1; atLineStart = true; } lineStart = offset; break; case '#': { const comment = new Comment_1.Comment(); offset = comment.parse({ src }, offset); this.contents.push(comment); atLineStart = false; } break; default: { const iEnd = Node_1.Node.endOfIndent(src, offset); const context = { atLineStart, indent: -1, inFlow: false, inCollection: false, lineStart, parent: this }; const 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; const ec = Collection_1.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_1.Range(offset, offset + 3); offset += 3; if (src[offset]) { offset = Node_1.Node.endOfWhiteSpace(src, offset); if (src[offset] === '#') { const comment = new Comment_1.Comment(); offset = comment.parse({ src }, offset); this.contents.push(comment); } switch (src[offset]) { case '\n': offset += 1; break; case undefined: break; default: this.error = new errors.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 */ parse(context, start) { context.root = this; this.context = context; const { src } = context; let offset = src.charCodeAt(start) === 0xfeff ? start + 1 : start; // skip BOM offset = this.parseDirectives(offset); offset = this.parseContents(offset); return offset; } setOrigRanges(cr, offset) { offset = super.setOrigRanges(cr, offset); this.directives.forEach(node => { offset = node.setOrigRanges(cr, offset); }); if (this.directivesEndMarker) offset = this.directivesEndMarker.setOrigRange(cr, offset); this.contents.forEach(node => { offset = node.setOrigRanges(cr, offset); }); if (this.documentEndMarker) offset = this.documentEndMarker.setOrigRange(cr, offset); return offset; } toString() { const { contents, directives, value } = this; if (value != null) return value; let str = directives.join(''); if (contents.length > 0) { if (directives.length > 0 || contents[0].type === constants.Type.COMMENT) str += '---\n'; str += contents.join(''); } if (str[str.length - 1] !== '\n') str += '\n'; return str; } } var Document_2 = Document; var Document_1 = { Document: Document_2 }; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var defineProperty = _defineProperty; var _rollupPluginBabelHelpers = { defineProperty: defineProperty }; class Alias extends Node_1.Node { /** * Parses an *alias from the source * * @param {ParseContext} context * @param {number} start - Index of first character * @returns {number} - Index of the character after this scalar */ parse(context, start) { this.context = context; const { src } = context; let offset = Node_1.Node.endOfIdentifier(src, start + 1); this.valueRange = new Range_1.Range(start + 1, offset); offset = Node_1.Node.endOfWhiteSpace(src, offset); offset = this.parseComment(offset); return offset; } } var Alias_2 = Alias; var Alias_1 = { Alias: Alias_2 }; const Chomp = { CLIP: 'CLIP', KEEP: 'KEEP', STRIP: 'STRIP' }; class BlockValue extends Node_1.Node { constructor(type, props) { super(type, props); this.blockIndent = null; this.chomping = Chomp.CLIP; this.header = null; } get includesTrailingLines() { return this.chomping === Chomp.KEEP; } get strValue() { if (!this.valueRange || !this.context) return null; let { start, end } = this.valueRange; const { indent, src } = this.context; if (this.valueRange.isEmpty()) return ''; let lastNewLine = null; let ch = src[end - 1]; while (ch === '\n' || ch === '\t' || ch === ' ') { end -= 1; if (end <= start) { if (this.chomping === Chomp.KEEP) break;else return ''; // probably never happens } if (ch === '\n') lastNewLine = end; ch = src[end - 1]; } let keepStart = end + 1; if (lastNewLine) { if (this.chomping === Chomp.KEEP) { keepStart = lastNewLine; end = this.valueRange.end; } else { end = lastNewLine; } } const bi = indent + this.blockIndent; const folded = this.type === constants.Type.BLOCK_FOLDED; let atStart = true; let str = ''; let sep = ''; let prevMoreIndented = false; for (let i = start; i < end; ++i) { for (let j = 0; j < bi; ++j) { if (src[i] !== ' ') break; i += 1; } const ch = src[i]; if (ch === '\n') { if (sep === '\n') str += '\n';else sep = '\n'; } else { const lineEnd = Node_1.Node.endOfLine(src, i); const line = src.slice(i, lineEnd); i = lineEnd; if (folded && (ch === ' ' || ch === '\t') && i < keepStart) { if (sep === ' ') sep = '\n';else if (!prevMoreIndented && !atStart && sep === '\n') sep = '\n\n'; str += sep + line; //+ ((lineEnd < end && src[lineEnd]) || '') sep = lineEnd < end && src[lineEnd] || ''; prevMoreIndented = true; } else { str += sep + line; sep = folded && i < keepStart ? ' ' : '\n'; prevMoreIndented = false; } if (atStart && line !== '') atStart = false; } } return this.chomping === Chomp.STRIP ? str : str + '\n'; } parseBlockHeader(start) { const { src } = this.context; let offset = start + 1; let bi = ''; while (true) { const ch = src[offset]; switch (ch) { case '-': this.chomping = Chomp.STRIP; break; case '+': this.chomping = Chomp.KEEP; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': bi += ch; break; default: this.blockIndent = Number(bi) || null; this.header = new Range_1.Range(start, offset); return offset; } offset += 1; } } parseBlockValue(start) { const { indent, src } = this.context; const explicit = !!this.blockIndent; let offset = start; let valueEnd = start; let minBlockIndent = 1; for (let ch = src[offset]; ch === '\n'; ch = src[offset]) { offset += 1; if (Node_1.Node.atDocumentBoundary(src, offset)) break; const end = Node_1.Node.endOfBlockIndent(src, indent, offset); // should not include tab? if (end === null) break; const ch = src[end]; const lineIndent = end - (offset + indent); if (!this.blockIndent) { // no explicit block indent, none yet detected if (src[end] !== '\n') { // first line with non-whitespace content if (lineIndent < minBlockIndent) { const msg = 'Block scalars with more-indented leading empty lines must use an explicit indentation indicator'; this.error = new errors.YAMLSemanticError(this, msg); } this.blockIndent = lineIndent; } else if (lineIndent > minBlockIndent) { // empty line with more whitespace minBlockIndent = lineIndent; } } else if (ch && ch !== '\n' && lineIndent < this.blockIndent) { if (src[end] === '#') break; if (!this.error) { const src = explicit ? 'explicit indentation indicator' : 'first line'; const msg = `Block scalars must not be less indented than their ${src}`; this.error = new errors.YAMLSemanticError(this, msg); } } if (src[end] === '\n') { offset = end; } else { offset = valueEnd = Node_1.Node.endOfLine(src, end); } } if (this.chomping !== Chomp.KEEP) { offset = src[valueEnd] ? valueEnd + 1 : valueEnd; } this.valueRange = new Range_1.Range(start + 1, offset); return offset; } /** * Parses a block value from the source * * Accepted forms are: * ``` * BS * block * lines * * BS #comment * block * lines * ``` * where the block style BS matches the regexp `[|>][-+1-9]*` and block lines * are empty or have an indent level greater than `indent`. * * @param {ParseContext} context * @param {number} start - Index of first character * @returns {number} - Index of the character after this block */ parse(context, start) { this.context = context; const { src } = context; let offset = this.parseBlockHeader(start); offset = Node_1.Node.endOfWhiteSpace(src, offset); offset = this.parseComment(offset); offset = this.parseBlockValue(offset); return offset; } setOrigRanges(cr, offset) { offset = super.setOrigRanges(cr, offset); return this.header ? this.header.setOrigRange(cr, offset) : offset; } } var BlockValue_2 = BlockValue; var Chomp_1 = Chomp; var BlockValue_1 = { BlockValue: BlockValue_2, Chomp: Chomp_1 }; class FlowCollection extends Node_1.Node { constructor(type, props) { super(type, props); this.items = null; } prevNodeIsJsonLike(idx = this.items.length) { const node = this.items[idx - 1]; return !!node && (node.jsonLike || node.type === constants.Type.COMMENT && this.prevNodeIsJsonLike(idx - 1)); } /** * @param {ParseContext} context * @param {number} start - Index of first character * @returns {number} - Index of the character after this */ parse(context, start) { this.context = context; const { parseNode, src } = context; let { indent, lineStart } = context; let char = src[start]; // { or [ this.items = [{ char, offset: start }]; let offset = Node_1.Node.endOfWhiteSpace(src, start + 1); char = src[offset]; while (char && char !== ']' && char !== '}') { switch (char) { case '\n': { lineStart = offset + 1; const wsEnd = Node_1.Node.endOfWhiteSpace(src, lineStart); if (src[wsEnd] === '\n') { const blankLine = new BlankLine_1.BlankLine(); lineStart = blankLine.parse({ src }, lineStart); this.items.push(blankLine); } offset = Node_1.Node.endOfIndent(src, lineStart); if (offset <= lineStart + indent) { char = src[offset]; if (offset < lineStart + indent || char !== ']' && char !== '}') { const msg = 'Insufficient indentation in flow collection'; this.error = new errors.YAMLSemanticError(this, msg); } } } break; case ',': { this.items.push({ char, offset }); offset += 1; } break; case '#': { const comment = new Comment_1.Comment(); offset = comment.parse({ src }, offset); this.items.push(comment); } break; case '?': case ':': { const next = src[offset + 1]; if (next === '\n' || next === '\t' || next === ' ' || next === ',' || // in-flow : after JSON-like key does not need to be followed by whitespace char === ':' && this.prevNodeIsJsonLike()) { this.items.push({ char, offset }); offset += 1; break; } } // fallthrough default: { const node = parseNode({ atLineStart: false, inCollection: false, inFlow: true, indent: -1, lineStart, parent: this }, offset); if (!node) { // at next document start this.valueRange = new Range_1.Range(start, offset); return offset; } this.items.push(node); offset = Node_1.Node.normalizeOffset(src, node.range.end); } } offset = Node_1.Node.endOfWhiteSpace(src, offset); char = src[offset]; } this.valueRange = new Range_1.Range(start, offset + 1); if (char) { this.items.push({ char, offset }); offset = Node_1.Node.endOfWhiteSpace(src, offset + 1); offset = this.parseComment(offset); } return offset; } setOrigRanges(cr, offset) { offset = super.setOrigRanges(cr, offset); this.items.forEach(node => { if (node instanceof Node_1.Node) { offset = node.setOrigRanges(cr, offset); } else if (cr.length === 0) { node.origOffset = node.offset; } else { let i = offset; while (i < cr.length) { if (cr[i] > node.offset) break;else ++i; } node.origOffset = node.offset + i; offset = i; } }); return offset; } toString() { const { context: { src }, items, range, value } = this; if (value != null) return value; const nodes = items.filter(item => item instanceof Node_1.Node); let str = ''; let prevEnd = range.start; nodes.forEach(node => { const prefix = src.slice(prevEnd, node.range.start); prevEnd = node.range.end; str += prefix + String(node); if (str[str.length - 1] === '\n' && src[prevEnd - 1] !== '\n' && src[prevEnd] === '\n') { // Comment range does not include the terminal newline, but its // stringified value does. Without this fix, newlines at comment ends // get duplicated. prevEnd += 1; } }); str += src.slice(prevEnd, range.end); return Node_1.Node.addStringTerminator(src, range.end, str); } } var FlowCollection_2 = FlowCollection; var FlowCollection_1 = { FlowCollection: FlowCollection_2 }; class PlainValue extends Node_1.Node { static endOfLine(src, start, inFlow) { let ch = src[start]; let offset = start; while (ch && ch !== '\n') { if (inFlow && (ch === '[' || ch === ']' || ch === '{' || ch === '}' || ch === ',')) break; const next = src[offset + 1]; if (ch === ':' && (!next || next === '\n' || next === '\t' || next === ' ' || inFlow && next === ',')) break; if ((ch === ' ' || ch === '\t') && next === '#') break; offset += 1; ch = next; } return offset; } get strValue() { if (!this.valueRange || !this.context) return null; let {