Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

corrected some useless lines #12

Open
wants to merge 2 commits into from

1 participant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 31, 2010
  1. @dotmaster
Commits on Jan 5, 2011
  1. @dotmaster

    removed useless lines

    dotmaster authored
This page is out of date. Refresh to see the latest.
View
23 examples/aComplexObject.yml
@@ -0,0 +1,23 @@
+-
+ anObject:
+ asubObj: &object3
+ subsub: x
+- &object4
+ x: abc
+ y:
+ x: *object3
+ z:
+ - *object3
+ y:
+ -
+ as: sff
+ f: 34
+ -
+ as: sfdf
+ f: 35
+ -
+ as: sasff
+ f: 37
+-
+ f: *object3
+
View
10 examples/hashWithLinks.yml
@@ -0,0 +1,10 @@
+---
+ users:
+ greg: &greg
+ name: greg
+ age: 31
+ email: 'isimpl@gmail.com'
+ dotmaster: *greg
+ ted: { name: ted, age: 32, email: ted@tedtalks.com }
+ something:
+ blah: 19
View
5 examples/listWithLinks.yml
@@ -0,0 +1,5 @@
+---
+ - &obj1
+ lots of milk
+ - *obj1
+ - 'something'
View
5 examples/run.js
@@ -1,7 +1,8 @@
var path = process.argv[2],
fs = require('fs'),
- yaml = require('../lib/yaml')
+ yaml = require('../lib/yaml'),
+ sys = require('sys');
if (!path)
throw new Error('provide path to yaml file')
@@ -9,4 +10,4 @@ if (!path)
console.log('\n')
console.log(fs.readFileSync(path).toString())
console.log('\noutputs:\n')
-console.log(yaml.eval(fs.readFileSync(path).toString()))
+console.log(sys.inspect(yaml.eval(fs.readFileSync(path).toString()),true,20))
View
43 lib/yaml.js
@@ -5,7 +5,7 @@
* Version triplet.
*/
-exports.version = '0.1.1'
+exports.version = '0.1.2'
// --- Helpers
@@ -53,6 +53,8 @@ var tokens = [
[']', /^\]/],
['-', /^\-/],
[':', /^[:]/],
+ ['&', /^\&([a-zA-Z0-9]*)/],
+ ['*', /^\*([a-zA-Z0-9]*)/],
]
/**
@@ -161,6 +163,18 @@ Parser.prototype.accept = function(type) {
}
/**
+ * Assert _value_ or throw an error _msg_.
+ *
+ * @param {string} type
+ * @param {string} msg
+ * @api private
+ */
+
+Parser.prototype.assert = function(val, msg) {
+ if (val) return
+ throw new Error(msg + ', ' + context(this.peek()[1].input))
+}
+/**
* Expect _type_ or throw an error _msg_.
*
* @param {string} type
@@ -230,7 +244,7 @@ Parser.prototype.parse = function() {
case '[':
return this.parseInlineList()
case 'id':
- return this.parseHash()
+ return this.parseHash()
case 'string':
return this.advanceValue()
case 'float':
@@ -261,15 +275,23 @@ Parser.prototype.parseDoc = function() {
* | id ':' - indent expr dedent
* )+
*/
-
+Parser.prototype.refLinks=[];
Parser.prototype.parseHash = function() {
- var id, hash = {}
+ var id, hash={}, ref, link
while (this.peekType('id') && (id = this.advanceValue())) {
this.expect(':', 'expected semi-colon after id')
- this.ignoreSpace()
+ this.ignoreSpace()
if (this.accept('indent'))
hash[id] = this.parse(),
this.expect('dedent', 'hash not properly dedented')
+ else if (this.peekType('&') && (ref = this.advanceValue())) //accept refs
+ this.expect('indent', 'expected indent after a ref'),
+ hash[id] = this.parse(),
+ this.refLinks[ref]=hash[id],
+ this.expect('dedent', 'hash not properly dedented')
+ else if (this.peekType('*') && (link = this.advanceValue())) //accept links
+ this.assert(this.refLinks[link],'YAML not well formatted got a link but have no ref to link it to'),
+ hash[id] = this.refLinks[link]
else
hash[id] = this.parse()
this.ignoreSpace()
@@ -306,12 +328,21 @@ Parser.prototype.parseInlineHash = function() {
*/
Parser.prototype.parseList = function() {
- var list = []
+ var list = [], val
while (this.accept('-')) {
this.ignoreSpace()
if (this.accept('indent'))
list.push(this.parse()),
this.expect('dedent', 'list item not properly dedented')
+ else if (this.peekType('&') && (ref = this.advanceValue())) //accept refs
+ this.expect('indent', 'expected indent after a ref'),
+ val=this.parse(),
+ list.push(val),
+ this.refLinks[ref]=val,
+ this.expect('dedent', 'hash not properly dedented')
+ else if (this.peekType('*') && (link = this.advanceValue())) //accept links
+ this.assert(this.refLinks[link],'YAML not well formatted got a link but have no ref to link it to'),
+ list.push( this.refLinks[link] )
else
list.push(this.parse())
this.ignoreSpace()
Something went wrong with that request. Please try again.