This repository has been archived by the owner on Apr 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate.py
executable file
·743 lines (669 loc) · 25.6 KB
/
generate.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
#!/usr/bin/env python
# Copyright (c) 2015 Kenneth Benzie
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from __future__ import print_function
from os import path
import xml.etree.ElementTree as XML
import getopt
import json
import sys
indent = ' '
prefix = ''
functions_only = False
includes = []
stub = None
stub_includes = []
stub_guards_on = False
stub_prefix = ''
stub_qualifier = ''
variables = []
class Variable:
name = ''
values = []
def __init__(self, name, values):
self.name = name
self.values = values
class DoxygenParam:
__name = None
__text = None
__form = None
def __init__(self, name, node):
if None == name:
raise Exception('Parameter name must not be None')
self.__name = name
if None != node:
param = node.find('param')
if None != param:
self.__text = param.text
form = param.attrib.get('form')
if '' != form:
self.__form = form
def output(self):
param = ''
if None != self.__text:
param += '@param'
if '' != self.__form:
param += '[' + self.__form + '] '
else:
param += ' '
param += self.__name + ' ' + self.__text
return param
class Doxygen:
brief = None
detail = None
params = []
ret = None
see = None
def __init__(self, node):
self.init(node)
def init(self, node):
self.params = []
if None != node:
brief = node.find('brief')
if None != brief:
self.brief = brief.text
else:
self.brief = None
detail = node.find('detail')
if None != detail:
self.detail = detail.text
else:
self.detail = None
ret = node.find('return')
if None != ret:
self.ret = ret.text
else:
self.ret = None
see = node.find('see')
if None != see:
self.see = see.text
else:
self.see = None
else:
self.brief = None
self.detail = None
self.ret = None
self.see = None
def empty(self):
if None == self.brief and None == self.detail and 0 == \
len(self.params) and None == self.ret and None == self.see:
return True
return False
def output(self):
sections = []
if None != self.brief:
sections.append('/// ' + '@brief ' + replace_prefix(self.brief) + '\n')
if None != self.detail:
detail = ''
for line in self.detail.split('\n'):
detail += '/// ' + line + '\n'
sections.append(replace_prefix(detail))
if 0 != len(self.params):
param_section = ''
for param in self.params:
param_section += '/// ' + param + '\n'
if '' != param_section:
sections.append(replace_prefix(param_section))
if None != self.ret:
sections.append('/// @return ' + replace_prefix(self.ret) + '\n')
if None != self.see:
sections.append('/// @see ' + replace_prefix(self.see) + '\n')
text = ''
if 0 != len(sections):
text = '///\n'.join(sections)
return text.strip()
def is_identifier(identifier):
nondigits = ['_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z']
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
if not identifier[:1] in nondigits:
return False
characters = nondigits + digits
for char in identifier[1:]:
if not char in characters:
return False
return True
def replace_prefix(identifier):
output = identifier.replace("${prefix}", prefix)
output = output.replace("${Prefix}", prefix.capitalize())
output = output.replace("${PREFIX}", prefix.upper())
return output
def replace_stub_prefix(identifier, name = ''):
output = identifier.replace("${stub_prefix}", name)
output = output.replace("${Stub_Prefix}", name.capitalize())
output = output.replace("${STUB_PREFIX}", name.upper())
return output
def replace_variables(text):
start = text.find('${')
while -1 != start:
end = text.find('}')
var_name = text[start + 2:end]
replaced = False
for variable in variables:
if var_name in variable.name:
text = text.replace('${' + var_name + '}', variable.values[0])
replaced = True
if not replaced:
raise Exception('could not replace:', var_name)
start = text.find('${')
return text
def replace_stub(text, name, arguments):
stub = ''
text = text.replace("${name}", name.replace("${prefix}", ""))
capturing = False
loop_variable = None
loop_iterator = ''
loop_lines = []
for line in text.split('\n'):
if '${foreach}' in line:
capturing = True
# Reset loop state
loop_variable = None
loop_iterator = ''
loop_lines = []
expr = line[line.find('(') + 1:line.find(')')]
in_pos = expr.find('in')
iter_name = expr[:in_pos].strip()
var_name = expr[in_pos + 2:].strip()
for variable in variables:
if var_name == variable.name:
for value in variable.values:
loop_variable = variable
loop_iterator = iter_name
if None == loop_variable:
raise Exception('invalid ${foreach} variable', var_name)
elif '${endforeach}' in line:
# TODO: Write loop
for value in loop_variable.values:
for line in loop_lines:
stub += line.replace('${' + loop_iterator + '}', value) + '\n'
capturing = False
elif capturing:
loop_lines.append(line)
else:
stub += line + '\n'
stub = stub.replace("${forward}", ', '.join(arguments))
# TODO: Support any numbered argument!
stub = stub.replace("${0}", arguments[0])
stub = replace_stub_prefix(stub, stub_prefix)
stub = replace_prefix(stub)
return replace_variables(stub)
def include(node, newline):
if not functions_only:
if None == node.text:
raise Exception('missing include file')
name = replace_prefix(node.text.strip())
include = '#' + node.tag + ' '
form = node.attrib.get('form')
if None == form or 'angle' == form:
include += '<' + name + '>'
elif 'quote' == form:
include += '"' + name + '"'
else:
raise Exception('invalid include form: ' + form)
if newline:
include += '\n'
print(include)
def define(node, newline):
if not functions_only:
docs = Doxygen(node.find('doxygen'))
define = '#' + node.tag + ' ' + replace_prefix(node.text.strip()).upper()
params = node.findall('param')
# TODO Output nice diagnostics for unexpected input, use is_identifier()
if 0 < len(params):
param_names = []
for param in params:
param_names.append(replace_prefix(param.text.strip()))
define += '(' + ', '.join(param_names) + ')'
value = node.find('value')
if None != value:
lines = value.text.split('\n')
if 1 < len(lines):
continuation = ' \\\n'
define += continuation + indent + (continuation + indent).join(lines)
elif 1 == len(lines):
define += ' ' + lines[0]
if newline:
define += '\n'
if not docs.empty():
print(docs.output())
print(define)
def struct(node, semicolon, newline):
if not functions_only:
doxygen = Doxygen(node.find('doxygen'))
struct = 'struct'
if node.text:
name = replace_prefix(node.text.strip())
if not is_identifier(name):
raise Exception('invalid struct name: ' + name)
struct += ' ' + name
scope = node.find('scope')
# TODO Output nice diagnostics for unexpected input, use is_identifier()
if None != scope:
members = scope.findall('member')
if 0 < len(members):
struct += ' {'
member_decls = []
for member in members:
if None != member:
doxygen_member = Doxygen(member.find('doxygen')).output()
type = member.find('type')
if None != type:
member_decl = ''
if '' != doxygen_member:
doxygen_members = doxygen_member.split('\n')
doxygen_member = ''
for line in doxygen_members:
doxygen_member += indent + line + '\n'
doxygen_member = doxygen_member.rstrip()
member_decl += doxygen_member + '\n'
member_decl += indent + replace_prefix(type.text.strip())
if member.text:
member_decl += ' ' + \
replace_prefix(member.text.strip())
member_decls.append(member_decl)
member_function = member.find('function')
if None != member_function:
function_form = member_function.attrib.get('form')
if 'pointer' != function_form:
raise Exception('struct member function is not a function pointer')
member_decl = ''
if '' != doxygen_member:
member_decl += indent + doxygen_member + '\n'
member_decl += indent + function(member_function, False, False, False)
member_decls.append(member_decl)
member_union = member.find('union')
if None != member_union:
union_decls = []
if '' != doxygen_member:
union_decls.append(doxygen_member)
union_decls.extend(union(member_union, False, False, False).split('\n'))
union_decl = '\n'.join([indent + decl for decl in union_decls])
member_decls.append(union_decl)
if 0 < len(member_decls):
struct += '\n' + ';\n'.join(member_decls) + ';\n'
struct += '}'
if semicolon:
struct += ';'
if newline:
struct += '\n\n'
docs = doxygen.output()
if '' != docs:
print(docs)
sys.stdout.write(struct)
def union(node, semicolon, newline, out = True):
if not functions_only:
union = 'union'
if node.text:
name = replace_prefix(node.text.strip())
if not is_identifier(name):
raise Exception('invalid union name: ' + name)
union += ' ' + name
scope = node.find('scope')
if None != scope:
members = scope.findall('member')
if 0 < len(members):
union += ' {'
member_decls = []
for member in members:
if None != member:
member_name = replace_prefix(member.text.strip())
type = member.find('type')
if None != type:
member_decl = indent + replace_prefix(type.text.strip())
if None == member.text:
raise Exception('union member has no name')
member_decl += ' ' + member_name
member_decls.append(member_decl)
struct = member.find('struct')
if None != struct:
struct_name = replace_prefix(struct.text.strip())
if None != struct_name:
member_decl = indent + 'struct ' + struct_name + \
' ' + member_name
member_decls.append(member_decl)
if 0 < len(member_decls):
union += '\n' + ';\n'.join(member_decls) + ';\n'
union += '}'
if semicolon:
union += ';'
if newline:
union += '\n\n'
if out:
sys.stdout.write(union)
else:
return union
def enum(node, semicolon, newline):
if not functions_only:
enum = 'enum'
doxygen = Doxygen(node.find('doxygen')).output()
if '' != doxygen:
enum = doxygen + '\n' + enum
if node.text:
name = node.text.strip()
if '' != name:
enum += ' ' + replace_prefix(name)
enum += ' {'
scope = node.find('scope')
# TODO Output nice diagnostics for unexpected input, use is_identifier()
if None == scope:
raise Exception("missing enum scope tag")
constants = scope.findall('constant')
if 0 < len(constants):
enum += '\n'
constant_decls = []
for constant in constants:
if None != constant:
decl = ''
doxygen = Doxygen(constant.find('doxygen')).output()
if '' != doxygen:
for line in doxygen.split('\n'):
decl = indent + line + '\n'
if None == constant.text:
raise Exception("invalid enum constant")
decl += indent + replace_prefix(constant.text.strip())
value = constant.find('value')
if None != value:
decl += ' = ' + replace_prefix(value.text.strip())
constant_decls.append(decl)
enum += ',\n'.join(constant_decls) + '\n'
enum += '}'
if semicolon:
enum += ';'
if newline:
enum += '\n\n'
sys.stdout.write(enum)
def typedef(node, newline):
if not functions_only:
docs = Doxygen(node.find('doxygen'))
name = replace_prefix(node.text.strip())
if None == name:
raise Exception('missing typedef type name')
type = node.find('type')
if None == type:
raise Exception('missing typedef type')
if '' != docs:
print(docs.output())
sys.stdout.write('typedef ')
if None != type.getchildren():
generate(type, False, False)
if None != type.text:
sys.stdout.write(replace_prefix(type.text.strip()))
sys.stdout.write(' ' + name);
print(';\n')
def function(node, semicolon, newline, out = True):
doxygen = Doxygen(node.find('doxygen'))
if None == node.text:
raise Exception('missing function name')
return_type = node.find('return')
if None == return_type:
raise Exception('missing function return')
if None == return_type.text:
raise Exception("missing function return type name")
doxygen_return = return_type.find('doxygen')
if None != doxygen_return:
tag = doxygen_return.find('return')
if None != tag:
doxygen.ret = tag.text
function = replace_prefix(return_type.text.strip()) + ' '
prefix_name = node.text.strip()
name = replace_prefix(replace_stub_prefix(prefix_name, stub_prefix))
prefix_name = replace_stub_prefix(prefix_name)
if None != stub:
name = name
form = node.attrib.get('form')
if None != form:
if 'pointer' == form:
function += '(*' + name + ')('
else:
raise Exception('invalid function form: ' + form)
else:
function += name + '('
params = node.findall('param')
doxygen.params = []
param_names = []
if 0 < len(params):
param_decls = []
for param in params:
param_type = param.find('type')
if None == param_type:
raise Exception('missing function parameter type')
if None == param_type.text:
raise Exception('missing function parameter type name')
decl = replace_prefix(param_type.text.strip())
if None != param.text:
decl += ' ' + replace_prefix(param.text.strip())
doxygen_param = DoxygenParam(param.text, param.find('doxygen')).output()
if '' != doxygen_param:
doxygen.params.append(doxygen_param)
param_names.append(param.text)
param_decls.append(decl)
function += ', '.join(param_decls)
function += ')'
if semicolon:
function += ';'
if newline:
function += '\n'
if not doxygen.empty() and None == stub:
function = doxygen.output() + '\n' + function
if out:
print(function)
if None != stub:
print('{\n' + replace_stub(stub.text, prefix_name, param_names) + '\n}\n')
else:
return function
def comment(node, newline):
comment = '// '
if node.text:
lines = node.text.split('\n')
comment += '\n// '.join(lines)
if newline:
comment += '\n'
print(comment)
def block(node):
generate(node, False, False)
print()
def scope(node, semicolon, newline):
scope = ''
open = True
close = True
form = node.attrib.get('form')
if form:
if 'open' == form:
close = False
elif 'close' == form:
open = False
else:
raise Exception('invalid scope form: ' + form)
name = ''
if node.text:
name = replace_prefix(node.text.strip())
if open:
if '' == name:
print('{')
else:
print(name + ' {')
generate(node, semicolon, newline)
if close:
if '' == name:
print('}')
else:
print('} // ' + name)
if newline:
print()
def guard(node, semicolon, newline):
if not node.text:
raise Exception('missing guard name')
name = replace_prefix(replace_stub_prefix(node.text.strip(), stub_prefix))
form = node.attrib.get('form')
if 'include' == form:
print('#ifndef ' + name)
print('#define ' + name + '\n')
generate(node, semicolon, True)
print('#endif // ' + name)
elif 'defined':
print('#ifdef ' + name)
generate(node, semicolon, False)
print('#endif // ' + name)
else:
print('#ifndef ' + name)
generate(node, semicolon, False)
print('#endif // ' + name)
if newline:
print()
def code(node):
if node.text:
print(replace_prefix(node.text))
def includes_stubs():
print(replace_prefix('#include <${prefix}/${prefix}.h>'))
for stub_include in stub_includes:
if '${foreach}' in stub_include:
loop = stub_include[stub_include.find('(') + 1 : stub_include.find(')')].split(' ')
elem = loop[0]
var_name = loop[2]
expr = stub_include[stub_include.find(')') + 1 : stub_include.find('${endforeach}')]
for variable in variables:
if var_name == variable.name:
for value in variable.values:
print('#include <' + expr.replace('${' + elem + '}', value) + '>')
else:
print(replace_prefix('#include <' + stub_include + '>'))
print()
def generate(parent, semicolon = True, newline = True):
global prefix
if None == stub:
for node in parent:
if 'include' == node.tag:
include(node, newline)
elif 'define' == node.tag:
define(node, newline)
elif 'struct' == node.tag:
struct(node, semicolon, newline)
elif 'union' == node.tag:
union(node, semicolon, newline)
elif 'enum' == node.tag:
enum(node, semicolon, newline)
elif 'typedef' == node.tag:
typedef(node, newline)
elif 'function' == node.tag:
function(node, semicolon, newline)
elif 'comment' == node.tag:
comment(node, newline)
elif 'block' == node.tag:
block(node)
elif 'scope' == node.tag:
scope(node, semicolon, newline)
elif 'guard' == node.tag:
guard(node, semicolon, newline)
elif 'code' == node.tag:
code(node)
else:
for node in parent:
if 'comment' == node.tag:
comment(node, True)
elif 'guard' == node.tag:
if stub_guards_on:
guard(node, True, True)
else:
for guard_node in node:
if 'function' == guard_node.tag:
function(guard_node, False, False)
elif 'scope' == node.tag:
scope(node, True, False)
elif 'function' == node.tag:
if '' != stub_qualifier:
sys.stdout.write(stub_qualifier + ' ')
function(node, False, False)
elif 'block' == node.tag:
# TODO: This is a hack to place include's correctly, it is not
# a general solution as the includes will be inserted more
# than once if there is more than <block></block> in the schema.
# This should be replaced with a general solution if it causes
# any problems.
includes_stubs()
def help():
print('generate.py [options] <schema>\n')
print('options:')
print(' -h show this help message')
print(' -p <prefix> identifier to be prefixed')
print(' -s <name> output function stubs')
print(' -v <variable>:<value>[;...] add user variable')
def main():
global indent
global prefix
global functions_only
global stub_guards_on
global stub
global stub_includes
global stub_prefix
global stub_qualifier
if 1 == len(sys.argv):
help()
sys.exit(1)
# TODO Add options for outputting header or source files
options, arguments = getopt.getopt(sys.argv[1:], 'hp:s:v:fg')
if 0 == len(arguments):
raise Exception('missing schema file')
if 1 != len(arguments):
for arg in arguments:
raise Exception('invalid argument:', arg)
schema = arguments[0]
if not path.exists(schema) or not path.isfile(schema):
raise Exception('invalid schema file:', schema);
tree = XML.parse(schema)
interface = tree.getroot()
for opt, arg in options:
if opt in ('-h'):
help()
sys.exit(0)
elif opt in ('-p'):
if not is_identifier(arg):
raise Exception('invalid C prefix:', arg)
prefix = arg
elif opt in ('-s'):
stubs = interface.find('stubs')
for node in stubs:
if 'stub' == node.tag:
if arg == node.attrib.get('name'):
stub = node
prefix_stub = node.attrib.get('prefix')
if '' != prefix_stub:
stub_prefix = prefix_stub
qual = node.attrib.get('qualifier')
if '' != qual:
stub_qualifier = qual
elif 'include' == node.tag:
stub_includes.append(node.text)
if None == stub:
raise Exception('could not find stub named:', arg)
elif opt in ('-v'):
name_end = str(arg).find(':')
variable = Variable(arg[0:name_end], arg[name_end + 1:].split(';'))
variables.append(variable)
elif opt in ('-f'):
functions_only = True
elif opt in ('-g'):
stub_guards_on = True
generate(interface)
if __name__ == '__main__':
main()