parser.js
6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
var PARSERS = require('./parsers')
var MARKER_START = '/**'
var MARKER_START_SKIP = '/***'
var MARKER_END = '*/'
/* ------- util functions ------- */
function merge (/* ...objects */) {
var k, obj
var res = {}
var objs = Array.prototype.slice.call(arguments)
for (var i = 0, l = objs.length; i < l; i++) {
obj = objs[i]
for (k in obj) {
if (obj.hasOwnProperty(k)) {
res[k] = obj[k]
}
}
}
return res
}
function find (list, filter) {
var k
var i = list.length
var matchs = true
while (i--) {
for (k in filter) {
if (filter.hasOwnProperty(k)) {
matchs = (filter[k] === list[i][k]) && matchs
}
}
if (matchs) { return list[i] }
}
return null
}
/* ------- parsing ------- */
/**
* Parses "@tag {type} name description"
* @param {string} str Raw doc string
* @param {Array[function]} parsers Array of parsers to be applied to the source
* @returns {object} parsed tag node
*/
function parse_tag (str, parsers) {
if (typeof str !== 'string' || str[0] !== '@') { return null }
var data = parsers.reduce(function (state, parser) {
var result
try {
result = parser(state.source, merge({}, state.data))
} catch (err) {
state.data.errors = (state.data.errors || [])
.concat(parser.name + ': ' + err.message)
}
if (result) {
state.source = state.source.slice(result.source.length)
state.data = merge(state.data, result.data)
}
return state
}, {
source: str,
data: {}
}).data
data.optional = !!data.optional
data.type = data.type === undefined ? '' : data.type
data.name = data.name === undefined ? '' : data.name
data.description = data.description === undefined ? '' : data.description
return data
}
/**
* Parses comment block (array of String lines)
*/
function parse_block (source, opts) {
var trim = opts.trim
? function trim (s) { return s.trim() }
: function trim (s) { return s }
var source_str = source
.map(function (line) { return trim(line.source) })
.join('\n')
source_str = trim(source_str)
var start = source[0].number
// merge source lines into tags
// we assume tag starts with "@"
source = source
.reduce(function (tags, line) {
line.source = trim(line.source)
if (line.source.match(/^\s*@(\S+)/)) {
tags.push({source: [line.source], line: line.number})
} else {
var tag = tags[tags.length - 1]
if (opts.join !== undefined && opts.join !== false && opts.join !== 0 &&
!line.startWithStar && tag.source.length > 0) {
var source
if (typeof opts.join === 'string') {
source = opts.join + line.source.replace(/^\s+/, '')
} else if (typeof opts.join === 'number') {
source = line.source
} else {
source = ' ' + line.source.replace(/^\s+/, '')
}
tag.source[tag.source.length - 1] += source
} else {
tag.source.push(line.source)
}
}
return tags
}, [{source: []}])
.map(function (tag) {
tag.source = trim(tag.source.join('\n'))
return tag
})
// Block description
var description = source.shift()
// skip if no descriptions and no tags
if (description.source === '' && source.length === 0) {
return null
}
var tags = source.reduce(function (tags, tag) {
var tag_node = parse_tag(tag.source, opts.parsers)
if (!tag_node) { return tags }
tag_node.line = tag.line
tag_node.source = tag.source
if (opts.dotted_names && tag_node.name.indexOf('.') !== -1) {
var parent_name
var parent_tag
var parent_tags = tags
var parts = tag_node.name.split('.')
while (parts.length > 1) {
parent_name = parts.shift()
parent_tag = find(parent_tags, {
tag: tag_node.tag,
name: parent_name
})
if (!parent_tag) {
parent_tag = {
tag: tag_node.tag,
line: Number(tag_node.line),
name: parent_name,
type: '',
description: ''
}
parent_tags.push(parent_tag)
}
parent_tag.tags = parent_tag.tags || []
parent_tags = parent_tag.tags
}
tag_node.name = parts[0]
parent_tags.push(tag_node)
return tags
}
return tags.concat(tag_node)
}, [])
return {
tags: tags,
line: start,
description: description.source,
source: source_str
}
}
/**
* Produces `extract` function with internal state initialized
*/
function mkextract (opts) {
var chunk = null
var indent = 0
var number = 0
opts = merge({}, {
trim: true,
dotted_names: false,
parsers: [
PARSERS.parse_tag,
PARSERS.parse_type,
PARSERS.parse_name,
PARSERS.parse_description
]
}, opts || {})
/**
* Read lines until they make a block
* Return parsed block once fullfilled or null otherwise
*/
return function extract (line) {
var result = null
var startPos = line.indexOf(MARKER_START)
var endPos = line.indexOf(MARKER_END)
// if open marker detected and it's not skip one
if (startPos !== -1 && line.indexOf(MARKER_START_SKIP) !== startPos) {
chunk = []
indent = startPos + MARKER_START.length
}
// if we are on middle of comment block
if (chunk) {
var lineStart = indent
var startWithStar = false
// figure out if we slice from opening marker pos
// or line start is shifted to the left
var nonSpaceChar = line.match(/\S/)
// skip for the first line starting with /** (fresh chunk)
// it always has the right indentation
if (chunk.length > 0 && nonSpaceChar) {
if (nonSpaceChar[0] === '*') {
lineStart = nonSpaceChar.index + 2
startWithStar = true
} else if (nonSpaceChar.index < indent) {
lineStart = nonSpaceChar.index
}
}
// slice the line until end or until closing marker start
chunk.push({
number: number,
startWithStar: startWithStar,
source: line.slice(lineStart, endPos === -1 ? line.length : endPos)
})
// finalize block if end marker detected
if (endPos !== -1) {
result = parse_block(chunk, opts)
chunk = null
indent = 0
}
}
number += 1
return result
}
}
/* ------- Public API ------- */
module.exports = function parse (source, opts) {
var block
var blocks = []
var extract = mkextract(opts)
var lines = source.split(/\n/)
for (var i = 0, l = lines.length; i < l; i++) {
block = extract(lines.shift())
if (block) {
blocks.push(block)
}
}
return blocks
}
module.exports.PARSERS = PARSERS
module.exports.mkextract = mkextract