c108737bc414d2c5406d10c3de43420920dd61c5
[platform/upstream/pygobject2.git] / codegen / scanvirtuals.py
1 #!/usr/bin/env python
2 import re
3 import sys
4
5
6 def main():
7     rx = re.compile(r'^\s*([\w\s\*]+)\(\s*\*\s*(\w+)\s*\)\s*\(([^()]*)\);',
8                     re.DOTALL|re.MULTILINE)
9     for f in sys.argv[1:]:
10         #print ";; From", f
11         buf = file(f).read()
12         for m in rx.findall(buf):
13             return_type =  m[0].strip()
14             if 'typedef' in return_type:
15                 continue
16             if return_type == 'void':
17                 return_type = 'none'
18             return_type = return_type.replace(' ', '')
19             virtual_name =  m[1]
20             if 'reserved' in virtual_name:
21                 continue
22             params = []
23             if not m[2]:
24                 print >> sys.stderr, repr(m)
25                 continue
26             for param in map(str.strip, m[2].split(',')):
27                 if '*' in param:
28                     tokens = param.split('*')
29                     ptype = tokens[0].strip() + '*'*(len(tokens) - 1)
30                     pname = tokens[-1].strip()
31                 else:
32                     if param == 'void':
33                         continue
34                     ptype, pname = map(str.strip, param.split())
35                 ptype = ptype.replace('const ', 'const-')
36                 while '[]' in pname:
37                     pname = pname.replace('[]', '')
38                     ptype += '[]'
39                 params.append((ptype, pname))
40             if not params:
41                 continue
42             objname = params[0][0].replace('*', '')
43             print '(define-virtual', virtual_name
44             print '  (of-object "%s")' % objname
45             print '  (return-type "%s")' % return_type
46             if len(params) > 1:
47                 print '  (parameters'
48                 for param in params[1:]:
49                     print '    \'("%s" "%s")' % param
50                 print '  )'
51             print ')'
52
53 if __name__ == '__main__':
54     main()