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'(\d[x0-9a-fA-F.UL]*|\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):
98 elif self.match('enum'):
100 elif self.match('class', 'interface'):
101 self.parse_interface(self.lookahead())
102 elif self.match('mask'):
103 self.parse_value('mask', 'Flags')
104 elif self.match('struct'):
106 elif self.match('value'):
107 self.parse_value('value', 'FakeEnum')
108 elif self.match('typedef'):
111 self.parse_prototype()
112 if not self.eof() and self.match(';'):
115 def parse_typedef(self):
116 self.consume('typedef')
117 if self.lookahead(2) in (';', ','):
118 base_type = self.consume()
123 type = 'Pointer(%s)' % type
124 name = self.consume()
125 print '%s = Alias("%s", %s)' % (name, name, type)
131 self.parse_declaration()
134 def parse_enum(self):
136 name = self.consume()
139 print '%s = Enum("%s", [' % (name, name)
142 while self.lookahead() != '}':
143 name = self.consume()
146 value = self.consume()
149 tags = self.parse_tags()
150 #print ' "%s",\t# %s' % (name, value)
151 print ' "%s",' % (name,)
158 def parse_value(self, ref_token, constructor):
159 self.consume(ref_token)
160 type = self.consume()
161 name = self.consume()
164 print '%s = %s(%s, [' % (name, constructor, type)
166 while self.lookahead() != '}':
167 name, value = self.parse_define()
173 def parse_define(self):
175 self.consume('define')
176 name = self.consume()
177 value = self.consume()
178 #print ' "%s",\t# %s' % (name, value)
179 print ' "%s",' % (name,)
182 def parse_struct(self):
183 self.consume('struct')
184 name = self.consume()
187 print '%s = Struct("%s", [' % (name, name)
190 while self.lookahead() != '}':
191 type, name = self.parse_named_type()
200 print ' (%s, "%s"),' % (type, name)
207 def parse_interface(self, ref_token):
208 self.consume(ref_token)
209 name = self.consume()
213 if self.lookahead() in ('public', 'protected'):
215 base = self.consume()
218 print '%s = Interface("%s", %s)' % (name, name, base)
219 print '%s.methods += [' % (name,)
221 while self.lookahead() != '}':
222 if self.lookahead() in ('public', 'private'):
226 self.parse_prototype('Method')
233 def parse_prototype(self, creator = 'Function'):
234 if self.match('extern', 'virtual'):
237 ret = self.parse_type()
239 if self.match('__stdcall', 'WINAPI'):
241 creator = 'Std' + creator
243 name = self.consume()
245 if not self.has_side_effects(name):
246 extra += ', sideeffects=False'
251 if self.match('void') and self.tokens[1] == ')':
253 while self.lookahead() != ')':
254 arg = self.parse_arg()
259 if self.lookahead() == 'const':
261 extra = ', const=True' + extra
263 if self.lookahead() == '=':
267 print ' %s(%s, "%s", [%s]%s),' % (creator, ret, name, ', '.join(args), extra)
270 tags = self.parse_tags()
272 type, name = self.parse_named_type()
274 arg = '(%s, "%s")' % (type, name)
275 if 'out' in tags or 'inout' in tags:
280 while not self.match(',', ')'):
285 def parse_tags(self):
289 while not self.match(']'):
293 if self.lookahead().startswith('__'):
294 # Parse __in, __out, etc tags
295 tag = self.consume()[2:]
298 while not self.match(')'):
301 tags.extend(tag.split('_'))
304 def parse_named_type(self):
305 type = self.parse_type()
306 name = self.consume()
309 length = self.consume()
314 length = "%s" % length
315 type = 'Array(%s, %s)' % (type, length)
318 int_tokens = ('unsigned', 'signed', 'int', 'long', 'short', 'char')
326 'uint16_t': 'UInt16',
328 'uint32_t': 'UInt32',
330 'uint64_t': 'UInt64',
333 def parse_type(self):
335 token = self.consume()
337 token = self.consume()
341 elif token in self.int_tokens:
347 while token in self.int_tokens:
348 if token == 'unsigned':
350 if token == 'signed':
358 if self.lookahead() in self.int_tokens:
359 token = self.consume()
375 type = self.type_table.get(token, token)
377 type = 'Const(%s)' % type
381 type = 'Pointer(%s)' % type
382 elif self.match('const'):
383 self.consume('const')
384 type = 'Const(%s)' % type
393 parser = DeclParser()
396 parser.parse(open(arg, 'rt').read())
398 parser.parse(sys.stdin.read())
401 if __name__ == '__main__':