Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

parseSeq.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parseSeq;
  6. var _constants = require("../constants");
  7. var _errors = require("../errors");
  8. var _Pair = _interopRequireDefault(require("./Pair"));
  9. var _parseUtils = require("./parseUtils");
  10. var _Seq = _interopRequireDefault(require("./Seq"));
  11. var _Collection = _interopRequireDefault(require("./Collection"));
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function parseSeq(doc, cst) {
  14. if (cst.type !== _constants.Type.SEQ && cst.type !== _constants.Type.FLOW_SEQ) {
  15. const msg = `A ${cst.type} node cannot be resolved as a sequence`;
  16. doc.errors.push(new _errors.YAMLSyntaxError(cst, msg));
  17. return null;
  18. }
  19. const {
  20. comments,
  21. items
  22. } = cst.type === _constants.Type.FLOW_SEQ ? resolveFlowSeqItems(doc, cst) : resolveBlockSeqItems(doc, cst);
  23. const seq = new _Seq.default();
  24. seq.items = items;
  25. (0, _parseUtils.resolveComments)(seq, comments);
  26. if (!doc.options.mapAsMap && items.some(it => it instanceof _Pair.default && it.key instanceof _Collection.default)) {
  27. const warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
  28. doc.warnings.push(new _errors.YAMLWarning(cst, warn));
  29. }
  30. cst.resolved = seq;
  31. return seq;
  32. }
  33. function resolveBlockSeqItems(doc, cst) {
  34. const comments = [];
  35. const items = [];
  36. for (let i = 0; i < cst.items.length; ++i) {
  37. const item = cst.items[i];
  38. switch (item.type) {
  39. case _constants.Type.BLANK_LINE:
  40. comments.push({
  41. before: items.length
  42. });
  43. break;
  44. case _constants.Type.COMMENT:
  45. comments.push({
  46. comment: item.comment,
  47. before: items.length
  48. });
  49. break;
  50. case _constants.Type.SEQ_ITEM:
  51. if (item.error) doc.errors.push(item.error);
  52. items.push(doc.resolveNode(item.node));
  53. if (item.hasProps) {
  54. const msg = 'Sequence items cannot have tags or anchors before the - indicator';
  55. doc.errors.push(new _errors.YAMLSemanticError(item, msg));
  56. }
  57. break;
  58. default:
  59. if (item.error) doc.errors.push(item.error);
  60. doc.errors.push(new _errors.YAMLSyntaxError(item, `Unexpected ${item.type} node in sequence`));
  61. }
  62. }
  63. return {
  64. comments,
  65. items
  66. };
  67. }
  68. function resolveFlowSeqItems(doc, cst) {
  69. const comments = [];
  70. const items = [];
  71. let explicitKey = false;
  72. let key = undefined;
  73. let keyStart = null;
  74. let next = '[';
  75. for (let i = 0; i < cst.items.length; ++i) {
  76. const item = cst.items[i];
  77. if (typeof item.char === 'string') {
  78. const {
  79. char,
  80. offset
  81. } = item;
  82. if (char !== ':' && (explicitKey || key !== undefined)) {
  83. if (explicitKey && key === undefined) key = next ? items.pop() : null;
  84. items.push(new _Pair.default(key));
  85. explicitKey = false;
  86. key = undefined;
  87. keyStart = null;
  88. }
  89. if (char === next) {
  90. next = null;
  91. } else if (!next && char === '?') {
  92. explicitKey = true;
  93. } else if (next !== '[' && char === ':' && key === undefined) {
  94. if (next === ',') {
  95. key = items.pop();
  96. if (key instanceof _Pair.default) {
  97. const msg = 'Chaining flow sequence pairs is invalid';
  98. const err = new _errors.YAMLSemanticError(cst, msg);
  99. err.offset = offset;
  100. doc.errors.push(err);
  101. }
  102. if (!explicitKey) (0, _parseUtils.checkKeyLength)(doc.errors, cst, i, key, keyStart);
  103. } else {
  104. key = null;
  105. }
  106. keyStart = null;
  107. explicitKey = false; // TODO: add error for non-explicit multiline plain key
  108. next = null;
  109. } else if (next === '[' || char !== ']' || i < cst.items.length - 1) {
  110. const msg = `Flow sequence contains an unexpected ${char}`;
  111. const err = new _errors.YAMLSyntaxError(cst, msg);
  112. err.offset = offset;
  113. doc.errors.push(err);
  114. }
  115. } else if (item.type === _constants.Type.BLANK_LINE) {
  116. comments.push({
  117. before: items.length
  118. });
  119. } else if (item.type === _constants.Type.COMMENT) {
  120. comments.push({
  121. comment: item.comment,
  122. before: items.length
  123. });
  124. } else {
  125. if (next) {
  126. const msg = `Expected a ${next} in flow sequence`;
  127. doc.errors.push(new _errors.YAMLSemanticError(item, msg));
  128. }
  129. const value = doc.resolveNode(item);
  130. if (key === undefined) {
  131. items.push(value);
  132. } else {
  133. items.push(new _Pair.default(key, value));
  134. key = undefined;
  135. }
  136. keyStart = item.range.start;
  137. next = ',';
  138. }
  139. }
  140. (0, _parseUtils.checkFlowCollectionEnd)(doc.errors, cst);
  141. if (key !== undefined) items.push(new _Pair.default(key));
  142. return {
  143. comments,
  144. items
  145. };
  146. }