parse-cst.js 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  1. import { j as _inherits, k as _createSuper, c as _classCallCheck, T as Type, b as _createClass, R as Range, N as Node, g as YAMLSemanticError, l as _get, m as _getPrototypeOf, Y as YAMLSyntaxError, C as Char, e as _defineProperty, P as PlainValue } from './PlainValue-ff5147c6.js';
  2. var BlankLine = /*#__PURE__*/function (_Node) {
  3. _inherits(BlankLine, _Node);
  4. var _super = _createSuper(BlankLine);
  5. function BlankLine() {
  6. _classCallCheck(this, BlankLine);
  7. return _super.call(this, Type.BLANK_LINE);
  8. }
  9. /* istanbul ignore next */
  10. _createClass(BlankLine, [{
  11. key: "parse",
  12. /**
  13. * Parses a blank line from the source
  14. *
  15. * @param {ParseContext} context
  16. * @param {number} start - Index of first \n character
  17. * @returns {number} - Index of the character after this
  18. */
  19. value: function parse(context, start) {
  20. this.context = context;
  21. this.range = new Range(start, start + 1);
  22. return start + 1;
  23. }
  24. }, {
  25. key: "includesTrailingLines",
  26. get: function get() {
  27. // This is never called from anywhere, but if it were,
  28. // this is the value it should return.
  29. return true;
  30. }
  31. }]);
  32. return BlankLine;
  33. }(Node);
  34. var CollectionItem = /*#__PURE__*/function (_Node) {
  35. _inherits(CollectionItem, _Node);
  36. var _super = _createSuper(CollectionItem);
  37. function CollectionItem(type, props) {
  38. var _this;
  39. _classCallCheck(this, CollectionItem);
  40. _this = _super.call(this, type, props);
  41. _this.node = null;
  42. return _this;
  43. }
  44. _createClass(CollectionItem, [{
  45. key: "parse",
  46. /**
  47. * @param {ParseContext} context
  48. * @param {number} start - Index of first character
  49. * @returns {number} - Index of the character after this
  50. */
  51. value: function parse(context, start) {
  52. this.context = context;
  53. var parseNode = context.parseNode,
  54. src = context.src;
  55. var atLineStart = context.atLineStart,
  56. lineStart = context.lineStart;
  57. if (!atLineStart && this.type === Type.SEQ_ITEM) this.error = new YAMLSemanticError(this, 'Sequence items must not have preceding content on the same line');
  58. var indent = atLineStart ? start - lineStart : context.indent;
  59. var offset = Node.endOfWhiteSpace(src, start + 1);
  60. var ch = src[offset];
  61. var inlineComment = ch === '#';
  62. var comments = [];
  63. var blankLine = null;
  64. while (ch === '\n' || ch === '#') {
  65. if (ch === '#') {
  66. var _end = Node.endOfLine(src, offset + 1);
  67. comments.push(new Range(offset, _end));
  68. offset = _end;
  69. } else {
  70. atLineStart = true;
  71. lineStart = offset + 1;
  72. var wsEnd = Node.endOfWhiteSpace(src, lineStart);
  73. if (src[wsEnd] === '\n' && comments.length === 0) {
  74. blankLine = new BlankLine();
  75. lineStart = blankLine.parse({
  76. src: src
  77. }, lineStart);
  78. }
  79. offset = Node.endOfIndent(src, lineStart);
  80. }
  81. ch = src[offset];
  82. }
  83. if (Node.nextNodeIsIndented(ch, offset - (lineStart + indent), this.type !== Type.SEQ_ITEM)) {
  84. this.node = parseNode({
  85. atLineStart: atLineStart,
  86. inCollection: false,
  87. indent: indent,
  88. lineStart: lineStart,
  89. parent: this
  90. }, offset);
  91. } else if (ch && lineStart > start + 1) {
  92. offset = lineStart - 1;
  93. }
  94. if (this.node) {
  95. if (blankLine) {
  96. // Only blank lines preceding non-empty nodes are captured. Note that
  97. // this means that collection item range start indices do not always
  98. // increase monotonically. -- eemeli/yaml#126
  99. var items = context.parent.items || context.parent.contents;
  100. if (items) items.push(blankLine);
  101. }
  102. if (comments.length) Array.prototype.push.apply(this.props, comments);
  103. offset = this.node.range.end;
  104. } else {
  105. if (inlineComment) {
  106. var c = comments[0];
  107. this.props.push(c);
  108. offset = c.end;
  109. } else {
  110. offset = Node.endOfLine(src, start + 1);
  111. }
  112. }
  113. var end = this.node ? this.node.valueRange.end : offset;
  114. this.valueRange = new Range(start, end);
  115. return offset;
  116. }
  117. }, {
  118. key: "setOrigRanges",
  119. value: function setOrigRanges(cr, offset) {
  120. offset = _get(_getPrototypeOf(CollectionItem.prototype), "setOrigRanges", this).call(this, cr, offset);
  121. return this.node ? this.node.setOrigRanges(cr, offset) : offset;
  122. }
  123. }, {
  124. key: "toString",
  125. value: function toString() {
  126. var src = this.context.src,
  127. node = this.node,
  128. range = this.range,
  129. value = this.value;
  130. if (value != null) return value;
  131. var str = node ? src.slice(range.start, node.range.start) + String(node) : src.slice(range.start, range.end);
  132. return Node.addStringTerminator(src, range.end, str);
  133. }
  134. }, {
  135. key: "includesTrailingLines",
  136. get: function get() {
  137. return !!this.node && this.node.includesTrailingLines;
  138. }
  139. }]);
  140. return CollectionItem;
  141. }(Node);
  142. var Comment = /*#__PURE__*/function (_Node) {
  143. _inherits(Comment, _Node);
  144. var _super = _createSuper(Comment);
  145. function Comment() {
  146. _classCallCheck(this, Comment);
  147. return _super.call(this, Type.COMMENT);
  148. }
  149. /**
  150. * Parses a comment line from the source
  151. *
  152. * @param {ParseContext} context
  153. * @param {number} start - Index of first character
  154. * @returns {number} - Index of the character after this scalar
  155. */
  156. _createClass(Comment, [{
  157. key: "parse",
  158. value: function parse(context, start) {
  159. this.context = context;
  160. var offset = this.parseComment(start);
  161. this.range = new Range(start, offset);
  162. return offset;
  163. }
  164. }]);
  165. return Comment;
  166. }(Node);
  167. function grabCollectionEndComments(node) {
  168. var cnode = node;
  169. while (cnode instanceof CollectionItem) {
  170. cnode = cnode.node;
  171. }
  172. if (!(cnode instanceof Collection)) return null;
  173. var len = cnode.items.length;
  174. var ci = -1;
  175. for (var i = len - 1; i >= 0; --i) {
  176. var n = cnode.items[i];
  177. if (n.type === Type.COMMENT) {
  178. // Keep sufficiently indented comments with preceding node
  179. var _n$context = n.context,
  180. indent = _n$context.indent,
  181. lineStart = _n$context.lineStart;
  182. if (indent > 0 && n.range.start >= lineStart + indent) break;
  183. ci = i;
  184. } else if (n.type === Type.BLANK_LINE) ci = i;else break;
  185. }
  186. if (ci === -1) return null;
  187. var ca = cnode.items.splice(ci, len - ci);
  188. var prevEnd = ca[0].range.start;
  189. while (true) {
  190. cnode.range.end = prevEnd;
  191. if (cnode.valueRange && cnode.valueRange.end > prevEnd) cnode.valueRange.end = prevEnd;
  192. if (cnode === node) break;
  193. cnode = cnode.context.parent;
  194. }
  195. return ca;
  196. }
  197. var Collection = /*#__PURE__*/function (_Node) {
  198. _inherits(Collection, _Node);
  199. var _super = _createSuper(Collection);
  200. _createClass(Collection, null, [{
  201. key: "nextContentHasIndent",
  202. value: function nextContentHasIndent(src, offset, indent) {
  203. var lineStart = Node.endOfLine(src, offset) + 1;
  204. offset = Node.endOfWhiteSpace(src, lineStart);
  205. var ch = src[offset];
  206. if (!ch) return false;
  207. if (offset >= lineStart + indent) return true;
  208. if (ch !== '#' && ch !== '\n') return false;
  209. return Collection.nextContentHasIndent(src, offset, indent);
  210. }
  211. }]);
  212. function Collection(firstItem) {
  213. var _this;
  214. _classCallCheck(this, Collection);
  215. _this = _super.call(this, firstItem.type === Type.SEQ_ITEM ? Type.SEQ : Type.MAP);
  216. for (var i = firstItem.props.length - 1; i >= 0; --i) {
  217. if (firstItem.props[i].start < firstItem.context.lineStart) {
  218. // props on previous line are assumed by the collection
  219. _this.props = firstItem.props.slice(0, i + 1);
  220. firstItem.props = firstItem.props.slice(i + 1);
  221. var itemRange = firstItem.props[0] || firstItem.valueRange;
  222. firstItem.range.start = itemRange.start;
  223. break;
  224. }
  225. }
  226. _this.items = [firstItem];
  227. var ec = grabCollectionEndComments(firstItem);
  228. if (ec) Array.prototype.push.apply(_this.items, ec);
  229. return _this;
  230. }
  231. _createClass(Collection, [{
  232. key: "parse",
  233. /**
  234. * @param {ParseContext} context
  235. * @param {number} start - Index of first character
  236. * @returns {number} - Index of the character after this
  237. */
  238. value: function parse(context, start) {
  239. this.context = context;
  240. var parseNode = context.parseNode,
  241. src = context.src; // It's easier to recalculate lineStart here rather than tracking down the
  242. // last context from which to read it -- eemeli/yaml#2
  243. var lineStart = Node.startOfLine(src, start);
  244. var firstItem = this.items[0]; // First-item context needs to be correct for later comment handling
  245. // -- eemeli/yaml#17
  246. firstItem.context.parent = this;
  247. this.valueRange = Range.copy(firstItem.valueRange);
  248. var indent = firstItem.range.start - firstItem.context.lineStart;
  249. var offset = start;
  250. offset = Node.normalizeOffset(src, offset);
  251. var ch = src[offset];
  252. var atLineStart = Node.endOfWhiteSpace(src, lineStart) === offset;
  253. var prevIncludesTrailingLines = false;
  254. while (ch) {
  255. while (ch === '\n' || ch === '#') {
  256. if (atLineStart && ch === '\n' && !prevIncludesTrailingLines) {
  257. var blankLine = new BlankLine();
  258. offset = blankLine.parse({
  259. src: src
  260. }, offset);
  261. this.valueRange.end = offset;
  262. if (offset >= src.length) {
  263. ch = null;
  264. break;
  265. }
  266. this.items.push(blankLine);
  267. offset -= 1; // blankLine.parse() consumes terminal newline
  268. } else if (ch === '#') {
  269. if (offset < lineStart + indent && !Collection.nextContentHasIndent(src, offset, indent)) {
  270. return offset;
  271. }
  272. var comment = new Comment();
  273. offset = comment.parse({
  274. indent: indent,
  275. lineStart: lineStart,
  276. src: src
  277. }, offset);
  278. this.items.push(comment);
  279. this.valueRange.end = offset;
  280. if (offset >= src.length) {
  281. ch = null;
  282. break;
  283. }
  284. }
  285. lineStart = offset + 1;
  286. offset = Node.endOfIndent(src, lineStart);
  287. if (Node.atBlank(src, offset)) {
  288. var wsEnd = Node.endOfWhiteSpace(src, offset);
  289. var next = src[wsEnd];
  290. if (!next || next === '\n' || next === '#') {
  291. offset = wsEnd;
  292. }
  293. }
  294. ch = src[offset];
  295. atLineStart = true;
  296. }
  297. if (!ch) {
  298. break;
  299. }
  300. if (offset !== lineStart + indent && (atLineStart || ch !== ':')) {
  301. if (offset < lineStart + indent) {
  302. if (lineStart > start) offset = lineStart;
  303. break;
  304. } else if (!this.error) {
  305. var msg = 'All collection items must start at the same column';
  306. this.error = new YAMLSyntaxError(this, msg);
  307. }
  308. }
  309. if (firstItem.type === Type.SEQ_ITEM) {
  310. if (ch !== '-') {
  311. if (lineStart > start) offset = lineStart;
  312. break;
  313. }
  314. } else if (ch === '-' && !this.error) {
  315. // map key may start with -, as long as it's followed by a non-whitespace char
  316. var _next = src[offset + 1];
  317. if (!_next || _next === '\n' || _next === '\t' || _next === ' ') {
  318. var _msg = 'A collection cannot be both a mapping and a sequence';
  319. this.error = new YAMLSyntaxError(this, _msg);
  320. }
  321. }
  322. var node = parseNode({
  323. atLineStart: atLineStart,
  324. inCollection: true,
  325. indent: indent,
  326. lineStart: lineStart,
  327. parent: this
  328. }, offset);
  329. if (!node) return offset; // at next document start
  330. this.items.push(node);
  331. this.valueRange.end = node.valueRange.end;
  332. offset = Node.normalizeOffset(src, node.range.end);
  333. ch = src[offset];
  334. atLineStart = false;
  335. prevIncludesTrailingLines = node.includesTrailingLines; // Need to reset lineStart and atLineStart here if preceding node's range
  336. // has advanced to check the current line's indentation level
  337. // -- eemeli/yaml#10 & eemeli/yaml#38
  338. if (ch) {
  339. var ls = offset - 1;
  340. var prev = src[ls];
  341. while (prev === ' ' || prev === '\t') {
  342. prev = src[--ls];
  343. }
  344. if (prev === '\n') {
  345. lineStart = ls + 1;
  346. atLineStart = true;
  347. }
  348. }
  349. var ec = grabCollectionEndComments(node);
  350. if (ec) Array.prototype.push.apply(this.items, ec);
  351. }
  352. return offset;
  353. }
  354. }, {
  355. key: "setOrigRanges",
  356. value: function setOrigRanges(cr, offset) {
  357. offset = _get(_getPrototypeOf(Collection.prototype), "setOrigRanges", this).call(this, cr, offset);
  358. this.items.forEach(function (node) {
  359. offset = node.setOrigRanges(cr, offset);
  360. });
  361. return offset;
  362. }
  363. }, {
  364. key: "toString",
  365. value: function toString() {
  366. var src = this.context.src,
  367. items = this.items,
  368. range = this.range,
  369. value = this.value;
  370. if (value != null) return value;
  371. var str = src.slice(range.start, items[0].range.start) + String(items[0]);
  372. for (var i = 1; i < items.length; ++i) {
  373. var item = items[i];
  374. var _item$context = item.context,
  375. atLineStart = _item$context.atLineStart,
  376. indent = _item$context.indent;
  377. if (atLineStart) for (var _i = 0; _i < indent; ++_i) {
  378. str += ' ';
  379. }
  380. str += String(item);
  381. }
  382. return Node.addStringTerminator(src, range.end, str);
  383. }
  384. }, {
  385. key: "includesTrailingLines",
  386. get: function get() {
  387. return this.items.length > 0;
  388. }
  389. }]);
  390. return Collection;
  391. }(Node);
  392. var Directive = /*#__PURE__*/function (_Node) {
  393. _inherits(Directive, _Node);
  394. var _super = _createSuper(Directive);
  395. function Directive() {
  396. var _this;
  397. _classCallCheck(this, Directive);
  398. _this = _super.call(this, Type.DIRECTIVE);
  399. _this.name = null;
  400. return _this;
  401. }
  402. _createClass(Directive, [{
  403. key: "parseName",
  404. value: function parseName(start) {
  405. var src = this.context.src;
  406. var offset = start;
  407. var ch = src[offset];
  408. while (ch && ch !== '\n' && ch !== '\t' && ch !== ' ') {
  409. ch = src[offset += 1];
  410. }
  411. this.name = src.slice(start, offset);
  412. return offset;
  413. }
  414. }, {
  415. key: "parseParameters",
  416. value: function parseParameters(start) {
  417. var src = this.context.src;
  418. var offset = start;
  419. var ch = src[offset];
  420. while (ch && ch !== '\n' && ch !== '#') {
  421. ch = src[offset += 1];
  422. }
  423. this.valueRange = new Range(start, offset);
  424. return offset;
  425. }
  426. }, {
  427. key: "parse",
  428. value: function parse(context, start) {
  429. this.context = context;
  430. var offset = this.parseName(start + 1);
  431. offset = this.parseParameters(offset);
  432. offset = this.parseComment(offset);
  433. this.range = new Range(start, offset);
  434. return offset;
  435. }
  436. }, {
  437. key: "parameters",
  438. get: function get() {
  439. var raw = this.rawValue;
  440. return raw ? raw.trim().split(/[ \t]+/) : [];
  441. }
  442. }]);
  443. return Directive;
  444. }(Node);
  445. var Document = /*#__PURE__*/function (_Node) {
  446. _inherits(Document, _Node);
  447. var _super = _createSuper(Document);
  448. _createClass(Document, null, [{
  449. key: "startCommentOrEndBlankLine",
  450. value: function startCommentOrEndBlankLine(src, start) {
  451. var offset = Node.endOfWhiteSpace(src, start);
  452. var ch = src[offset];
  453. return ch === '#' || ch === '\n' ? offset : start;
  454. }
  455. }]);
  456. function Document() {
  457. var _this;
  458. _classCallCheck(this, Document);
  459. _this = _super.call(this, Type.DOCUMENT);
  460. _this.directives = null;
  461. _this.contents = null;
  462. _this.directivesEndMarker = null;
  463. _this.documentEndMarker = null;
  464. return _this;
  465. }
  466. _createClass(Document, [{
  467. key: "parseDirectives",
  468. value: function parseDirectives(start) {
  469. var src = this.context.src;
  470. this.directives = [];
  471. var atLineStart = true;
  472. var hasDirectives = false;
  473. var offset = start;
  474. while (!Node.atDocumentBoundary(src, offset, Char.DIRECTIVES_END)) {
  475. offset = Document.startCommentOrEndBlankLine(src, offset);
  476. switch (src[offset]) {
  477. case '\n':
  478. if (atLineStart) {
  479. var blankLine = new BlankLine();
  480. offset = blankLine.parse({
  481. src: src
  482. }, offset);
  483. if (offset < src.length) {
  484. this.directives.push(blankLine);
  485. }
  486. } else {
  487. offset += 1;
  488. atLineStart = true;
  489. }
  490. break;
  491. case '#':
  492. {
  493. var comment = new Comment();
  494. offset = comment.parse({
  495. src: src
  496. }, offset);
  497. this.directives.push(comment);
  498. atLineStart = false;
  499. }
  500. break;
  501. case '%':
  502. {
  503. var directive = new Directive();
  504. offset = directive.parse({
  505. parent: this,
  506. src: src
  507. }, offset);
  508. this.directives.push(directive);
  509. hasDirectives = true;
  510. atLineStart = false;
  511. }
  512. break;
  513. default:
  514. if (hasDirectives) {
  515. this.error = new YAMLSemanticError(this, 'Missing directives-end indicator line');
  516. } else if (this.directives.length > 0) {
  517. this.contents = this.directives;
  518. this.directives = [];
  519. }
  520. return offset;
  521. }
  522. }
  523. if (src[offset]) {
  524. this.directivesEndMarker = new Range(offset, offset + 3);
  525. return offset + 3;
  526. }
  527. if (hasDirectives) {
  528. this.error = new YAMLSemanticError(this, 'Missing directives-end indicator line');
  529. } else if (this.directives.length > 0) {
  530. this.contents = this.directives;
  531. this.directives = [];
  532. }
  533. return offset;
  534. }
  535. }, {
  536. key: "parseContents",
  537. value: function parseContents(start) {
  538. var _this$context = this.context,
  539. parseNode = _this$context.parseNode,
  540. src = _this$context.src;
  541. if (!this.contents) this.contents = [];
  542. var lineStart = start;
  543. while (src[lineStart - 1] === '-') {
  544. lineStart -= 1;
  545. }
  546. var offset = Node.endOfWhiteSpace(src, start);
  547. var atLineStart = lineStart === start;
  548. this.valueRange = new Range(offset);
  549. while (!Node.atDocumentBoundary(src, offset, Char.DOCUMENT_END)) {
  550. switch (src[offset]) {
  551. case '\n':
  552. if (atLineStart) {
  553. var blankLine = new BlankLine();
  554. offset = blankLine.parse({
  555. src: src
  556. }, offset);
  557. if (offset < src.length) {
  558. this.contents.push(blankLine);
  559. }
  560. } else {
  561. offset += 1;
  562. atLineStart = true;
  563. }
  564. lineStart = offset;
  565. break;
  566. case '#':
  567. {
  568. var comment = new Comment();
  569. offset = comment.parse({
  570. src: src
  571. }, offset);
  572. this.contents.push(comment);
  573. atLineStart = false;
  574. }
  575. break;
  576. default:
  577. {
  578. var iEnd = Node.endOfIndent(src, offset);
  579. var context = {
  580. atLineStart: atLineStart,
  581. indent: -1,
  582. inFlow: false,
  583. inCollection: false,
  584. lineStart: lineStart,
  585. parent: this
  586. };
  587. var node = parseNode(context, iEnd);
  588. if (!node) return this.valueRange.end = iEnd; // at next document start
  589. this.contents.push(node);
  590. offset = node.range.end;
  591. atLineStart = false;
  592. var ec = grabCollectionEndComments(node);
  593. if (ec) Array.prototype.push.apply(this.contents, ec);
  594. }
  595. }
  596. offset = Document.startCommentOrEndBlankLine(src, offset);
  597. }
  598. this.valueRange.end = offset;
  599. if (src[offset]) {
  600. this.documentEndMarker = new Range(offset, offset + 3);
  601. offset += 3;
  602. if (src[offset]) {
  603. offset = Node.endOfWhiteSpace(src, offset);
  604. if (src[offset] === '#') {
  605. var _comment = new Comment();
  606. offset = _comment.parse({
  607. src: src
  608. }, offset);
  609. this.contents.push(_comment);
  610. }
  611. switch (src[offset]) {
  612. case '\n':
  613. offset += 1;
  614. break;
  615. case undefined:
  616. break;
  617. default:
  618. this.error = new YAMLSyntaxError(this, 'Document end marker line cannot have a non-comment suffix');
  619. }
  620. }
  621. }
  622. return offset;
  623. }
  624. /**
  625. * @param {ParseContext} context
  626. * @param {number} start - Index of first character
  627. * @returns {number} - Index of the character after this
  628. */
  629. }, {
  630. key: "parse",
  631. value: function parse(context, start) {
  632. context.root = this;
  633. this.context = context;
  634. var src = context.src;
  635. var offset = src.charCodeAt(start) === 0xfeff ? start + 1 : start; // skip BOM
  636. offset = this.parseDirectives(offset);
  637. offset = this.parseContents(offset);
  638. return offset;
  639. }
  640. }, {
  641. key: "setOrigRanges",
  642. value: function setOrigRanges(cr, offset) {
  643. offset = _get(_getPrototypeOf(Document.prototype), "setOrigRanges", this).call(this, cr, offset);
  644. this.directives.forEach(function (node) {
  645. offset = node.setOrigRanges(cr, offset);
  646. });
  647. if (this.directivesEndMarker) offset = this.directivesEndMarker.setOrigRange(cr, offset);
  648. this.contents.forEach(function (node) {
  649. offset = node.setOrigRanges(cr, offset);
  650. });
  651. if (this.documentEndMarker) offset = this.documentEndMarker.setOrigRange(cr, offset);
  652. return offset;
  653. }
  654. }, {
  655. key: "toString",
  656. value: function toString() {
  657. var contents = this.contents,
  658. directives = this.directives,
  659. value = this.value;
  660. if (value != null) return value;
  661. var str = directives.join('');
  662. if (contents.length > 0) {
  663. if (directives.length > 0 || contents[0].type === Type.COMMENT) str += '---\n';
  664. str += contents.join('');
  665. }
  666. if (str[str.length - 1] !== '\n') str += '\n';
  667. return str;
  668. }
  669. }]);
  670. return Document;
  671. }(Node);
  672. var Alias = /*#__PURE__*/function (_Node) {
  673. _inherits(Alias, _Node);
  674. var _super = _createSuper(Alias);
  675. function Alias() {
  676. _classCallCheck(this, Alias);
  677. return _super.apply(this, arguments);
  678. }
  679. _createClass(Alias, [{
  680. key: "parse",
  681. /**
  682. * Parses an *alias from the source
  683. *
  684. * @param {ParseContext} context
  685. * @param {number} start - Index of first character
  686. * @returns {number} - Index of the character after this scalar
  687. */
  688. value: function parse(context, start) {
  689. this.context = context;
  690. var src = context.src;
  691. var offset = Node.endOfIdentifier(src, start + 1);
  692. this.valueRange = new Range(start + 1, offset);
  693. offset = Node.endOfWhiteSpace(src, offset);
  694. offset = this.parseComment(offset);
  695. return offset;
  696. }
  697. }]);
  698. return Alias;
  699. }(Node);
  700. var Chomp = {
  701. CLIP: 'CLIP',
  702. KEEP: 'KEEP',
  703. STRIP: 'STRIP'
  704. };
  705. var BlockValue = /*#__PURE__*/function (_Node) {
  706. _inherits(BlockValue, _Node);
  707. var _super = _createSuper(BlockValue);
  708. function BlockValue(type, props) {
  709. var _this;
  710. _classCallCheck(this, BlockValue);
  711. _this = _super.call(this, type, props);
  712. _this.blockIndent = null;
  713. _this.chomping = Chomp.CLIP;
  714. _this.header = null;
  715. return _this;
  716. }
  717. _createClass(BlockValue, [{
  718. key: "parseBlockHeader",
  719. value: function parseBlockHeader(start) {
  720. var src = this.context.src;
  721. var offset = start + 1;
  722. var bi = '';
  723. while (true) {
  724. var ch = src[offset];
  725. switch (ch) {
  726. case '-':
  727. this.chomping = Chomp.STRIP;
  728. break;
  729. case '+':
  730. this.chomping = Chomp.KEEP;
  731. break;
  732. case '0':
  733. case '1':
  734. case '2':
  735. case '3':
  736. case '4':
  737. case '5':
  738. case '6':
  739. case '7':
  740. case '8':
  741. case '9':
  742. bi += ch;
  743. break;
  744. default:
  745. this.blockIndent = Number(bi) || null;
  746. this.header = new Range(start, offset);
  747. return offset;
  748. }
  749. offset += 1;
  750. }
  751. }
  752. }, {
  753. key: "parseBlockValue",
  754. value: function parseBlockValue(start) {
  755. var _this$context = this.context,
  756. indent = _this$context.indent,
  757. src = _this$context.src;
  758. var explicit = !!this.blockIndent;
  759. var offset = start;
  760. var valueEnd = start;
  761. var minBlockIndent = 1;
  762. for (var ch = src[offset]; ch === '\n'; ch = src[offset]) {
  763. offset += 1;
  764. if (Node.atDocumentBoundary(src, offset)) break;
  765. var end = Node.endOfBlockIndent(src, indent, offset); // should not include tab?
  766. if (end === null) break;
  767. var _ch = src[end];
  768. var lineIndent = end - (offset + indent);
  769. if (!this.blockIndent) {
  770. // no explicit block indent, none yet detected
  771. if (src[end] !== '\n') {
  772. // first line with non-whitespace content
  773. if (lineIndent < minBlockIndent) {
  774. var msg = 'Block scalars with more-indented leading empty lines must use an explicit indentation indicator';
  775. this.error = new YAMLSemanticError(this, msg);
  776. }
  777. this.blockIndent = lineIndent;
  778. } else if (lineIndent > minBlockIndent) {
  779. // empty line with more whitespace
  780. minBlockIndent = lineIndent;
  781. }
  782. } else if (_ch && _ch !== '\n' && lineIndent < this.blockIndent) {
  783. if (src[end] === '#') break;
  784. if (!this.error) {
  785. var _src = explicit ? 'explicit indentation indicator' : 'first line';
  786. var _msg = "Block scalars must not be less indented than their ".concat(_src);
  787. this.error = new YAMLSemanticError(this, _msg);
  788. }
  789. }
  790. if (src[end] === '\n') {
  791. offset = end;
  792. } else {
  793. offset = valueEnd = Node.endOfLine(src, end);
  794. }
  795. }
  796. if (this.chomping !== Chomp.KEEP) {
  797. offset = src[valueEnd] ? valueEnd + 1 : valueEnd;
  798. }
  799. this.valueRange = new Range(start + 1, offset);
  800. return offset;
  801. }
  802. /**
  803. * Parses a block value from the source
  804. *
  805. * Accepted forms are:
  806. * ```
  807. * BS
  808. * block
  809. * lines
  810. *
  811. * BS #comment
  812. * block
  813. * lines
  814. * ```
  815. * where the block style BS matches the regexp `[|>][-+1-9]*` and block lines
  816. * are empty or have an indent level greater than `indent`.
  817. *
  818. * @param {ParseContext} context
  819. * @param {number} start - Index of first character
  820. * @returns {number} - Index of the character after this block
  821. */
  822. }, {
  823. key: "parse",
  824. value: function parse(context, start) {
  825. this.context = context;
  826. var src = context.src;
  827. var offset = this.parseBlockHeader(start);
  828. offset = Node.endOfWhiteSpace(src, offset);
  829. offset = this.parseComment(offset);
  830. offset = this.parseBlockValue(offset);
  831. return offset;
  832. }
  833. }, {
  834. key: "setOrigRanges",
  835. value: function setOrigRanges(cr, offset) {
  836. offset = _get(_getPrototypeOf(BlockValue.prototype), "setOrigRanges", this).call(this, cr, offset);
  837. return this.header ? this.header.setOrigRange(cr, offset) : offset;
  838. }
  839. }, {
  840. key: "includesTrailingLines",
  841. get: function get() {
  842. return this.chomping === Chomp.KEEP;
  843. }
  844. }, {
  845. key: "strValue",
  846. get: function get() {
  847. if (!this.valueRange || !this.context) return null;
  848. var _this$valueRange = this.valueRange,
  849. start = _this$valueRange.start,
  850. end = _this$valueRange.end;
  851. var _this$context2 = this.context,
  852. indent = _this$context2.indent,
  853. src = _this$context2.src;
  854. if (this.valueRange.isEmpty()) return '';
  855. var lastNewLine = null;
  856. var ch = src[end - 1];
  857. while (ch === '\n' || ch === '\t' || ch === ' ') {
  858. end -= 1;
  859. if (end <= start) {
  860. if (this.chomping === Chomp.KEEP) break;else return ''; // probably never happens
  861. }
  862. if (ch === '\n') lastNewLine = end;
  863. ch = src[end - 1];
  864. }
  865. var keepStart = end + 1;
  866. if (lastNewLine) {
  867. if (this.chomping === Chomp.KEEP) {
  868. keepStart = lastNewLine;
  869. end = this.valueRange.end;
  870. } else {
  871. end = lastNewLine;
  872. }
  873. }
  874. var bi = indent + this.blockIndent;
  875. var folded = this.type === Type.BLOCK_FOLDED;
  876. var atStart = true;
  877. var str = '';
  878. var sep = '';
  879. var prevMoreIndented = false;
  880. for (var i = start; i < end; ++i) {
  881. for (var j = 0; j < bi; ++j) {
  882. if (src[i] !== ' ') break;
  883. i += 1;
  884. }
  885. var _ch2 = src[i];
  886. if (_ch2 === '\n') {
  887. if (sep === '\n') str += '\n';else sep = '\n';
  888. } else {
  889. var lineEnd = Node.endOfLine(src, i);
  890. var line = src.slice(i, lineEnd);
  891. i = lineEnd;
  892. if (folded && (_ch2 === ' ' || _ch2 === '\t') && i < keepStart) {
  893. if (sep === ' ') sep = '\n';else if (!prevMoreIndented && !atStart && sep === '\n') sep = '\n\n';
  894. str += sep + line; //+ ((lineEnd < end && src[lineEnd]) || '')
  895. sep = lineEnd < end && src[lineEnd] || '';
  896. prevMoreIndented = true;
  897. } else {
  898. str += sep + line;
  899. sep = folded && i < keepStart ? ' ' : '\n';
  900. prevMoreIndented = false;
  901. }
  902. if (atStart && line !== '') atStart = false;
  903. }
  904. }
  905. return this.chomping === Chomp.STRIP ? str : str + '\n';
  906. }
  907. }]);
  908. return BlockValue;
  909. }(Node);
  910. var FlowCollection = /*#__PURE__*/function (_Node) {
  911. _inherits(FlowCollection, _Node);
  912. var _super = _createSuper(FlowCollection);
  913. function FlowCollection(type, props) {
  914. var _this;
  915. _classCallCheck(this, FlowCollection);
  916. _this = _super.call(this, type, props);
  917. _this.items = null;
  918. return _this;
  919. }
  920. _createClass(FlowCollection, [{
  921. key: "prevNodeIsJsonLike",
  922. value: function prevNodeIsJsonLike() {
  923. var idx = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.items.length;
  924. var node = this.items[idx - 1];
  925. return !!node && (node.jsonLike || node.type === Type.COMMENT && this.prevNodeIsJsonLike(idx - 1));
  926. }
  927. /**
  928. * @param {ParseContext} context
  929. * @param {number} start - Index of first character
  930. * @returns {number} - Index of the character after this
  931. */
  932. }, {
  933. key: "parse",
  934. value: function parse(context, start) {
  935. this.context = context;
  936. var parseNode = context.parseNode,
  937. src = context.src;
  938. var indent = context.indent,
  939. lineStart = context.lineStart;
  940. var char = src[start]; // { or [
  941. this.items = [{
  942. char: char,
  943. offset: start
  944. }];
  945. var offset = Node.endOfWhiteSpace(src, start + 1);
  946. char = src[offset];
  947. while (char && char !== ']' && char !== '}') {
  948. switch (char) {
  949. case '\n':
  950. {
  951. lineStart = offset + 1;
  952. var wsEnd = Node.endOfWhiteSpace(src, lineStart);
  953. if (src[wsEnd] === '\n') {
  954. var blankLine = new BlankLine();
  955. lineStart = blankLine.parse({
  956. src: src
  957. }, lineStart);
  958. this.items.push(blankLine);
  959. }
  960. offset = Node.endOfIndent(src, lineStart);
  961. if (offset <= lineStart + indent) {
  962. char = src[offset];
  963. if (offset < lineStart + indent || char !== ']' && char !== '}') {
  964. var msg = 'Insufficient indentation in flow collection';
  965. this.error = new YAMLSemanticError(this, msg);
  966. }
  967. }
  968. }
  969. break;
  970. case ',':
  971. {
  972. this.items.push({
  973. char: char,
  974. offset: offset
  975. });
  976. offset += 1;
  977. }
  978. break;
  979. case '#':
  980. {
  981. var comment = new Comment();
  982. offset = comment.parse({
  983. src: src
  984. }, offset);
  985. this.items.push(comment);
  986. }
  987. break;
  988. case '?':
  989. case ':':
  990. {
  991. var next = src[offset + 1];
  992. if (next === '\n' || next === '\t' || next === ' ' || next === ',' || // in-flow : after JSON-like key does not need to be followed by whitespace
  993. char === ':' && this.prevNodeIsJsonLike()) {
  994. this.items.push({
  995. char: char,
  996. offset: offset
  997. });
  998. offset += 1;
  999. break;
  1000. }
  1001. }
  1002. // fallthrough
  1003. default:
  1004. {
  1005. var node = parseNode({
  1006. atLineStart: false,
  1007. inCollection: false,
  1008. inFlow: true,
  1009. indent: -1,
  1010. lineStart: lineStart,
  1011. parent: this
  1012. }, offset);
  1013. if (!node) {
  1014. // at next document start
  1015. this.valueRange = new Range(start, offset);
  1016. return offset;
  1017. }
  1018. this.items.push(node);
  1019. offset = Node.normalizeOffset(src, node.range.end);
  1020. }
  1021. }
  1022. offset = Node.endOfWhiteSpace(src, offset);
  1023. char = src[offset];
  1024. }
  1025. this.valueRange = new Range(start, offset + 1);
  1026. if (char) {
  1027. this.items.push({
  1028. char: char,
  1029. offset: offset
  1030. });
  1031. offset = Node.endOfWhiteSpace(src, offset + 1);
  1032. offset = this.parseComment(offset);
  1033. }
  1034. return offset;
  1035. }
  1036. }, {
  1037. key: "setOrigRanges",
  1038. value: function setOrigRanges(cr, offset) {
  1039. offset = _get(_getPrototypeOf(FlowCollection.prototype), "setOrigRanges", this).call(this, cr, offset);
  1040. this.items.forEach(function (node) {
  1041. if (node instanceof Node) {
  1042. offset = node.setOrigRanges(cr, offset);
  1043. } else if (cr.length === 0) {
  1044. node.origOffset = node.offset;
  1045. } else {
  1046. var i = offset;
  1047. while (i < cr.length) {
  1048. if (cr[i] > node.offset) break;else ++i;
  1049. }
  1050. node.origOffset = node.offset + i;
  1051. offset = i;
  1052. }
  1053. });
  1054. return offset;
  1055. }
  1056. }, {
  1057. key: "toString",
  1058. value: function toString() {
  1059. var src = this.context.src,
  1060. items = this.items,
  1061. range = this.range,
  1062. value = this.value;
  1063. if (value != null) return value;
  1064. var nodes = items.filter(function (item) {
  1065. return item instanceof Node;
  1066. });
  1067. var str = '';
  1068. var prevEnd = range.start;
  1069. nodes.forEach(function (node) {
  1070. var prefix = src.slice(prevEnd, node.range.start);
  1071. prevEnd = node.range.end;
  1072. str += prefix + String(node);
  1073. if (str[str.length - 1] === '\n' && src[prevEnd - 1] !== '\n' && src[prevEnd] === '\n') {
  1074. // Comment range does not include the terminal newline, but its
  1075. // stringified value does. Without this fix, newlines at comment ends
  1076. // get duplicated.
  1077. prevEnd += 1;
  1078. }
  1079. });
  1080. str += src.slice(prevEnd, range.end);
  1081. return Node.addStringTerminator(src, range.end, str);
  1082. }
  1083. }]);
  1084. return FlowCollection;
  1085. }(Node);
  1086. var QuoteDouble = /*#__PURE__*/function (_Node) {
  1087. _inherits(QuoteDouble, _Node);
  1088. var _super = _createSuper(QuoteDouble);
  1089. function QuoteDouble() {
  1090. _classCallCheck(this, QuoteDouble);
  1091. return _super.apply(this, arguments);
  1092. }
  1093. _createClass(QuoteDouble, [{
  1094. key: "parseCharCode",
  1095. value: function parseCharCode(offset, length, errors) {
  1096. var src = this.context.src;
  1097. var cc = src.substr(offset, length);
  1098. var ok = cc.length === length && /^[0-9a-fA-F]+$/.test(cc);
  1099. var code = ok ? parseInt(cc, 16) : NaN;
  1100. if (isNaN(code)) {
  1101. errors.push(new YAMLSyntaxError(this, "Invalid escape sequence ".concat(src.substr(offset - 2, length + 2))));
  1102. return src.substr(offset - 2, length + 2);
  1103. }
  1104. return String.fromCodePoint(code);
  1105. }
  1106. /**
  1107. * Parses a "double quoted" value from the source
  1108. *
  1109. * @param {ParseContext} context
  1110. * @param {number} start - Index of first character
  1111. * @returns {number} - Index of the character after this scalar
  1112. */
  1113. }, {
  1114. key: "parse",
  1115. value: function parse(context, start) {
  1116. this.context = context;
  1117. var src = context.src;
  1118. var offset = QuoteDouble.endOfQuote(src, start + 1);
  1119. this.valueRange = new Range(start, offset);
  1120. offset = Node.endOfWhiteSpace(src, offset);
  1121. offset = this.parseComment(offset);
  1122. return offset;
  1123. }
  1124. }, {
  1125. key: "strValue",
  1126. /**
  1127. * @returns {string | { str: string, errors: YAMLSyntaxError[] }}
  1128. */
  1129. get: function get() {
  1130. if (!this.valueRange || !this.context) return null;
  1131. var errors = [];
  1132. var _this$valueRange = this.valueRange,
  1133. start = _this$valueRange.start,
  1134. end = _this$valueRange.end;
  1135. var _this$context = this.context,
  1136. indent = _this$context.indent,
  1137. src = _this$context.src;
  1138. if (src[end - 1] !== '"') errors.push(new YAMLSyntaxError(this, 'Missing closing "quote')); // Using String#replace is too painful with escaped newlines preceded by
  1139. // escaped backslashes; also, this should be faster.
  1140. var str = '';
  1141. for (var i = start + 1; i < end - 1; ++i) {
  1142. var ch = src[i];
  1143. if (ch === '\n') {
  1144. if (Node.atDocumentBoundary(src, i + 1)) errors.push(new YAMLSemanticError(this, 'Document boundary indicators are not allowed within string values'));
  1145. var _Node$foldNewline = Node.foldNewline(src, i, indent),
  1146. fold = _Node$foldNewline.fold,
  1147. offset = _Node$foldNewline.offset,
  1148. error = _Node$foldNewline.error;
  1149. str += fold;
  1150. i = offset;
  1151. if (error) errors.push(new YAMLSemanticError(this, 'Multi-line double-quoted string needs to be sufficiently indented'));
  1152. } else if (ch === '\\') {
  1153. i += 1;
  1154. switch (src[i]) {
  1155. case '0':
  1156. str += '\0';
  1157. break;
  1158. // null character
  1159. case 'a':
  1160. str += '\x07';
  1161. break;
  1162. // bell character
  1163. case 'b':
  1164. str += '\b';
  1165. break;
  1166. // backspace
  1167. case 'e':
  1168. str += '\x1b';
  1169. break;
  1170. // escape character
  1171. case 'f':
  1172. str += '\f';
  1173. break;
  1174. // form feed
  1175. case 'n':
  1176. str += '\n';
  1177. break;
  1178. // line feed
  1179. case 'r':
  1180. str += '\r';
  1181. break;
  1182. // carriage return
  1183. case 't':
  1184. str += '\t';
  1185. break;
  1186. // horizontal tab
  1187. case 'v':
  1188. str += '\v';
  1189. break;
  1190. // vertical tab
  1191. case 'N':
  1192. str += "\x85";
  1193. break;
  1194. // Unicode next line
  1195. case '_':
  1196. str += "\xA0";
  1197. break;
  1198. // Unicode non-breaking space
  1199. case 'L':
  1200. str += "\u2028";
  1201. break;
  1202. // Unicode line separator
  1203. case 'P':
  1204. str += "\u2029";
  1205. break;
  1206. // Unicode paragraph separator
  1207. case ' ':
  1208. str += ' ';
  1209. break;
  1210. case '"':
  1211. str += '"';
  1212. break;
  1213. case '/':
  1214. str += '/';
  1215. break;
  1216. case '\\':
  1217. str += '\\';
  1218. break;
  1219. case '\t':
  1220. str += '\t';
  1221. break;
  1222. case 'x':
  1223. str += this.parseCharCode(i + 1, 2, errors);
  1224. i += 2;
  1225. break;
  1226. case 'u':
  1227. str += this.parseCharCode(i + 1, 4, errors);
  1228. i += 4;
  1229. break;
  1230. case 'U':
  1231. str += this.parseCharCode(i + 1, 8, errors);
  1232. i += 8;
  1233. break;
  1234. case '\n':
  1235. // skip escaped newlines, but still trim the following line
  1236. while (src[i + 1] === ' ' || src[i + 1] === '\t') {
  1237. i += 1;
  1238. }
  1239. break;
  1240. default:
  1241. errors.push(new YAMLSyntaxError(this, "Invalid escape sequence ".concat(src.substr(i - 1, 2))));
  1242. str += '\\' + src[i];
  1243. }
  1244. } else if (ch === ' ' || ch === '\t') {
  1245. // trim trailing whitespace
  1246. var wsStart = i;
  1247. var next = src[i + 1];
  1248. while (next === ' ' || next === '\t') {
  1249. i += 1;
  1250. next = src[i + 1];
  1251. }
  1252. if (next !== '\n') str += i > wsStart ? src.slice(wsStart, i + 1) : ch;
  1253. } else {
  1254. str += ch;
  1255. }
  1256. }
  1257. return errors.length > 0 ? {
  1258. errors: errors,
  1259. str: str
  1260. } : str;
  1261. }
  1262. }], [{
  1263. key: "endOfQuote",
  1264. value: function endOfQuote(src, offset) {
  1265. var ch = src[offset];
  1266. while (ch && ch !== '"') {
  1267. offset += ch === '\\' ? 2 : 1;
  1268. ch = src[offset];
  1269. }
  1270. return offset + 1;
  1271. }
  1272. }]);
  1273. return QuoteDouble;
  1274. }(Node);
  1275. var QuoteSingle = /*#__PURE__*/function (_Node) {
  1276. _inherits(QuoteSingle, _Node);
  1277. var _super = _createSuper(QuoteSingle);
  1278. function QuoteSingle() {
  1279. _classCallCheck(this, QuoteSingle);
  1280. return _super.apply(this, arguments);
  1281. }
  1282. _createClass(QuoteSingle, [{
  1283. key: "parse",
  1284. /**
  1285. * Parses a 'single quoted' value from the source
  1286. *
  1287. * @param {ParseContext} context
  1288. * @param {number} start - Index of first character
  1289. * @returns {number} - Index of the character after this scalar
  1290. */
  1291. value: function parse(context, start) {
  1292. this.context = context;
  1293. var src = context.src;
  1294. var offset = QuoteSingle.endOfQuote(src, start + 1);
  1295. this.valueRange = new Range(start, offset);
  1296. offset = Node.endOfWhiteSpace(src, offset);
  1297. offset = this.parseComment(offset);
  1298. return offset;
  1299. }
  1300. }, {
  1301. key: "strValue",
  1302. /**
  1303. * @returns {string | { str: string, errors: YAMLSyntaxError[] }}
  1304. */
  1305. get: function get() {
  1306. if (!this.valueRange || !this.context) return null;
  1307. var errors = [];
  1308. var _this$valueRange = this.valueRange,
  1309. start = _this$valueRange.start,
  1310. end = _this$valueRange.end;
  1311. var _this$context = this.context,
  1312. indent = _this$context.indent,
  1313. src = _this$context.src;
  1314. if (src[end - 1] !== "'") errors.push(new YAMLSyntaxError(this, "Missing closing 'quote"));
  1315. var str = '';
  1316. for (var i = start + 1; i < end - 1; ++i) {
  1317. var ch = src[i];
  1318. if (ch === '\n') {
  1319. if (Node.atDocumentBoundary(src, i + 1)) errors.push(new YAMLSemanticError(this, 'Document boundary indicators are not allowed within string values'));
  1320. var _Node$foldNewline = Node.foldNewline(src, i, indent),
  1321. fold = _Node$foldNewline.fold,
  1322. offset = _Node$foldNewline.offset,
  1323. error = _Node$foldNewline.error;
  1324. str += fold;
  1325. i = offset;
  1326. if (error) errors.push(new YAMLSemanticError(this, 'Multi-line single-quoted string needs to be sufficiently indented'));
  1327. } else if (ch === "'") {
  1328. str += ch;
  1329. i += 1;
  1330. if (src[i] !== "'") errors.push(new YAMLSyntaxError(this, 'Unescaped single quote? This should not happen.'));
  1331. } else if (ch === ' ' || ch === '\t') {
  1332. // trim trailing whitespace
  1333. var wsStart = i;
  1334. var next = src[i + 1];
  1335. while (next === ' ' || next === '\t') {
  1336. i += 1;
  1337. next = src[i + 1];
  1338. }
  1339. if (next !== '\n') str += i > wsStart ? src.slice(wsStart, i + 1) : ch;
  1340. } else {
  1341. str += ch;
  1342. }
  1343. }
  1344. return errors.length > 0 ? {
  1345. errors: errors,
  1346. str: str
  1347. } : str;
  1348. }
  1349. }], [{
  1350. key: "endOfQuote",
  1351. value: function endOfQuote(src, offset) {
  1352. var ch = src[offset];
  1353. while (ch) {
  1354. if (ch === "'") {
  1355. if (src[offset + 1] !== "'") break;
  1356. ch = src[offset += 2];
  1357. } else {
  1358. ch = src[offset += 1];
  1359. }
  1360. }
  1361. return offset + 1;
  1362. }
  1363. }]);
  1364. return QuoteSingle;
  1365. }(Node);
  1366. function createNewNode(type, props) {
  1367. switch (type) {
  1368. case Type.ALIAS:
  1369. return new Alias(type, props);
  1370. case Type.BLOCK_FOLDED:
  1371. case Type.BLOCK_LITERAL:
  1372. return new BlockValue(type, props);
  1373. case Type.FLOW_MAP:
  1374. case Type.FLOW_SEQ:
  1375. return new FlowCollection(type, props);
  1376. case Type.MAP_KEY:
  1377. case Type.MAP_VALUE:
  1378. case Type.SEQ_ITEM:
  1379. return new CollectionItem(type, props);
  1380. case Type.COMMENT:
  1381. case Type.PLAIN:
  1382. return new PlainValue(type, props);
  1383. case Type.QUOTE_DOUBLE:
  1384. return new QuoteDouble(type, props);
  1385. case Type.QUOTE_SINGLE:
  1386. return new QuoteSingle(type, props);
  1387. /* istanbul ignore next */
  1388. default:
  1389. return null;
  1390. // should never happen
  1391. }
  1392. }
  1393. /**
  1394. * @param {boolean} atLineStart - Node starts at beginning of line
  1395. * @param {boolean} inFlow - true if currently in a flow context
  1396. * @param {boolean} inCollection - true if currently in a collection context
  1397. * @param {number} indent - Current level of indentation
  1398. * @param {number} lineStart - Start of the current line
  1399. * @param {Node} parent - The parent of the node
  1400. * @param {string} src - Source of the YAML document
  1401. */
  1402. var ParseContext = /*#__PURE__*/function () {
  1403. _createClass(ParseContext, null, [{
  1404. key: "parseType",
  1405. value: function parseType(src, offset, inFlow) {
  1406. switch (src[offset]) {
  1407. case '*':
  1408. return Type.ALIAS;
  1409. case '>':
  1410. return Type.BLOCK_FOLDED;
  1411. case '|':
  1412. return Type.BLOCK_LITERAL;
  1413. case '{':
  1414. return Type.FLOW_MAP;
  1415. case '[':
  1416. return Type.FLOW_SEQ;
  1417. case '?':
  1418. return !inFlow && Node.atBlank(src, offset + 1, true) ? Type.MAP_KEY : Type.PLAIN;
  1419. case ':':
  1420. return !inFlow && Node.atBlank(src, offset + 1, true) ? Type.MAP_VALUE : Type.PLAIN;
  1421. case '-':
  1422. return !inFlow && Node.atBlank(src, offset + 1, true) ? Type.SEQ_ITEM : Type.PLAIN;
  1423. case '"':
  1424. return Type.QUOTE_DOUBLE;
  1425. case "'":
  1426. return Type.QUOTE_SINGLE;
  1427. default:
  1428. return Type.PLAIN;
  1429. }
  1430. }
  1431. }]);
  1432. function ParseContext() {
  1433. var _this = this;
  1434. var orig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  1435. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  1436. atLineStart = _ref.atLineStart,
  1437. inCollection = _ref.inCollection,
  1438. inFlow = _ref.inFlow,
  1439. indent = _ref.indent,
  1440. lineStart = _ref.lineStart,
  1441. parent = _ref.parent;
  1442. _classCallCheck(this, ParseContext);
  1443. _defineProperty(this, "parseNode", function (overlay, start) {
  1444. if (Node.atDocumentBoundary(_this.src, start)) return null;
  1445. var context = new ParseContext(_this, overlay);
  1446. var _context$parseProps = context.parseProps(start),
  1447. props = _context$parseProps.props,
  1448. type = _context$parseProps.type,
  1449. valueStart = _context$parseProps.valueStart;
  1450. var node = createNewNode(type, props);
  1451. var offset = node.parse(context, valueStart);
  1452. node.range = new Range(start, offset);
  1453. /* istanbul ignore if */
  1454. if (offset <= start) {
  1455. // This should never happen, but if it does, let's make sure to at least
  1456. // step one character forward to avoid a busy loop.
  1457. node.error = new Error("Node#parse consumed no characters");
  1458. node.error.parseEnd = offset;
  1459. node.error.source = node;
  1460. node.range.end = start + 1;
  1461. }
  1462. if (context.nodeStartsCollection(node)) {
  1463. if (!node.error && !context.atLineStart && context.parent.type === Type.DOCUMENT) {
  1464. node.error = new YAMLSyntaxError(node, 'Block collection must not have preceding content here (e.g. directives-end indicator)');
  1465. }
  1466. var collection = new Collection(node);
  1467. offset = collection.parse(new ParseContext(context), offset);
  1468. collection.range = new Range(start, offset);
  1469. return collection;
  1470. }
  1471. return node;
  1472. });
  1473. this.atLineStart = atLineStart != null ? atLineStart : orig.atLineStart || false;
  1474. this.inCollection = inCollection != null ? inCollection : orig.inCollection || false;
  1475. this.inFlow = inFlow != null ? inFlow : orig.inFlow || false;
  1476. this.indent = indent != null ? indent : orig.indent;
  1477. this.lineStart = lineStart != null ? lineStart : orig.lineStart;
  1478. this.parent = parent != null ? parent : orig.parent || {};
  1479. this.root = orig.root;
  1480. this.src = orig.src;
  1481. }
  1482. _createClass(ParseContext, [{
  1483. key: "nodeStartsCollection",
  1484. value: function nodeStartsCollection(node) {
  1485. var inCollection = this.inCollection,
  1486. inFlow = this.inFlow,
  1487. src = this.src;
  1488. if (inCollection || inFlow) return false;
  1489. if (node instanceof CollectionItem) return true; // check for implicit key
  1490. var offset = node.range.end;
  1491. if (src[offset] === '\n' || src[offset - 1] === '\n') return false;
  1492. offset = Node.endOfWhiteSpace(src, offset);
  1493. return src[offset] === ':';
  1494. } // Anchor and tag are before type, which determines the node implementation
  1495. // class; hence this intermediate step.
  1496. }, {
  1497. key: "parseProps",
  1498. value: function parseProps(offset) {
  1499. var inFlow = this.inFlow,
  1500. parent = this.parent,
  1501. src = this.src;
  1502. var props = [];
  1503. var lineHasProps = false;
  1504. offset = this.atLineStart ? Node.endOfIndent(src, offset) : Node.endOfWhiteSpace(src, offset);
  1505. var ch = src[offset];
  1506. while (ch === Char.ANCHOR || ch === Char.COMMENT || ch === Char.TAG || ch === '\n') {
  1507. if (ch === '\n') {
  1508. var lineStart = offset + 1;
  1509. var inEnd = Node.endOfIndent(src, lineStart);
  1510. var indentDiff = inEnd - (lineStart + this.indent);
  1511. var noIndicatorAsIndent = parent.type === Type.SEQ_ITEM && parent.context.atLineStart;
  1512. if (!Node.nextNodeIsIndented(src[inEnd], indentDiff, !noIndicatorAsIndent)) break;
  1513. this.atLineStart = true;
  1514. this.lineStart = lineStart;
  1515. lineHasProps = false;
  1516. offset = inEnd;
  1517. } else if (ch === Char.COMMENT) {
  1518. var end = Node.endOfLine(src, offset + 1);
  1519. props.push(new Range(offset, end));
  1520. offset = end;
  1521. } else {
  1522. var _end = Node.endOfIdentifier(src, offset + 1);
  1523. if (ch === 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))) {
  1524. // Let's presume we're dealing with a YAML 1.0 domain tag here, rather
  1525. // than an empty but 'foo.bar' private-tagged node in a flow collection
  1526. // followed without whitespace by a plain string starting with a year
  1527. // or date divided by something.
  1528. _end = Node.endOfIdentifier(src, _end + 5);
  1529. }
  1530. props.push(new Range(offset, _end));
  1531. lineHasProps = true;
  1532. offset = Node.endOfWhiteSpace(src, _end);
  1533. }
  1534. ch = src[offset];
  1535. } // '- &a : b' has an anchor on an empty node
  1536. if (lineHasProps && ch === ':' && Node.atBlank(src, offset + 1, true)) offset -= 1;
  1537. var type = ParseContext.parseType(src, offset, inFlow);
  1538. return {
  1539. props: props,
  1540. type: type,
  1541. valueStart: offset
  1542. };
  1543. }
  1544. /**
  1545. * Parses a node from the source
  1546. * @param {ParseContext} overlay
  1547. * @param {number} start - Index of first non-whitespace character for the node
  1548. * @returns {?Node} - null if at a document boundary
  1549. */
  1550. }]);
  1551. return ParseContext;
  1552. }();
  1553. // Published as 'yaml/parse-cst'
  1554. function parse(src) {
  1555. var cr = [];
  1556. if (src.indexOf('\r') !== -1) {
  1557. src = src.replace(/\r\n?/g, function (match, offset) {
  1558. if (match.length > 1) cr.push(offset);
  1559. return '\n';
  1560. });
  1561. }
  1562. var documents = [];
  1563. var offset = 0;
  1564. do {
  1565. var doc = new Document();
  1566. var context = new ParseContext({
  1567. src: src
  1568. });
  1569. offset = doc.parse(context, offset);
  1570. documents.push(doc);
  1571. } while (offset < src.length);
  1572. documents.setOrigRanges = function () {
  1573. if (cr.length === 0) return false;
  1574. for (var i = 1; i < cr.length; ++i) {
  1575. cr[i] -= i;
  1576. }
  1577. var crOffset = 0;
  1578. for (var _i = 0; _i < documents.length; ++_i) {
  1579. crOffset = documents[_i].setOrigRanges(cr, crOffset);
  1580. }
  1581. cr.splice(0, cr.length);
  1582. return true;
  1583. };
  1584. documents.toString = function () {
  1585. return documents.join('...\n');
  1586. };
  1587. return documents;
  1588. }
  1589. export { parse };