Skip to content

Commit

Permalink
implement the variable reader
Browse files Browse the repository at this point in the history
  • Loading branch information
ichiriac committed Jan 8, 2017
1 parent 7ff0b34 commit cf2eb94
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var Lexer = function (tokens) {

// breaking symbols
var lexerSymbols = [
',', '=', ':', '(', ')', '[', ']', '{', '}', '@', '"', '\'', '\\', '<', '>'
',', '=', ':', '(', ')', '[', ']', '{', '}', '@', '"', '\'', '\\', '<', '>', '$'
];

// whitespace chars
Expand Down
26 changes: 21 additions & 5 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ var Parser = function (lexer, grammar) {
this.parsers = {
type: this.parseType,
variable: this.parseVarName,
name: this.parseVarName, // alias of variable
text: this.parseText,
version: this.parseVersion,
array: this.parseArray,
Expand All @@ -58,7 +57,7 @@ var Parser = function (lexer, grammar) {
},
{
property: 'name',
parser: 'name',
parser: 'variable',
optional: true
},
{
Expand All @@ -71,7 +70,7 @@ var Parser = function (lexer, grammar) {
property: 'what',
parser: 'type',
optional: true,
default: 'void'
default: false
},
{
property: 'description',
Expand All @@ -94,7 +93,12 @@ var Parser = function (lexer, grammar) {
property: 'version',
parser: 'version',
optional: true,
default: 'latest'
default: {
major: 0,
minor: 0,
patch: 0,
label: null
}
},
{
property: 'description',
Expand Down Expand Up @@ -299,6 +303,7 @@ Parser.prototype.parseRule = function (rule) {
var result = this.parsers[rule.parser].apply(this, []);
if (result === null) {
this.lexer.unlex(backup);
this.lexer.backup = backup;
if (typeof rule.default !== 'undefined') {
return rule.default;
}
Expand Down Expand Up @@ -395,8 +400,19 @@ Parser.prototype.parseListOfTypes = function (charEnd) {
return result;
};

/**
* Reads a variable name
*/
Parser.prototype.parseVarName = function () {

if (this.token === '$') {
this.token = this.lexer.lex(); // eat && continue
if (this.token === this.lexer._t.T_STRING) {
var result = this.lexer.text;
this.token = this.lexer.lex(); // eat && continue
return result;
}
}
return null;
};

/**
Expand Down
21 changes: 21 additions & 0 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('Test parser', function () {
ast.body[0].what.name.should.be.exactly('void');
ast.body[0].description.should.be.exactly('Some extra informations');
});

it('test return optional values', function () {
var ast = doc.parse('/* @return \\Foo\\Bar[] */');
ast.body[0].kind.should.be.exactly('return');
Expand All @@ -87,4 +88,24 @@ describe('Test parser', function () {
ast.body[0].what.value.fqn.should.be.exactly(true);
ast.body[0].description.should.be.exactly('');
});

it('test return defaults', function () {
var ast = doc.parse('/* @return */');
ast.body[0].kind.should.be.exactly('return');
ast.body[0].what.should.be.exactly(false);
ast.body[0].description.should.be.exactly('');
});

it('test param', function () {
var ast = doc.parse([
'/**',
' * Description',
' * @param String $var Foo is Bar',
' */'
].join('\n'));
ast.body[0].kind.should.be.exactly('param');
ast.body[0].type.name.should.be.exactly('String');
ast.body[0].name.should.be.exactly('var');
ast.body[0].description.should.be.exactly('Foo is Bar');
});
});

0 comments on commit cf2eb94

Please sign in to comment.