forked from mafintosh/protocol-buffers-schema
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokenize.js
37 lines (34 loc) · 753 Bytes
/
tokenize.js
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
module.exports = function (sch) {
var noComments = function (line) {
var i = line.indexOf('//')
return i > -1 ? line.slice(0, i) : line
}
var noMultilineComments = function () {
var inside = false
return function (token) {
if (token === '/*') {
inside = true
return false
}
if (token === '*/') {
inside = false
return false
}
return !inside
}
}
var trim = function (line) {
return line.trim()
}
return sch
.replace(/([;,{}\(\)=\:\[\]<>]|\/\*|\*\/)/g, ' $1 ')
.split(/\n/)
.map(trim)
.filter(Boolean)
.map(noComments)
.map(trim)
.filter(Boolean)
.join('\n')
.split(/\s+|\n+/gm)
.filter(noMultilineComments())
}