import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import { YAMLSemanticError, YAMLSyntaxError } from '../errors'; import Node from './Node'; import Range from './Range'; var QuoteSingle = /*#__PURE__*/function (_Node) { _inherits(QuoteSingle, _Node); function QuoteSingle() { _classCallCheck(this, QuoteSingle); return _possibleConstructorReturn(this, _getPrototypeOf(QuoteSingle).apply(this, arguments)); } _createClass(QuoteSingle, [{ key: "parse", /** * Parses a 'single quoted' value from the source * * @param {ParseContext} context * @param {number} start - Index of first character * @returns {number} - Index of the character after this scalar */ value: function parse(context, start) { this.context = context; var src = context.src; var offset = QuoteSingle.endOfQuote(src, start + 1); this.valueRange = new Range(start, offset); offset = Node.endOfWhiteSpace(src, offset); offset = this.parseComment(offset); return offset; } }, { key: "strValue", /** * @returns {string | { str: string, errors: YAMLSyntaxError[] }} */ get: function get() { if (!this.valueRange || !this.context) return null; var errors = []; var _this$valueRange = this.valueRange, start = _this$valueRange.start, end = _this$valueRange.end; var _this$context = this.context, indent = _this$context.indent, src = _this$context.src; if (src[end - 1] !== "'") errors.push(new YAMLSyntaxError(this, "Missing closing 'quote")); var str = ''; for (var i = start + 1; i < end - 1; ++i) { var ch = src[i]; if (ch === '\n') { if (Node.atDocumentBoundary(src, i + 1)) errors.push(new YAMLSemanticError(this, 'Document boundary indicators are not allowed within string values')); var _Node$foldNewline = Node.foldNewline(src, i, indent), fold = _Node$foldNewline.fold, offset = _Node$foldNewline.offset, error = _Node$foldNewline.error; str += fold; i = offset; if (error) errors.push(new YAMLSemanticError(this, 'Multi-line single-quoted string needs to be sufficiently indented')); } else if (ch === "'") { str += ch; i += 1; if (src[i] !== "'") errors.push(new YAMLSyntaxError(this, 'Unescaped single quote? This should not happen.')); } else if (ch === ' ' || ch === '\t') { // trim trailing whitespace var wsStart = i; var next = src[i + 1]; while (next === ' ' || next === '\t') { i += 1; next = src[i + 1]; } if (next !== '\n') str += i > wsStart ? src.slice(wsStart, i + 1) : ch; } else { str += ch; } } return errors.length > 0 ? { errors: errors, str: str } : str; } }], [{ key: "endOfQuote", value: function endOfQuote(src, offset) { var ch = src[offset]; while (ch) { if (ch === "'") { if (src[offset + 1] !== "'") break; ch = src[offset += 2]; } else { ch = src[offset += 1]; } } return offset + 1; } }]); return QuoteSingle; }(Node); export { QuoteSingle as default };