Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

corrected some useless lines #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/aComplexObject.yml
Original file line number Diff line number Diff line change
@@ -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

10 changes: 10 additions & 0 deletions examples/hashWithLinks.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions examples/listWithLinks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- &obj1
lots of milk
- *obj1
- 'something'
5 changes: 3 additions & 2 deletions examples/run.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

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')

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))
43 changes: 37 additions & 6 deletions lib/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Version triplet.
*/

exports.version = '0.1.1'
exports.version = '0.1.2'

// --- Helpers

Expand Down Expand Up @@ -53,6 +53,8 @@ var tokens = [
[']', /^\]/],
['-', /^\-/],
[':', /^[:]/],
['&', /^\&([a-zA-Z0-9]*)/],
['*', /^\*([a-zA-Z0-9]*)/],
]

/**
Expand Down Expand Up @@ -160,6 +162,18 @@ Parser.prototype.accept = function(type) {
return this.advance()
}

/**
* 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_.
*
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down