From 45aab0b9ea01515f9bf98f30e782296cb3cf6d47 Mon Sep 17 00:00:00 2001 From: Aleksey V Zapparov Date: Wed, 2 Nov 2011 00:39:54 +0100 Subject: [PATCH] Improved examples. Fixed bugs. --- examples/demo.js | 8 - examples/demo.multi.js | 8 - examples/demo.multi.yml | 6 - examples/demo.yml | 52 ------ examples/single.js | 10 ++ examples/single.yml | 174 ++++++++++++++++++++ examples/visionmedia-compat/boolean.yaml | 7 - examples/visionmedia-compat/config.yml | 8 - examples/visionmedia-compat/dates.yaml | 7 - examples/visionmedia-compat/hash.yml | 20 --- examples/visionmedia-compat/list.nested.yml | 8 - examples/visionmedia-compat/list.yml | 4 - examples/visionmedia-compat/null.yaml | 8 - examples/visionmedia-compat/run.js | 19 --- lib/js-yaml/constructor.js | 37 +++-- lib/js-yaml/core.js | 4 +- lib/js-yaml/resolver.js | 2 +- 17 files changed, 214 insertions(+), 168 deletions(-) delete mode 100644 examples/demo.js delete mode 100644 examples/demo.multi.js delete mode 100644 examples/demo.multi.yml delete mode 100644 examples/demo.yml create mode 100644 examples/single.js create mode 100644 examples/single.yml delete mode 100644 examples/visionmedia-compat/boolean.yaml delete mode 100644 examples/visionmedia-compat/config.yml delete mode 100644 examples/visionmedia-compat/dates.yaml delete mode 100644 examples/visionmedia-compat/hash.yml delete mode 100644 examples/visionmedia-compat/list.nested.yml delete mode 100644 examples/visionmedia-compat/list.yml delete mode 100644 examples/visionmedia-compat/null.yaml delete mode 100644 examples/visionmedia-compat/run.js diff --git a/examples/demo.js b/examples/demo.js deleted file mode 100644 index 1d3c8690..00000000 --- a/examples/demo.js +++ /dev/null @@ -1,8 +0,0 @@ -try { - var str = require('fs').readFileSync(__dirname + '/demo.yml', 'utf8'), - yaml = require(__dirname + '/../lib/js-yaml'); - console.log(yaml.loadAll(str).shift()); -} catch (e) { - console.log(e.toString()); - console.log(e.stack); -} diff --git a/examples/demo.multi.js b/examples/demo.multi.js deleted file mode 100644 index 997492bf..00000000 --- a/examples/demo.multi.js +++ /dev/null @@ -1,8 +0,0 @@ -try { - var str = require('fs').readFileSync(__dirname + '/demo.multi.yml', 'utf8'), - yaml = require(__dirname + '/../lib/js-yaml'); - console.log(yaml.loadAll(str)); -} catch (e) { - console.log(e.toString()); - console.log(e.stack); -} diff --git a/examples/demo.multi.yml b/examples/demo.multi.yml deleted file mode 100644 index 4ddbcdfe..00000000 --- a/examples/demo.multi.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- -pid: '/home/www/pids/thin.pid' -wait: 12.34 ---- -pid: '/home/www/pids/unicorn.pid' -wait: 23.45 diff --git a/examples/demo.yml b/examples/demo.yml deleted file mode 100644 index 2e08aff6..00000000 --- a/examples/demo.yml +++ /dev/null @@ -1,52 +0,0 @@ ---- -pid: '/home/www/pids/thin.pid' -wait: 12.34 -listen: {host: localhost, port: 3000} -modules: ['users', 'i18n'] -users: &crew - puzrin: - role: manager - ixti: - role: developer -authors: *crew -require: - - logger - - debugger -dbg-mode: true -# some comments -non-ascii-ключ: значение # more comments -folded: > - Multi - line - text -literal: | - More - samples -pairs: !!pairs - - meeting: with team. - - meeting: with boss. - - break: lunch. - - meeting: with client. -omap: !!omap - - aardvark: African pig-like ant eater. Ugly. - - anteater: South-American ant eater. Two species. - - anaconda: South-American constrictor snake. Scaly. -set: !!set - ? Mark McGwire - ? Sammy Sosa - ? Ken Griffey -merge_test: - - &FOOBAR { xx: 1, yy: 2 } - - # Should include FOOBAR - << : *FOOBAR - zx : 3 -generica: !!binary "\ - R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\ - OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\ - +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\ - AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=" -genericb: !!binary | - R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5 - OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+ - +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC - AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs= diff --git a/examples/single.js b/examples/single.js new file mode 100644 index 00000000..9d9600dc --- /dev/null +++ b/examples/single.js @@ -0,0 +1,10 @@ +var fs = require('fs'), + jsyaml = require(__dirname + '/../lib/js-yaml'), + source = __dirname + '/single.yml'; + +try { + var str = fs.readFileSync(source, 'utf8'); + console.log(jsyaml.load(str)); +} catch (e) { + console.log(e.stack || e.toString()); +} diff --git a/examples/single.yml b/examples/single.yml new file mode 100644 index 00000000..a2cea354 --- /dev/null +++ b/examples/single.yml @@ -0,0 +1,174 @@ +--- +# Collection Types ############################################################# +################################################################################ + +# http://yaml.org/type/map.html -----------------------------------------------# + +map: + # Unordered set of key: value pairs. + Block style: !!map + Clark : Evans + Brian : Ingerson + Oren : Ben-Kiki + Flow style: !!map { Clark: Evans, Brian: Ingerson, Oren: Ben-Kiki } + +# http://yaml.org/type/omap.html ----------------------------------------------# + +omap: + # Explicitly typed ordered map (dictionary). + Bestiary: !!omap + - aardvark: African pig-like ant eater. Ugly. + - anteater: South-American ant eater. Two species. + - anaconda: South-American constrictor snake. Scaly. + # Etc. + # Flow style + Numbers: !!omap [ one: 1, two: 2, three : 3 ] + +# http://yaml.org/type/pairs.html ---------------------------------------------# + +pairs: + # Explicitly typed pairs. + Block tasks: !!pairs + - meeting: with team. + - meeting: with boss. + - break: lunch. + - meeting: with client. + Flow tasks: !!pairs [ meeting: with team, meeting: with boss ] + +# http://yaml.org/type/set.html -----------------------------------------------# + +set: + # Explicitly typed set. + baseball players: !!set + ? Mark McGwire + ? Sammy Sosa + ? Ken Griffey + # Flow style + baseball teams: !!set { Boston Red Sox, Detroit Tigers, New York Yankees } + +# http://yaml.org/type/seq.html -----------------------------------------------# + +seq: + # Ordered sequence of nodes + Block style: !!seq + - Mercury # Rotates - no light/dark sides. + - Venus # Deadliest. Aptly named. + - Earth # Mostly dirt. + - Mars # Seems empty. + - Jupiter # The king. + - Saturn # Pretty. + - Uranus # Where the sun hardly shines. + - Neptune # Boring. No rings. + - Pluto # You call this a planet? + Flow style: !!seq [ Mercury, Venus, Earth, Mars, # Rocks + Jupiter, Saturn, Uranus, Neptune, # Gas + Pluto ] # Overrated + + +# Scalar Types ################################################################# +################################################################################ + +# http://yaml.org/type/binary.html --------------------------------------------# + +binary: + canonical: !!binary "\ + R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\ + OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\ + +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\ + AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=" + generic: !!binary | + R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5 + OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+ + +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC + AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs= + description: + The binary value above is a tiny arrow encoded as a gif image. + +# http://yaml.org/type/bool.html ----------------------------------------------# + +bool: + canonical: y + answer: NO + logical: True + option: on + +# http://yaml.org/type/float.html ---------------------------------------------# + +float: + canonical: 6.8523015e+5 + exponentioal: 685.230_15e+03 + fixed: 685_230.15 + sexagesimal: 190:20:30.15 + negative infinity: -.inf + not a number: .NaN + +# http://yaml.org/type/int.html -----------------------------------------------# + +int: + canonical: 685230 + decimal: +685_230 + octal: 02472256 + hexadecimal: 0x_0A_74_AE + binary: 0b1010_0111_0100_1010_1110 + sexagesimal: 190:20:30 + +# http://yaml.org/type/merge.html ---------------------------------------------# + +merge: + - &CENTER { x: 1, y: 2 } + - &LEFT { x: 0, y: 2 } + - &BIG { r: 10 } + - &SMALL { r: 1 } + + # All the following maps are equal: + + - # Explicit keys + x: 1 + y: 2 + r: 10 + label: nothing + + - # Merge one map + << : *CENTER + r: 10 + label: center + + - # Merge multiple maps + << : [ *CENTER, *BIG ] + label: center/big + + - # Override + << : [ *BIG, *LEFT, *SMALL ] + x: 1 + label: big/left/small + +# http://yaml.org/type/null.html ----------------------------------------------# + +null: + # This mapping has four keys, + # one has a value. + empty: + canonical: ~ + english: null + ~: null key + # This sequence has five + # entries, two have values. + sparse: + - ~ + - 2nd entry + - + - 4th entry + - Null + +# http://yaml.org/type/str.html -----------------------------------------------# + +string: abcd + +# http://yaml.org/type/timestamp.html -----------------------------------------# + +timestamp: + canonical: 2001-12-15T02:59:43.1Z + valid iso8601: 2001-12-14t21:59:43.10-05:00 + space separated: 2001-12-14 21:59:43.10 -5 + no time zone (Z): 2001-12-15 2:59:43.10 + date (00:00:00Z): 2002-12-14 diff --git a/examples/visionmedia-compat/boolean.yaml b/examples/visionmedia-compat/boolean.yaml deleted file mode 100644 index cece741b..00000000 --- a/examples/visionmedia-compat/boolean.yaml +++ /dev/null @@ -1,7 +0,0 @@ ---- - title: Some Title - falseme: I can see this - what: no - say: yes - trueme: found - hope: to see this diff --git a/examples/visionmedia-compat/config.yml b/examples/visionmedia-compat/config.yml deleted file mode 100644 index aac7dc67..00000000 --- a/examples/visionmedia-compat/config.yml +++ /dev/null @@ -1,8 +0,0 @@ ---- - pid: '/home/www/pids/thin.pid' - wait: 30 - port: 3000 - timeout: 15 - servers: 2 - require: [] - dbg-mode: false diff --git a/examples/visionmedia-compat/dates.yaml b/examples/visionmedia-compat/dates.yaml deleted file mode 100644 index 2a58d6c1..00000000 --- a/examples/visionmedia-compat/dates.yaml +++ /dev/null @@ -1,7 +0,0 @@ ---- - date0: 2011-05-21 - date1: 2011-5-21 - date2: 2011-1-2 - date3: 2011-02-21 17:56 - date4: 2011-04-21 5:35 - date5: 2011-06-02 20:45:30 diff --git a/examples/visionmedia-compat/hash.yml b/examples/visionmedia-compat/hash.yml deleted file mode 100644 index c45447c9..00000000 --- a/examples/visionmedia-compat/hash.yml +++ /dev/null @@ -1,20 +0,0 @@ ---- - users: - tj: - name: tj - age: 23 - email: 'tj@vision-media.ca' - bob: - name: 'bob' - age: 27 - ted: { name: ted, age: 32, email: ted@tedtalks.com } - country: - name: Österreich - website: http://en.wikipedia.org/wiki/Austria - space: - description: space, the final frontier - brackets: - square: Square [brackets] can go in the middle of strings - squiggle: Squiggle {brackets} can also go in the middle of strings! - extrasquare: [Scratch that] brackets can go at the beginning as long as they close and have text after. - extrasquiggle: {Scratch that} squigs can go at the beginning also! diff --git a/examples/visionmedia-compat/list.nested.yml b/examples/visionmedia-compat/list.nested.yml deleted file mode 100644 index f7fe1c22..00000000 --- a/examples/visionmedia-compat/list.nested.yml +++ /dev/null @@ -1,8 +0,0 @@ ---- - - - - 1 - - 2 - - - - 3 - - - - 4 diff --git a/examples/visionmedia-compat/list.yml b/examples/visionmedia-compat/list.yml deleted file mode 100644 index c82445c2..00000000 --- a/examples/visionmedia-compat/list.yml +++ /dev/null @@ -1,4 +0,0 @@ ---- - - lots of milk - - 'cookies' - - 'something' diff --git a/examples/visionmedia-compat/null.yaml b/examples/visionmedia-compat/null.yaml deleted file mode 100644 index 26a7068a..00000000 --- a/examples/visionmedia-compat/null.yaml +++ /dev/null @@ -1,8 +0,0 @@ ---- - test: A test for null values - thisis: null - thistoo: NULL - notnull: yes - tildeis: ~ - capitalized: Null - end: test passed? diff --git a/examples/visionmedia-compat/run.js b/examples/visionmedia-compat/run.js deleted file mode 100644 index 9a84eebd..00000000 --- a/examples/visionmedia-compat/run.js +++ /dev/null @@ -1,19 +0,0 @@ - -var path = process.argv[2], - fs = require('fs'), - yaml = require('../../lib/js-yaml') - -if (!path) - throw new Error('provide path to yaml file') - -fs.readFile(path, function(err, fileContents) { - fileContents = fileContents.toString() - console.log('\n') - console.log(fileContents) - console.log('\noutputs:\n') - try { - console.log(yaml.eval(fileContents)) - } catch (err) { - console.error(err.toString()); - } -}) diff --git a/lib/js-yaml/constructor.js b/lib/js-yaml/constructor.js index 415dd86c..c22a566b 100644 --- a/lib/js-yaml/constructor.js +++ b/lib/js-yaml/constructor.js @@ -15,7 +15,9 @@ var ConstructorError = exports.ConstructorError = var BOOL_VALUES = { + 'y': true, 'yes': true, + 'n': false, 'no': false, 'true': true, 'false': false, @@ -116,7 +118,18 @@ var BaseConstructor = exports.BaseConstructor = new JS.Class('BaseConstructor', this.recursiveObjects.store(node, null); - constructor = this.yamlConstructors[node.tag || null]; + if (undefined !== this.yamlConstructors[node.tag]) { + constructor = this.yamlConstructors[node.tag]; + } else { + if (undefined !== this.yamlConstructors[null]) { + constructor = this.yamlConstructors[null]; + } else { + throw new ConstructorError(null, null, + "can't find any constructor for tag=" + node.tag, + node.startMark); + } + } + data = constructor.call(this, node); if (data instanceof $$.Populator) { @@ -252,12 +265,12 @@ var SafeConstructor = exports.SafeConstructor = new JS.Class('SafeConstructor', if (valueNode.isA(__.MappingNode)) { this.flattenMapping(valueNode); - valueNode.value.forEach(function (value) { + $$.each(valueNode.value, function (value) { merge.push(value); }); } else if (valueNode.isA(__.SequenceNode)) { submerge = []; - valueNode.forEach(function (subnode) { + $$.each(valueNode.value, function (subnode) { if (!subnode.isA(__.MappingNode)) { throw new ConstructorError("while constructing a mapping", node.startMark, "expected a mapping for merging, but found " + subnode.id, @@ -266,8 +279,11 @@ var SafeConstructor = exports.SafeConstructor = new JS.Class('SafeConstructor', this.flattenMapping(subnode); submerge.push(subnode.value); }.bind(this)); - $$.reverse(submerge).forEach(function (value, idx) { - merge[idx] = value; + + $$.reverse(submerge).forEach(function (values) { + values.forEach(function (value) { + merge.push(value); + }); }); } else { throw new ConstructorError("while constructing a mapping", node.startMark, @@ -283,7 +299,7 @@ var SafeConstructor = exports.SafeConstructor = new JS.Class('SafeConstructor', } if (!!merge.length) { - node.value.forEach(function (value) { merge.push(value); }); + $$.each(node.value, function (value) { merge.push(value); }); node.value = merge; } }, @@ -306,7 +322,7 @@ var SafeConstructor = exports.SafeConstructor = new JS.Class('SafeConstructor', }, constructYamlInt: function (node) { - var value = this.constructScalar(node).replace('_', ''), + var value = this.constructScalar(node).replace(/_/g, ''), sign = ('-' === value[0]) ? -1 : 1, base, digits = []; @@ -317,7 +333,7 @@ var SafeConstructor = exports.SafeConstructor = new JS.Class('SafeConstructor', if ('0' === value) { return 0; } else if (/^0b/.test(value)) { - return sign * parseInt(value, 2); + return sign * parseInt(value.slice(2), 2); } else if (/^0x/.test(value)) { return sign * parseInt(value, 16); } else if ('0' === value[0]) { @@ -329,9 +345,10 @@ var SafeConstructor = exports.SafeConstructor = new JS.Class('SafeConstructor', value = 0; base = 1; digits.forEach(function (d) { - value += d * base; + value += (d * base); base *= 60; }); + console.log(sign, value, sign * value); return sign * value; } else { return sign * parseInt(value, 10); @@ -339,7 +356,7 @@ var SafeConstructor = exports.SafeConstructor = new JS.Class('SafeConstructor', }, constructYamlFloat: function (node) { - var value = this.constructScalar(node).replace('_', ''), + var value = this.constructScalar(node).replace(/_/g, ''), sign = ('-' === value[0]) ? -1 : 1, base, digits = []; diff --git a/lib/js-yaml/core.js b/lib/js-yaml/core.js index c59f5acb..76c1a258 100644 --- a/lib/js-yaml/core.js +++ b/lib/js-yaml/core.js @@ -29,7 +29,7 @@ $$.import = function import_modules() { // iterates through all object keys-value pairs calling iterator on each one -// example: $$.each(hash, function (val, key) { /* ... */ }); +// example: $$.each(objOrArr, function (val, key) { /* ... */ }); $$.each = function each(obj, iterator, context) { var keys, i, l; @@ -77,7 +77,7 @@ $$.extend = function extend() { // returns reversed copy of array $$.reverse = function reverse(arr) { var result = [], i, l; - for (i = 0, l = arguments.length; i < l; i++) { + for (i = 0, l = arr.length; i < l; i++) { result.unshift(arr[i]); } return result; diff --git a/lib/js-yaml/resolver.js b/lib/js-yaml/resolver.js index e4b7b294..1b5ed775 100644 --- a/lib/js-yaml/resolver.js +++ b/lib/js-yaml/resolver.js @@ -87,7 +87,7 @@ exports.Resolver = new JS.Class('Resolver', exports.BaseResolver, { }); exports.Resolver.addImplicitResolver('tag:yaml.org,2002:bool', - new RegExp('^(?:yes|Yes|YES|no|No|NO' + + new RegExp('^(?:y|yes|Yes|YES|n|no|No|NO' + '|true|True|TRUE|false|False|FALSE' + '|on|On|ON|off|Off|OFF)$'), ['y', 'Y', 'n', 'N', 't', 'T', 'f', 'F', 'o', 'O']);