2 ##########################################################################
4 # Copyright 2011 Jose Fonseca
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 ##########################################################################/
28 '''Script to parse C declarations and spew API definitions.
39 token_re = re.compile(r'(\w+|\s+|.)')
41 multi_comment_re = re.compile(r'/\*.*?\*/', flags = re.DOTALL)
42 single_comment_re = re.compile(r'//.*',)
47 def has_side_effects(self, name):
51 def tokenize(self, s):
52 s = self.multi_comment_re.sub('', s)
53 s = self.single_comment_re.sub('', s)
54 self.tokens = self.token_re.split(s)
55 self.tokens = [token for token in self.tokens if self.filter_token(token)]
57 def filter_token(self, token):
58 if not token or token.isspace():
60 if token.startswith('AVAILABLE_') or token.startswith('DEPRECATED_'):
66 def lookahead(self, index = 0):
68 return self.tokens[index]
72 def match(self, *ref_tokens):
73 return self.lookahead() in ref_tokens
75 def consume(self, *ref_tokens):
77 raise Exception('unexpected EOF')
78 token = self.tokens.pop(0)
79 if ref_tokens and token not in ref_tokens:
80 raise Exception('token mismatch', token, ref_tokens)
84 return not self.tokens
91 #print self.tokens[0:10]
92 self.parse_declaration()
94 def parse_declaration(self):
96 if self.match('enum'):
98 elif self.match('interface'):
99 self.parse_interface()
100 elif self.match('mask'):
101 self.parse_value('mask', 'Flags')
102 elif self.match('struct'):
104 elif self.match('value'):
105 self.parse_value('value', 'FakeEnum')
106 elif self.match('typedef'):
109 self.parse_prototype()
110 if not self.eof() and self.match(';'):
113 def parse_typedef(self):
114 self.consume('typedef')
115 if self.lookahead(2) in (';', ','):
116 base_type = self.consume()
121 type = 'Pointer(%s)' % type
122 name = self.consume()
123 print '%s = Alias("%s", %s)' % (name, name, type)
129 self.parse_declaration()
132 def parse_enum(self):
134 name = self.consume()
137 print '%s = Enum("%s", [' % (name, name)
140 while self.lookahead() != '}':
141 name = self.consume()
144 value = self.consume()
147 tags = self.parse_tags()
148 #print ' "%s",\t# %s' % (name, value)
149 print ' "%s",' % (name,)
156 def parse_value(self, ref_token, constructor):
157 self.consume(ref_token)
158 type = self.consume()
159 name = self.consume()
162 print '%s = %s(%s, [' % (name, constructor, type)
164 while self.lookahead() != '}':
166 self.consume('define')
167 name = self.consume()
168 value = self.consume()
169 #print ' "%s",\t# %s' % (name, value)
170 print ' "%s",' % (name,)
176 def parse_struct(self):
177 self.consume('struct')
178 name = self.consume()
181 print '%s = Struct("%s", [' % (name, name)
184 while self.lookahead() != '}':
185 type, name = self.parse_named_type()
189 print ' (%s, "%s"),' % (type, name)
196 def parse_interface(self):
197 self.consume('interface')
198 name = self.consume()
202 base = self.consume()
205 print '%s = Interface("%s", %s)' % (name, name, base)
206 print '%s.methods += [' % (name,)
208 while self.lookahead() != '}':
209 self.parse_prototype('Method')
216 def parse_prototype(self, creator = 'Function'):
217 if self.match('extern'):
220 ret = self.parse_type()
222 if self.match('__stdcall'):
224 creator = 'StdFunction'
226 name = self.consume()
228 if not self.has_side_effects(name):
229 extra += ', sideeffects=False'
234 if self.match('void') and self.tokens[1] == ')':
236 while self.lookahead() != ')':
237 arg = self.parse_arg()
241 self.consume() == ')'
243 print ' %s(%s, "%s", [%s]%s),' % (creator, ret, name, ', '.join(args), extra)
246 tags = self.parse_tags()
248 type, name = self.parse_named_type()
250 arg = '(%s, "%s")' % (type, name)
255 def parse_tags(self):
259 while not self.match(']'):
265 def parse_named_type(self):
266 type = self.parse_type()
267 name = self.consume()
270 length = self.consume()
272 type = 'Array(%s, "%s")' % (type, length)
275 int_tokens = ('unsigned', 'signed', 'int', 'long', 'short', 'char')
283 'uint16_t': 'UInt16',
285 'uint32_t': 'UInt32',
287 'uint64_t': 'UInt64',
290 def parse_type(self):
292 token = self.consume()
294 token = self.consume()
298 elif token in self.int_tokens:
304 while token in self.int_tokens:
305 if token == 'unsigned':
307 if token == 'signed':
315 if self.lookahead() in self.int_tokens:
316 token = self.consume()
332 type = self.type_table.get(token, token)
334 type = 'Const(%s)' % type
338 type = 'OpaquePointer(%s)' % type
339 elif self.match('const'):
340 self.consume('const')
341 type = 'Const(%s)' % type
353 for arg in sys.argv[1:]:
354 parser.parse(open(arg, 'rt').read())
357 if __name__ == '__main__':