loader.js 43 KB

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