Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

loader.js 42KB

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