26
|
1 #!/usr/bin/env python
|
|
2 # -*- coding: utf-8 -*-
|
|
3
|
|
4 import sys
|
|
5 import re
|
|
6
|
|
7 reserved_words = [ "if", "for", "switch", "return", "while", "else", ]
|
|
8
|
|
9 #PATTERN = r"([a-zA-Z_]\w*)\s+([a-zA-Z_]\w*)\s*\(([^;]*)\)\s*\{"
|
|
10 #PATTERN = r"((?:[a-zA-Z_]\w*)\s+)+?([a-zA-Z_]\w*)\s*\(([^;]*)\)\s*\{"
|
|
11 PATTERN = r"([a-zA-Z_][\w\s]*\**)\s([a-zA-Z_]\w*)\s*\(([^/;]*)\)\s*\{"
|
|
12 # TODO: 関数パラメータ内にコメントがあると正しく動かない!
|
|
13 PROG = re.compile(PATTERN, re.S)
|
|
14
|
|
15 def truncate_comments(data):
|
|
16 pass
|
|
17
|
|
18 def check_reserved_word(decl):
|
|
19 """ return true if decl's type and name is not reserved word. """
|
|
20
|
|
21 if decl["name"] in reserved_words or decl["type"] in reserved_words:
|
|
22 return False
|
|
23 return True
|
|
24
|
|
25 def read_decls(file):
|
|
26 declarators = []
|
|
27
|
|
28 # open the file and read all lines into a string.
|
|
29 try:
|
|
30 fo = open(file, 'r')
|
|
31 lines = fo.readlines()
|
|
32 data = "".join(lines)
|
|
33 truncate_comments(data)
|
|
34 except IOError:
|
|
35 print "cannot read file %s" % file
|
|
36
|
|
37 # find all matched strings.
|
|
38 # moiter is iterator of MatchObject.
|
|
39 moiter = PROG.finditer(data)
|
|
40 for mo in moiter:
|
|
41 tmp = { "type": mo.group(1),
|
|
42 "name": mo.group(2),
|
|
43 "parms": mo.group(3),
|
|
44 "offset": mo.start() }
|
|
45 if check_reserved_word(tmp):
|
|
46 declarators.append(tmp)
|
|
47
|
|
48 return declarators
|
|
49
|
|
50 def debug_print(decl):
|
|
51 for (key,value) in decl.items():
|
|
52 if isinstance(value, str):
|
|
53 decl[key] = value.replace("\n"," ").replace("\t"," ")
|
|
54
|
|
55 print "Type:\t%s" % decl["type"]
|
|
56 print "Name:\t%s" % decl["name"]
|
|
57 print "Params:\t%s" % decl["parms"]
|
|
58 print "offset:\t%d" % decl["offset"]
|
|
59 print ""
|
|
60 #s = "%s %s ( %s );" % (decl["type"], decl["name"], decl["parms"])
|
|
61 #print s, "/* offset: %d */" % decl["offset"]
|
|
62
|
|
63 def format_print(decl, file):
|
|
64 for (key,value) in decl.items():
|
|
65 if isinstance(value, str):
|
|
66 decl[key] = value.replace("\n"," ").replace("\t"," ")
|
|
67
|
|
68 print "/* defined in file %s at offset %d */" % (file,decl["offset"])
|
|
69 print "%s %s (%s);" % (decl["type"],decl["name"],decl["parms"])
|
|
70 print ""
|
|
71
|
|
72 def main():
|
|
73 for file in sys.argv[1:]:
|
|
74 decls = read_decls(file)
|
|
75
|
|
76 if decls==None or len(decls)==0:
|
|
77 print "%s have no function definition!" % file
|
|
78 continue
|
|
79 for decl in decls:
|
|
80 #debug_print(decl)
|
|
81 format_print(decl, file)
|
|
82 #print decls[0]
|
|
83
|
|
84
|
|
85 main()
|
|
86
|