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('interface'):
101 self.parse_interface()
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()
195 print ' (%s, "%s"),' % (type, name)
202 def parse_interface(self):
203 self.consume('interface')
204 name = self.consume()
208 base = self.consume()
211 print '%s = Interface("%s", %s)' % (name, name, base)
212 print '%s.methods += [' % (name,)
214 while self.lookahead() != '}':
215 self.parse_prototype('Method')
222 def parse_prototype(self, creator = 'Function'):
223 if self.match('extern'):
226 ret = self.parse_type()
228 if self.match('__stdcall'):
230 creator = 'StdFunction'
232 name = self.consume()
234 if not self.has_side_effects(name):
235 extra += ', sideeffects=False'
240 if self.match('void') and self.tokens[1] == ')':
242 while self.lookahead() != ')':
243 arg = self.parse_arg()
247 self.consume() == ')'
249 print ' %s(%s, "%s", [%s]%s),' % (creator, ret, name, ', '.join(args), extra)
252 tags = self.parse_tags()
254 type, name = self.parse_named_type()
256 arg = '(%s, "%s")' % (type, name)
261 def parse_tags(self):
265 while not self.match(']'):
271 def parse_named_type(self):
272 type = self.parse_type()
273 name = self.consume()
276 length = self.consume()
281 length = "%s" % length
282 type = 'Array(%s, %s)' % (type, length)
285 int_tokens = ('unsigned', 'signed', 'int', 'long', 'short', 'char')
293 'uint16_t': 'UInt16',
295 'uint32_t': 'UInt32',
297 'uint64_t': 'UInt64',
300 def parse_type(self):
302 token = self.consume()
304 token = self.consume()
308 elif token in self.int_tokens:
314 while token in self.int_tokens:
315 if token == 'unsigned':
317 if token == 'signed':
325 if self.lookahead() in self.int_tokens:
326 token = self.consume()
342 type = self.type_table.get(token, token)
344 type = 'Const(%s)' % type
348 type = 'OpaquePointer(%s)' % type
349 elif self.match('const'):
350 self.consume('const')
351 type = 'Const(%s)' % type
360 parser = DeclParser()
363 parser.parse(open(arg, 'rt').read())
365 parser.parse(sys.stdin.read())
368 if __name__ == '__main__':