2 ##########################################################################
4 # Copyright 2010 VMware, Inc.
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 ##########################################################################/
34 sys.stderr.write(str(x) + '\n')
39 def __init__(self, stream):
44 """Base class for parsers that read line-based formats."""
46 def __init__(self, stream):
54 raise NotImplementedError
57 line = self._stream.readline()
61 self._line = line.rstrip('\r\n')
64 assert self._line is not None
68 assert self._line is not None
74 assert self._line is not None
77 def skip_whitespace(self):
78 while not self.eof() and self.match_whitespace() or self.match_comment():
81 def match_whitespace(self):
82 line = self.lookahead()
83 return not line.strip()
85 def match_comment(self):
89 class TxtParser(LineParser):
91 property_re = re.compile(r'^\w+:')
92 prototype_re = re.compile(r'^(\w+)\((.*)\)$')
94 def __init__(self, stream, prefix=''):
95 LineParser.__init__(self, stream)
100 while not line.startswith("New Procedures and Functions"):
101 line = self.consume()
104 def parse_procs(self):
107 line = self.consume()
110 if not line.startswith(' '):
112 lines.append(line.strip())
113 if line.endswith(';'):
114 self.parse_proc(' '.join(lines))
117 token_re = re.compile(r'(\w+|\s+|.)')
118 get_function_re = re.compile(r'^Get[A-Z]\w+')
120 def parse_proc(self, prototype):
122 tokens = self.token_re.split(prototype)
123 self.tokens = [token for token in tokens if token.strip()]
126 ret = self.parse_type()
128 name = self.tokens.pop(0)
130 if self.get_function_re.match(name):
131 extra += ', sideeffects=False'
132 name = self.prefix + name
134 assert self.tokens.pop(0) == '('
136 while self.tokens[0] != ')':
137 arg = self.parse_arg()
139 if self.tokens[0] == ',':
141 print ' GlFunction(%s, "%s", [%s]%s),' % (ret, name, ', '.join(args), extra)
144 type = self.parse_type()
145 name = self.tokens.pop(0)
146 return '(%s, "%s")' % (type, name)
148 def parse_type(self):
149 token = self.tokens.pop(0)
151 return 'Const(%s)' % self.parse_type()
156 while self.tokens[0] == '*':
157 type = 'OpaquePointer(%s)' % type
163 for arg in sys.argv[1:]:
164 parser = TxtParser(open(arg, 'rt'), prefix='gl')
168 if __name__ == '__main__':