react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / lib / js-yaml / loader.js
80698 views'use strict';12/*eslint-disable max-len,no-use-before-define*/34var common = require('./common');5var YAMLException = require('./exception');6var Mark = require('./mark');7var DEFAULT_SAFE_SCHEMA = require('./schema/default_safe');8var DEFAULT_FULL_SCHEMA = require('./schema/default_full');91011var _hasOwnProperty = Object.prototype.hasOwnProperty;121314var CONTEXT_FLOW_IN = 1;15var CONTEXT_FLOW_OUT = 2;16var CONTEXT_BLOCK_IN = 3;17var CONTEXT_BLOCK_OUT = 4;181920var CHOMPING_CLIP = 1;21var CHOMPING_STRIP = 2;22var CHOMPING_KEEP = 3;232425var PATTERN_NON_PRINTABLE = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;26var PATTERN_NON_ASCII_LINE_BREAKS = /[\x85\u2028\u2029]/;27var PATTERN_FLOW_INDICATORS = /[,\[\]\{\}]/;28var PATTERN_TAG_HANDLE = /^(?:!|!!|![a-z\-]+!)$/i;29var PATTERN_TAG_URI = /^(?:!|[^,\[\]\{\}])(?:%[0-9a-f]{2}|[0-9a-z\-#;\/\?:@&=\+\$,_\.!~\*'\(\)\[\]])*$/i;303132function is_EOL(c) {33return (c === 0x0A/* LF */) || (c === 0x0D/* CR */);34}3536function is_WHITE_SPACE(c) {37return (c === 0x09/* Tab */) || (c === 0x20/* Space */);38}3940function is_WS_OR_EOL(c) {41return (c === 0x09/* Tab */) ||42(c === 0x20/* Space */) ||43(c === 0x0A/* LF */) ||44(c === 0x0D/* CR */);45}4647function is_FLOW_INDICATOR(c) {48return 0x2C/* , */ === c ||490x5B/* [ */ === c ||500x5D/* ] */ === c ||510x7B/* { */ === c ||520x7D/* } */ === c;53}5455function fromHexCode(c) {56var lc;5758if ((0x30/* 0 */ <= c) && (c <= 0x39/* 9 */)) {59return c - 0x30;60}6162/*eslint-disable no-bitwise*/63lc = c | 0x20;6465if ((0x61/* a */ <= lc) && (lc <= 0x66/* f */)) {66return lc - 0x61 + 10;67}6869return -1;70}7172function escapedHexLen(c) {73if (c === 0x78/* x */) { return 2; }74if (c === 0x75/* u */) { return 4; }75if (c === 0x55/* U */) { return 8; }76return 0;77}7879function fromDecimalCode(c) {80if ((0x30/* 0 */ <= c) && (c <= 0x39/* 9 */)) {81return c - 0x30;82}8384return -1;85}8687function simpleEscapeSequence(c) {88return (c === 0x30/* 0 */) ? '\x00' :89(c === 0x61/* a */) ? '\x07' :90(c === 0x62/* b */) ? '\x08' :91(c === 0x74/* t */) ? '\x09' :92(c === 0x09/* Tab */) ? '\x09' :93(c === 0x6E/* n */) ? '\x0A' :94(c === 0x76/* v */) ? '\x0B' :95(c === 0x66/* f */) ? '\x0C' :96(c === 0x72/* r */) ? '\x0D' :97(c === 0x65/* e */) ? '\x1B' :98(c === 0x20/* Space */) ? ' ' :99(c === 0x22/* " */) ? '\x22' :100(c === 0x2F/* / */) ? '/' :101(c === 0x5C/* \ */) ? '\x5C' :102(c === 0x4E/* N */) ? '\x85' :103(c === 0x5F/* _ */) ? '\xA0' :104(c === 0x4C/* L */) ? '\u2028' :105(c === 0x50/* P */) ? '\u2029' : '';106}107108function charFromCodepoint(c) {109if (c <= 0xFFFF) {110return String.fromCharCode(c);111}112// Encode UTF-16 surrogate pair113// https://en.wikipedia.org/wiki/UTF-16#Code_points_U.2B010000_to_U.2B10FFFF114return String.fromCharCode(((c - 0x010000) >> 10) + 0xD800,115((c - 0x010000) & 0x03FF) + 0xDC00);116}117118var simpleEscapeCheck = new Array(256); // integer, for fast access119var simpleEscapeMap = new Array(256);120for (var i = 0; i < 256; i++) {121simpleEscapeCheck[i] = simpleEscapeSequence(i) ? 1 : 0;122simpleEscapeMap[i] = simpleEscapeSequence(i);123}124125126function State(input, options) {127this.input = input;128129this.filename = options['filename'] || null;130this.schema = options['schema'] || DEFAULT_FULL_SCHEMA;131this.onWarning = options['onWarning'] || null;132this.legacy = options['legacy'] || false;133134this.implicitTypes = this.schema.compiledImplicit;135this.typeMap = this.schema.compiledTypeMap;136137this.length = input.length;138this.position = 0;139this.line = 0;140this.lineStart = 0;141this.lineIndent = 0;142143this.documents = [];144145/*146this.version;147this.checkLineBreaks;148this.tagMap;149this.anchorMap;150this.tag;151this.anchor;152this.kind;153this.result;*/154155}156157158function generateError(state, message) {159return new YAMLException(160message,161new Mark(state.filename, state.input, state.position, state.line, (state.position - state.lineStart)));162}163164function throwError(state, message) {165throw generateError(state, message);166}167168function throwWarning(state, message) {169var error = generateError(state, message);170171if (state.onWarning) {172state.onWarning.call(null, error);173} else {174throw error;175}176}177178179var directiveHandlers = {180181YAML: function handleYamlDirective(state, name, args) {182183var match, major, minor;184185if (null !== state.version) {186throwError(state, 'duplication of %YAML directive');187}188189if (1 !== args.length) {190throwError(state, 'YAML directive accepts exactly one argument');191}192193match = /^([0-9]+)\.([0-9]+)$/.exec(args[0]);194195if (null === match) {196throwError(state, 'ill-formed argument of the YAML directive');197}198199major = parseInt(match[1], 10);200minor = parseInt(match[2], 10);201202if (1 !== major) {203throwError(state, 'unacceptable YAML version of the document');204}205206state.version = args[0];207state.checkLineBreaks = (minor < 2);208209if (1 !== minor && 2 !== minor) {210throwWarning(state, 'unsupported YAML version of the document');211}212},213214TAG: function handleTagDirective(state, name, args) {215216var handle, prefix;217218if (2 !== args.length) {219throwError(state, 'TAG directive accepts exactly two arguments');220}221222handle = args[0];223prefix = args[1];224225if (!PATTERN_TAG_HANDLE.test(handle)) {226throwError(state, 'ill-formed tag handle (first argument) of the TAG directive');227}228229if (_hasOwnProperty.call(state.tagMap, handle)) {230throwError(state, 'there is a previously declared suffix for "' + handle + '" tag handle');231}232233if (!PATTERN_TAG_URI.test(prefix)) {234throwError(state, 'ill-formed tag prefix (second argument) of the TAG directive');235}236237state.tagMap[handle] = prefix;238}239};240241242function captureSegment(state, start, end, checkJson) {243var _position, _length, _character, _result;244245if (start < end) {246_result = state.input.slice(start, end);247248if (checkJson) {249for (_position = 0, _length = _result.length;250_position < _length;251_position += 1) {252_character = _result.charCodeAt(_position);253if (!(0x09 === _character ||2540x20 <= _character && _character <= 0x10FFFF)) {255throwError(state, 'expected valid JSON character');256}257}258}259260state.result += _result;261}262}263264function mergeMappings(state, destination, source) {265var sourceKeys, key, index, quantity;266267if (!common.isObject(source)) {268throwError(state, 'cannot merge mappings; the provided source object is unacceptable');269}270271sourceKeys = Object.keys(source);272273for (index = 0, quantity = sourceKeys.length; index < quantity; index += 1) {274key = sourceKeys[index];275276if (!_hasOwnProperty.call(destination, key)) {277destination[key] = source[key];278}279}280}281282function storeMappingPair(state, _result, keyTag, keyNode, valueNode) {283var index, quantity;284285keyNode = String(keyNode);286287if (null === _result) {288_result = {};289}290291if ('tag:yaml.org,2002:merge' === keyTag) {292if (Array.isArray(valueNode)) {293for (index = 0, quantity = valueNode.length; index < quantity; index += 1) {294mergeMappings(state, _result, valueNode[index]);295}296} else {297mergeMappings(state, _result, valueNode);298}299} else {300_result[keyNode] = valueNode;301}302303return _result;304}305306function readLineBreak(state) {307var ch;308309ch = state.input.charCodeAt(state.position);310311if (0x0A/* LF */ === ch) {312state.position++;313} else if (0x0D/* CR */ === ch) {314state.position++;315if (0x0A/* LF */ === state.input.charCodeAt(state.position)) {316state.position++;317}318} else {319throwError(state, 'a line break is expected');320}321322state.line += 1;323state.lineStart = state.position;324}325326function skipSeparationSpace(state, allowComments, checkIndent) {327var lineBreaks = 0,328ch = state.input.charCodeAt(state.position);329330while (0 !== ch) {331while (is_WHITE_SPACE(ch)) {332ch = state.input.charCodeAt(++state.position);333}334335if (allowComments && 0x23/* # */ === ch) {336do {337ch = state.input.charCodeAt(++state.position);338} while (ch !== 0x0A/* LF */ && ch !== 0x0D/* CR */ && 0 !== ch);339}340341if (is_EOL(ch)) {342readLineBreak(state);343344ch = state.input.charCodeAt(state.position);345lineBreaks++;346state.lineIndent = 0;347348while (0x20/* Space */ === ch) {349state.lineIndent++;350ch = state.input.charCodeAt(++state.position);351}352} else {353break;354}355}356357if (-1 !== checkIndent && 0 !== lineBreaks && state.lineIndent < checkIndent) {358throwWarning(state, 'deficient indentation');359}360361return lineBreaks;362}363364function testDocumentSeparator(state) {365var _position = state.position,366ch;367368ch = state.input.charCodeAt(_position);369370// Condition state.position === state.lineStart is tested371// in parent on each call, for efficiency. No needs to test here again.372if ((0x2D/* - */ === ch || 0x2E/* . */ === ch) &&373state.input.charCodeAt(_position + 1) === ch &&374state.input.charCodeAt(_position + 2) === ch) {375376_position += 3;377378ch = state.input.charCodeAt(_position);379380if (ch === 0 || is_WS_OR_EOL(ch)) {381return true;382}383}384385return false;386}387388function writeFoldedLines(state, count) {389if (1 === count) {390state.result += ' ';391} else if (count > 1) {392state.result += common.repeat('\n', count - 1);393}394}395396397function readPlainScalar(state, nodeIndent, withinFlowCollection) {398var preceding,399following,400captureStart,401captureEnd,402hasPendingContent,403_line,404_lineStart,405_lineIndent,406_kind = state.kind,407_result = state.result,408ch;409410ch = state.input.charCodeAt(state.position);411412if (is_WS_OR_EOL(ch) ||413is_FLOW_INDICATOR(ch) ||4140x23/* # */ === ch ||4150x26/* & */ === ch ||4160x2A/* * */ === ch ||4170x21/* ! */ === ch ||4180x7C/* | */ === ch ||4190x3E/* > */ === ch ||4200x27/* ' */ === ch ||4210x22/* " */ === ch ||4220x25/* % */ === ch ||4230x40/* @ */ === ch ||4240x60/* ` */ === ch) {425return false;426}427428if (0x3F/* ? */ === ch || 0x2D/* - */ === ch) {429following = state.input.charCodeAt(state.position + 1);430431if (is_WS_OR_EOL(following) ||432withinFlowCollection && is_FLOW_INDICATOR(following)) {433return false;434}435}436437state.kind = 'scalar';438state.result = '';439captureStart = captureEnd = state.position;440hasPendingContent = false;441442while (0 !== ch) {443if (0x3A/* : */ === ch) {444following = state.input.charCodeAt(state.position + 1);445446if (is_WS_OR_EOL(following) ||447withinFlowCollection && is_FLOW_INDICATOR(following)) {448break;449}450451} else if (0x23/* # */ === ch) {452preceding = state.input.charCodeAt(state.position - 1);453454if (is_WS_OR_EOL(preceding)) {455break;456}457458} else if ((state.position === state.lineStart && testDocumentSeparator(state)) ||459withinFlowCollection && is_FLOW_INDICATOR(ch)) {460break;461462} else if (is_EOL(ch)) {463_line = state.line;464_lineStart = state.lineStart;465_lineIndent = state.lineIndent;466skipSeparationSpace(state, false, -1);467468if (state.lineIndent >= nodeIndent) {469hasPendingContent = true;470ch = state.input.charCodeAt(state.position);471continue;472} else {473state.position = captureEnd;474state.line = _line;475state.lineStart = _lineStart;476state.lineIndent = _lineIndent;477break;478}479}480481if (hasPendingContent) {482captureSegment(state, captureStart, captureEnd, false);483writeFoldedLines(state, state.line - _line);484captureStart = captureEnd = state.position;485hasPendingContent = false;486}487488if (!is_WHITE_SPACE(ch)) {489captureEnd = state.position + 1;490}491492ch = state.input.charCodeAt(++state.position);493}494495captureSegment(state, captureStart, captureEnd, false);496497if (state.result) {498return true;499}500501state.kind = _kind;502state.result = _result;503return false;504}505506function readSingleQuotedScalar(state, nodeIndent) {507var ch,508captureStart, captureEnd;509510ch = state.input.charCodeAt(state.position);511512if (0x27/* ' */ !== ch) {513return false;514}515516state.kind = 'scalar';517state.result = '';518state.position++;519captureStart = captureEnd = state.position;520521while (0 !== (ch = state.input.charCodeAt(state.position))) {522if (0x27/* ' */ === ch) {523captureSegment(state, captureStart, state.position, true);524ch = state.input.charCodeAt(++state.position);525526if (0x27/* ' */ === ch) {527captureStart = captureEnd = state.position;528state.position++;529} else {530return true;531}532533} else if (is_EOL(ch)) {534captureSegment(state, captureStart, captureEnd, true);535writeFoldedLines(state, skipSeparationSpace(state, false, nodeIndent));536captureStart = captureEnd = state.position;537538} else if (state.position === state.lineStart && testDocumentSeparator(state)) {539throwError(state, 'unexpected end of the document within a single quoted scalar');540541} else {542state.position++;543captureEnd = state.position;544}545}546547throwError(state, 'unexpected end of the stream within a single quoted scalar');548}549550function readDoubleQuotedScalar(state, nodeIndent) {551var captureStart,552captureEnd,553hexLength,554hexResult,555tmp, tmpEsc,556ch;557558ch = state.input.charCodeAt(state.position);559560if (0x22/* " */ !== ch) {561return false;562}563564state.kind = 'scalar';565state.result = '';566state.position++;567captureStart = captureEnd = state.position;568569while (0 !== (ch = state.input.charCodeAt(state.position))) {570if (0x22/* " */ === ch) {571captureSegment(state, captureStart, state.position, true);572state.position++;573return true;574575} else if (0x5C/* \ */ === ch) {576captureSegment(state, captureStart, state.position, true);577ch = state.input.charCodeAt(++state.position);578579if (is_EOL(ch)) {580skipSeparationSpace(state, false, nodeIndent);581582// TODO: rework to inline fn with no type cast?583} else if (ch < 256 && simpleEscapeCheck[ch]) {584state.result += simpleEscapeMap[ch];585state.position++;586587} else if ((tmp = escapedHexLen(ch)) > 0) {588hexLength = tmp;589hexResult = 0;590591for (; hexLength > 0; hexLength--) {592ch = state.input.charCodeAt(++state.position);593594if ((tmp = fromHexCode(ch)) >= 0) {595hexResult = (hexResult << 4) + tmp;596597} else {598throwError(state, 'expected hexadecimal character');599}600}601602state.result += charFromCodepoint(hexResult);603604state.position++;605606} else {607throwError(state, 'unknown escape sequence');608}609610captureStart = captureEnd = state.position;611612} else if (is_EOL(ch)) {613captureSegment(state, captureStart, captureEnd, true);614writeFoldedLines(state, skipSeparationSpace(state, false, nodeIndent));615captureStart = captureEnd = state.position;616617} else if (state.position === state.lineStart && testDocumentSeparator(state)) {618throwError(state, 'unexpected end of the document within a double quoted scalar');619620} else {621state.position++;622captureEnd = state.position;623}624}625626throwError(state, 'unexpected end of the stream within a double quoted scalar');627}628629function readFlowCollection(state, nodeIndent) {630var readNext = true,631_line,632_tag = state.tag,633_result,634_anchor = state.anchor,635following,636terminator,637isPair,638isExplicitPair,639isMapping,640keyNode,641keyTag,642valueNode,643ch;644645ch = state.input.charCodeAt(state.position);646647if (ch === 0x5B/* [ */) {648terminator = 0x5D;/* ] */649isMapping = false;650_result = [];651} else if (ch === 0x7B/* { */) {652terminator = 0x7D;/* } */653isMapping = true;654_result = {};655} else {656return false;657}658659if (null !== state.anchor) {660state.anchorMap[state.anchor] = _result;661}662663ch = state.input.charCodeAt(++state.position);664665while (0 !== ch) {666skipSeparationSpace(state, true, nodeIndent);667668ch = state.input.charCodeAt(state.position);669670if (ch === terminator) {671state.position++;672state.tag = _tag;673state.anchor = _anchor;674state.kind = isMapping ? 'mapping' : 'sequence';675state.result = _result;676return true;677} else if (!readNext) {678throwError(state, 'missed comma between flow collection entries');679}680681keyTag = keyNode = valueNode = null;682isPair = isExplicitPair = false;683684if (0x3F/* ? */ === ch) {685following = state.input.charCodeAt(state.position + 1);686687if (is_WS_OR_EOL(following)) {688isPair = isExplicitPair = true;689state.position++;690skipSeparationSpace(state, true, nodeIndent);691}692}693694_line = state.line;695composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true);696keyTag = state.tag;697keyNode = state.result;698skipSeparationSpace(state, true, nodeIndent);699700ch = state.input.charCodeAt(state.position);701702if ((isExplicitPair || state.line === _line) && 0x3A/* : */ === ch) {703isPair = true;704ch = state.input.charCodeAt(++state.position);705skipSeparationSpace(state, true, nodeIndent);706composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true);707valueNode = state.result;708}709710if (isMapping) {711storeMappingPair(state, _result, keyTag, keyNode, valueNode);712} else if (isPair) {713_result.push(storeMappingPair(state, null, keyTag, keyNode, valueNode));714} else {715_result.push(keyNode);716}717718skipSeparationSpace(state, true, nodeIndent);719720ch = state.input.charCodeAt(state.position);721722if (0x2C/* , */ === ch) {723readNext = true;724ch = state.input.charCodeAt(++state.position);725} else {726readNext = false;727}728}729730throwError(state, 'unexpected end of the stream within a flow collection');731}732733function readBlockScalar(state, nodeIndent) {734var captureStart,735folding,736chomping = CHOMPING_CLIP,737detectedIndent = false,738textIndent = nodeIndent,739emptyLines = 0,740atMoreIndented = false,741tmp,742ch;743744ch = state.input.charCodeAt(state.position);745746if (ch === 0x7C/* | */) {747folding = false;748} else if (ch === 0x3E/* > */) {749folding = true;750} else {751return false;752}753754state.kind = 'scalar';755state.result = '';756757while (0 !== ch) {758ch = state.input.charCodeAt(++state.position);759760if (0x2B/* + */ === ch || 0x2D/* - */ === ch) {761if (CHOMPING_CLIP === chomping) {762chomping = (0x2B/* + */ === ch) ? CHOMPING_KEEP : CHOMPING_STRIP;763} else {764throwError(state, 'repeat of a chomping mode identifier');765}766767} else if ((tmp = fromDecimalCode(ch)) >= 0) {768if (tmp === 0) {769throwError(state, 'bad explicit indentation width of a block scalar; it cannot be less than one');770} else if (!detectedIndent) {771textIndent = nodeIndent + tmp - 1;772detectedIndent = true;773} else {774throwError(state, 'repeat of an indentation width identifier');775}776777} else {778break;779}780}781782if (is_WHITE_SPACE(ch)) {783do { ch = state.input.charCodeAt(++state.position); }784while (is_WHITE_SPACE(ch));785786if (0x23/* # */ === ch) {787do { ch = state.input.charCodeAt(++state.position); }788while (!is_EOL(ch) && (0 !== ch));789}790}791792while (0 !== ch) {793readLineBreak(state);794state.lineIndent = 0;795796ch = state.input.charCodeAt(state.position);797798while ((!detectedIndent || state.lineIndent < textIndent) &&799(0x20/* Space */ === ch)) {800state.lineIndent++;801ch = state.input.charCodeAt(++state.position);802}803804if (!detectedIndent && state.lineIndent > textIndent) {805textIndent = state.lineIndent;806}807808if (is_EOL(ch)) {809emptyLines++;810continue;811}812813// End of the scalar.814if (state.lineIndent < textIndent) {815816// Perform the chomping.817if (chomping === CHOMPING_KEEP) {818state.result += common.repeat('\n', emptyLines);819} else if (chomping === CHOMPING_CLIP) {820if (detectedIndent) { // i.e. only if the scalar is not empty.821state.result += '\n';822}823}824825// Break this `while` cycle and go to the funciton's epilogue.826break;827}828829// Folded style: use fancy rules to handle line breaks.830if (folding) {831832// Lines starting with white space characters (more-indented lines) are not folded.833if (is_WHITE_SPACE(ch)) {834atMoreIndented = true;835state.result += common.repeat('\n', emptyLines + 1);836837// End of more-indented block.838} else if (atMoreIndented) {839atMoreIndented = false;840state.result += common.repeat('\n', emptyLines + 1);841842// Just one line break - perceive as the same line.843} else if (0 === emptyLines) {844if (detectedIndent) { // i.e. only if we have already read some scalar content.845state.result += ' ';846}847848// Several line breaks - perceive as different lines.849} else {850state.result += common.repeat('\n', emptyLines);851}852853// Literal style: just add exact number of line breaks between content lines.854} else if (detectedIndent) {855// If current line isn't the first one - count line break from the last content line.856state.result += common.repeat('\n', emptyLines + 1);857} else {858// In case of the first content line - count only empty lines.859}860861detectedIndent = true;862emptyLines = 0;863captureStart = state.position;864865while (!is_EOL(ch) && (0 !== ch)) {866ch = state.input.charCodeAt(++state.position);867}868869captureSegment(state, captureStart, state.position, false);870}871872return true;873}874875function readBlockSequence(state, nodeIndent) {876var _line,877_tag = state.tag,878_anchor = state.anchor,879_result = [],880following,881detected = false,882ch;883884if (null !== state.anchor) {885state.anchorMap[state.anchor] = _result;886}887888ch = state.input.charCodeAt(state.position);889890while (0 !== ch) {891892if (0x2D/* - */ !== ch) {893break;894}895896following = state.input.charCodeAt(state.position + 1);897898if (!is_WS_OR_EOL(following)) {899break;900}901902detected = true;903state.position++;904905if (skipSeparationSpace(state, true, -1)) {906if (state.lineIndent <= nodeIndent) {907_result.push(null);908ch = state.input.charCodeAt(state.position);909continue;910}911}912913_line = state.line;914composeNode(state, nodeIndent, CONTEXT_BLOCK_IN, false, true);915_result.push(state.result);916skipSeparationSpace(state, true, -1);917918ch = state.input.charCodeAt(state.position);919920if ((state.line === _line || state.lineIndent > nodeIndent) && (0 !== ch)) {921throwError(state, 'bad indentation of a sequence entry');922} else if (state.lineIndent < nodeIndent) {923break;924}925}926927if (detected) {928state.tag = _tag;929state.anchor = _anchor;930state.kind = 'sequence';931state.result = _result;932return true;933}934return false;935}936937function readBlockMapping(state, nodeIndent, flowIndent) {938var following,939allowCompact,940_line,941_tag = state.tag,942_anchor = state.anchor,943_result = {},944keyTag = null,945keyNode = null,946valueNode = null,947atExplicitKey = false,948detected = false,949ch;950951if (null !== state.anchor) {952state.anchorMap[state.anchor] = _result;953}954955ch = state.input.charCodeAt(state.position);956957while (0 !== ch) {958following = state.input.charCodeAt(state.position + 1);959_line = state.line; // Save the current line.960961//962// Explicit notation case. There are two separate blocks:963// first for the key (denoted by "?") and second for the value (denoted by ":")964//965if ((0x3F/* ? */ === ch || 0x3A/* : */ === ch) && is_WS_OR_EOL(following)) {966967if (0x3F/* ? */ === ch) {968if (atExplicitKey) {969storeMappingPair(state, _result, keyTag, keyNode, null);970keyTag = keyNode = valueNode = null;971}972973detected = true;974atExplicitKey = true;975allowCompact = true;976977} else if (atExplicitKey) {978// i.e. 0x3A/* : */ === character after the explicit key.979atExplicitKey = false;980allowCompact = true;981982} else {983throwError(state, 'incomplete explicit mapping pair; a key node is missed');984}985986state.position += 1;987ch = following;988989//990// Implicit notation case. Flow-style node as the key first, then ":", and the value.991//992} else if (composeNode(state, flowIndent, CONTEXT_FLOW_OUT, false, true)) {993994if (state.line === _line) {995ch = state.input.charCodeAt(state.position);996997while (is_WHITE_SPACE(ch)) {998ch = state.input.charCodeAt(++state.position);999}10001001if (0x3A/* : */ === ch) {1002ch = state.input.charCodeAt(++state.position);10031004if (!is_WS_OR_EOL(ch)) {1005throwError(state, 'a whitespace character is expected after the key-value separator within a block mapping');1006}10071008if (atExplicitKey) {1009storeMappingPair(state, _result, keyTag, keyNode, null);1010keyTag = keyNode = valueNode = null;1011}10121013detected = true;1014atExplicitKey = false;1015allowCompact = false;1016keyTag = state.tag;1017keyNode = state.result;10181019} else if (detected) {1020throwError(state, 'can not read an implicit mapping pair; a colon is missed');10211022} else {1023state.tag = _tag;1024state.anchor = _anchor;1025return true; // Keep the result of `composeNode`.1026}10271028} else if (detected) {1029throwError(state, 'can not read a block mapping entry; a multiline key may not be an implicit key');10301031} else {1032state.tag = _tag;1033state.anchor = _anchor;1034return true; // Keep the result of `composeNode`.1035}10361037} else {1038break; // Reading is done. Go to the epilogue.1039}10401041//1042// Common reading code for both explicit and implicit notations.1043//1044if (state.line === _line || state.lineIndent > nodeIndent) {1045if (composeNode(state, nodeIndent, CONTEXT_BLOCK_OUT, true, allowCompact)) {1046if (atExplicitKey) {1047keyNode = state.result;1048} else {1049valueNode = state.result;1050}1051}10521053if (!atExplicitKey) {1054storeMappingPair(state, _result, keyTag, keyNode, valueNode);1055keyTag = keyNode = valueNode = null;1056}10571058skipSeparationSpace(state, true, -1);1059ch = state.input.charCodeAt(state.position);1060}10611062if (state.lineIndent > nodeIndent && (0 !== ch)) {1063throwError(state, 'bad indentation of a mapping entry');1064} else if (state.lineIndent < nodeIndent) {1065break;1066}1067}10681069//1070// Epilogue.1071//10721073// Special case: last mapping's node contains only the key in explicit notation.1074if (atExplicitKey) {1075storeMappingPair(state, _result, keyTag, keyNode, null);1076}10771078// Expose the resulting mapping.1079if (detected) {1080state.tag = _tag;1081state.anchor = _anchor;1082state.kind = 'mapping';1083state.result = _result;1084}10851086return detected;1087}10881089function readTagProperty(state) {1090var _position,1091isVerbatim = false,1092isNamed = false,1093tagHandle,1094tagName,1095ch;10961097ch = state.input.charCodeAt(state.position);10981099if (0x21/* ! */ !== ch) {1100return false;1101}11021103if (null !== state.tag) {1104throwError(state, 'duplication of a tag property');1105}11061107ch = state.input.charCodeAt(++state.position);11081109if (0x3C/* < */ === ch) {1110isVerbatim = true;1111ch = state.input.charCodeAt(++state.position);11121113} else if (0x21/* ! */ === ch) {1114isNamed = true;1115tagHandle = '!!';1116ch = state.input.charCodeAt(++state.position);11171118} else {1119tagHandle = '!';1120}11211122_position = state.position;11231124if (isVerbatim) {1125do { ch = state.input.charCodeAt(++state.position); }1126while (0 !== ch && 0x3E/* > */ !== ch);11271128if (state.position < state.length) {1129tagName = state.input.slice(_position, state.position);1130ch = state.input.charCodeAt(++state.position);1131} else {1132throwError(state, 'unexpected end of the stream within a verbatim tag');1133}1134} else {1135while (0 !== ch && !is_WS_OR_EOL(ch)) {11361137if (0x21/* ! */ === ch) {1138if (!isNamed) {1139tagHandle = state.input.slice(_position - 1, state.position + 1);11401141if (!PATTERN_TAG_HANDLE.test(tagHandle)) {1142throwError(state, 'named tag handle cannot contain such characters');1143}11441145isNamed = true;1146_position = state.position + 1;1147} else {1148throwError(state, 'tag suffix cannot contain exclamation marks');1149}1150}11511152ch = state.input.charCodeAt(++state.position);1153}11541155tagName = state.input.slice(_position, state.position);11561157if (PATTERN_FLOW_INDICATORS.test(tagName)) {1158throwError(state, 'tag suffix cannot contain flow indicator characters');1159}1160}11611162if (tagName && !PATTERN_TAG_URI.test(tagName)) {1163throwError(state, 'tag name cannot contain such characters: ' + tagName);1164}11651166if (isVerbatim) {1167state.tag = tagName;11681169} else if (_hasOwnProperty.call(state.tagMap, tagHandle)) {1170state.tag = state.tagMap[tagHandle] + tagName;11711172} else if ('!' === tagHandle) {1173state.tag = '!' + tagName;11741175} else if ('!!' === tagHandle) {1176state.tag = 'tag:yaml.org,2002:' + tagName;11771178} else {1179throwError(state, 'undeclared tag handle "' + tagHandle + '"');1180}11811182return true;1183}11841185function readAnchorProperty(state) {1186var _position,1187ch;11881189ch = state.input.charCodeAt(state.position);11901191if (0x26/* & */ !== ch) {1192return false;1193}11941195if (null !== state.anchor) {1196throwError(state, 'duplication of an anchor property');1197}11981199ch = state.input.charCodeAt(++state.position);1200_position = state.position;12011202while (0 !== ch && !is_WS_OR_EOL(ch) && !is_FLOW_INDICATOR(ch)) {1203ch = state.input.charCodeAt(++state.position);1204}12051206if (state.position === _position) {1207throwError(state, 'name of an anchor node must contain at least one character');1208}12091210state.anchor = state.input.slice(_position, state.position);1211return true;1212}12131214function readAlias(state) {1215var _position, alias,1216len = state.length,1217input = state.input,1218ch;12191220ch = state.input.charCodeAt(state.position);12211222if (0x2A/* * */ !== ch) {1223return false;1224}12251226ch = state.input.charCodeAt(++state.position);1227_position = state.position;12281229while (0 !== ch && !is_WS_OR_EOL(ch) && !is_FLOW_INDICATOR(ch)) {1230ch = state.input.charCodeAt(++state.position);1231}12321233if (state.position === _position) {1234throwError(state, 'name of an alias node must contain at least one character');1235}12361237alias = state.input.slice(_position, state.position);12381239if (!state.anchorMap.hasOwnProperty(alias)) {1240throwError(state, 'unidentified alias "' + alias + '"');1241}12421243state.result = state.anchorMap[alias];1244skipSeparationSpace(state, true, -1);1245return true;1246}12471248function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact) {1249var allowBlockStyles,1250allowBlockScalars,1251allowBlockCollections,1252indentStatus = 1, // 1: this>parent, 0: this=parent, -1: this<parent1253atNewLine = false,1254hasContent = false,1255typeIndex,1256typeQuantity,1257type,1258flowIndent,1259blockIndent,1260_result;12611262state.tag = null;1263state.anchor = null;1264state.kind = null;1265state.result = null;12661267allowBlockStyles = allowBlockScalars = allowBlockCollections =1268CONTEXT_BLOCK_OUT === nodeContext ||1269CONTEXT_BLOCK_IN === nodeContext;12701271if (allowToSeek) {1272if (skipSeparationSpace(state, true, -1)) {1273atNewLine = true;12741275if (state.lineIndent > parentIndent) {1276indentStatus = 1;1277} else if (state.lineIndent === parentIndent) {1278indentStatus = 0;1279} else if (state.lineIndent < parentIndent) {1280indentStatus = -1;1281}1282}1283}12841285if (1 === indentStatus) {1286while (readTagProperty(state) || readAnchorProperty(state)) {1287if (skipSeparationSpace(state, true, -1)) {1288atNewLine = true;1289allowBlockCollections = allowBlockStyles;12901291if (state.lineIndent > parentIndent) {1292indentStatus = 1;1293} else if (state.lineIndent === parentIndent) {1294indentStatus = 0;1295} else if (state.lineIndent < parentIndent) {1296indentStatus = -1;1297}1298} else {1299allowBlockCollections = false;1300}1301}1302}13031304if (allowBlockCollections) {1305allowBlockCollections = atNewLine || allowCompact;1306}13071308if (1 === indentStatus || CONTEXT_BLOCK_OUT === nodeContext) {1309if (CONTEXT_FLOW_IN === nodeContext || CONTEXT_FLOW_OUT === nodeContext) {1310flowIndent = parentIndent;1311} else {1312flowIndent = parentIndent + 1;1313}13141315blockIndent = state.position - state.lineStart;13161317if (1 === indentStatus) {1318if (allowBlockCollections &&1319(readBlockSequence(state, blockIndent) ||1320readBlockMapping(state, blockIndent, flowIndent)) ||1321readFlowCollection(state, flowIndent)) {1322hasContent = true;1323} else {1324if ((allowBlockScalars && readBlockScalar(state, flowIndent)) ||1325readSingleQuotedScalar(state, flowIndent) ||1326readDoubleQuotedScalar(state, flowIndent)) {1327hasContent = true;13281329} else if (readAlias(state)) {1330hasContent = true;13311332if (null !== state.tag || null !== state.anchor) {1333throwError(state, 'alias node should not have any properties');1334}13351336} else if (readPlainScalar(state, flowIndent, CONTEXT_FLOW_IN === nodeContext)) {1337hasContent = true;13381339if (null === state.tag) {1340state.tag = '?';1341}1342}13431344if (null !== state.anchor) {1345state.anchorMap[state.anchor] = state.result;1346}1347}1348} else if (0 === indentStatus) {1349// Special case: block sequences are allowed to have same indentation level as the parent.1350// http://www.yaml.org/spec/1.2/spec.html#id27997841351hasContent = allowBlockCollections && readBlockSequence(state, blockIndent);1352}1353}13541355if (null !== state.tag && '!' !== state.tag) {1356if ('?' === state.tag) {1357for (typeIndex = 0, typeQuantity = state.implicitTypes.length;1358typeIndex < typeQuantity;1359typeIndex += 1) {1360type = state.implicitTypes[typeIndex];13611362// Implicit resolving is not allowed for non-scalar types, and '?'1363// non-specific tag is only assigned to plain scalars. So, it isn't1364// needed to check for 'kind' conformity.13651366if (type.resolve(state.result)) { // `state.result` updated in resolver if matched1367state.result = type.construct(state.result);1368state.tag = type.tag;1369if (null !== state.anchor) {1370state.anchorMap[state.anchor] = state.result;1371}1372break;1373}1374}1375} else if (_hasOwnProperty.call(state.typeMap, state.tag)) {1376type = state.typeMap[state.tag];13771378if (null !== state.result && type.kind !== state.kind) {1379throwError(state, 'unacceptable node kind for !<' + state.tag + '> tag; it should be "' + type.kind + '", not "' + state.kind + '"');1380}13811382if (!type.resolve(state.result)) { // `state.result` updated in resolver if matched1383throwError(state, 'cannot resolve a node with !<' + state.tag + '> explicit tag');1384} else {1385state.result = type.construct(state.result);1386if (null !== state.anchor) {1387state.anchorMap[state.anchor] = state.result;1388}1389}1390} else {1391throwWarning(state, 'unknown tag !<' + state.tag + '>');1392}1393}13941395return null !== state.tag || null !== state.anchor || hasContent;1396}13971398function readDocument(state) {1399var documentStart = state.position,1400_position,1401directiveName,1402directiveArgs,1403hasDirectives = false,1404ch;14051406state.version = null;1407state.checkLineBreaks = state.legacy;1408state.tagMap = {};1409state.anchorMap = {};14101411while (0 !== (ch = state.input.charCodeAt(state.position))) {1412skipSeparationSpace(state, true, -1);14131414ch = state.input.charCodeAt(state.position);14151416if (state.lineIndent > 0 || 0x25/* % */ !== ch) {1417break;1418}14191420hasDirectives = true;1421ch = state.input.charCodeAt(++state.position);1422_position = state.position;14231424while (0 !== ch && !is_WS_OR_EOL(ch)) {1425ch = state.input.charCodeAt(++state.position);1426}14271428directiveName = state.input.slice(_position, state.position);1429directiveArgs = [];14301431if (directiveName.length < 1) {1432throwError(state, 'directive name must not be less than one character in length');1433}14341435while (0 !== ch) {1436while (is_WHITE_SPACE(ch)) {1437ch = state.input.charCodeAt(++state.position);1438}14391440if (0x23/* # */ === ch) {1441do { ch = state.input.charCodeAt(++state.position); }1442while (0 !== ch && !is_EOL(ch));1443break;1444}14451446if (is_EOL(ch)) {1447break;1448}14491450_position = state.position;14511452while (0 !== ch && !is_WS_OR_EOL(ch)) {1453ch = state.input.charCodeAt(++state.position);1454}14551456directiveArgs.push(state.input.slice(_position, state.position));1457}14581459if (0 !== ch) {1460readLineBreak(state);1461}14621463if (_hasOwnProperty.call(directiveHandlers, directiveName)) {1464directiveHandlers[directiveName](state, directiveName, directiveArgs);1465} else {1466throwWarning(state, 'unknown document directive "' + directiveName + '"');1467}1468}14691470skipSeparationSpace(state, true, -1);14711472if (0 === state.lineIndent &&14730x2D/* - */ === state.input.charCodeAt(state.position) &&14740x2D/* - */ === state.input.charCodeAt(state.position + 1) &&14750x2D/* - */ === state.input.charCodeAt(state.position + 2)) {1476state.position += 3;1477skipSeparationSpace(state, true, -1);14781479} else if (hasDirectives) {1480throwError(state, 'directives end mark is expected');1481}14821483composeNode(state, state.lineIndent - 1, CONTEXT_BLOCK_OUT, false, true);1484skipSeparationSpace(state, true, -1);14851486if (state.checkLineBreaks &&1487PATTERN_NON_ASCII_LINE_BREAKS.test(state.input.slice(documentStart, state.position))) {1488throwWarning(state, 'non-ASCII line breaks are interpreted as content');1489}14901491state.documents.push(state.result);14921493if (state.position === state.lineStart && testDocumentSeparator(state)) {14941495if (0x2E/* . */ === state.input.charCodeAt(state.position)) {1496state.position += 3;1497skipSeparationSpace(state, true, -1);1498}1499return;1500}15011502if (state.position < (state.length - 1)) {1503throwError(state, 'end of the stream or a document separator is expected');1504} else {1505return;1506}1507}150815091510function loadDocuments(input, options) {1511input = String(input);1512options = options || {};15131514if (input.length !== 0) {15151516// Add tailing `\n` if not exists1517if (0x0A/* LF */ !== input.charCodeAt(input.length - 1) &&15180x0D/* CR */ !== input.charCodeAt(input.length - 1)) {1519input += '\n';1520}15211522// Strip BOM1523if (input.charCodeAt(0) === 0xFEFF) {1524input = input.slice(1);1525}1526}15271528var state = new State(input, options);15291530if (PATTERN_NON_PRINTABLE.test(state.input)) {1531throwError(state, 'the stream contains non-printable characters');1532}15331534// Use 0 as string terminator. That significantly simplifies bounds check.1535state.input += '\0';15361537while (0x20/* Space */ === state.input.charCodeAt(state.position)) {1538state.lineIndent += 1;1539state.position += 1;1540}15411542while (state.position < (state.length - 1)) {1543readDocument(state);1544}15451546return state.documents;1547}154815491550function loadAll(input, iterator, options) {1551var documents = loadDocuments(input, options), index, length;15521553for (index = 0, length = documents.length; index < length; index += 1) {1554iterator(documents[index]);1555}1556}155715581559function load(input, options) {1560var documents = loadDocuments(input, options), index, length;15611562if (0 === documents.length) {1563/*eslint-disable no-undefined*/1564return undefined;1565} else if (1 === documents.length) {1566return documents[0];1567}1568throw new YAMLException('expected a single document in the stream, but found more');1569}157015711572function safeLoadAll(input, output, options) {1573loadAll(input, output, common.extend({ schema: DEFAULT_SAFE_SCHEMA }, options));1574}157515761577function safeLoad(input, options) {1578return load(input, common.extend({ schema: DEFAULT_SAFE_SCHEMA }, options));1579}158015811582module.exports.loadAll = loadAll;1583module.exports.load = load;1584module.exports.safeLoadAll = safeLoadAll;1585module.exports.safeLoad = safeLoad;158615871588