-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.js
173 lines (136 loc) · 3.37 KB
/
parse.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
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
module.exports = source => {
const DOC_RE = /\/\/\/(.*)[\n|\r]/g
let docr = DOC_RE.exec(source)
let lines = []
if (!docr) {
return {}
}
while (docr) {
if (docr[1]) {
lines.push(docr[1].trim())
}
docr = DOC_RE.exec(source)
}
const output = {
namespace: [],
types: {}
}
let lastType = null
let lastMethod = null
let hasConstructor = false
let last = null
const parseParams = line => {
const params = {}
line = line.replace(/\((.*)\)/, (_, sig) => {
sig.split(',').filter(Boolean).forEach(param => {
let defaultValue
param = param.replace(/\s*=\s*(.*)$/, (_, val) => {
defaultValue = val
return ''
})
const name = param.match(/\w+$/)
if (!name) {
console.error('missing param name', line)
}
params[name] = {
const: !!param.match(/\s+const|const\s+/),
default: defaultValue,
reference: !!param.match(/&/),
pointer: !!param.match(/\*/),
comment: ''
}
})
return ''
})
return params
}
const parseLine = line => {
const words = line.split(' ')
const keyword = words.shift()
switch (keyword) {
case 'namespace': {
output.namespace.push(words[0])
break
}
case 'comment': {
if (!last.comment) {
last.comment = ''
}
last.comment += ' ' + words.join(' ')
last.comment = last.comment.trim()
break
}
case 'function': {
lastType = words[0].match(/\w+/)[0]
last = lastMethod = output.types[lastType] = {
raw: words.join(' '),
params: parseParams(line)
}
break
}
case 'struct':
case 'class': {
lastType = words.shift()
last = output.types[lastType] = { members: [] }
break
}
case 'property': {
last = {
TYPE: words,
type: keyword,
name: words.pop()
}
output.types[lastType].members.push(last)
break
}
case 'operator': {
last = {
type: keyword,
name: words.join(' ')
}
output.types[lastType].members.push(last)
break
}
case 'return': {
const type = words[0]
last = {
TYPE: type,
reference: type.includes('&'),
pointer: type.includes('*')
}
lastMethod['return'] = last
break
}
case 'param': {
const identifier = words.shift()
if (!last.params[identifier]) {
console.error(`Unknown parameter ${identifier}`)
process.exit(1)
}
last.params[identifier].comment += words.join(' ')
break
}
case 'overload':
case 'constructor':
case 'method': {
last = lastMethod = {
name: /\w+/.exec(words[0])[0],
raw: words.join(' '),
type: keyword,
overload: keyword === 'overload',
params: parseParams(line)
}
if (keyword === 'constructor' && hasConstructor) {
lastMethod.overload = true
}
if (keyword === 'constructor') {
hasConstructor = true
}
output.types[lastType].members.push(lastMethod)
break
}
}
}
lines.forEach(parseLine)
return output
}