Skip to content

Commit cf2eb94

Browse files
committed
implement the variable reader
1 parent 7ff0b34 commit cf2eb94

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

src/lexer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var Lexer = function (tokens) {
1717

1818
// breaking symbols
1919
var lexerSymbols = [
20-
',', '=', ':', '(', ')', '[', ']', '{', '}', '@', '"', '\'', '\\', '<', '>'
20+
',', '=', ':', '(', ')', '[', ']', '{', '}', '@', '"', '\'', '\\', '<', '>', '$'
2121
];
2222

2323
// whitespace chars

src/parser.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ var Parser = function (lexer, grammar) {
3838
this.parsers = {
3939
type: this.parseType,
4040
variable: this.parseVarName,
41-
name: this.parseVarName, // alias of variable
4241
text: this.parseText,
4342
version: this.parseVersion,
4443
array: this.parseArray,
@@ -58,7 +57,7 @@ var Parser = function (lexer, grammar) {
5857
},
5958
{
6059
property: 'name',
61-
parser: 'name',
60+
parser: 'variable',
6261
optional: true
6362
},
6463
{
@@ -71,7 +70,7 @@ var Parser = function (lexer, grammar) {
7170
property: 'what',
7271
parser: 'type',
7372
optional: true,
74-
default: 'void'
73+
default: false
7574
},
7675
{
7776
property: 'description',
@@ -94,7 +93,12 @@ var Parser = function (lexer, grammar) {
9493
property: 'version',
9594
parser: 'version',
9695
optional: true,
97-
default: 'latest'
96+
default: {
97+
major: 0,
98+
minor: 0,
99+
patch: 0,
100+
label: null
101+
}
98102
},
99103
{
100104
property: 'description',
@@ -299,6 +303,7 @@ Parser.prototype.parseRule = function (rule) {
299303
var result = this.parsers[rule.parser].apply(this, []);
300304
if (result === null) {
301305
this.lexer.unlex(backup);
306+
this.lexer.backup = backup;
302307
if (typeof rule.default !== 'undefined') {
303308
return rule.default;
304309
}
@@ -395,8 +400,19 @@ Parser.prototype.parseListOfTypes = function (charEnd) {
395400
return result;
396401
};
397402

403+
/**
404+
* Reads a variable name
405+
*/
398406
Parser.prototype.parseVarName = function () {
399-
407+
if (this.token === '$') {
408+
this.token = this.lexer.lex(); // eat && continue
409+
if (this.token === this.lexer._t.T_STRING) {
410+
var result = this.lexer.text;
411+
this.token = this.lexer.lex(); // eat && continue
412+
return result;
413+
}
414+
}
415+
return null;
400416
};
401417

402418
/**

test/parser.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ describe('Test parser', function () {
7878
ast.body[0].what.name.should.be.exactly('void');
7979
ast.body[0].description.should.be.exactly('Some extra informations');
8080
});
81+
8182
it('test return optional values', function () {
8283
var ast = doc.parse('/* @return \\Foo\\Bar[] */');
8384
ast.body[0].kind.should.be.exactly('return');
@@ -87,4 +88,24 @@ describe('Test parser', function () {
8788
ast.body[0].what.value.fqn.should.be.exactly(true);
8889
ast.body[0].description.should.be.exactly('');
8990
});
91+
92+
it('test return defaults', function () {
93+
var ast = doc.parse('/* @return */');
94+
ast.body[0].kind.should.be.exactly('return');
95+
ast.body[0].what.should.be.exactly(false);
96+
ast.body[0].description.should.be.exactly('');
97+
});
98+
99+
it('test param', function () {
100+
var ast = doc.parse([
101+
'/**',
102+
' * Description',
103+
' * @param String $var Foo is Bar',
104+
' */'
105+
].join('\n'));
106+
ast.body[0].kind.should.be.exactly('param');
107+
ast.body[0].type.name.should.be.exactly('String');
108+
ast.body[0].name.should.be.exactly('var');
109+
ast.body[0].description.should.be.exactly('Foo is Bar');
110+
});
90111
});

0 commit comments

Comments
 (0)