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 43KB

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