diff options
author | Jeremy Faivre <contact@jeremyfa.com> | 2019-09-17 00:38:17 +0200 |
---|---|---|
committer | Jeremy Faivre <contact@jeremyfa.com> | 2019-09-17 00:38:17 +0200 |
commit | bad40c73962a62521b7f9a46f2e05b20e7c8cb0b (patch) | |
tree | 569cead7324d4a4ffd637d62fabcee91261ef009 /lib/Inline.js | |
parent | 0bbb7075908e5aead75170e5e8b8c9f6e326e6f0 (diff) | |
download | yamljs-bad40c73962a62521b7f9a46f2e05b20e7c8cb0b.tar.gz |
Update compiled files
Diffstat (limited to 'lib/Inline.js')
-rw-r--r-- | lib/Inline.js | 883 |
1 files changed, 479 insertions, 404 deletions
diff --git a/lib/Inline.js b/lib/Inline.js index aaf980b..b3995fa 100644 --- a/lib/Inline.js +++ b/lib/Inline.js @@ -1,6 +1,6 @@ -// Generated by CoffeeScript 1.12.4 +// Generated by CoffeeScript 2.4.1 var DumpException, Escaper, Inline, ParseException, ParseMore, Pattern, Unescaper, Utils, - indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + indexOf = [].indexOf; Pattern = require('./Pattern'); @@ -17,469 +17,544 @@ ParseMore = require('./Exception/ParseMore'); DumpException = require('./Exception/DumpException'); Inline = (function() { - function Inline() {} + // Inline YAML parsing and dumping + class Inline { + // Configure YAML inline. - Inline.REGEX_QUOTED_STRING = '(?:"(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\']*(?:\'\'[^\']*)*)\')'; + // @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types (a JavaScript resource or object), false otherwise + // @param [Function] objectDecoder A function to deserialize custom objects, null otherwise - Inline.PATTERN_TRAILING_COMMENTS = new Pattern('^\\s*#.*$'); + static configure(exceptionOnInvalidType = null, objectDecoder = null) { + // Update settings + this.settings.exceptionOnInvalidType = exceptionOnInvalidType; + this.settings.objectDecoder = objectDecoder; + } - Inline.PATTERN_QUOTED_SCALAR = new Pattern('^' + Inline.REGEX_QUOTED_STRING); + // Converts a YAML string to a JavaScript object. - Inline.PATTERN_THOUSAND_NUMERIC_SCALAR = new Pattern('^(-|\\+)?[0-9,]+(\\.[0-9]+)?$'); + // @param [String] value A YAML string + // @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types (a JavaScript resource or object), false otherwise + // @param [Function] objectDecoder A function to deserialize custom objects, null otherwise - Inline.PATTERN_SCALAR_BY_DELIMITERS = {}; + // @return [Object] A JavaScript object representing the YAML string - Inline.settings = {}; + // @throw [ParseException] - Inline.configure = function(exceptionOnInvalidType, objectDecoder) { - if (exceptionOnInvalidType == null) { - exceptionOnInvalidType = null; - } - if (objectDecoder == null) { - objectDecoder = null; + static parse(value, exceptionOnInvalidType = false, objectDecoder = null) { + var context, result; + // Update settings from last call of Inline.parse() + this.settings.exceptionOnInvalidType = exceptionOnInvalidType; + this.settings.objectDecoder = objectDecoder; + if (value == null) { + return ''; + } + value = Utils.trim(value); + if (0 === value.length) { + return ''; + } + // Keep a context object to pass through static methods + context = { + exceptionOnInvalidType, + objectDecoder, + i: 0 + }; + switch (value.charAt(0)) { + case '[': + result = this.parseSequence(value, context); + ++context.i; + break; + case '{': + result = this.parseMapping(value, context); + ++context.i; + break; + default: + result = this.parseScalar(value, null, ['"', "'"], context); + } + // Some comments are allowed at the end + if (this.PATTERN_TRAILING_COMMENTS.replace(value.slice(context.i), '') !== '') { + throw new ParseException('Unexpected characters near "' + value.slice(context.i) + '".'); + } + return result; } - this.settings.exceptionOnInvalidType = exceptionOnInvalidType; - this.settings.objectDecoder = objectDecoder; - }; - Inline.parse = function(value, exceptionOnInvalidType, objectDecoder) { - var context, result; - if (exceptionOnInvalidType == null) { - exceptionOnInvalidType = false; - } - if (objectDecoder == null) { - objectDecoder = null; - } - this.settings.exceptionOnInvalidType = exceptionOnInvalidType; - this.settings.objectDecoder = objectDecoder; - if (value == null) { - return ''; - } - value = Utils.trim(value); - if (0 === value.length) { - return ''; - } - context = { - exceptionOnInvalidType: exceptionOnInvalidType, - objectDecoder: objectDecoder, - i: 0 - }; - switch (value.charAt(0)) { - case '[': - result = this.parseSequence(value, context); - ++context.i; - break; - case '{': - result = this.parseMapping(value, context); - ++context.i; - break; - default: - result = this.parseScalar(value, null, ['"', "'"], context); - } - if (this.PATTERN_TRAILING_COMMENTS.replace(value.slice(context.i), '') !== '') { - throw new ParseException('Unexpected characters near "' + value.slice(context.i) + '".'); - } - return result; - }; + // Dumps a given JavaScript variable to a YAML string. - Inline.dump = function(value, exceptionOnInvalidType, objectEncoder) { - var ref, result, type; - if (exceptionOnInvalidType == null) { - exceptionOnInvalidType = false; - } - if (objectEncoder == null) { - objectEncoder = null; - } - if (value == null) { - return 'null'; - } - type = typeof value; - if (type === 'object') { - if (value instanceof Date) { - return value.toISOString(); - } else if (objectEncoder != null) { - result = objectEncoder(value); - if (typeof result === 'string' || (result != null)) { - return result; + // @param [Object] value The JavaScript variable to convert + // @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types (a JavaScript resource or object), false otherwise + // @param [Function] objectEncoder A function to serialize custom objects, null otherwise + + // @return [String] The YAML string representing the JavaScript object + + // @throw [DumpException] + + static dump(value, exceptionOnInvalidType = false, objectEncoder = null) { + var ref, result, type; + if (value == null) { + return 'null'; + } + type = typeof value; + if (type === 'object') { + if (value instanceof Date) { + return value.toISOString(); + } else if (objectEncoder != null) { + result = objectEncoder(value); + if (typeof result === 'string' || (result != null)) { + return result; + } } + return this.dumpObject(value); } - return this.dumpObject(value); - } - if (type === 'boolean') { - return (value ? 'true' : 'false'); - } - if (Utils.isDigits(value)) { - return (type === 'string' ? "'" + value + "'" : String(parseInt(value))); - } - if (Utils.isNumeric(value)) { - return (type === 'string' ? "'" + value + "'" : String(parseFloat(value))); - } - if (type === 'number') { - return (value === 2e308 ? '.Inf' : (value === -2e308 ? '-.Inf' : (isNaN(value) ? '.NaN' : value))); - } - if (Escaper.requiresDoubleQuoting(value)) { - return Escaper.escapeWithDoubleQuotes(value); - } - if (Escaper.requiresSingleQuoting(value)) { - return Escaper.escapeWithSingleQuotes(value); - } - if ('' === value) { - return '""'; - } - if (Utils.PATTERN_DATE.test(value)) { - return "'" + value + "'"; - } - if ((ref = value.toLowerCase()) === 'null' || ref === '~' || ref === 'true' || ref === 'false') { - return "'" + value + "'"; - } - return value; - }; - - Inline.dumpObject = function(value, exceptionOnInvalidType, objectSupport) { - var j, key, len1, output, val; - if (objectSupport == null) { - objectSupport = null; - } - if (value instanceof Array) { - output = []; - for (j = 0, len1 = value.length; j < len1; j++) { - val = value[j]; - output.push(this.dump(val)); + if (type === 'boolean') { + return (value ? 'true' : 'false'); } - return '[' + output.join(', ') + ']'; - } else { - output = []; - for (key in value) { - val = value[key]; - output.push(this.dump(key) + ': ' + this.dump(val)); + if (Utils.isDigits(value)) { + return (type === 'string' ? "'" + value + "'" : String(parseInt(value))); + } + if (Utils.isNumeric(value)) { + return (type === 'string' ? "'" + value + "'" : String(parseFloat(value))); + } + if (type === 'number') { + return (value === 2e308 ? '.Inf' : (value === -2e308 ? '-.Inf' : (isNaN(value) ? '.NaN' : value))); + } + if (Escaper.requiresDoubleQuoting(value)) { + return Escaper.escapeWithDoubleQuotes(value); + } + if (Escaper.requiresSingleQuoting(value)) { + return Escaper.escapeWithSingleQuotes(value); + } + if ('' === value) { + return '""'; } - return '{' + output.join(', ') + '}'; + if (Utils.PATTERN_DATE.test(value)) { + return "'" + value + "'"; + } + if ((ref = value.toLowerCase()) === 'null' || ref === '~' || ref === 'true' || ref === 'false') { + return "'" + value + "'"; + } + // Default + return value; } - }; - Inline.parseScalar = function(scalar, delimiters, stringDelimiters, context, evaluate) { - var i, joinedDelimiters, match, output, pattern, ref, ref1, strpos, tmp; - if (delimiters == null) { - delimiters = null; - } - if (stringDelimiters == null) { - stringDelimiters = ['"', "'"]; - } - if (context == null) { - context = null; - } - if (evaluate == null) { - evaluate = true; - } - if (context == null) { - context = { - exceptionOnInvalidType: this.settings.exceptionOnInvalidType, - objectDecoder: this.settings.objectDecoder, - i: 0 - }; - } - i = context.i; - if (ref = scalar.charAt(i), indexOf.call(stringDelimiters, ref) >= 0) { - output = this.parseQuotedScalar(scalar, context); - i = context.i; - if (delimiters != null) { - tmp = Utils.ltrim(scalar.slice(i), ' '); - if (!(ref1 = tmp.charAt(0), indexOf.call(delimiters, ref1) >= 0)) { - throw new ParseException('Unexpected characters (' + scalar.slice(i) + ').'); + static dumpObject(value, exceptionOnInvalidType, objectSupport = null) { + var j, key, len1, output, val; + // Array + if (value instanceof Array) { + output = []; + for (j = 0, len1 = value.length; j < len1; j++) { + val = value[j]; + output.push(this.dump(val)); + } + return '[' + output.join(', ') + ']'; + } else { + // Mapping + output = []; + for (key in value) { + val = value[key]; + output.push(this.dump(key) + ': ' + this.dump(val)); } + return '{' + output.join(', ') + '}'; } - } else { - if (!delimiters) { - output = scalar.slice(i); - i += output.length; - strpos = output.indexOf(' #'); - if (strpos !== -1) { - output = Utils.rtrim(output.slice(0, strpos)); + } + + // Parses a scalar to a YAML string. + + // @param [Object] scalar + // @param [Array] delimiters + // @param [Array] stringDelimiters + // @param [Object] context + // @param [Boolean] evaluate + + // @return [String] A YAML string + + // @throw [ParseException] When malformed inline YAML string is parsed + + static parseScalar(scalar, delimiters = null, stringDelimiters = ['"', "'"], context = null, evaluate = true) { + var i, joinedDelimiters, match, output, pattern, ref, ref1, strpos, tmp; + if (context == null) { + context = { + exceptionOnInvalidType: this.settings.exceptionOnInvalidType, + objectDecoder: this.settings.objectDecoder, + i: 0 + }; + } + ({i} = context); + if (ref = scalar.charAt(i), indexOf.call(stringDelimiters, ref) >= 0) { + // Quoted scalar + output = this.parseQuotedScalar(scalar, context); + ({i} = context); + if (delimiters != null) { + tmp = Utils.ltrim(scalar.slice(i), ' '); + if (!(ref1 = tmp.charAt(0), indexOf.call(delimiters, ref1) >= 0)) { + throw new ParseException('Unexpected characters (' + scalar.slice(i) + ').'); + } } } else { - joinedDelimiters = delimiters.join('|'); - pattern = this.PATTERN_SCALAR_BY_DELIMITERS[joinedDelimiters]; - if (pattern == null) { - pattern = new Pattern('^(.+?)(' + joinedDelimiters + ')'); - this.PATTERN_SCALAR_BY_DELIMITERS[joinedDelimiters] = pattern; - } - if (match = pattern.exec(scalar.slice(i))) { - output = match[1]; + // "normal" string + if (!delimiters) { + output = scalar.slice(i); i += output.length; + // Remove comments + strpos = output.indexOf(' #'); + if (strpos !== -1) { + output = Utils.rtrim(output.slice(0, strpos)); + } } else { - throw new ParseException('Malformed inline YAML string (' + scalar + ').'); + joinedDelimiters = delimiters.join('|'); + pattern = this.PATTERN_SCALAR_BY_DELIMITERS[joinedDelimiters]; + if (pattern == null) { + pattern = new Pattern('^(.+?)(' + joinedDelimiters + ')'); + this.PATTERN_SCALAR_BY_DELIMITERS[joinedDelimiters] = pattern; + } + if (match = pattern.exec(scalar.slice(i))) { + output = match[1]; + i += output.length; + } else { + throw new ParseException('Malformed inline YAML string (' + scalar + ').'); + } + } + if (evaluate) { + output = this.evaluateScalar(output, context); } } - if (evaluate) { - output = this.evaluateScalar(output, context); - } + context.i = i; + return output; } - context.i = i; - return output; - }; - Inline.parseQuotedScalar = function(scalar, context) { - var i, match, output; - i = context.i; - if (!(match = this.PATTERN_QUOTED_SCALAR.exec(scalar.slice(i)))) { - throw new ParseMore('Malformed inline YAML string (' + scalar.slice(i) + ').'); - } - output = match[0].substr(1, match[0].length - 2); - if ('"' === scalar.charAt(i)) { - output = Unescaper.unescapeDoubleQuotedString(output); - } else { - output = Unescaper.unescapeSingleQuotedString(output); - } - i += match[0].length; - context.i = i; - return output; - }; + // Parses a quoted scalar to YAML. - Inline.parseSequence = function(sequence, context) { - var e, i, isQuoted, len, output, ref, value; - output = []; - len = sequence.length; - i = context.i; - i += 1; - while (i < len) { - context.i = i; - switch (sequence.charAt(i)) { - case '[': - output.push(this.parseSequence(sequence, context)); - i = context.i; - break; - case '{': - output.push(this.parseMapping(sequence, context)); - i = context.i; - break; - case ']': - return output; - case ',': - case ' ': - case "\n": - break; - default: - isQuoted = ((ref = sequence.charAt(i)) === '"' || ref === "'"); - value = this.parseScalar(sequence, [',', ']'], ['"', "'"], context); - i = context.i; - if (!isQuoted && typeof value === 'string' && (value.indexOf(': ') !== -1 || value.indexOf(":\n") !== -1)) { - try { - value = this.parseMapping('{' + value + '}'); - } catch (error) { - e = error; - } - } - output.push(value); - --i; - } - ++i; - } - throw new ParseMore('Malformed inline YAML string ' + sequence); - }; + // @param [String] scalar + // @param [Object] context - Inline.parseMapping = function(mapping, context) { - var done, i, key, len, output, shouldContinueWhileLoop, value; - output = {}; - len = mapping.length; - i = context.i; - i += 1; - shouldContinueWhileLoop = false; - while (i < len) { - context.i = i; - switch (mapping.charAt(i)) { - case ' ': - case ',': - case "\n": - ++i; - context.i = i; - shouldContinueWhileLoop = true; - break; - case '}': - return output; + // @return [String] A YAML string + + // @throw [ParseMore] When malformed inline YAML string is parsed + + static parseQuotedScalar(scalar, context) { + var i, match, output; + ({i} = context); + if (!(match = this.PATTERN_QUOTED_SCALAR.exec(scalar.slice(i)))) { + throw new ParseMore('Malformed inline YAML string (' + scalar.slice(i) + ').'); } - if (shouldContinueWhileLoop) { - shouldContinueWhileLoop = false; - continue; + output = match[0].substr(1, match[0].length - 2); + if ('"' === scalar.charAt(i)) { + output = Unescaper.unescapeDoubleQuotedString(output); + } else { + output = Unescaper.unescapeSingleQuotedString(output); } - key = this.parseScalar(mapping, [':', ' ', "\n"], ['"', "'"], context, false); - i = context.i; - done = false; + i += match[0].length; + context.i = i; + return output; + } + + // Parses a sequence to a YAML string. + + // @param [String] sequence + // @param [Object] context + + // @return [String] A YAML string + + // @throw [ParseMore] When malformed inline YAML string is parsed + + static parseSequence(sequence, context) { + var e, i, isQuoted, len, output, ref, value; + output = []; + len = sequence.length; + ({i} = context); + i += 1; + // [foo, bar, ...] while (i < len) { context.i = i; - switch (mapping.charAt(i)) { + switch (sequence.charAt(i)) { case '[': - value = this.parseSequence(mapping, context); - i = context.i; - if (output[key] === void 0) { - output[key] = value; - } - done = true; + // Nested sequence + output.push(this.parseSequence(sequence, context)); + ({i} = context); break; case '{': - value = this.parseMapping(mapping, context); - i = context.i; - if (output[key] === void 0) { - output[key] = value; - } - done = true; + // Nested mapping + output.push(this.parseMapping(sequence, context)); + ({i} = context); break; - case ':': + case ']': + return output; + case ',': case ' ': case "\n": break; default: - value = this.parseScalar(mapping, [',', '}'], ['"', "'"], context); - i = context.i; - if (output[key] === void 0) { - output[key] = value; + // Do nothing + isQuoted = ((ref = sequence.charAt(i)) === '"' || ref === "'"); + value = this.parseScalar(sequence, [',', ']'], ['"', "'"], context); + ({i} = context); + if (!isQuoted && typeof value === 'string' && (value.indexOf(': ') !== -1 || value.indexOf(":\n") !== -1)) { + try { + // Embedded mapping? + value = this.parseMapping('{' + value + '}'); + } catch (error) { + e = error; + } } - done = true; + // No, it's not + output.push(value); --i; } ++i; - if (done) { - break; - } } + throw new ParseMore('Malformed inline YAML string ' + sequence); } - throw new ParseMore('Malformed inline YAML string ' + mapping); - }; - Inline.evaluateScalar = function(scalar, context) { - var cast, date, exceptionOnInvalidType, firstChar, firstSpace, firstWord, objectDecoder, raw, scalarLower, subValue, trimmedScalar; - scalar = Utils.trim(scalar); - scalarLower = scalar.toLowerCase(); - switch (scalarLower) { - case 'null': - case '': - case '~': - return null; - case 'true': - return true; - case 'false': - return false; - case '.inf': - return 2e308; - case '.nan': - return 0/0; - case '-.inf': - return 2e308; - default: - firstChar = scalarLower.charAt(0); - switch (firstChar) { - case '!': - firstSpace = scalar.indexOf(' '); - if (firstSpace === -1) { - firstWord = scalarLower; - } else { - firstWord = scalarLower.slice(0, firstSpace); - } - switch (firstWord) { - case '!': - if (firstSpace !== -1) { - return parseInt(this.parseScalar(scalar.slice(2))); - } - return null; - case '!str': - return Utils.ltrim(scalar.slice(4)); - case '!!str': - return Utils.ltrim(scalar.slice(5)); - case '!!int': - return parseInt(this.parseScalar(scalar.slice(5))); - case '!!bool': - return Utils.parseBoolean(this.parseScalar(scalar.slice(6)), false); - case '!!float': - return parseFloat(this.parseScalar(scalar.slice(7))); - case '!!timestamp': - return Utils.stringToDate(Utils.ltrim(scalar.slice(11))); - default: - if (context == null) { - context = { - exceptionOnInvalidType: this.settings.exceptionOnInvalidType, - objectDecoder: this.settings.objectDecoder, - i: 0 - }; - } - objectDecoder = context.objectDecoder, exceptionOnInvalidType = context.exceptionOnInvalidType; - if (objectDecoder) { - trimmedScalar = Utils.rtrim(scalar); - firstSpace = trimmedScalar.indexOf(' '); - if (firstSpace === -1) { - return objectDecoder(trimmedScalar, null); - } else { - subValue = Utils.ltrim(trimmedScalar.slice(firstSpace + 1)); - if (!(subValue.length > 0)) { - subValue = null; - } - return objectDecoder(trimmedScalar.slice(0, firstSpace), subValue); - } - } - if (exceptionOnInvalidType) { - throw new ParseException('Custom object support when parsing a YAML file has been disabled.'); - } - return null; - } + // Parses a mapping to a YAML string. + + // @param [String] mapping + // @param [Object] context + + // @return [String] A YAML string + + // @throw [ParseMore] When malformed inline YAML string is parsed + + static parseMapping(mapping, context) { + var done, i, key, len, output, shouldContinueWhileLoop, value; + output = {}; + len = mapping.length; + ({i} = context); + i += 1; + // {foo: bar, bar:foo, ...} + shouldContinueWhileLoop = false; + while (i < len) { + context.i = i; + switch (mapping.charAt(i)) { + case ' ': + case ',': + case "\n": + ++i; + context.i = i; + shouldContinueWhileLoop = true; break; - case '0': - if ('0x' === scalar.slice(0, 2)) { - return Utils.hexDec(scalar); - } else if (Utils.isDigits(scalar)) { - return Utils.octDec(scalar); - } else if (Utils.isNumeric(scalar)) { - return parseFloat(scalar); - } else { - return scalar; - } + case '}': + return output; + } + if (shouldContinueWhileLoop) { + shouldContinueWhileLoop = false; + continue; + } + // Key + key = this.parseScalar(mapping, [':', ' ', "\n"], ['"', "'"], context, false); + ({i} = context); + // Value + done = false; + while (i < len) { + context.i = i; + switch (mapping.charAt(i)) { + case '[': + // Nested sequence + value = this.parseSequence(mapping, context); + ({i} = context); + // Spec: Keys MUST be unique; first one wins. + // Parser cannot abort this mapping earlier, since lines + // are processed sequentially. + if (output[key] === void 0) { + output[key] = value; + } + done = true; + break; + case '{': + // Nested mapping + value = this.parseMapping(mapping, context); + ({i} = context); + // Spec: Keys MUST be unique; first one wins. + // Parser cannot abort this mapping earlier, since lines + // are processed sequentially. + if (output[key] === void 0) { + output[key] = value; + } + done = true; + break; + case ':': + case ' ': + case "\n": + break; + default: + // Do nothing + value = this.parseScalar(mapping, [',', '}'], ['"', "'"], context); + ({i} = context); + // Spec: Keys MUST be unique; first one wins. + // Parser cannot abort this mapping earlier, since lines + // are processed sequentially. + if (output[key] === void 0) { + output[key] = value; + } + done = true; + --i; + } + ++i; + if (done) { break; - case '+': - if (Utils.isDigits(scalar)) { - raw = scalar; - cast = parseInt(raw); - if (raw === String(cast)) { - return cast; + } + } + } + throw new ParseMore('Malformed inline YAML string ' + mapping); + } + + // Evaluates scalars and replaces magic values. + + // @param [String] scalar + + // @return [String] A YAML string + + static evaluateScalar(scalar, context) { + var cast, date, exceptionOnInvalidType, firstChar, firstSpace, firstWord, objectDecoder, raw, scalarLower, subValue, trimmedScalar; + scalar = Utils.trim(scalar); + scalarLower = scalar.toLowerCase(); + switch (scalarLower) { + case 'null': + case '': + case '~': + return null; + case 'true': + return true; + case 'false': + return false; + case '.inf': + return 2e308; + case '.nan': + return 0/0; + case '-.inf': + return 2e308; + default: + firstChar = scalarLower.charAt(0); + switch (firstChar) { + case '!': + firstSpace = scalar.indexOf(' '); + if (firstSpace === -1) { + firstWord = scalarLower; } else { - return raw; + firstWord = scalarLower.slice(0, firstSpace); } - } else if (Utils.isNumeric(scalar)) { - return parseFloat(scalar); - } else if (this.PATTERN_THOUSAND_NUMERIC_SCALAR.test(scalar)) { - return parseFloat(scalar.replace(',', '')); - } - return scalar; - case '-': - if (Utils.isDigits(scalar.slice(1))) { - if ('0' === scalar.charAt(1)) { - return -Utils.octDec(scalar.slice(1)); + switch (firstWord) { + case '!': + if (firstSpace !== -1) { + return parseInt(this.parseScalar(scalar.slice(2))); + } + return null; + case '!str': + return Utils.ltrim(scalar.slice(4)); + case '!!str': + return Utils.ltrim(scalar.slice(5)); + case '!!int': + return parseInt(this.parseScalar(scalar.slice(5))); + case '!!bool': + return Utils.parseBoolean(this.parseScalar(scalar.slice(6)), false); + case '!!float': + return parseFloat(this.parseScalar(scalar.slice(7))); + case '!!timestamp': + return Utils.stringToDate(Utils.ltrim(scalar.slice(11))); + default: + if (context == null) { + context = { + exceptionOnInvalidType: this.settings.exceptionOnInvalidType, + objectDecoder: this.settings.objectDecoder, + i: 0 + }; + } + ({objectDecoder, exceptionOnInvalidType} = context); + if (objectDecoder) { + // If objectDecoder function is given, we can do custom decoding of custom types + trimmedScalar = Utils.rtrim(scalar); + firstSpace = trimmedScalar.indexOf(' '); + if (firstSpace === -1) { + return objectDecoder(trimmedScalar, null); + } else { + subValue = Utils.ltrim(trimmedScalar.slice(firstSpace + 1)); + if (!(subValue.length > 0)) { + subValue = null; + } + return objectDecoder(trimmedScalar.slice(0, firstSpace), subValue); + } + } + if (exceptionOnInvalidType) { + throw new ParseException('Custom object support when parsing a YAML file has been disabled.'); + } + return null; + } + break; + case '0': + if ('0x' === scalar.slice(0, 2)) { + return Utils.hexDec(scalar); + } else if (Utils.isDigits(scalar)) { + return Utils.octDec(scalar); + } else if (Utils.isNumeric(scalar)) { + return parseFloat(scalar); } else { - raw = scalar.slice(1); + return scalar; + } + break; + case '+': + if (Utils.isDigits(scalar)) { + raw = scalar; cast = parseInt(raw); if (raw === String(cast)) { - return -cast; + return cast; } else { - return -raw; + return raw; } + } else if (Utils.isNumeric(scalar)) { + return parseFloat(scalar); + } else if (this.PATTERN_THOUSAND_NUMERIC_SCALAR.test(scalar)) { + return parseFloat(scalar.replace(',', '')); } - } else if (Utils.isNumeric(scalar)) { - return parseFloat(scalar); - } else if (this.PATTERN_THOUSAND_NUMERIC_SCALAR.test(scalar)) { - return parseFloat(scalar.replace(',', '')); - } - return scalar; - default: - if (date = Utils.stringToDate(scalar)) { - return date; - } else if (Utils.isNumeric(scalar)) { - return parseFloat(scalar); - } else if (this.PATTERN_THOUSAND_NUMERIC_SCALAR.test(scalar)) { - return parseFloat(scalar.replace(',', '')); - } - return scalar; - } + return scalar; + case '-': + if (Utils.isDigits(scalar.slice(1))) { + if ('0' === scalar.charAt(1)) { + return -Utils.octDec(scalar.slice(1)); + } else { + raw = scalar.slice(1); + cast = parseInt(raw); + if (raw === String(cast)) { + return -cast; + } else { + return -raw; + } + } + } else if (Utils.isNumeric(scalar)) { + return parseFloat(scalar); + } else if (this.PATTERN_THOUSAND_NUMERIC_SCALAR.test(scalar)) { + return parseFloat(scalar.replace(',', '')); + } + return scalar; + default: + if (date = Utils.stringToDate(scalar)) { + return date; + } else if (Utils.isNumeric(scalar)) { + return parseFloat(scalar); + } else if (this.PATTERN_THOUSAND_NUMERIC_SCALAR.test(scalar)) { + return parseFloat(scalar.replace(',', '')); + } + return scalar; + } + } } + }; + // Quoted string regular expression + Inline.REGEX_QUOTED_STRING = '(?:"(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\']*(?:\'\'[^\']*)*)\')'; + + // Pre-compiled patterns + + Inline.PATTERN_TRAILING_COMMENTS = new Pattern('^\\s*#.*$'); + + Inline.PATTERN_QUOTED_SCALAR = new Pattern('^' + Inline.REGEX_QUOTED_STRING); + + Inline.PATTERN_THOUSAND_NUMERIC_SCALAR = new Pattern('^(-|\\+)?[0-9,]+(\\.[0-9]+)?$'); + + Inline.PATTERN_SCALAR_BY_DELIMITERS = {}; + + // Settings + Inline.settings = {}; + return Inline; -})(); +}).call(this); module.exports = Inline; |