Skip to content

Commit

Permalink
implement object parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ichiriac committed Jan 8, 2017
1 parent 547b20f commit 9861453
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,14 @@ Parser.prototype.parseArray = function () {
return null;
};

/**
* Parse an object
*/
Parser.prototype.parseObject = function () {

if (this.token === '{') {
return this.readJson();
}
return null;
};

/**
Expand Down Expand Up @@ -619,8 +625,8 @@ Parser.prototype.parseStatement = function () {

Parser.prototype.readArray = function (endChar) {
var result = [];
this.token = this.lexer.lex(); // consume start char
do {
this.token = this.lexer.lex(); // consume start char
var item = this.parseTopStatement();
if (item !== null) { // ignore
item = this.getJsonValue(item);
Expand All @@ -638,6 +644,7 @@ Parser.prototype.readArray = function (endChar) {
if (this.token !== ',') {
break;
}
this.token = this.lexer.lex();
}
} while (this.token !== endChar && this.token !== this.lexer._t.T_EOF);
if (this.token === endChar) {
Expand All @@ -648,11 +655,13 @@ Parser.prototype.readArray = function (endChar) {

Parser.prototype.readJson = function () {
var result = {};
this.token = this.lexer.lex();
do {
this.token = this.lexer.lex();
var item = this.parseTopStatement();
if (item !== null) { // ignore
if (this.token === '=>') {
if (item.kind === 'key') {
result[item.name] = item.value;
} else if (this.token === '=>') {
item = this.getJsonKey(item);
if (item !== null) {
this.token = this.lexer.lex();
Expand All @@ -664,6 +673,7 @@ Parser.prototype.readJson = function () {
if (this.token !== ',') {
break;
}
this.token = this.lexer.lex();
}
} while (this.token !== '}' && this.token !== this.lexer._t.T_EOF);
if (this.token === '}') {
Expand Down
135 changes: 131 additions & 4 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,44 @@ var should = require('should');
var DocBlockParser = require('../src/index');

describe('Test parser', function () {
var doc = new DocBlockParser();
var doc = new DocBlockParser({
boolean: [
{
property: 'value',
parser: 'boolean'
}
],
array: [
{
property: 'value',
parser: 'array'
}
],
number: [
{
property: 'value',
parser: 'number'
}
],
string: [
{
property: 'value',
parser: 'string'
}
],
text: [
{
property: 'value',
parser: 'text'
}
],
object: [
{
property: 'value',
parser: 'object'
}
]
});

it('extend grammar', function () {
var doc2 = new DocBlockParser({
Expand All @@ -34,9 +71,9 @@ describe('Test parser', function () {
ast.body[0].type.should.be.exactly('test');
ast.body[0].options.length.should.be.exactly(3);
ast.body[0].options[0].kind.should.be.exactly('number');
ast.body[0].options[0].value.should.be.exactly('123');
ast.body[0].options[0].value.should.be.exactly(123);
ast.body[0].options[1].kind.should.be.exactly('number');
ast.body[0].options[1].value.should.be.exactly('1.23');
ast.body[0].options[1].value.should.be.exactly(1.23);
ast.body[0].options[2].kind.should.be.exactly('null');
});

Expand All @@ -49,7 +86,7 @@ describe('Test parser', function () {
ast.body[0].type.should.be.exactly('test');
ast.body[0].options.length.should.be.exactly(1);
ast.body[0].options[0].kind.should.be.exactly('array');
console.log(ast.body[0].options[0]);
// @todo console.log(ast.body[0].options[0]);
});

it('test boolean', function () {
Expand Down Expand Up @@ -134,4 +171,94 @@ describe('Test parser', function () {
ast.body[0].version.patch.should.be.exactly(314);
ast.body[0].version.label.should.be.exactly('beta.5');
});

it('test deprecated options', function () {
var ast = doc.parse([
'/**',
' * @deprecated Foo',
' */'
].join('\n'));
ast.body[0].kind.should.be.exactly('deprecated');
ast.body[0].description.should.be.exactly('Foo');
});

it('test boolean', function () {
var ast = doc.parse([
'/**',
' * @boolean true',
' * @boolean false',
' * @boolean',
' */'
].join('\n'));
ast.body[0].kind.should.be.exactly('boolean');
ast.body[0].value.should.be.exactly(true);
ast.body[1].kind.should.be.exactly('boolean');
ast.body[1].value.should.be.exactly(false);
ast.body[2].kind.should.be.exactly('boolean');
should.equal(ast.body[2].value, null);
});

it('test number', function () {
var ast = doc.parse([
'/**',
' * @number 123',
' * @number 1.23',
' * @number',
' */'
].join('\n'));
ast.body[0].kind.should.be.exactly('number');
ast.body[0].value.should.be.exactly(123);
ast.body[1].kind.should.be.exactly('number');
ast.body[1].value.should.be.exactly(1.23);
ast.body[2].kind.should.be.exactly('number');
should.equal(ast.body[2].value, null);
});

it('test string', function () {
var ast = doc.parse([
'/**',
' * @string "azerty"',
' * @string \'azerty\'',
' * @string',
' */'
].join('\n'));
ast.body[0].kind.should.be.exactly('string');
ast.body[0].value.should.be.exactly('azerty');
ast.body[1].kind.should.be.exactly('string');
ast.body[1].value.should.be.exactly('azerty');
ast.body[2].kind.should.be.exactly('string');
should.equal(ast.body[2].value, null);
});

it('test array', function () {
var ast = doc.parse([
'/**',
' * @array [1, 2, 3]',
' * @array array(4, 5, 6)',
' * @array',
' */'
].join('\n'));
ast.body[0].kind.should.be.exactly('array');
ast.body[0].value.should.be.eql([1, 2, 3]);
ast.body[1].kind.should.be.exactly('array');
ast.body[1].value.should.be.eql([4, 5, 6]);
ast.body[2].kind.should.be.exactly('array');
should.equal(ast.body[2].value, null);
});

it('test object', function () {
var ast = doc.parse([
'/**',
' * @object { foo: 1 }',
' * @object { "bar": false }',
' * @object',
' */'
].join('\n'));
ast.body[0].kind.should.be.exactly('object');
ast.body[0].value.should.be.eql({foo: 1});
ast.body[1].kind.should.be.exactly('object');
ast.body[1].value.should.be.eql({bar: false});
ast.body[2].kind.should.be.exactly('object');
should.equal(ast.body[2].value, null);
});
});

0 comments on commit 9861453

Please sign in to comment.