Skip to content

Commit caa80e6

Browse files
committed
beautify
1 parent 65d16e4 commit caa80e6

11 files changed

Lines changed: 300 additions & 133 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
coverage/
3+
npm-debug.log

.jsbeautifyrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"index_size": 2,
3+
"jslint_happy": true,
4+
"wrap_line_length": 80
5+
}

README.md

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,67 @@
1-
# docblock-parser
1+
# DocBlock & Annotations Parser
22

33
[![npm version](https://badge.fury.io/js/docblock-parser.svg)](https://www.npmjs.com/package/parsedoc)
44
[![Build Status](https://travis-ci.org/glayzzle/docblock-parser.svg?branch=master)](https://travis-ci.org/glayzzle/docblock-parser)
55
[![Coverage Status](https://coveralls.io/repos/github/glayzzle/docblock-parser/badge.svg?branch=master)](https://coveralls.io/github/glayzzle/docblock-parser?branch=master)
66
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
77
[![Gitter](https://img.shields.io/badge/GITTER-join%20chat-green.svg)](https://gitter.im/glayzzle/Lobby)
88

9-
Parses docblocks comments
10-
9+
This library is a javascript LALR(1) parser that parses docblocks and extracts
10+
annotations under an structured syntax tree.
1111

1212
# Install
1313

14-
```
14+
```sh
1515
npm install parsedoc --save
1616
```
17+
18+
And simple usage :
19+
20+
```js
21+
var ParseDoc = require('parsedoc');
22+
var reader = new ParseDoc();
23+
var data = reader.parse('/** @hello world */');
24+
```
25+
26+
27+
# Supported syntaxes
28+
29+
```php
30+
/**
31+
* Some description
32+
* @return boolean
33+
* @return map<string, SomeClass>
34+
* @author Ioan CHIRIAC <me@domain.com>
35+
* @throws Exception
36+
* @deprecated
37+
* @table('tableName', true)
38+
* @table(
39+
* name='tableName',
40+
* primary=true
41+
* )
42+
* @annotation1 @annotation2
43+
* @Target(["METHOD", "PROPERTY"])
44+
* @Attributes([
45+
* @Attribute("stringProperty", type = "string"),
46+
* @Attribute("annotProperty", type = "SomeAnnotationClass"),
47+
* ])
48+
* @json({
49+
* "key": "value",
50+
* "object": { "inner": true },
51+
* "list": [1, 2, 3]
52+
* })
53+
* <node>
54+
* Some inner multi line content
55+
* </node>
56+
*/
57+
```
58+
59+
# AST structure
60+
61+
```js
62+
63+
```
64+
65+
# Misc
66+
67+
This library is released under BSD-3 license clause.

gruntfile.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
module.exports = function(grunt) {
2-
1+
/**
2+
* The automated build configuration
3+
*/
4+
module.exports = function (grunt) {
35
// Project configuration.
46
grunt.initConfig({
57
pkg: grunt.file.readJSON('package.json'),
@@ -19,11 +21,11 @@ module.exports = function(grunt) {
1921
documentation: {
2022
parser: {
2123
options: {
22-
destination: "docs/",
23-
format: "md",
24-
version: "<%= pkg.version %>",
25-
name: "<%= pkg.name %>",
26-
filename: "parser.md",
24+
destination: 'docs/',
25+
format: 'md',
26+
version: '<%= pkg.version %>',
27+
name: '<%= pkg.name %>',
28+
filename: 'parser.md',
2729
shallow: false
2830
},
2931
files: [{
@@ -32,11 +34,11 @@ module.exports = function(grunt) {
3234
},
3335
lexer: {
3436
options: {
35-
destination: "docs/",
36-
format: "md",
37-
version: "<%= pkg.version %>",
38-
name: "<%= pkg.name %>",
39-
filename: "lexer.md",
37+
destination: 'docs/',
38+
format: 'md',
39+
version: '<%= pkg.version %>',
40+
name: '<%= pkg.name %>',
41+
filename: 'lexer.md',
4042
shallow: false
4143
},
4244
files: [{
@@ -45,11 +47,11 @@ module.exports = function(grunt) {
4547
},
4648
main: {
4749
options: {
48-
destination: "docs/",
49-
format: "md",
50-
version: "<%= pkg.version %>",
51-
name: "<%= pkg.name %>",
52-
filename: "README.md",
50+
destination: 'docs/',
51+
format: 'md',
52+
version: '<%= pkg.version %>',
53+
name: '<%= pkg.name %>',
54+
filename: 'README.md',
5355
shallow: true
5456
},
5557
files: [{
@@ -60,6 +62,7 @@ module.exports = function(grunt) {
6062
uglify: {
6163
options: {
6264
compress: {
65+
// eslint-disable-next-line camelcase
6366
keep_fnames: true
6467
},
6568
sourceMap: true,
@@ -81,5 +84,4 @@ module.exports = function(grunt) {
8184
// Default task(s).
8285
grunt.registerTask('default', ['browserify', 'uglify']);
8386
grunt.registerTask('doc', ['documentation']);
84-
8587
};

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
"type": "git",
1212
"url": "git+https://github.com/glayzzle/docblock-parser.git"
1313
},
14+
"xo": {
15+
"space": 2,
16+
"envs": ["node", "mocha"]
17+
},
1418
"keywords": [
1519
"doc",
1620
"parser",
@@ -36,4 +40,4 @@
3640
"should": "^11.1.2",
3741
"xo": "^0.17.1"
3842
}
39-
}
43+
}

src/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* @authors https://github.com/glayzzle/docblock-parser/graphs/contributors
44
* @url http://glayzzle.com/docblock-parser
55
*/
6-
7-
"use strict";
6+
'use strict';
87

98
var token = require('./token');
109
var Lexer = require('./lexer');
@@ -16,18 +15,18 @@ var Parser = require('./parser');
1615
* @property {Lexer} lexer
1716
* @property {Parser} parser
1817
*/
19-
var API = function() {
18+
var API = function (grammar) {
2019
this.token = token;
2120
this.lexer = new Lexer(this.token);
22-
this.parser = new Parser(this.lexer);
21+
this.parser = new Parser(this.lexer, grammar);
2322
};
2423

2524
/**
2625
* Parsing the specified input
2726
* @return {Array} AST
2827
*/
29-
API.prototype.parse = function(input) {
28+
API.prototype.parse = function (input) {
3029
return this.parser.parse(input);
31-
}
30+
};
3231

3332
module.exports = API;

src/lexer.js

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
* @authors https://github.com/glayzzle/docblock-parser/graphs/contributors
44
* @url http://glayzzle.com/docblock-parser
55
*/
6-
7-
"use strict";
6+
'use strict';
87

98
/**
109
* @constructor Lexer
1110
* @property {String} text Current parsed text (attached to current token)
1211
* @property {Number} offset Current offset
1312
* @property {String|Number} token Current parsed token
1413
*/
15-
var Lexer = function(tokens) {
14+
var Lexer = function (tokens) {
1615
this._t = tokens;
1716
};
1817

19-
2018
// breaking symbols
2119
var lexerSymbols = [
2220
',', '=', ':', '(', ')', '[', ']', '{', '}', '@'
@@ -28,32 +26,31 @@ var lexerWhiteSpace = [' ', '\t', '\r', '\n'];
2826
/**
2927
* Initialize the lexer with specified text
3028
*/
31-
Lexer.prototype.read = function(input) {
29+
Lexer.prototype.read = function (input) {
3230
this._input = input;
3331
this.offset = 0;
34-
this.text = "";
32+
this.text = '';
3533
this.token = null;
3634
};
3735

3836
/**
3937
* Consumes a char
4038
* @return {String}
4139
*/
42-
Lexer.prototype.input = function() {
40+
Lexer.prototype.input = function () {
4341
if (this.offset < this._input.length) {
4442
this.ch = this._input[this.offset++];
4543
this.text += this.ch;
4644
return this.ch;
47-
} else {
48-
return null;
4945
}
46+
return null;
5047
};
5148

5249
/**
5350
* Revert back the current consumed char
5451
* @return {void}
5552
*/
56-
Lexer.prototype.unput = function() {
53+
Lexer.prototype.unput = function () {
5754
this.offset--;
5855
this.text = this.text.substring(0, this.text.length - 1);
5956
};
@@ -62,7 +59,7 @@ Lexer.prototype.unput = function() {
6259
* Revert back the current consumed token
6360
* @return {String|Number} the previous token
6461
*/
65-
Lexer.prototype.unlex = function() {
62+
Lexer.prototype.unlex = function () {
6663
this.offset = this.__offset;
6764
this.text = this.__text;
6865
this.token = this.__token;
@@ -73,7 +70,7 @@ Lexer.prototype.unlex = function() {
7370
* Consumes the next token (ignores whitespaces)
7471
* @return {String|Number} the current token
7572
*/
76-
Lexer.prototype.lex = function() {
73+
Lexer.prototype.lex = function () {
7774
// backup
7875
this.__offset = this.offset;
7976
this.__text = this.text;
@@ -91,11 +88,12 @@ Lexer.prototype.lex = function() {
9188
* Eats a token (see lex for public usage) including whitespace
9289
* @return {String|Number} the current token
9390
*/
94-
Lexer.prototype.next = function() {
95-
this.text = "";
91+
Lexer.prototype.next = function () {
92+
this.text = '';
9693
var ch = this.input();
97-
if (ch === null) return this._t.T_EOF;
98-
if (ch === '"' || ch === "'") {
94+
if (ch === null) {
95+
return this._t.T_EOF;
96+
} else if (ch === '"' || ch === '\'') {
9997
var tKey = ch;
10098
do {
10199
ch = this.input();
@@ -105,9 +103,9 @@ Lexer.prototype.next = function() {
105103
} while (ch !== tKey && this.offset < this._input.length);
106104
return this._t.T_TEXT;
107105
} else if (lexerSymbols.indexOf(ch) > -1) {
108-
if (ch === ':')
106+
if (ch === ':') {
109107
ch = '=>'; // alias
110-
if (ch === '=' && this._input[this.offset] === '>') {
108+
} else if (ch === '=' && this._input[this.offset] === '>') {
111109
ch += this.input();
112110
}
113111
return ch;
@@ -116,32 +114,35 @@ Lexer.prototype.next = function() {
116114
while (lexerWhiteSpace.indexOf(ch) > -1) {
117115
ch = this.input();
118116
}
119-
if (ch !== null) this.unput();
117+
if (ch !== null) {
118+
this.unput();
119+
}
120120
return this._t.T_WHITESPACE;
121-
} else {
122-
ch = ch.charCodeAt(0);
123-
if (ch > 47 && ch < 58) {
124-
while (ch > 47 && ch < 58 && ch !== null) {
125-
ch = this.input();
126-
if (ch !== null)
127-
ch = ch.charCodeAt(0);
121+
}
122+
ch = ch.charCodeAt(0);
123+
if (ch > 47 && ch < 58) {
124+
while (ch > 47 && ch < 58 && ch !== null) {
125+
ch = this.input();
126+
if (ch !== null) {
127+
ch = ch.charCodeAt(0);
128128
}
129-
if (ch !== null) this.unput();
130-
return this._t.T_NUM;
131-
} else {
132-
do {
133-
ch = this.input();
134-
if (
135-
lexerSymbols.indexOf(ch) > -1 ||
136-
lexerWhiteSpace.indexOf(ch) > -1
137-
) {
138-
this.unput();
139-
break;
140-
}
141-
} while (this.offset < this._input.length);
142-
return this._t.T_STRING;
143129
}
130+
if (ch !== null) {
131+
this.unput();
132+
}
133+
return this._t.T_NUM;
144134
}
135+
do {
136+
ch = this.input();
137+
if (
138+
lexerSymbols.indexOf(ch) > -1 ||
139+
lexerWhiteSpace.indexOf(ch) > -1
140+
) {
141+
this.unput();
142+
break;
143+
}
144+
} while (this.offset < this._input.length);
145+
return this._t.T_STRING;
145146
};
146147

147148
// exports

0 commit comments

Comments
 (0)