Parser.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. // Generated by CoffeeScript 1.10.0
  2. var Inline, ParseException, Parser, Pattern, Utils;
  3. Inline = require('./Inline');
  4. Pattern = require('./Pattern');
  5. Utils = require('./Utils');
  6. ParseException = require('./Exception/ParseException');
  7. Parser = (function() {
  8. Parser.prototype.PATTERN_FOLDED_SCALAR_ALL = new Pattern('^(?:(?<type>![^\\|>]*)\\s+)?(?<separator>\\||>)(?<modifiers>\\+|\\-|\\d+|\\+\\d+|\\-\\d+|\\d+\\+|\\d+\\-)?(?<comments> +#.*)?$');
  9. Parser.prototype.PATTERN_FOLDED_SCALAR_END = new Pattern('(?<separator>\\||>)(?<modifiers>\\+|\\-|\\d+|\\+\\d+|\\-\\d+|\\d+\\+|\\d+\\-)?(?<comments> +#.*)?$');
  10. Parser.prototype.PATTERN_SEQUENCE_ITEM = new Pattern('^\\-((?<leadspaces>\\s+)(?<value>.+?))?\\s*$');
  11. Parser.prototype.PATTERN_ANCHOR_VALUE = new Pattern('^&(?<ref>[^ ]+) *(?<value>.*)');
  12. Parser.prototype.PATTERN_COMPACT_NOTATION = new Pattern('^(?<key>' + Inline.REGEX_QUOTED_STRING + '|[^ \'"\\{\\[].*?) *\\:(\\s+(?<value>.+?))?\\s*$');
  13. Parser.prototype.PATTERN_MAPPING_ITEM = new Pattern('^(?<key>' + Inline.REGEX_QUOTED_STRING + '|[^ \'"\\[\\{].*?) *\\:(\\s+(?<value>.+?))?\\s*$');
  14. Parser.prototype.PATTERN_DECIMAL = new Pattern('\\d+');
  15. Parser.prototype.PATTERN_INDENT_SPACES = new Pattern('^ +');
  16. Parser.prototype.PATTERN_TRAILING_LINES = new Pattern('(\n*)$');
  17. Parser.prototype.PATTERN_YAML_HEADER = new Pattern('^\\%YAML[: ][\\d\\.]+.*\n');
  18. Parser.prototype.PATTERN_LEADING_COMMENTS = new Pattern('^(\\#.*?\n)+');
  19. Parser.prototype.PATTERN_DOCUMENT_MARKER_START = new Pattern('^\\-\\-\\-.*?\n');
  20. Parser.prototype.PATTERN_DOCUMENT_MARKER_END = new Pattern('^\\.\\.\\.\\s*$');
  21. Parser.prototype.PATTERN_FOLDED_SCALAR_BY_INDENTATION = {};
  22. Parser.prototype.CONTEXT_NONE = 0;
  23. Parser.prototype.CONTEXT_SEQUENCE = 1;
  24. Parser.prototype.CONTEXT_MAPPING = 2;
  25. function Parser(offset) {
  26. this.offset = offset != null ? offset : 0;
  27. this.lines = [];
  28. this.currentLineNb = -1;
  29. this.currentLine = '';
  30. this.refs = {};
  31. }
  32. Parser.prototype.parse = function(value, exceptionOnInvalidType, objectDecoder) {
  33. var alias, allowOverwrite, block, c, context, data, e, error, error1, error2, first, i, indent, isRef, j, k, key, l, lastKey, len, len1, len2, len3, lineCount, m, matches, mergeNode, n, name, parsed, parsedItem, parser, ref, ref1, ref2, refName, refValue, val, values;
  34. if (exceptionOnInvalidType == null) {
  35. exceptionOnInvalidType = false;
  36. }
  37. if (objectDecoder == null) {
  38. objectDecoder = null;
  39. }
  40. this.currentLineNb = -1;
  41. this.currentLine = '';
  42. this.lines = this.cleanup(value).split("\n");
  43. data = null;
  44. context = this.CONTEXT_NONE;
  45. allowOverwrite = false;
  46. while (this.moveToNextLine()) {
  47. if (this.isCurrentLineEmpty()) {
  48. continue;
  49. }
  50. if ("\t" === this.currentLine[0]) {
  51. throw new ParseException('A YAML file cannot contain tabs as indentation.', this.getRealCurrentLineNb() + 1, this.currentLine);
  52. }
  53. isRef = mergeNode = false;
  54. if (values = this.PATTERN_SEQUENCE_ITEM.exec(this.currentLine)) {
  55. if (this.CONTEXT_MAPPING === context) {
  56. throw new ParseException('You cannot define a sequence item when in a mapping');
  57. }
  58. context = this.CONTEXT_SEQUENCE;
  59. if (data == null) {
  60. data = [];
  61. }
  62. if ((values.value != null) && (matches = this.PATTERN_ANCHOR_VALUE.exec(values.value))) {
  63. isRef = matches.ref;
  64. values.value = matches.value;
  65. }
  66. if (!(values.value != null) || '' === Utils.trim(values.value, ' ') || Utils.ltrim(values.value, ' ').indexOf('#') === 0) {
  67. if (this.currentLineNb < this.lines.length - 1 && !this.isNextLineUnIndentedCollection()) {
  68. c = this.getRealCurrentLineNb() + 1;
  69. parser = new Parser(c);
  70. parser.refs = this.refs;
  71. data.push(parser.parse(this.getNextEmbedBlock(null, true), exceptionOnInvalidType, objectDecoder));
  72. } else {
  73. data.push(null);
  74. }
  75. } else {
  76. if (((ref = values.leadspaces) != null ? ref.length : void 0) && (matches = this.PATTERN_COMPACT_NOTATION.exec(values.value))) {
  77. c = this.getRealCurrentLineNb();
  78. parser = new Parser(c);
  79. parser.refs = this.refs;
  80. block = values.value;
  81. indent = this.getCurrentLineIndentation();
  82. if (this.isNextLineIndented(false)) {
  83. block += "\n" + this.getNextEmbedBlock(indent + values.leadspaces.length + 1, true);
  84. }
  85. data.push(parser.parse(block, exceptionOnInvalidType, objectDecoder));
  86. } else {
  87. data.push(this.parseValue(values.value, exceptionOnInvalidType, objectDecoder));
  88. }
  89. }
  90. } else if ((values = this.PATTERN_MAPPING_ITEM.exec(this.currentLine)) && values.key.indexOf(' #') === -1) {
  91. if (this.CONTEXT_SEQUENCE === context) {
  92. throw new ParseException('You cannot define a mapping item when in a sequence');
  93. }
  94. context = this.CONTEXT_MAPPING;
  95. if (data == null) {
  96. data = {};
  97. }
  98. Inline.configure(exceptionOnInvalidType, objectDecoder);
  99. try {
  100. key = Inline.parseScalar(values.key);
  101. } catch (error) {
  102. e = error;
  103. e.parsedLine = this.getRealCurrentLineNb() + 1;
  104. e.snippet = this.currentLine;
  105. throw e;
  106. }
  107. if ('<<' === key) {
  108. mergeNode = true;
  109. allowOverwrite = true;
  110. if (((ref1 = values.value) != null ? ref1.indexOf('*') : void 0) === 0) {
  111. refName = values.value.slice(1);
  112. if (this.refs[refName] == null) {
  113. throw new ParseException('Reference "' + refName + '" does not exist.', this.getRealCurrentLineNb() + 1, this.currentLine);
  114. }
  115. refValue = this.refs[refName];
  116. if (typeof refValue !== 'object') {
  117. throw new ParseException('YAML merge keys used with a scalar value instead of an object.', this.getRealCurrentLineNb() + 1, this.currentLine);
  118. }
  119. if (refValue instanceof Array) {
  120. for (i = j = 0, len = refValue.length; j < len; i = ++j) {
  121. value = refValue[i];
  122. if (data[name = String(i)] == null) {
  123. data[name] = value;
  124. }
  125. }
  126. } else {
  127. for (key in refValue) {
  128. value = refValue[key];
  129. if (data[key] == null) {
  130. data[key] = value;
  131. }
  132. }
  133. }
  134. } else {
  135. if ((values.value != null) && values.value !== '') {
  136. value = values.value;
  137. } else {
  138. value = this.getNextEmbedBlock();
  139. }
  140. c = this.getRealCurrentLineNb() + 1;
  141. parser = new Parser(c);
  142. parser.refs = this.refs;
  143. parsed = parser.parse(value, exceptionOnInvalidType);
  144. if (typeof parsed !== 'object') {
  145. throw new ParseException('YAML merge keys used with a scalar value instead of an object.', this.getRealCurrentLineNb() + 1, this.currentLine);
  146. }
  147. if (parsed instanceof Array) {
  148. for (l = 0, len1 = parsed.length; l < len1; l++) {
  149. parsedItem = parsed[l];
  150. if (typeof parsedItem !== 'object') {
  151. throw new ParseException('Merge items must be objects.', this.getRealCurrentLineNb() + 1, parsedItem);
  152. }
  153. if (parsedItem instanceof Array) {
  154. for (i = m = 0, len2 = parsedItem.length; m < len2; i = ++m) {
  155. value = parsedItem[i];
  156. k = String(i);
  157. if (!data.hasOwnProperty(k)) {
  158. data[k] = value;
  159. }
  160. }
  161. } else {
  162. for (key in parsedItem) {
  163. value = parsedItem[key];
  164. if (!data.hasOwnProperty(key)) {
  165. data[key] = value;
  166. }
  167. }
  168. }
  169. }
  170. } else {
  171. for (key in parsed) {
  172. value = parsed[key];
  173. if (!data.hasOwnProperty(key)) {
  174. data[key] = value;
  175. }
  176. }
  177. }
  178. }
  179. } else if ((values.value != null) && (matches = this.PATTERN_ANCHOR_VALUE.exec(values.value))) {
  180. isRef = matches.ref;
  181. values.value = matches.value;
  182. }
  183. if (mergeNode) {
  184. } else if (!(values.value != null) || '' === Utils.trim(values.value, ' ') || Utils.ltrim(values.value, ' ').indexOf('#') === 0) {
  185. if (!(this.isNextLineIndented()) && !(this.isNextLineUnIndentedCollection())) {
  186. if (allowOverwrite || data[key] === void 0) {
  187. data[key] = null;
  188. }
  189. } else {
  190. c = this.getRealCurrentLineNb() + 1;
  191. parser = new Parser(c);
  192. parser.refs = this.refs;
  193. val = parser.parse(this.getNextEmbedBlock(), exceptionOnInvalidType, objectDecoder);
  194. if (allowOverwrite || data[key] === void 0) {
  195. data[key] = val;
  196. }
  197. }
  198. } else {
  199. val = this.parseValue(values.value, exceptionOnInvalidType, objectDecoder);
  200. if (allowOverwrite || data[key] === void 0) {
  201. data[key] = val;
  202. }
  203. }
  204. } else {
  205. lineCount = this.lines.length;
  206. if (1 === lineCount || (2 === lineCount && Utils.isEmpty(this.lines[1]))) {
  207. try {
  208. value = Inline.parse(this.lines[0], exceptionOnInvalidType, objectDecoder);
  209. } catch (error1) {
  210. e = error1;
  211. e.parsedLine = this.getRealCurrentLineNb() + 1;
  212. e.snippet = this.currentLine;
  213. throw e;
  214. }
  215. if (typeof value === 'object') {
  216. if (value instanceof Array) {
  217. first = value[0];
  218. } else {
  219. for (key in value) {
  220. first = value[key];
  221. break;
  222. }
  223. }
  224. if (typeof first === 'string' && first.indexOf('*') === 0) {
  225. data = [];
  226. for (n = 0, len3 = value.length; n < len3; n++) {
  227. alias = value[n];
  228. data.push(this.refs[alias.slice(1)]);
  229. }
  230. value = data;
  231. }
  232. }
  233. return value;
  234. } else if ((ref2 = Utils.ltrim(value).charAt(0)) === '[' || ref2 === '{') {
  235. try {
  236. return Inline.parse(value, exceptionOnInvalidType, objectDecoder);
  237. } catch (error2) {
  238. e = error2;
  239. e.parsedLine = this.getRealCurrentLineNb() + 1;
  240. e.snippet = this.currentLine;
  241. throw e;
  242. }
  243. }
  244. throw new ParseException('Unable to parse.', this.getRealCurrentLineNb() + 1, this.currentLine);
  245. }
  246. if (isRef) {
  247. if (data instanceof Array) {
  248. this.refs[isRef] = data[data.length - 1];
  249. } else {
  250. lastKey = null;
  251. for (key in data) {
  252. lastKey = key;
  253. }
  254. this.refs[isRef] = data[lastKey];
  255. }
  256. }
  257. }
  258. if (Utils.isEmpty(data)) {
  259. return null;
  260. } else {
  261. return data;
  262. }
  263. };
  264. Parser.prototype.getRealCurrentLineNb = function() {
  265. return this.currentLineNb + this.offset;
  266. };
  267. Parser.prototype.getCurrentLineIndentation = function() {
  268. return this.currentLine.length - Utils.ltrim(this.currentLine, ' ').length;
  269. };
  270. Parser.prototype.getNextEmbedBlock = function(indentation, includeUnindentedCollection) {
  271. var data, indent, isItUnindentedCollection, newIndent, removeComments, removeCommentsPattern, unindentedEmbedBlock;
  272. if (indentation == null) {
  273. indentation = null;
  274. }
  275. if (includeUnindentedCollection == null) {
  276. includeUnindentedCollection = false;
  277. }
  278. this.moveToNextLine();
  279. if (indentation == null) {
  280. newIndent = this.getCurrentLineIndentation();
  281. unindentedEmbedBlock = this.isStringUnIndentedCollectionItem(this.currentLine);
  282. if (!(this.isCurrentLineEmpty()) && 0 === newIndent && !unindentedEmbedBlock) {
  283. throw new ParseException('Indentation problem.', this.getRealCurrentLineNb() + 1, this.currentLine);
  284. }
  285. } else {
  286. newIndent = indentation;
  287. }
  288. data = [this.currentLine.slice(newIndent)];
  289. if (!includeUnindentedCollection) {
  290. isItUnindentedCollection = this.isStringUnIndentedCollectionItem(this.currentLine);
  291. }
  292. removeCommentsPattern = this.PATTERN_FOLDED_SCALAR_END;
  293. removeComments = !removeCommentsPattern.test(this.currentLine);
  294. while (this.moveToNextLine()) {
  295. indent = this.getCurrentLineIndentation();
  296. if (indent === newIndent) {
  297. removeComments = !removeCommentsPattern.test(this.currentLine);
  298. }
  299. if (isItUnindentedCollection && !this.isStringUnIndentedCollectionItem(this.currentLine) && indent === newIndent) {
  300. this.moveToPreviousLine();
  301. break;
  302. }
  303. if (this.isCurrentLineBlank()) {
  304. data.push(this.currentLine.slice(newIndent));
  305. continue;
  306. }
  307. if (removeComments && this.isCurrentLineComment()) {
  308. if (indent === newIndent) {
  309. continue;
  310. }
  311. }
  312. if (indent >= newIndent) {
  313. data.push(this.currentLine.slice(newIndent));
  314. } else if (Utils.ltrim(this.currentLine).charAt(0) === '#') {
  315. } else if (0 === indent) {
  316. this.moveToPreviousLine();
  317. break;
  318. } else {
  319. throw new ParseException('Indentation problem.', this.getRealCurrentLineNb() + 1, this.currentLine);
  320. }
  321. }
  322. return data.join("\n");
  323. };
  324. Parser.prototype.moveToNextLine = function() {
  325. if (this.currentLineNb >= this.lines.length - 1) {
  326. return false;
  327. }
  328. this.currentLine = this.lines[++this.currentLineNb];
  329. return true;
  330. };
  331. Parser.prototype.moveToPreviousLine = function() {
  332. this.currentLine = this.lines[--this.currentLineNb];
  333. };
  334. Parser.prototype.parseValue = function(value, exceptionOnInvalidType, objectDecoder) {
  335. var e, error, error1, foldedIndent, matches, modifiers, pos, ref, ref1, val;
  336. if (0 === value.indexOf('*')) {
  337. pos = value.indexOf('#');
  338. if (pos !== -1) {
  339. value = value.substr(1, pos - 2);
  340. } else {
  341. value = value.slice(1);
  342. }
  343. if (this.refs[value] === void 0) {
  344. throw new ParseException('Reference "' + value + '" does not exist.', this.currentLine);
  345. }
  346. return this.refs[value];
  347. }
  348. if (matches = this.PATTERN_FOLDED_SCALAR_ALL.exec(value)) {
  349. modifiers = (ref = matches.modifiers) != null ? ref : '';
  350. foldedIndent = Math.abs(parseInt(modifiers));
  351. if (isNaN(foldedIndent)) {
  352. foldedIndent = 0;
  353. }
  354. val = this.parseFoldedScalar(matches.separator, this.PATTERN_DECIMAL.replace(modifiers, ''), foldedIndent);
  355. if (matches.type != null) {
  356. Inline.configure(exceptionOnInvalidType, objectDecoder);
  357. return Inline.parseScalar(matches.type + ' ' + val);
  358. } else {
  359. return val;
  360. }
  361. }
  362. try {
  363. return Inline.parse(value, exceptionOnInvalidType, objectDecoder);
  364. } catch (error) {
  365. e = error;
  366. if (((ref1 = value.charAt(0)) === '[' || ref1 === '{') && e instanceof ParseException && this.isNextLineIndented()) {
  367. value += "\n" + this.getNextEmbedBlock();
  368. try {
  369. return Inline.parse(value, exceptionOnInvalidType, objectDecoder);
  370. } catch (error1) {
  371. e = error1;
  372. e.parsedLine = this.getRealCurrentLineNb() + 1;
  373. e.snippet = this.currentLine;
  374. throw e;
  375. }
  376. } else {
  377. e.parsedLine = this.getRealCurrentLineNb() + 1;
  378. e.snippet = this.currentLine;
  379. throw e;
  380. }
  381. }
  382. };
  383. Parser.prototype.parseFoldedScalar = function(separator, indicator, indentation) {
  384. var isCurrentLineBlank, j, len, line, matches, newText, notEOF, pattern, ref, text;
  385. if (indicator == null) {
  386. indicator = '';
  387. }
  388. if (indentation == null) {
  389. indentation = 0;
  390. }
  391. notEOF = this.moveToNextLine();
  392. if (!notEOF) {
  393. return '';
  394. }
  395. isCurrentLineBlank = this.isCurrentLineBlank();
  396. text = '';
  397. while (notEOF && isCurrentLineBlank) {
  398. if (notEOF = this.moveToNextLine()) {
  399. text += "\n";
  400. isCurrentLineBlank = this.isCurrentLineBlank();
  401. }
  402. }
  403. if (0 === indentation) {
  404. if (matches = this.PATTERN_INDENT_SPACES.exec(this.currentLine)) {
  405. indentation = matches[0].length;
  406. }
  407. }
  408. if (indentation > 0) {
  409. pattern = this.PATTERN_FOLDED_SCALAR_BY_INDENTATION[indentation];
  410. if (pattern == null) {
  411. pattern = new Pattern('^ {' + indentation + '}(.*)$');
  412. Parser.prototype.PATTERN_FOLDED_SCALAR_BY_INDENTATION[indentation] = pattern;
  413. }
  414. while (notEOF && (isCurrentLineBlank || (matches = pattern.exec(this.currentLine)))) {
  415. if (isCurrentLineBlank) {
  416. text += this.currentLine.slice(indentation);
  417. } else {
  418. text += matches[1];
  419. }
  420. if (notEOF = this.moveToNextLine()) {
  421. text += "\n";
  422. isCurrentLineBlank = this.isCurrentLineBlank();
  423. }
  424. }
  425. } else if (notEOF) {
  426. text += "\n";
  427. }
  428. if (notEOF) {
  429. this.moveToPreviousLine();
  430. }
  431. if ('>' === separator) {
  432. newText = '';
  433. ref = text.split("\n");
  434. for (j = 0, len = ref.length; j < len; j++) {
  435. line = ref[j];
  436. if (line.length === 0 || line.charAt(0) === ' ') {
  437. newText = Utils.rtrim(newText, ' ') + line + "\n";
  438. } else {
  439. newText += line + ' ';
  440. }
  441. }
  442. text = newText;
  443. }
  444. if ('+' !== indicator) {
  445. text = Utils.rtrim(text);
  446. }
  447. if ('' === indicator) {
  448. text = this.PATTERN_TRAILING_LINES.replace(text, "\n");
  449. } else if ('-' === indicator) {
  450. text = this.PATTERN_TRAILING_LINES.replace(text, '');
  451. }
  452. return text;
  453. };
  454. Parser.prototype.isNextLineIndented = function(ignoreComments) {
  455. var EOF, currentIndentation, ret;
  456. if (ignoreComments == null) {
  457. ignoreComments = true;
  458. }
  459. currentIndentation = this.getCurrentLineIndentation();
  460. EOF = !this.moveToNextLine();
  461. if (ignoreComments) {
  462. while (!EOF && this.isCurrentLineEmpty()) {
  463. EOF = !this.moveToNextLine();
  464. }
  465. } else {
  466. while (!EOF && this.isCurrentLineBlank()) {
  467. EOF = !this.moveToNextLine();
  468. }
  469. }
  470. if (EOF) {
  471. return false;
  472. }
  473. ret = false;
  474. if (this.getCurrentLineIndentation() > currentIndentation) {
  475. ret = true;
  476. }
  477. this.moveToPreviousLine();
  478. return ret;
  479. };
  480. Parser.prototype.isCurrentLineEmpty = function() {
  481. var trimmedLine;
  482. trimmedLine = Utils.trim(this.currentLine, ' ');
  483. return trimmedLine.length === 0 || trimmedLine.charAt(0) === '#';
  484. };
  485. Parser.prototype.isCurrentLineBlank = function() {
  486. return '' === Utils.trim(this.currentLine, ' ');
  487. };
  488. Parser.prototype.isCurrentLineComment = function() {
  489. var ltrimmedLine;
  490. ltrimmedLine = Utils.ltrim(this.currentLine, ' ');
  491. return ltrimmedLine.charAt(0) === '#';
  492. };
  493. Parser.prototype.cleanup = function(value) {
  494. var count, i, indent, j, l, len, len1, line, lines, ref, ref1, ref2, smallestIndent, trimmedValue;
  495. if (value.indexOf("\r") !== -1) {
  496. value = value.split("\r\n").join("\n").split("\r").join("\n");
  497. }
  498. count = 0;
  499. ref = this.PATTERN_YAML_HEADER.replaceAll(value, ''), value = ref[0], count = ref[1];
  500. this.offset += count;
  501. ref1 = this.PATTERN_LEADING_COMMENTS.replaceAll(value, '', 1), trimmedValue = ref1[0], count = ref1[1];
  502. if (count === 1) {
  503. this.offset += Utils.subStrCount(value, "\n") - Utils.subStrCount(trimmedValue, "\n");
  504. value = trimmedValue;
  505. }
  506. ref2 = this.PATTERN_DOCUMENT_MARKER_START.replaceAll(value, '', 1), trimmedValue = ref2[0], count = ref2[1];
  507. if (count === 1) {
  508. this.offset += Utils.subStrCount(value, "\n") - Utils.subStrCount(trimmedValue, "\n");
  509. value = trimmedValue;
  510. value = this.PATTERN_DOCUMENT_MARKER_END.replace(value, '');
  511. }
  512. lines = value.split("\n");
  513. smallestIndent = -1;
  514. for (j = 0, len = lines.length; j < len; j++) {
  515. line = lines[j];
  516. if (Utils.trim(line, ' ').length === 0) {
  517. continue;
  518. }
  519. indent = line.length - Utils.ltrim(line).length;
  520. if (smallestIndent === -1 || indent < smallestIndent) {
  521. smallestIndent = indent;
  522. }
  523. }
  524. if (smallestIndent > 0) {
  525. for (i = l = 0, len1 = lines.length; l < len1; i = ++l) {
  526. line = lines[i];
  527. lines[i] = line.slice(smallestIndent);
  528. }
  529. value = lines.join("\n");
  530. }
  531. return value;
  532. };
  533. Parser.prototype.isNextLineUnIndentedCollection = function(currentIndentation) {
  534. var notEOF, ret;
  535. if (currentIndentation == null) {
  536. currentIndentation = null;
  537. }
  538. if (currentIndentation == null) {
  539. currentIndentation = this.getCurrentLineIndentation();
  540. }
  541. notEOF = this.moveToNextLine();
  542. while (notEOF && this.isCurrentLineEmpty()) {
  543. notEOF = this.moveToNextLine();
  544. }
  545. if (false === notEOF) {
  546. return false;
  547. }
  548. ret = false;
  549. if (this.getCurrentLineIndentation() === currentIndentation && this.isStringUnIndentedCollectionItem(this.currentLine)) {
  550. ret = true;
  551. }
  552. this.moveToPreviousLine();
  553. return ret;
  554. };
  555. Parser.prototype.isStringUnIndentedCollectionItem = function() {
  556. return this.currentLine === '-' || this.currentLine.slice(0, 2) === '- ';
  557. };
  558. return Parser;
  559. })();
  560. module.exports = Parser;