This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
/
generate_complement.py
executable file
·215 lines (174 loc) · 7.96 KB
/
generate_complement.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
#!/usr/bin/python
#
# ----------------------------------------------------------------------------
# Simple regular expression that obtains super class, protocols
# and properties from Obj-C header files
# ----------------------------------------------------------------------------
#
# Author: Ricardo Quesada
# Copyright 2012 (C) Zynga, Inc
#
# License: MIT
#
'''
Parses objective-c header files and obtains protocol information, class hierarchy information and properties information since
this information is not present in BridgeSupport.
'''
__docformat__ = 'restructuredtext'
# python
import sys
import re
import getopt
import ast
class ObjC(object):
def __init__(self, filenames, exception_file, output_file, verbose):
self.filenames = filenames
self.entries = {}
self.output = output_file
self.exception = exception_file
self.verbose = verbose
def log(self, what):
if self.verbose:
print what
def parse_exception_file(self):
if self.exception:
f = open(self.exception)
self.entries = ast.literal_eval(f.read())
f.close()
def parse(self):
self.parse_exception_file()
for filename in self.filenames:
f = open(filename, 'r')
l = f.readlines()
# regexp based on Objective Pascal parser ( http://www.objectivepascal.com/ )
regex_objc_class = r"^\s*@interface\s+([\S]+)\s*:\s*([\w]+)\s*(<.*>)*"
regex_objc_class_extension = r"^\s*@interface\s+([\S]+)\s*\(\s*([\w]+)\s*\)\s*"
regex_objc_property_attributes = r"^@property\s*\(([^)]*)\)\s*([a-zA-Z_0-9]+)\s*(<.*>)?(\bint\b|\blong\b|\bchar\b)?(\*)*\s*(.*)\s*;"
regex_objc_protocol = r"^\s*@protocol\s+([\S]+)\s*(<.*>)*"
regex_obj_end = r"^\s*@end\s*$"
current_class = None
for line in l:
interface = re.match(regex_objc_class, line)
interface_ext = re.match(regex_objc_class_extension, line)
property_attribs = re.match(regex_objc_property_attributes, line)
protocol_declaration = re.match(regex_objc_protocol, line)
end = re.match(regex_obj_end, line)
# Found @interface
if interface:
classname = interface.group(1)
subclass = interface.group(2)
protocols = interface.group(3)
if classname in self.entries:
self.log('Key already on dictionary %s\n' % classname)
else:
if protocols:
# strip '<>'
protocols = protocols.strip('<> ')
# remove spaces
protocols = protocols.replace(' ', '')
# split by ','
protocols = protocols.split(',')
else:
protocols = []
self.entries[classname] = {'subclass': subclass, 'protocols': protocols}
current_class = classname
self.log('--> %s' % current_class)
# Found @interface bis
elif interface_ext:
classname = interface_ext.group(1)
current_class = classname
self.log('--> %s' % current_class)
# Found @protocol
elif protocol_declaration:
classname = protocol_declaration.group(1)
protocols = protocol_declaration.group(2)
if classname in self.entries:
self.log('Key already on dictionary %s\n' % classname)
else:
if protocols:
# strip '<>'
protocols = protocols.strip('<> ')
# remove spaces
protocols = protocols.replace(' ', '')
# split by ','
protocols = protocols.split(',')
else:
protocols = []
self.entries[classname] = {'protocols': protocols}
current_class = classname
# Found @property
elif property_attribs:
if not current_class:
raise Exception("Fatal: Unparented attrib: %s (%s)" % (str(property_attribs.groups()), filename))
if not 'properties' in self.entries[current_class]:
self.entries[current_class]['properties'] = {}
l = []
# 1: attributes
# 2: type
# 3: type protocol (optional)
# 4: type 2nd word like int, long, char (optional)
# 5: type pointer '*' (optional)
# 6: property name
l.append(property_attribs.group(1))
if property_attribs.group(4):
l.append("%s %s%s%s" % (property_attribs.group(2), property_attribs.group(3), property_attribs.group(4), property_attribs.group(5)))
else:
l.append("%s%s%s%s" % (property_attribs.group(2), property_attribs.group(3), property_attribs.group(4), property_attribs.group(5)))
if ' ' in property_attribs.group(6) or property_attribs.group(6) == None:
sys.stderr.write('Error. Could not add property. File:%s line:%s\n' % (filename, line))
print property_attribs.groups()
else:
attrib_name = property_attribs.group(6)
# XXX HACK XXX. I should fix the regexp instead of doing this hack
# strip possible '*' from it
attrib_name = attrib_name.replace('*', '')
self.entries[current_class]['properties'][attrib_name] = l
# Found @end
elif end:
self.log('<-- %s (%s)' % (current_class, filename))
current_class = None
else:
# ignore
pass
f.close()
self.write_output()
def write_output(self):
if not self.output:
fd = sys.stdout
else:
fd = open(self.output, 'w')
fd.write(str(self.entries))
def help():
print "%s v1.0 - An utility to obtain superclass and protocols from an Objective-C interface" % sys.argv[0]
print "Usage:"
print "\t-o --output\tFile that will have the output. Default: stdout"
print "\t-e --exception\tFile that contains the exception rules for the parser. Exception rules have precedence over the parsed data"
print "\t\t\tUseful if the parser generates invalid or wrong info for certain classes."
print "\t-v --verbose\tVerbose output. Useful to find possible errors."
print "\nExample:"
print "\t%s -o cocos2d-mac-class_hierarchy-protocols.txt -e cocos2d-mac-class_hierarchy-protocols-exceptions.txt *.h Support/*.h Platforms/*.h Platforms/Mac/*.h " % sys.argv[0]
sys.exit(-1)
if __name__ == "__main__":
if len(sys.argv) == 1:
help()
input_file = None
output_file = None
verbose = False
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv, "e:o:v", ["exception=", "output=", "verbose"])
for opt, arg in opts:
if opt in ("-e", "--exception"):
input_file = arg
if opt in ("-o", "--output"):
output_file = arg
if opt in ("-v", "--verbose"):
verbose = True
except getopt.GetoptError, e:
print e
opts, args = getopt.getopt(argv, "", [])
if args == None:
help()
instance = ObjC(args, input_file, output_file, verbose)
instance.parse()
print 'Ok'