-
Notifications
You must be signed in to change notification settings - Fork 0
/
hppuml.py
379 lines (324 loc) · 9.8 KB
/
hppuml.py
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import re
def clean_chars(data):
''' remove irregular characters '''
data = data.replace('\r', '')
return data
def _replace_ab(data, a, b, sub):
''' remove stuff between a and b and replace with sub '''
start = data.find(a)
if start >= 0:
stop = data.find(b, start)
if stop >= 0:
data = data[:start] + sub + data[stop + len(b):]
return True, data
return False, data
def _remove_ab(data, a, b):
''' remove stuff between a and b '''
return _replace_ab(data, a, b, '')
def remove_comments(data):
''' remove comments '''
found = True
while found:
found, data = _remove_ab(data, '/*', '*/')
found = True
data += '\n'
while found:
found, data = _remove_ab(data, '//', '\n')
found = True
data += '\n'
while found:
found, data = _remove_ab(data, '#', '\n')
return data
def clean_format(data):
''' clean up format with no content change '''
# add space to beginning and end
data = ' ' + data + ' '
# add spacing for key separators
for c in [';', '(', ')', '{', '}', '[', ']', ',', '*']:
data = data.replace(c, ' ' + c + ' ')
# replace tab and new line with space
data = data.replace('\t', ' ')
data = data.replace('\n', ' ')
# deal with colon
data = data.replace(':', ' : ')
# since : has been padded in last line, we expect some space here
data = re.sub(' *: : *', '::', data)
# <> shouldn't have spaces inside them
data = re.sub('< *', '<', data)
data = re.sub(' *>', '>', data)
# reduce all padding to 1 space only
data = re.sub(' *', ' ', data)
# * should follow type name
data = data.replace(' *', '*')
return data
def replace_keywords(data):
''' replace keywords to make parsing easier '''
def _derive_brackets(data):
''' find a list of {} in stream '''
l = []
for i, c in enumerate(data):
if c == '{':
l.append((i, 1))
elif c == '}':
l.append((i, -1))
return l
def _integrate_paths(der, data_length):
''' integrate hierarchical paths for each section from the derivative '''
# 0 is the global level
level = 0
stack = []
paths = []
# add a ender to trick the loop into picking up the last piece
der.append([data_length-1, -1])
for i, e in der:
# add to stack
if len(stack) == level:
# creating a child also increments the parent
if level > 0:
stack[level-1] += 1
stack.append(0)
else:
stack[level] += 1
# clear stacks beyond this one, since we incremented
stack = stack[:level+1]
# only keep the relevent stacks as path
paths.append((i, stack[:level+1]))
level += e
return paths
def _assign_path(l, data, path):
''' assign data to the nested list defined by path '''
curr = l
# create empty lists to prepare for the last item
for path_level, path_i in enumerate(path[:-1]):
while len(curr) <= path_i:
curr.append([])
curr = curr[path_i]
# fill the path with data for last iteration
last_i = path[-1]
while len(curr) <= last_i:
curr.append([])
curr[last_i] = data
def _fulfill_paths(data, paths):
''' fetch sections of data based on paths '''
filled = []
last_i = -1
for i, path in paths:
# create the structure to hold the data
_assign_path(filled, data[last_i+1:i], path)
last_i = i
return filled
def collect_scope(data):
''' turn stream data into nested list of scopes '''
# find the 'derivative' of {}
der = _derive_brackets(data)
# find paths by integrating the derivative
paths = _integrate_paths(der, len(data))
# fetch data based on paths
filled = _fulfill_paths(data, paths)
return filled
def _clean_generic_string(data):
''' clean generic string (not in a class) to remove noise '''
# anything before the last ; is noise
start = data.rfind(';')
if start > 0:
data = data[start+1:]
# we only care about class
if 'class' in data:
# we don't care about inherentence
data = re.sub('class', '', data)
data = re.sub(':.*$', '', data)
data = data.strip()
else:
data = ''
return data
def remove_noise(scopes):
''' find and remove stuff we don't care about '''
for i in range(len(scopes)):
if isinstance(scopes[i], basestring):
# we process the strings
scopes[i] = _clean_generic_string(scopes[i])
else:
# for a non-class scope, process deeper
if not scopes[i-1]:
scopes[i] = remove_noise(scopes[i])
# remove empties
scopes = [a for a in scopes if a]
# clean hollow lists
if len(scopes) == 1:
return scopes[0]
else:
return scopes
def class_collapse(scopes):
''' collapse nested list of scopes and make map of classes '''
classes = {}
for i in range(len(scopes)):
if not isinstance(scopes[i], basestring):
# the last in the list must be our class name
# todo: maybe support nested classes
name = scopes[i-1]
# ignore all internal scopes and replace with ;
value = ';'.join([a for a in scopes[i] if isinstance(a, basestring)])
classes[name] = value
return classes
def remove_class_noise(data):
''' remove things we don't care about inside a class '''
for s in ['virtual', 'const', 'override', 'explicit',
# Qt specific
'Q_OBJECT', 'slots']:
data = data.replace(' ' + s + ' ', ' ')
found = True
while found:
found, data = _remove_ab(data, ' typedef', ';')
data = re.sub('= [^,^;^)]*', '', data)
return data
def _extract_protection(data, prot):
''' extract the protection level '''
# only look for isolated : , must ignore ::
start = data.find(' : ')
if start > 0:
prot = data[:start].strip()
data = data[start+3:]
return data, prot
def _split_name_type(data):
''' split a expression to name and type '''
data = data.strip()
start = data.rfind(' ')
if start > 0:
name = data[start+1:]
type_ = data[:start]
return name, type_
else:
# not sure what to do, probably ctor / dtor
return data, ''
def _clean_method_name(data):
''' clean name (and param list) of a method '''
data = re.sub(' *\( *', '(', data)
data = re.sub(' *\) *', ')', data)
data = re.sub(' *, *', ', ', data)
return data
def process_class(data):
''' process class text into list of members and methods '''
lines = []
prot = 'private'
for line in data.split(';'):
# ignore empty expressions
if not line.strip():
continue
line, prot = _extract_protection(line, prot)
role = 'member'
name, type_ = _split_name_type(line)
start = line.find('(')
if start > 0:
role = 'method'
name, type_ = _split_name_type(line[:start])
name = _clean_method_name(name + line[start:])
lines.append({
'prot': prot,
'role': role,
'name': name,
'type': type_
})
return lines
def _output_sep():
''' output separator '''
return '-'*40 + '\n'
def _get_marker(prot):
''' get the marker for protection level '''
if prot == 'public':
return '+'
elif prot == 'signals':
return '<'
elif prot == 'protected':
return '#'
elif prot == 'private':
return '-'
else:
# treat others as private
return '-'
def _output_line(e):
''' output a method / member in format '''
return _get_marker(e['prot']) + e['name'] + ': ' + e['type'] + '\n'
def output_class(class_, lines):
''' output class in nice format '''
s = _output_sep()
s += class_ + '\n'
s += _output_sep()
for e in lines:
if e['role'] == 'member':
s += _output_line(e)
s += _output_sep()
for e in lines:
if e['role'] == 'method':
s += _output_line(e)
s += _output_sep()
return s
def get_html(data, output):
template = '''
<!DOCTYPE html>
<html>
<head>
<style>
input[type=submit],
body {
font-family: "Liberation Mono";
font-size: 12pt;
}
footer {
position: fixed;
bottom: 10px;
right: 30px;
}
.adjuster {
display: flex;
justify-content: center;
}
.horizontal {
display: inline-block;
padding: 10px;
}
</style>
</head>
<body>
<div class="adjuster">
<div class="horizontal">
<form method="POST" action="/bettercallshao/hppuml/">
<p>
<span> Input: paste C++ class header here to </span>
<span> <input type="submit" value="convert"/> </span>
</p>
<textarea name="data" cols="80" rows="40">{{data}}</textarea>
</form>
</div>
<div class="horizontal">
<p> Output: UML styled text </p>
<textarea name="data" cols="80" rows="40">{{output}}</textarea>
</div>
</div>
<footer>
<a href="https://github.com/bettercallshao/hppuml">github:bettercallshao/hppuml</a>
</footer>
</body>
</html>
'''
return template.replace('{{data}}', data).replace('{{output}}', output)
def run(data):
''' entry point for overall process '''
source_data = data
data = clean_chars(data)
data = remove_comments(data)
data = clean_format(data)
scopes = collect_scope(data)
scopes = remove_noise(scopes)
classes = class_collapse(scopes)
output = ''
for class_ in classes:
data = classes[class_]
data = remove_class_noise(data)
lines = process_class(data)
output += output_class(class_, lines)
return get_html(source_data, output)
if __name__ == '__main__':
data = 'class A { public: int foo(); int bar; }'
if 'data' in Hook['params']:
data = Hook['params']['data']
print(run(data))