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