loader.js 41 KB

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