Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ParseContext.js 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  8. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  9. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  10. var _constants = require("../constants");
  11. var _errors = require("../errors");
  12. var _Alias = _interopRequireDefault(require("./Alias"));
  13. var _BlockValue = _interopRequireDefault(require("./BlockValue"));
  14. var _Collection = _interopRequireDefault(require("./Collection"));
  15. var _CollectionItem = _interopRequireDefault(require("./CollectionItem"));
  16. var _FlowCollection = _interopRequireDefault(require("./FlowCollection"));
  17. var _Node = _interopRequireDefault(require("./Node"));
  18. var _PlainValue = _interopRequireDefault(require("./PlainValue"));
  19. var _QuoteDouble = _interopRequireDefault(require("./QuoteDouble"));
  20. var _QuoteSingle = _interopRequireDefault(require("./QuoteSingle"));
  21. var _Range = _interopRequireDefault(require("./Range"));
  22. /**
  23. * @param {boolean} atLineStart - Node starts at beginning of line
  24. * @param {boolean} inFlow - true if currently in a flow context
  25. * @param {boolean} inCollection - true if currently in a collection context
  26. * @param {number} indent - Current level of indentation
  27. * @param {number} lineStart - Start of the current line
  28. * @param {Node} parent - The parent of the node
  29. * @param {string} src - Source of the YAML document
  30. */
  31. var ParseContext =
  32. /*#__PURE__*/
  33. function () {
  34. (0, _createClass2.default)(ParseContext, null, [{
  35. key: "parseType",
  36. value: function parseType(src, offset, inFlow) {
  37. switch (src[offset]) {
  38. case '*':
  39. return _constants.Type.ALIAS;
  40. case '>':
  41. return _constants.Type.BLOCK_FOLDED;
  42. case '|':
  43. return _constants.Type.BLOCK_LITERAL;
  44. case '{':
  45. return _constants.Type.FLOW_MAP;
  46. case '[':
  47. return _constants.Type.FLOW_SEQ;
  48. case '?':
  49. return !inFlow && _Node.default.atBlank(src, offset + 1, true) ? _constants.Type.MAP_KEY : _constants.Type.PLAIN;
  50. case ':':
  51. return !inFlow && _Node.default.atBlank(src, offset + 1, true) ? _constants.Type.MAP_VALUE : _constants.Type.PLAIN;
  52. case '-':
  53. return !inFlow && _Node.default.atBlank(src, offset + 1, true) ? _constants.Type.SEQ_ITEM : _constants.Type.PLAIN;
  54. case '"':
  55. return _constants.Type.QUOTE_DOUBLE;
  56. case "'":
  57. return _constants.Type.QUOTE_SINGLE;
  58. default:
  59. return _constants.Type.PLAIN;
  60. }
  61. }
  62. }]);
  63. function ParseContext() {
  64. var _this = this;
  65. var orig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  66. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  67. atLineStart = _ref.atLineStart,
  68. inCollection = _ref.inCollection,
  69. inFlow = _ref.inFlow,
  70. indent = _ref.indent,
  71. lineStart = _ref.lineStart,
  72. parent = _ref.parent;
  73. (0, _classCallCheck2.default)(this, ParseContext);
  74. (0, _defineProperty2.default)(this, "parseNode", function (overlay, start) {
  75. if (_Node.default.atDocumentBoundary(_this.src, start)) return null;
  76. var context = new ParseContext(_this, overlay);
  77. var _context$parseProps = context.parseProps(start),
  78. props = _context$parseProps.props,
  79. type = _context$parseProps.type,
  80. valueStart = _context$parseProps.valueStart;
  81. var node;
  82. switch (type) {
  83. case _constants.Type.ALIAS:
  84. node = new _Alias.default(type, props);
  85. break;
  86. case _constants.Type.BLOCK_FOLDED:
  87. case _constants.Type.BLOCK_LITERAL:
  88. node = new _BlockValue.default(type, props);
  89. break;
  90. case _constants.Type.FLOW_MAP:
  91. case _constants.Type.FLOW_SEQ:
  92. node = new _FlowCollection.default(type, props);
  93. break;
  94. case _constants.Type.MAP_KEY:
  95. case _constants.Type.MAP_VALUE:
  96. case _constants.Type.SEQ_ITEM:
  97. node = new _CollectionItem.default(type, props);
  98. break;
  99. case _constants.Type.COMMENT:
  100. case _constants.Type.PLAIN:
  101. node = new _PlainValue.default(type, props);
  102. break;
  103. case _constants.Type.QUOTE_DOUBLE:
  104. node = new _QuoteDouble.default(type, props);
  105. break;
  106. case _constants.Type.QUOTE_SINGLE:
  107. node = new _QuoteSingle.default(type, props);
  108. break;
  109. default:
  110. node.error = new _errors.YAMLSyntaxError(node, "Unknown node type: ".concat(JSON.stringify(type)));
  111. node.range = new _Range.default(start, start + 1);
  112. return node;
  113. }
  114. var offset = node.parse(context, valueStart);
  115. node.range = new _Range.default(start, offset);
  116. if (offset <= start) {
  117. node.error = new Error("Node#parse consumed no characters");
  118. node.error.parseEnd = offset;
  119. node.error.source = node;
  120. node.range.end = start + 1;
  121. }
  122. if (context.nodeStartsCollection(node)) {
  123. if (!node.error && !context.atLineStart && context.parent.type === _constants.Type.DOCUMENT) {
  124. node.error = new _errors.YAMLSyntaxError(node, 'Block collection must not have preceding content here (e.g. directives-end indicator)');
  125. }
  126. var collection = new _Collection.default(node);
  127. offset = collection.parse(new ParseContext(context), offset);
  128. collection.range = new _Range.default(start, offset);
  129. return collection;
  130. }
  131. return node;
  132. });
  133. this.atLineStart = atLineStart != null ? atLineStart : orig.atLineStart || false;
  134. this.inCollection = inCollection != null ? inCollection : orig.inCollection || false;
  135. this.inFlow = inFlow != null ? inFlow : orig.inFlow || false;
  136. this.indent = indent != null ? indent : orig.indent;
  137. this.lineStart = lineStart != null ? lineStart : orig.lineStart;
  138. this.parent = parent != null ? parent : orig.parent || {};
  139. this.root = orig.root;
  140. this.src = orig.src;
  141. } // for logging
  142. (0, _createClass2.default)(ParseContext, [{
  143. key: "nodeStartsCollection",
  144. value: function nodeStartsCollection(node) {
  145. var inCollection = this.inCollection,
  146. inFlow = this.inFlow,
  147. src = this.src;
  148. if (inCollection || inFlow) return false;
  149. if (node instanceof _CollectionItem.default) return true; // check for implicit key
  150. var offset = node.range.end;
  151. if (src[offset] === '\n' || src[offset - 1] === '\n') return false;
  152. offset = _Node.default.endOfWhiteSpace(src, offset);
  153. return src[offset] === ':';
  154. } // Anchor and tag are before type, which determines the node implementation
  155. // class; hence this intermediate step.
  156. }, {
  157. key: "parseProps",
  158. value: function parseProps(offset) {
  159. var inFlow = this.inFlow,
  160. parent = this.parent,
  161. src = this.src;
  162. var props = [];
  163. var lineHasProps = false;
  164. offset = _Node.default.endOfWhiteSpace(src, offset);
  165. var ch = src[offset];
  166. while (ch === _constants.Char.ANCHOR || ch === _constants.Char.COMMENT || ch === _constants.Char.TAG || ch === '\n') {
  167. if (ch === '\n') {
  168. var lineStart = offset + 1;
  169. var inEnd = _Node.default.endOfIndent(src, lineStart);
  170. var indentDiff = inEnd - (lineStart + this.indent);
  171. var noIndicatorAsIndent = parent.type === _constants.Type.SEQ_ITEM && parent.context.atLineStart;
  172. if (!_Node.default.nextNodeIsIndented(src[inEnd], indentDiff, !noIndicatorAsIndent)) break;
  173. this.atLineStart = true;
  174. this.lineStart = lineStart;
  175. lineHasProps = false;
  176. offset = inEnd;
  177. } else if (ch === _constants.Char.COMMENT) {
  178. var end = _Node.default.endOfLine(src, offset + 1);
  179. props.push(new _Range.default(offset, end));
  180. offset = end;
  181. } else {
  182. var _end = _Node.default.endOfIdentifier(src, offset + 1);
  183. if (ch === _constants.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))) {
  184. // Let's presume we're dealing with a YAML 1.0 domain tag here, rather
  185. // than an empty but 'foo.bar' private-tagged node in a flow collection
  186. // followed without whitespace by a plain string starting with a year
  187. // or date divided by something.
  188. _end = _Node.default.endOfIdentifier(src, _end + 5);
  189. }
  190. props.push(new _Range.default(offset, _end));
  191. lineHasProps = true;
  192. offset = _Node.default.endOfWhiteSpace(src, _end);
  193. }
  194. ch = src[offset];
  195. } // '- &a : b' has an anchor on an empty node
  196. if (lineHasProps && ch === ':' && _Node.default.atBlank(src, offset + 1, true)) offset -= 1;
  197. var type = ParseContext.parseType(src, offset, inFlow);
  198. return {
  199. props: props,
  200. type: type,
  201. valueStart: offset
  202. };
  203. }
  204. /**
  205. * Parses a node from the source
  206. * @param {ParseContext} overlay
  207. * @param {number} start - Index of first non-whitespace character for the node
  208. * @returns {?Node} - null if at a document boundary
  209. */
  210. }, {
  211. key: "pretty",
  212. get: function get() {
  213. var obj = {
  214. start: "".concat(this.lineStart, " + ").concat(this.indent),
  215. in: [],
  216. parent: this.parent.type
  217. };
  218. if (!this.atLineStart) obj.start += ' + N';
  219. if (this.inCollection) obj.in.push('collection');
  220. if (this.inFlow) obj.in.push('flow');
  221. return obj;
  222. }
  223. }]);
  224. return ParseContext;
  225. }();
  226. exports.default = ParseContext;