loader.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  1. 'use strict';
  2. /*eslint-disable max-len,no-use-before-define*/
  3. var common = require('./common');
  4. var YAMLException = require('./exception');
  5. var makeSnippet = require('./snippet');
  6. var DEFAULT_SCHEMA = require('./schema/default');
  7. var _hasOwnProperty = Object.prototype.hasOwnProperty;
  8. var CONTEXT_FLOW_IN = 1;
  9. var CONTEXT_FLOW_OUT = 2;
  10. var CONTEXT_BLOCK_IN = 3;
  11. var CONTEXT_BLOCK_OUT = 4;
  12. var CHOMPING_CLIP = 1;
  13. var CHOMPING_STRIP = 2;
  14. var CHOMPING_KEEP = 3;
  15. var PATTERN_NON_PRINTABLE = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;
  16. var PATTERN_NON_ASCII_LINE_BREAKS = /[\x85\u2028\u2029]/;
  17. var PATTERN_FLOW_INDICATORS = /[,\[\]\{\}]/;
  18. var PATTERN_TAG_HANDLE = /^(?:!|!!|![a-z\-]+!)$/i;
  19. var PATTERN_TAG_URI = /^(?:!|[^,\[\]\{\}])(?:%[0-9a-f]{2}|[0-9a-z\-#;\/\?:@&=\+\$,_\.!~\*'\(\)\[\]])*$/i;
  20. function _class(obj) { return Object.prototype.toString.call(obj); }
  21. function is_EOL(c) {
  22. return (c === 0x0A/* LF */) || (c === 0x0D/* CR */);
  23. }
  24. function is_WHITE_SPACE(c) {
  25. return (c === 0x09/* Tab */) || (c === 0x20/* Space */);
  26. }
  27. function is_WS_OR_EOL(c) {
  28. return (c === 0x09/* Tab */) ||
  29. (c === 0x20/* Space */) ||
  30. (c === 0x0A/* LF */) ||
  31. (c === 0x0D/* CR */);
  32. }
  33. function is_FLOW_INDICATOR(c) {
  34. return c === 0x2C/* , */ ||
  35. c === 0x5B/* [ */ ||
  36. c === 0x5D/* ] */ ||
  37. c === 0x7B/* { */ ||
  38. c === 0x7D/* } */;
  39. }
  40. function fromHexCode(c) {
  41. var lc;
  42. if ((0x30/* 0 */ <= c) && (c <= 0x39/* 9 */)) {
  43. return c - 0x30;
  44. }
  45. /*eslint-disable no-bitwise*/
  46. lc = c | 0x20;
  47. if ((0x61/* a */ <= lc) && (lc <= 0x66/* f */)) {
  48. return lc - 0x61 + 10;
  49. }
  50. return -1;
  51. }
  52. function escapedHexLen(c) {
  53. if (c === 0x78/* x */) { return 2; }
  54. if (c === 0x75/* u */) { return 4; }
  55. if (c === 0x55/* U */) { return 8; }
  56. return 0;
  57. }
  58. function fromDecimalCode(c) {
  59. if ((0x30/* 0 */ <= c) && (c <= 0x39/* 9 */)) {
  60. return c - 0x30;
  61. }
  62. return -1;
  63. }
  64. function simpleEscapeSequence(c) {
  65. /* eslint-disable indent */
  66. return (c === 0x30/* 0 */) ? '\x00' :
  67. (c === 0x61/* a */) ? '\x07' :
  68. (c === 0x62/* b */) ? '\x08' :
  69. (c === 0x74/* t */) ? '\x09' :
  70. (c === 0x09/* Tab */) ? '\x09' :
  71. (c === 0x6E/* n */) ? '\x0A' :
  72. (c === 0x76/* v */) ? '\x0B' :
  73. (c === 0x66/* f */) ? '\x0C' :
  74. (c === 0x72/* r */) ? '\x0D' :
  75. (c === 0x65/* e */) ? '\x1B' :
  76. (c === 0x20/* Space */) ? ' ' :
  77. (c === 0x22/* " */) ? '\x22' :
  78. (c === 0x2F/* / */) ? '/' :
  79. (c === 0x5C/* \ */) ? '\x5C' :
  80. (c === 0x4E/* N */) ? '\x85' :
  81. (c === 0x5F/* _ */) ? '\xA0' :
  82. (c === 0x4C/* L */) ? '\u2028' :
  83. (c === 0x50/* P */) ? '\u2029' : '';
  84. }
  85. function charFromCodepoint(c) {
  86. if (c <= 0xFFFF) {
  87. return String.fromCharCode(c);
  88. }
  89. // Encode UTF-16 surrogate pair
  90. // https://en.wikipedia.org/wiki/UTF-16#Code_points_U.2B010000_to_U.2B10FFFF
  91. return String.fromCharCode(
  92. ((c - 0x010000) >> 10) + 0xD800,
  93. ((c - 0x010000) & 0x03FF) + 0xDC00
  94. );
  95. }
  96. // set a property of a literal object, while protecting against prototype pollution,
  97. // see https://github.com/nodeca/js-yaml/issues/164 for more details
  98. function setProperty(object, key, value) {
  99. // used for this specific key only because Object.defineProperty is slow
  100. if (key === '__proto__') {
  101. Object.defineProperty(object, key, {
  102. configurable: true,
  103. enumerable: true,
  104. writable: true,
  105. value: value
  106. });
  107. } else {
  108. object[key] = value;
  109. }
  110. }
  111. var simpleEscapeCheck = new Array(256); // integer, for fast access
  112. var simpleEscapeMap = new Array(256);
  113. for (var i = 0; i < 256; i++) {
  114. simpleEscapeCheck[i] = simpleEscapeSequence(i) ? 1 : 0;
  115. simpleEscapeMap[i] = simpleEscapeSequence(i);
  116. }
  117. function State(input, options) {
  118. this.input = input;
  119. this.filename = options['filename'] || null;
  120. this.schema = options['schema'] || DEFAULT_SCHEMA;
  121. this.onWarning = options['onWarning'] || null;
  122. // (Hidden) Remove? makes the loader to expect YAML 1.1 documents
  123. // if such documents have no explicit %YAML directive
  124. this.legacy = options['legacy'] || false;
  125. this.json = options['json'] || false;
  126. this.listener = options['listener'] || null;
  127. this.implicitTypes = this.schema.compiledImplicit;
  128. this.typeMap = this.schema.compiledTypeMap;
  129. this.length = input.length;
  130. this.position = 0;
  131. this.line = 0;
  132. this.lineStart = 0;
  133. this.lineIndent = 0;
  134. // position of first leading tab in the current line,
  135. // used to make sure there are no tabs in the indentation
  136. this.firstTabInLine = -1;
  137. this.documents = [];
  138. /*
  139. this.version;
  140. this.checkLineBreaks;
  141. this.tagMap;
  142. this.anchorMap;
  143. this.tag;
  144. this.anchor;
  145. this.kind;
  146. this.result;*/
  147. }
  148. function generateError(state, message) {
  149. var mark = {
  150. name: state.filename,
  151. buffer: state.input.slice(0, -1), // omit trailing \0
  152. position: state.position,
  153. line: state.line,
  154. column: state.position - state.lineStart
  155. };
  156. mark.snippet = makeSnippet(mark);
  157. return new YAMLException(message, mark);
  158. }
  159. function throwError(state, message) {
  160. throw generateError(state, message);
  161. }
  162. function throwWarning(state, message) {
  163. if (state.onWarning) {
  164. state.onWarning.call(null, generateError(state, message));
  165. }
  166. }
  167. var directiveHandlers = {
  168. YAML: function handleYamlDirective(state, name, args) {
  169. var match, major, minor;
  170. if (state.version !== null) {
  171. throwError(state, 'duplication of %YAML directive');
  172. }
  173. if (args.length !== 1) {
  174. throwError(state, 'YAML directive accepts exactly one argument');
  175. }
  176. match = /^([0-9]+)\.([0-9]+)$/.exec(args[0]);
  177. if (match === null) {
  178. throwError(state, 'ill-formed argument of the YAML directive');
  179. }
  180. major = parseInt(match[1], 10);
  181. minor = parseInt(match[2], 10);
  182. if (major !== 1) {
  183. throwError(state, 'unacceptable YAML version of the document');
  184. }
  185. state.version = args[0];
  186. state.checkLineBreaks = (minor < 2);
  187. if (minor !== 1 && minor !== 2) {
  188. throwWarning(state, 'unsupported YAML version of the document');
  189. }
  190. },
  191. TAG: function handleTagDirective(state, name, args) {
  192. var handle, prefix;
  193. if (args.length !== 2) {
  194. throwError(state, 'TAG directive accepts exactly two arguments');
  195. }
  196. handle = args[0];
  197. prefix = args[1];
  198. if (!PATTERN_TAG_HANDLE.test(handle)) {
  199. throwError(state, 'ill-formed tag handle (first argument) of the TAG directive');
  200. }
  201. if (_hasOwnProperty.call(state.tagMap, handle)) {
  202. throwError(state, 'there is a previously declared suffix for "' + handle + '" tag handle');
  203. }
  204. if (!PATTERN_TAG_URI.test(prefix)) {
  205. throwError(state, 'ill-formed tag prefix (second argument) of the TAG directive');
  206. }
  207. try {
  208. prefix = decodeURIComponent(prefix);
  209. } catch (err) {
  210. throwError(state, 'tag prefix is malformed: ' + prefix);
  211. }
  212. state.tagMap[handle] = prefix;
  213. }
  214. };
  215. function captureSegment(state, start, end, checkJson) {
  216. var _position, _length, _character, _result;
  217. if (start < end) {
  218. _result = state.input.slice(start, end);
  219. if (checkJson) {
  220. for (_position = 0, _length = _result.length; _position < _length; _position += 1) {
  221. _character = _result.charCodeAt(_position);
  222. if (!(_character === 0x09 ||
  223. (0x20 <= _character && _character <= 0x10FFFF))) {
  224. throwError(state, 'expected valid JSON character');
  225. }
  226. }
  227. } else if (PATTERN_NON_PRINTABLE.test(_result)) {
  228. throwError(state, 'the stream contains non-printable characters');
  229. }
  230. state.result += _result;
  231. }
  232. }
  233. function mergeMappings(state, destination, source, overridableKeys) {
  234. var sourceKeys, key, index, quantity;
  235. if (!common.isObject(source)) {
  236. throwError(state, 'cannot merge mappings; the provided source object is unacceptable');
  237. }
  238. sourceKeys = Object.keys(source);
  239. for (index = 0, quantity = sourceKeys.length; index < quantity; index += 1) {
  240. key = sourceKeys[index];
  241. if (!_hasOwnProperty.call(destination, key)) {
  242. setProperty(destination, key, source[key]);
  243. overridableKeys[key] = true;
  244. }
  245. }
  246. }
  247. function storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valueNode,
  248. startLine, startLineStart, startPos) {
  249. var index, quantity;
  250. // The output is a plain object here, so keys can only be strings.
  251. // We need to convert keyNode to a string, but doing so can hang the process
  252. // (deeply nested arrays that explode exponentially using aliases).
  253. if (Array.isArray(keyNode)) {
  254. keyNode = Array.prototype.slice.call(keyNode);
  255. for (index = 0, quantity = keyNode.length; index < quantity; index += 1) {
  256. if (Array.isArray(keyNode[index])) {
  257. throwError(state, 'nested arrays are not supported inside keys');
  258. }
  259. if (typeof keyNode === 'object' && _class(keyNode[index]) === '[object Object]') {
  260. keyNode[index] = '[object Object]';
  261. }
  262. }
  263. }
  264. // Avoid code execution in load() via toString property
  265. // (still use its own toString for arrays, timestamps,
  266. // and whatever user schema extensions happen to have @@toStringTag)
  267. if (typeof keyNode === 'object' && _class(keyNode) === '[object Object]') {
  268. keyNode = '[object Object]';
  269. }
  270. keyNode = String(keyNode);
  271. if (_result === null) {
  272. _result = {};
  273. }
  274. if (keyTag === 'tag:yaml.org,2002:merge') {
  275. if (Array.isArray(valueNode)) {
  276. for (index = 0, quantity = valueNode.length; index < quantity; index += 1) {
  277. mergeMappings(state, _result, valueNode[index], overridableKeys);
  278. }
  279. } else {
  280. mergeMappings(state, _result, valueNode, overridableKeys);
  281. }
  282. } else {
  283. if (!state.json &&
  284. !_hasOwnProperty.call(overridableKeys, keyNode) &&
  285. _hasOwnProperty.call(_result, keyNode)) {
  286. state.line = startLine || state.line;
  287. state.lineStart = startLineStart || state.lineStart;
  288. state.position = startPos || state.position;
  289. throwError(state, 'duplicated mapping key');
  290. }
  291. setProperty(_result, keyNode, valueNode);
  292. delete overridableKeys[keyNode];
  293. }
  294. return _result;
  295. }
  296. function readLineBreak(state) {
  297. var ch;
  298. ch = state.input.charCodeAt(state.position);
  299. if (ch === 0x0A/* LF */) {
  300. state.position++;
  301. } else if (ch === 0x0D/* CR */) {
  302. state.position++;
  303. if (state.input.charCodeAt(state.position) === 0x0A/* LF */) {
  304. state.position++;
  305. }
  306. } else {
  307. throwError(state, 'a line break is expected');
  308. }
  309. state.line += 1;
  310. state.lineStart = state.position;
  311. state.firstTabInLine = -1;
  312. }
  313. function skipSeparationSpace(state, allowComments, checkIndent) {
  314. var lineBreaks = 0,
  315. ch = state.input.charCodeAt(state.position);
  316. while (ch !== 0) {
  317. while (is_WHITE_SPACE(ch)) {
  318. if (ch === 0x09/* Tab */ && state.firstTabInLine === -1) {
  319. state.firstTabInLine = state.position;
  320. }
  321. ch = state.input.charCodeAt(++state.position);
  322. }
  323. if (allowComments && ch === 0x23/* # */) {
  324. do {
  325. ch = state.input.charCodeAt(++state.position);
  326. } while (ch !== 0x0A/* LF */ && ch !== 0x0D/* CR */ && ch !== 0);
  327. }
  328. if (is_EOL(ch)) {
  329. readLineBreak(state);
  330. ch = state.input.charCodeAt(state.position);
  331. lineBreaks++;
  332. state.lineIndent = 0;
  333. while (ch === 0x20/* Space */) {
  334. state.lineIndent++;
  335. ch = state.input.charCodeAt(++state.position);
  336. }
  337. } else {
  338. break;
  339. }
  340. }
  341. if (checkIndent !== -1 && lineBreaks !== 0 && state.lineIndent < checkIndent) {
  342. throwWarning(state, 'deficient indentation');
  343. }
  344. return lineBreaks;
  345. }
  346. function testDocumentSeparator(state) {
  347. var _position = state.position,
  348. ch;
  349. ch = state.input.charCodeAt(_position);
  350. // Condition state.position === state.lineStart is tested
  351. // in parent on each call, for efficiency. No needs to test here again.
  352. if ((ch === 0x2D/* - */ || ch === 0x2E/* . */) &&
  353. ch === state.input.charCodeAt(_position + 1) &&
  354. ch === state.input.charCodeAt(_position + 2)) {
  355. _position += 3;
  356. ch = state.input.charCodeAt(_position);
  357. if (ch === 0 || is_WS_OR_EOL(ch)) {
  358. return true;
  359. }
  360. }
  361. return false;
  362. }
  363. function writeFoldedLines(state, count) {
  364. if (count === 1) {
  365. state.result += ' ';
  366. } else if (count > 1) {
  367. state.result += common.repeat('\n', count - 1);
  368. }
  369. }
  370. function readPlainScalar(state, nodeIndent, withinFlowCollection) {
  371. var preceding,
  372. following,
  373. captureStart,
  374. captureEnd,
  375. hasPendingContent,
  376. _line,
  377. _lineStart,
  378. _lineIndent,
  379. _kind = state.kind,
  380. _result = state.result,
  381. ch;
  382. ch = state.input.charCodeAt(state.position);
  383. if (is_WS_OR_EOL(ch) ||
  384. is_FLOW_INDICATOR(ch) ||
  385. ch === 0x23/* # */ ||
  386. ch === 0x26/* & */ ||
  387. ch === 0x2A/* * */ ||
  388. ch === 0x21/* ! */ ||
  389. ch === 0x7C/* | */ ||
  390. ch === 0x3E/* > */ ||
  391. ch === 0x27/* ' */ ||
  392. ch === 0x22/* " */ ||
  393. ch === 0x25/* % */ ||
  394. ch === 0x40/* @ */ ||
  395. ch === 0x60/* ` */) {
  396. return false;
  397. }
  398. if (ch === 0x3F/* ? */ || ch === 0x2D/* - */) {
  399. following = state.input.charCodeAt(state.position + 1);
  400. if (is_WS_OR_EOL(following) ||
  401. withinFlowCollection && is_FLOW_INDICATOR(following)) {
  402. return false;
  403. }
  404. }
  405. state.kind = 'scalar';
  406. state.result = '';
  407. captureStart = captureEnd = state.position;
  408. hasPendingContent = false;
  409. while (ch !== 0) {
  410. if (ch === 0x3A/* : */) {
  411. following = state.input.charCodeAt(state.position + 1);
  412. if (is_WS_OR_EOL(following) ||
  413. withinFlowCollection && is_FLOW_INDICATOR(following)) {
  414. break;
  415. }
  416. } else if (ch === 0x23/* # */) {
  417. preceding = state.input.charCodeAt(state.position - 1);
  418. if (is_WS_OR_EOL(preceding)) {
  419. break;
  420. }
  421. } else if ((state.position === state.lineStart && testDocumentSeparator(state)) ||
  422. withinFlowCollection && is_FLOW_INDICATOR(ch)) {
  423. break;
  424. } else if (is_EOL(ch)) {
  425. _line = state.line;
  426. _lineStart = state.lineStart;
  427. _lineIndent = state.lineIndent;
  428. skipSeparationSpace(state, false, -1);
  429. if (state.lineIndent >= nodeIndent) {
  430. hasPendingContent = true;
  431. ch = state.input.charCodeAt(state.position);
  432. continue;
  433. } else {
  434. state.position = captureEnd;
  435. state.line = _line;
  436. state.lineStart = _lineStart;
  437. state.lineIndent = _lineIndent;
  438. break;
  439. }
  440. }
  441. if (hasPendingContent) {
  442. captureSegment(state, captureStart, captureEnd, false);
  443. writeFoldedLines(state, state.line - _line);
  444. captureStart = captureEnd = state.position;
  445. hasPendingContent = false;
  446. }
  447. if (!is_WHITE_SPACE(ch)) {
  448. captureEnd = state.position + 1;
  449. }
  450. ch = state.input.charCodeAt(++state.position);
  451. }
  452. captureSegment(state, captureStart, captureEnd, false);
  453. if (state.result) {
  454. return true;
  455. }
  456. state.kind = _kind;
  457. state.result = _result;
  458. return false;
  459. }
  460. function readSingleQuotedScalar(state, nodeIndent) {
  461. var ch,
  462. captureStart, captureEnd;
  463. ch = state.input.charCodeAt(state.position);
  464. if (ch !== 0x27/* ' */) {
  465. return false;
  466. }
  467. state.kind = 'scalar';
  468. state.result = '';
  469. state.position++;
  470. captureStart = captureEnd = state.position;
  471. while ((ch = state.input.charCodeAt(state.position)) !== 0) {
  472. if (ch === 0x27/* ' */) {
  473. captureSegment(state, captureStart, state.position, true);
  474. ch = state.input.charCodeAt(++state.position);
  475. if (ch === 0x27/* ' */) {
  476. captureStart = state.position;
  477. state.position++;
  478. captureEnd = state.position;
  479. } else {
  480. return true;
  481. }
  482. } else if (is_EOL(ch)) {
  483. captureSegment(state, captureStart, captureEnd, true);
  484. writeFoldedLines(state, skipSeparationSpace(state, false, nodeIndent));
  485. captureStart = captureEnd = state.position;
  486. } else if (state.position === state.lineStart && testDocumentSeparator(state)) {
  487. throwError(state, 'unexpected end of the document within a single quoted scalar');
  488. } else {
  489. state.position++;
  490. captureEnd = state.position;
  491. }
  492. }
  493. throwError(state, 'unexpected end of the stream within a single quoted scalar');
  494. }
  495. function readDoubleQuotedScalar(state, nodeIndent) {
  496. var captureStart,
  497. captureEnd,
  498. hexLength,
  499. hexResult,
  500. tmp,
  501. ch;
  502. ch = state.input.charCodeAt(state.position);
  503. if (ch !== 0x22/* " */) {
  504. return false;
  505. }
  506. state.kind = 'scalar';
  507. state.result = '';
  508. state.position++;
  509. captureStart = captureEnd = state.position;
  510. while ((ch = state.input.charCodeAt(state.position)) !== 0) {
  511. if (ch === 0x22/* " */) {
  512. captureSegment(state, captureStart, state.position, true);
  513. state.position++;
  514. return true;
  515. } else if (ch === 0x5C/* \ */) {
  516. captureSegment(state, captureStart, state.position, true);
  517. ch = state.input.charCodeAt(++state.position);
  518. if (is_EOL(ch)) {
  519. skipSeparationSpace(state, false, nodeIndent);
  520. // TODO: rework to inline fn with no type cast?
  521. } else if (ch < 256 && simpleEscapeCheck[ch]) {
  522. state.result += simpleEscapeMap[ch];
  523. state.position++;
  524. } else if ((tmp = escapedHexLen(ch)) > 0) {
  525. hexLength = tmp;
  526. hexResult = 0;
  527. for (; hexLength > 0; hexLength--) {
  528. ch = state.input.charCodeAt(++state.position);
  529. if ((tmp = fromHexCode(ch)) >= 0) {
  530. hexResult = (hexResult << 4) + tmp;
  531. } else {
  532. throwError(state, 'expected hexadecimal character');
  533. }
  534. }
  535. state.result += charFromCodepoint(hexResult);
  536. state.position++;
  537. } else {
  538. throwError(state, 'unknown escape sequence');
  539. }
  540. captureStart = captureEnd = state.position;
  541. } else if (is_EOL(ch)) {
  542. captureSegment(state, captureStart, captureEnd, true);
  543. writeFoldedLines(state, skipSeparationSpace(state, false, nodeIndent));
  544. captureStart = captureEnd = state.position;
  545. } else if (state.position === state.lineStart && testDocumentSeparator(state)) {
  546. throwError(state, 'unexpected end of the document within a double quoted scalar');
  547. } else {
  548. state.position++;
  549. captureEnd = state.position;
  550. }
  551. }
  552. throwError(state, 'unexpected end of the stream within a double quoted scalar');
  553. }
  554. function readFlowCollection(state, nodeIndent) {
  555. var readNext = true,
  556. _line,
  557. _lineStart,
  558. _pos,
  559. _tag = state.tag,
  560. _result,
  561. _anchor = state.anchor,
  562. following,
  563. terminator,
  564. isPair,
  565. isExplicitPair,
  566. isMapping,
  567. overridableKeys = Object.create(null),
  568. keyNode,
  569. keyTag,
  570. valueNode,
  571. ch;
  572. ch = state.input.charCodeAt(state.position);
  573. if (ch === 0x5B/* [ */) {
  574. terminator = 0x5D;/* ] */
  575. isMapping = false;
  576. _result = [];
  577. } else if (ch === 0x7B/* { */) {
  578. terminator = 0x7D;/* } */
  579. isMapping = true;
  580. _result = {};
  581. } else {
  582. return false;
  583. }
  584. if (state.anchor !== null) {
  585. state.anchorMap[state.anchor] = _result;
  586. }
  587. ch = state.input.charCodeAt(++state.position);
  588. while (ch !== 0) {
  589. skipSeparationSpace(state, true, nodeIndent);
  590. ch = state.input.charCodeAt(state.position);
  591. if (ch === terminator) {
  592. state.position++;
  593. state.tag = _tag;
  594. state.anchor = _anchor;
  595. state.kind = isMapping ? 'mapping' : 'sequence';
  596. state.result = _result;
  597. return true;
  598. } else if (!readNext) {
  599. throwError(state, 'missed comma between flow collection entries');
  600. } else if (ch === 0x2C/* , */) {
  601. // "flow collection entries can never be completely empty", as per YAML 1.2, section 7.4
  602. throwError(state, "expected the node content, but found ','");
  603. }
  604. keyTag = keyNode = valueNode = null;
  605. isPair = isExplicitPair = false;
  606. if (ch === 0x3F/* ? */) {
  607. following = state.input.charCodeAt(state.position + 1);
  608. if (is_WS_OR_EOL(following)) {
  609. isPair = isExplicitPair = true;
  610. state.position++;
  611. skipSeparationSpace(state, true, nodeIndent);
  612. }
  613. }
  614. _line = state.line; // Save the current line.
  615. _lineStart = state.lineStart;
  616. _pos = state.position;
  617. composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true);
  618. keyTag = state.tag;
  619. keyNode = state.result;
  620. skipSeparationSpace(state, true, nodeIndent);
  621. ch = state.input.charCodeAt(state.position);
  622. if ((isExplicitPair || state.line === _line) && ch === 0x3A/* : */) {
  623. isPair = true;
  624. ch = state.input.charCodeAt(++state.position);
  625. skipSeparationSpace(state, true, nodeIndent);
  626. composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true);
  627. valueNode = state.result;
  628. }
  629. if (isMapping) {
  630. storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valueNode, _line, _lineStart, _pos);
  631. } else if (isPair) {
  632. _result.push(storeMappingPair(state, null, overridableKeys, keyTag, keyNode, valueNode, _line, _lineStart, _pos));
  633. } else {
  634. _result.push(keyNode);
  635. }
  636. skipSeparationSpace(state, true, nodeIndent);
  637. ch = state.input.charCodeAt(state.position);
  638. if (ch === 0x2C/* , */) {
  639. readNext = true;
  640. ch = state.input.charCodeAt(++state.position);
  641. } else {
  642. readNext = false;
  643. }
  644. }
  645. throwError(state, 'unexpected end of the stream within a flow collection');
  646. }
  647. function readBlockScalar(state, nodeIndent) {
  648. var captureStart,
  649. folding,
  650. chomping = CHOMPING_CLIP,
  651. didReadContent = false,
  652. detectedIndent = false,
  653. textIndent = nodeIndent,
  654. emptyLines = 0,
  655. atMoreIndented = false,
  656. tmp,
  657. ch;
  658. ch = state.input.charCodeAt(state.position);
  659. if (ch === 0x7C/* | */) {
  660. folding = false;
  661. } else if (ch === 0x3E/* > */) {
  662. folding = true;
  663. } else {
  664. return false;
  665. }
  666. state.kind = 'scalar';
  667. state.result = '';
  668. while (ch !== 0) {
  669. ch = state.input.charCodeAt(++state.position);
  670. if (ch === 0x2B/* + */ || ch === 0x2D/* - */) {
  671. if (CHOMPING_CLIP === chomping) {
  672. chomping = (ch === 0x2B/* + */) ? CHOMPING_KEEP : CHOMPING_STRIP;
  673. } else {
  674. throwError(state, 'repeat of a chomping mode identifier');
  675. }
  676. } else if ((tmp = fromDecimalCode(ch)) >= 0) {
  677. if (tmp === 0) {
  678. throwError(state, 'bad explicit indentation width of a block scalar; it cannot be less than one');
  679. } else if (!detectedIndent) {
  680. textIndent = nodeIndent + tmp - 1;
  681. detectedIndent = true;
  682. } else {
  683. throwError(state, 'repeat of an indentation width identifier');
  684. }
  685. } else {
  686. break;
  687. }
  688. }
  689. if (is_WHITE_SPACE(ch)) {
  690. do { ch = state.input.charCodeAt(++state.position); }
  691. while (is_WHITE_SPACE(ch));
  692. if (ch === 0x23/* # */) {
  693. do { ch = state.input.charCodeAt(++state.position); }
  694. while (!is_EOL(ch) && (ch !== 0));
  695. }
  696. }
  697. while (ch !== 0) {
  698. readLineBreak(state);
  699. state.lineIndent = 0;
  700. ch = state.input.charCodeAt(state.position);
  701. while ((!detectedIndent || state.lineIndent < textIndent) &&
  702. (ch === 0x20/* Space */)) {
  703. state.lineIndent++;
  704. ch = state.input.charCodeAt(++state.position);
  705. }
  706. if (!detectedIndent && state.lineIndent > textIndent) {
  707. textIndent = state.lineIndent;
  708. }
  709. if (is_EOL(ch)) {
  710. emptyLines++;
  711. continue;
  712. }
  713. // End of the scalar.
  714. if (state.lineIndent < textIndent) {
  715. // Perform the chomping.
  716. if (chomping === CHOMPING_KEEP) {
  717. state.result += common.repeat('\n', didReadContent ? 1 + emptyLines : emptyLines);
  718. } else if (chomping === CHOMPING_CLIP) {
  719. if (didReadContent) { // i.e. only if the scalar is not empty.
  720. state.result += '\n';
  721. }
  722. }
  723. // Break this `while` cycle and go to the funciton's epilogue.
  724. break;
  725. }
  726. // Folded style: use fancy rules to handle line breaks.
  727. if (folding) {
  728. // Lines starting with white space characters (more-indented lines) are not folded.
  729. if (is_WHITE_SPACE(ch)) {
  730. atMoreIndented = true;
  731. // except for the first content line (cf. Example 8.1)
  732. state.result += common.repeat('\n', didReadContent ? 1 + emptyLines : emptyLines);
  733. // End of more-indented block.
  734. } else if (atMoreIndented) {
  735. atMoreIndented = false;
  736. state.result += common.repeat('\n', emptyLines + 1);
  737. // Just one line break - perceive as the same line.
  738. } else if (emptyLines === 0) {
  739. if (didReadContent) { // i.e. only if we have already read some scalar content.
  740. state.result += ' ';
  741. }
  742. // Several line breaks - perceive as different lines.
  743. } else {
  744. state.result += common.repeat('\n', emptyLines);
  745. }
  746. // Literal style: just add exact number of line breaks between content lines.
  747. } else {
  748. // Keep all line breaks except the header line break.
  749. state.result += common.repeat('\n', didReadContent ? 1 + emptyLines : emptyLines);
  750. }
  751. didReadContent = true;
  752. detectedIndent = true;
  753. emptyLines = 0;
  754. captureStart = state.position;
  755. while (!is_EOL(ch) && (ch !== 0)) {
  756. ch = state.input.charCodeAt(++state.position);
  757. }
  758. captureSegment(state, captureStart, state.position, false);
  759. }
  760. return true;
  761. }
  762. function readBlockSequence(state, nodeIndent) {
  763. var _line,
  764. _tag = state.tag,
  765. _anchor = state.anchor,
  766. _result = [],
  767. following,
  768. detected = false,
  769. ch;
  770. // there is a leading tab before this token, so it can't be a block sequence/mapping;
  771. // it can still be flow sequence/mapping or a scalar
  772. if (state.firstTabInLine !== -1) return false;
  773. if (state.anchor !== null) {
  774. state.anchorMap[state.anchor] = _result;
  775. }
  776. ch = state.input.charCodeAt(state.position);
  777. while (ch !== 0) {
  778. if (state.firstTabInLine !== -1) {
  779. state.position = state.firstTabInLine;
  780. throwError(state, 'tab characters must not be used in indentation');
  781. }
  782. if (ch !== 0x2D/* - */) {
  783. break;
  784. }
  785. following = state.input.charCodeAt(state.position + 1);
  786. if (!is_WS_OR_EOL(following)) {
  787. break;
  788. }
  789. detected = true;
  790. state.position++;
  791. if (skipSeparationSpace(state, true, -1)) {
  792. if (state.lineIndent <= nodeIndent) {
  793. _result.push(null);
  794. ch = state.input.charCodeAt(state.position);
  795. continue;
  796. }
  797. }
  798. _line = state.line;
  799. composeNode(state, nodeIndent, CONTEXT_BLOCK_IN, false, true);
  800. _result.push(state.result);
  801. skipSeparationSpace(state, true, -1);
  802. ch = state.input.charCodeAt(state.position);
  803. if ((state.line === _line || state.lineIndent > nodeIndent) && (ch !== 0)) {
  804. throwError(state, 'bad indentation of a sequence entry');
  805. } else if (state.lineIndent < nodeIndent) {
  806. break;
  807. }
  808. }
  809. if (detected) {
  810. state.tag = _tag;
  811. state.anchor = _anchor;
  812. state.kind = 'sequence';
  813. state.result = _result;
  814. return true;
  815. }
  816. return false;
  817. }
  818. function readBlockMapping(state, nodeIndent, flowIndent) {
  819. var following,
  820. allowCompact,
  821. _line,
  822. _keyLine,
  823. _keyLineStart,
  824. _keyPos,
  825. _tag = state.tag,
  826. _anchor = state.anchor,
  827. _result = {},
  828. overridableKeys = Object.create(null),
  829. keyTag = null,
  830. keyNode = null,
  831. valueNode = null,
  832. atExplicitKey = false,
  833. detected = false,
  834. ch;
  835. // there is a leading tab before this token, so it can't be a block sequence/mapping;
  836. // it can still be flow sequence/mapping or a scalar
  837. if (state.firstTabInLine !== -1) return false;
  838. if (state.anchor !== null) {
  839. state.anchorMap[state.anchor] = _result;
  840. }
  841. ch = state.input.charCodeAt(state.position);
  842. while (ch !== 0) {
  843. if (!atExplicitKey && state.firstTabInLine !== -1) {
  844. state.position = state.firstTabInLine;
  845. throwError(state, 'tab characters must not be used in indentation');
  846. }
  847. following = state.input.charCodeAt(state.position + 1);
  848. _line = state.line; // Save the current line.
  849. //
  850. // Explicit notation case. There are two separate blocks:
  851. // first for the key (denoted by "?") and second for the value (denoted by ":")
  852. //
  853. if ((ch === 0x3F/* ? */ || ch === 0x3A/* : */) && is_WS_OR_EOL(following)) {
  854. if (ch === 0x3F/* ? */) {
  855. if (atExplicitKey) {
  856. storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, null, _keyLine, _keyLineStart, _keyPos);
  857. keyTag = keyNode = valueNode = null;
  858. }
  859. detected = true;
  860. atExplicitKey = true;
  861. allowCompact = true;
  862. } else if (atExplicitKey) {
  863. // i.e. 0x3A/* : */ === character after the explicit key.
  864. atExplicitKey = false;
  865. allowCompact = true;
  866. } else {
  867. throwError(state, 'incomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line');
  868. }
  869. state.position += 1;
  870. ch = following;
  871. //
  872. // Implicit notation case. Flow-style node as the key first, then ":", and the value.
  873. //
  874. } else {
  875. _keyLine = state.line;
  876. _keyLineStart = state.lineStart;
  877. _keyPos = state.position;
  878. if (!composeNode(state, flowIndent, CONTEXT_FLOW_OUT, false, true)) {
  879. // Neither implicit nor explicit notation.
  880. // Reading is done. Go to the epilogue.
  881. break;
  882. }
  883. if (state.line === _line) {
  884. ch = state.input.charCodeAt(state.position);
  885. while (is_WHITE_SPACE(ch)) {
  886. ch = state.input.charCodeAt(++state.position);
  887. }
  888. if (ch === 0x3A/* : */) {
  889. ch = state.input.charCodeAt(++state.position);
  890. if (!is_WS_OR_EOL(ch)) {
  891. throwError(state, 'a whitespace character is expected after the key-value separator within a block mapping');
  892. }
  893. if (atExplicitKey) {
  894. storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, null, _keyLine, _keyLineStart, _keyPos);
  895. keyTag = keyNode = valueNode = null;
  896. }
  897. detected = true;
  898. atExplicitKey = false;
  899. allowCompact = false;
  900. keyTag = state.tag;
  901. keyNode = state.result;
  902. } else if (detected) {
  903. throwError(state, 'can not read an implicit mapping pair; a colon is missed');
  904. } else {
  905. state.tag = _tag;
  906. state.anchor = _anchor;
  907. return true; // Keep the result of `composeNode`.
  908. }
  909. } else if (detected) {
  910. throwError(state, 'can not read a block mapping entry; a multiline key may not be an implicit key');
  911. } else {
  912. state.tag = _tag;
  913. state.anchor = _anchor;
  914. return true; // Keep the result of `composeNode`.
  915. }
  916. }
  917. //
  918. // Common reading code for both explicit and implicit notations.
  919. //
  920. if (state.line === _line || state.lineIndent > nodeIndent) {
  921. if (atExplicitKey) {
  922. _keyLine = state.line;
  923. _keyLineStart = state.lineStart;
  924. _keyPos = state.position;
  925. }
  926. if (composeNode(state, nodeIndent, CONTEXT_BLOCK_OUT, true, allowCompact)) {
  927. if (atExplicitKey) {
  928. keyNode = state.result;
  929. } else {
  930. valueNode = state.result;
  931. }
  932. }
  933. if (!atExplicitKey) {
  934. storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valueNode, _keyLine, _keyLineStart, _keyPos);
  935. keyTag = keyNode = valueNode = null;
  936. }
  937. skipSeparationSpace(state, true, -1);
  938. ch = state.input.charCodeAt(state.position);
  939. }
  940. if ((state.line === _line || state.lineIndent > nodeIndent) && (ch !== 0)) {
  941. throwError(state, 'bad indentation of a mapping entry');
  942. } else if (state.lineIndent < nodeIndent) {
  943. break;
  944. }
  945. }
  946. //
  947. // Epilogue.
  948. //
  949. // Special case: last mapping's node contains only the key in explicit notation.
  950. if (atExplicitKey) {
  951. storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, null, _keyLine, _keyLineStart, _keyPos);
  952. }
  953. // Expose the resulting mapping.
  954. if (detected) {
  955. state.tag = _tag;
  956. state.anchor = _anchor;
  957. state.kind = 'mapping';
  958. state.result = _result;
  959. }
  960. return detected;
  961. }
  962. function readTagProperty(state) {
  963. var _position,
  964. isVerbatim = false,
  965. isNamed = false,
  966. tagHandle,
  967. tagName,
  968. ch;
  969. ch = state.input.charCodeAt(state.position);
  970. if (ch !== 0x21/* ! */) return false;
  971. if (state.tag !== null) {
  972. throwError(state, 'duplication of a tag property');
  973. }
  974. ch = state.input.charCodeAt(++state.position);
  975. if (ch === 0x3C/* < */) {
  976. isVerbatim = true;
  977. ch = state.input.charCodeAt(++state.position);
  978. } else if (ch === 0x21/* ! */) {
  979. isNamed = true;
  980. tagHandle = '!!';
  981. ch = state.input.charCodeAt(++state.position);
  982. } else {
  983. tagHandle = '!';
  984. }
  985. _position = state.position;
  986. if (isVerbatim) {
  987. do { ch = state.input.charCodeAt(++state.position); }
  988. while (ch !== 0 && ch !== 0x3E/* > */);
  989. if (state.position < state.length) {
  990. tagName = state.input.slice(_position, state.position);
  991. ch = state.input.charCodeAt(++state.position);
  992. } else {
  993. throwError(state, 'unexpected end of the stream within a verbatim tag');
  994. }
  995. } else {
  996. while (ch !== 0 && !is_WS_OR_EOL(ch)) {
  997. if (ch === 0x21/* ! */) {
  998. if (!isNamed) {
  999. tagHandle = state.input.slice(_position - 1, state.position + 1);
  1000. if (!PATTERN_TAG_HANDLE.test(tagHandle)) {
  1001. throwError(state, 'named tag handle cannot contain such characters');
  1002. }
  1003. isNamed = true;
  1004. _position = state.position + 1;
  1005. } else {
  1006. throwError(state, 'tag suffix cannot contain exclamation marks');
  1007. }
  1008. }
  1009. ch = state.input.charCodeAt(++state.position);
  1010. }
  1011. tagName = state.input.slice(_position, state.position);
  1012. if (PATTERN_FLOW_INDICATORS.test(tagName)) {
  1013. throwError(state, 'tag suffix cannot contain flow indicator characters');
  1014. }
  1015. }
  1016. if (tagName && !PATTERN_TAG_URI.test(tagName)) {
  1017. throwError(state, 'tag name cannot contain such characters: ' + tagName);
  1018. }
  1019. try {
  1020. tagName = decodeURIComponent(tagName);
  1021. } catch (err) {
  1022. throwError(state, 'tag name is malformed: ' + tagName);
  1023. }
  1024. if (isVerbatim) {
  1025. state.tag = tagName;
  1026. } else if (_hasOwnProperty.call(state.tagMap, tagHandle)) {
  1027. state.tag = state.tagMap[tagHandle] + tagName;
  1028. } else if (tagHandle === '!') {
  1029. state.tag = '!' + tagName;
  1030. } else if (tagHandle === '!!') {
  1031. state.tag = 'tag:yaml.org,2002:' + tagName;
  1032. } else {
  1033. throwError(state, 'undeclared tag handle "' + tagHandle + '"');
  1034. }
  1035. return true;
  1036. }
  1037. function readAnchorProperty(state) {
  1038. var _position,
  1039. ch;
  1040. ch = state.input.charCodeAt(state.position);
  1041. if (ch !== 0x26/* & */) return false;
  1042. if (state.anchor !== null) {
  1043. throwError(state, 'duplication of an anchor property');
  1044. }
  1045. ch = state.input.charCodeAt(++state.position);
  1046. _position = state.position;
  1047. while (ch !== 0 && !is_WS_OR_EOL(ch) && !is_FLOW_INDICATOR(ch)) {
  1048. ch = state.input.charCodeAt(++state.position);
  1049. }
  1050. if (state.position === _position) {
  1051. throwError(state, 'name of an anchor node must contain at least one character');
  1052. }
  1053. state.anchor = state.input.slice(_position, state.position);
  1054. return true;
  1055. }
  1056. function readAlias(state) {
  1057. var _position, alias,
  1058. ch;
  1059. ch = state.input.charCodeAt(state.position);
  1060. if (ch !== 0x2A/* * */) return false;
  1061. ch = state.input.charCodeAt(++state.position);
  1062. _position = state.position;
  1063. while (ch !== 0 && !is_WS_OR_EOL(ch) && !is_FLOW_INDICATOR(ch)) {
  1064. ch = state.input.charCodeAt(++state.position);
  1065. }
  1066. if (state.position === _position) {
  1067. throwError(state, 'name of an alias node must contain at least one character');
  1068. }
  1069. alias = state.input.slice(_position, state.position);
  1070. if (!_hasOwnProperty.call(state.anchorMap, alias)) {
  1071. throwError(state, 'unidentified alias "' + alias + '"');
  1072. }
  1073. state.result = state.anchorMap[alias];
  1074. skipSeparationSpace(state, true, -1);
  1075. return true;
  1076. }
  1077. function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact) {
  1078. var allowBlockStyles,
  1079. allowBlockScalars,
  1080. allowBlockCollections,
  1081. indentStatus = 1, // 1: this>parent, 0: this=parent, -1: this<parent
  1082. atNewLine = false,
  1083. hasContent = false,
  1084. typeIndex,
  1085. typeQuantity,
  1086. typeList,
  1087. type,
  1088. flowIndent,
  1089. blockIndent;
  1090. if (state.listener !== null) {
  1091. state.listener('open', state);
  1092. }
  1093. state.tag = null;
  1094. state.anchor = null;
  1095. state.kind = null;
  1096. state.result = null;
  1097. allowBlockStyles = allowBlockScalars = allowBlockCollections =
  1098. CONTEXT_BLOCK_OUT === nodeContext ||
  1099. CONTEXT_BLOCK_IN === nodeContext;
  1100. if (allowToSeek) {
  1101. if (skipSeparationSpace(state, true, -1)) {
  1102. atNewLine = true;
  1103. if (state.lineIndent > parentIndent) {
  1104. indentStatus = 1;
  1105. } else if (state.lineIndent === parentIndent) {
  1106. indentStatus = 0;
  1107. } else if (state.lineIndent < parentIndent) {
  1108. indentStatus = -1;
  1109. }
  1110. }
  1111. }
  1112. if (indentStatus === 1) {
  1113. while (readTagProperty(state) || readAnchorProperty(state)) {
  1114. if (skipSeparationSpace(state, true, -1)) {
  1115. atNewLine = true;
  1116. allowBlockCollections = allowBlockStyles;
  1117. if (state.lineIndent > parentIndent) {
  1118. indentStatus = 1;
  1119. } else if (state.lineIndent === parentIndent) {
  1120. indentStatus = 0;
  1121. } else if (state.lineIndent < parentIndent) {
  1122. indentStatus = -1;
  1123. }
  1124. } else {
  1125. allowBlockCollections = false;
  1126. }
  1127. }
  1128. }
  1129. if (allowBlockCollections) {
  1130. allowBlockCollections = atNewLine || allowCompact;
  1131. }
  1132. if (indentStatus === 1 || CONTEXT_BLOCK_OUT === nodeContext) {
  1133. if (CONTEXT_FLOW_IN === nodeContext || CONTEXT_FLOW_OUT === nodeContext) {
  1134. flowIndent = parentIndent;
  1135. } else {
  1136. flowIndent = parentIndent + 1;
  1137. }
  1138. blockIndent = state.position - state.lineStart;
  1139. if (indentStatus === 1) {
  1140. if (allowBlockCollections &&
  1141. (readBlockSequence(state, blockIndent) ||
  1142. readBlockMapping(state, blockIndent, flowIndent)) ||
  1143. readFlowCollection(state, flowIndent)) {
  1144. hasContent = true;
  1145. } else {
  1146. if ((allowBlockScalars && readBlockScalar(state, flowIndent)) ||
  1147. readSingleQuotedScalar(state, flowIndent) ||
  1148. readDoubleQuotedScalar(state, flowIndent)) {
  1149. hasContent = true;
  1150. } else if (readAlias(state)) {
  1151. hasContent = true;
  1152. if (state.tag !== null || state.anchor !== null) {
  1153. throwError(state, 'alias node should not have any properties');
  1154. }
  1155. } else if (readPlainScalar(state, flowIndent, CONTEXT_FLOW_IN === nodeContext)) {
  1156. hasContent = true;
  1157. if (state.tag === null) {
  1158. state.tag = '?';
  1159. }
  1160. }
  1161. if (state.anchor !== null) {
  1162. state.anchorMap[state.anchor] = state.result;
  1163. }
  1164. }
  1165. } else if (indentStatus === 0) {
  1166. // Special case: block sequences are allowed to have same indentation level as the parent.
  1167. // http://www.yaml.org/spec/1.2/spec.html#id2799784
  1168. hasContent = allowBlockCollections && readBlockSequence(state, blockIndent);
  1169. }
  1170. }
  1171. if (state.tag === null) {
  1172. if (state.anchor !== null) {
  1173. state.anchorMap[state.anchor] = state.result;
  1174. }
  1175. } else if (state.tag === '?') {
  1176. // Implicit resolving is not allowed for non-scalar types, and '?'
  1177. // non-specific tag is only automatically assigned to plain scalars.
  1178. //
  1179. // We only need to check kind conformity in case user explicitly assigns '?'
  1180. // tag, for example like this: "!<?> [0]"
  1181. //
  1182. if (state.result !== null && state.kind !== 'scalar') {
  1183. throwError(state, 'unacceptable node kind for !<?> tag; it should be "scalar", not "' + state.kind + '"');
  1184. }
  1185. for (typeIndex = 0, typeQuantity = state.implicitTypes.length; typeIndex < typeQuantity; typeIndex += 1) {
  1186. type = state.implicitTypes[typeIndex];
  1187. if (type.resolve(state.result)) { // `state.result` updated in resolver if matched
  1188. state.result = type.construct(state.result);
  1189. state.tag = type.tag;
  1190. if (state.anchor !== null) {
  1191. state.anchorMap[state.anchor] = state.result;
  1192. }
  1193. break;
  1194. }
  1195. }
  1196. } else if (state.tag !== '!') {
  1197. if (_hasOwnProperty.call(state.typeMap[state.kind || 'fallback'], state.tag)) {
  1198. type = state.typeMap[state.kind || 'fallback'][state.tag];
  1199. } else {
  1200. // looking for multi type
  1201. type = null;
  1202. typeList = state.typeMap.multi[state.kind || 'fallback'];
  1203. for (typeIndex = 0, typeQuantity = typeList.length; typeIndex < typeQuantity; typeIndex += 1) {
  1204. if (state.tag.slice(0, typeList[typeIndex].tag.length) === typeList[typeIndex].tag) {
  1205. type = typeList[typeIndex];
  1206. break;
  1207. }
  1208. }
  1209. }
  1210. if (!type) {
  1211. throwError(state, 'unknown tag !<' + state.tag + '>');
  1212. }
  1213. if (state.result !== null && type.kind !== state.kind) {
  1214. throwError(state, 'unacceptable node kind for !<' + state.tag + '> tag; it should be "' + type.kind + '", not "' + state.kind + '"');
  1215. }
  1216. if (!type.resolve(state.result, state.tag)) { // `state.result` updated in resolver if matched
  1217. throwError(state, 'cannot resolve a node with !<' + state.tag + '> explicit tag');
  1218. } else {
  1219. state.result = type.construct(state.result, state.tag);
  1220. if (state.anchor !== null) {
  1221. state.anchorMap[state.anchor] = state.result;
  1222. }
  1223. }
  1224. }
  1225. if (state.listener !== null) {
  1226. state.listener('close', state);
  1227. }
  1228. return state.tag !== null || state.anchor !== null || hasContent;
  1229. }
  1230. function readDocument(state) {
  1231. var documentStart = state.position,
  1232. _position,
  1233. directiveName,
  1234. directiveArgs,
  1235. hasDirectives = false,
  1236. ch;
  1237. state.version = null;
  1238. state.checkLineBreaks = state.legacy;
  1239. state.tagMap = Object.create(null);
  1240. state.anchorMap = Object.create(null);
  1241. while ((ch = state.input.charCodeAt(state.position)) !== 0) {
  1242. skipSeparationSpace(state, true, -1);
  1243. ch = state.input.charCodeAt(state.position);
  1244. if (state.lineIndent > 0 || ch !== 0x25/* % */) {
  1245. break;
  1246. }
  1247. hasDirectives = true;
  1248. ch = state.input.charCodeAt(++state.position);
  1249. _position = state.position;
  1250. while (ch !== 0 && !is_WS_OR_EOL(ch)) {
  1251. ch = state.input.charCodeAt(++state.position);
  1252. }
  1253. directiveName = state.input.slice(_position, state.position);
  1254. directiveArgs = [];
  1255. if (directiveName.length < 1) {
  1256. throwError(state, 'directive name must not be less than one character in length');
  1257. }
  1258. while (ch !== 0) {
  1259. while (is_WHITE_SPACE(ch)) {
  1260. ch = state.input.charCodeAt(++state.position);
  1261. }
  1262. if (ch === 0x23/* # */) {
  1263. do { ch = state.input.charCodeAt(++state.position); }
  1264. while (ch !== 0 && !is_EOL(ch));
  1265. break;
  1266. }
  1267. if (is_EOL(ch)) break;
  1268. _position = state.position;
  1269. while (ch !== 0 && !is_WS_OR_EOL(ch)) {
  1270. ch = state.input.charCodeAt(++state.position);
  1271. }
  1272. directiveArgs.push(state.input.slice(_position, state.position));
  1273. }
  1274. if (ch !== 0) readLineBreak(state);
  1275. if (_hasOwnProperty.call(directiveHandlers, directiveName)) {
  1276. directiveHandlers[directiveName](state, directiveName, directiveArgs);
  1277. } else {
  1278. throwWarning(state, 'unknown document directive "' + directiveName + '"');
  1279. }
  1280. }
  1281. skipSeparationSpace(state, true, -1);
  1282. if (state.lineIndent === 0 &&
  1283. state.input.charCodeAt(state.position) === 0x2D/* - */ &&
  1284. state.input.charCodeAt(state.position + 1) === 0x2D/* - */ &&
  1285. state.input.charCodeAt(state.position + 2) === 0x2D/* - */) {
  1286. state.position += 3;
  1287. skipSeparationSpace(state, true, -1);
  1288. } else if (hasDirectives) {
  1289. throwError(state, 'directives end mark is expected');
  1290. }
  1291. composeNode(state, state.lineIndent - 1, CONTEXT_BLOCK_OUT, false, true);
  1292. skipSeparationSpace(state, true, -1);
  1293. if (state.checkLineBreaks &&
  1294. PATTERN_NON_ASCII_LINE_BREAKS.test(state.input.slice(documentStart, state.position))) {
  1295. throwWarning(state, 'non-ASCII line breaks are interpreted as content');
  1296. }
  1297. state.documents.push(state.result);
  1298. if (state.position === state.lineStart && testDocumentSeparator(state)) {
  1299. if (state.input.charCodeAt(state.position) === 0x2E/* . */) {
  1300. state.position += 3;
  1301. skipSeparationSpace(state, true, -1);
  1302. }
  1303. return;
  1304. }
  1305. if (state.position < (state.length - 1)) {
  1306. throwError(state, 'end of the stream or a document separator is expected');
  1307. } else {
  1308. return;
  1309. }
  1310. }
  1311. function loadDocuments(input, options) {
  1312. input = String(input);
  1313. options = options || {};
  1314. if (input.length !== 0) {
  1315. // Add tailing `\n` if not exists
  1316. if (input.charCodeAt(input.length - 1) !== 0x0A/* LF */ &&
  1317. input.charCodeAt(input.length - 1) !== 0x0D/* CR */) {
  1318. input += '\n';
  1319. }
  1320. // Strip BOM
  1321. if (input.charCodeAt(0) === 0xFEFF) {
  1322. input = input.slice(1);
  1323. }
  1324. }
  1325. var state = new State(input, options);
  1326. var nullpos = input.indexOf('\0');
  1327. if (nullpos !== -1) {
  1328. state.position = nullpos;
  1329. throwError(state, 'null byte is not allowed in input');
  1330. }
  1331. // Use 0 as string terminator. That significantly simplifies bounds check.
  1332. state.input += '\0';
  1333. while (state.input.charCodeAt(state.position) === 0x20/* Space */) {
  1334. state.lineIndent += 1;
  1335. state.position += 1;
  1336. }
  1337. while (state.position < (state.length - 1)) {
  1338. readDocument(state);
  1339. }
  1340. return state.documents;
  1341. }
  1342. function loadAll(input, iterator, options) {
  1343. if (iterator !== null && typeof iterator === 'object' && typeof options === 'undefined') {
  1344. options = iterator;
  1345. iterator = null;
  1346. }
  1347. var documents = loadDocuments(input, options);
  1348. if (typeof iterator !== 'function') {
  1349. return documents;
  1350. }
  1351. for (var index = 0, length = documents.length; index < length; index += 1) {
  1352. iterator(documents[index]);
  1353. }
  1354. }
  1355. function load(input, options) {
  1356. var documents = loadDocuments(input, options);
  1357. if (documents.length === 0) {
  1358. /*eslint-disable no-undefined*/
  1359. return undefined;
  1360. } else if (documents.length === 1) {
  1361. return documents[0];
  1362. }
  1363. throw new YAMLException('expected a single document in the stream, but found more');
  1364. }
  1365. module.exports.loadAll = loadAll;
  1366. module.exports.load = load;