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 TypemapParser(LineParser):
93 self.skip_whitespace()
96 fields = [field.strip() for field in line.split(',')]
101 self.skip_whitespace()
104 def match_comment(self):
105 line = self.lookahead()
106 return line.startswith('#')
109 class SpecParser(LineParser):
111 property_re = re.compile(r'^\w+:')
112 prototype_re = re.compile(r'^(\w+)\((.*)\)$')
114 def __init__(self, stream, prefix='', typemap = None):
115 LineParser.__init__(self, stream)
119 self.typemap = typemap
124 self.skip_whitespace()
125 while not self.eof():
126 line = self.lookahead()
127 if self.property_re.match(line):
128 self.parse_property()
129 elif self.prototype_re.match(line):
130 self.parse_prototype()
133 self.skip_whitespace()
135 def parse_property(self):
136 line = self.consume()
137 name, value = line.split(':', 1)
138 if name == 'category':
139 values = value.split()
140 #self.prefix = values[0]
142 get_function_re = re.compile(r'^Get[A-Z]\w+')
144 def parse_prototype(self):
145 line = self.consume()
146 mo = self.prototype_re.match(line)
147 function_name, arg_names = mo.groups()
148 arg_names = [arg_name.strip() for arg_name in arg_names.split(',') if arg_name.strip()]
151 if self.get_function_re.match(function_name):
152 extra += ', sideeffects=False'
153 function_name = self.prefix + function_name
158 line = self.lookahead()
159 while line.startswith('\t'):
160 fields = line.split(None, 2)
161 if fields[0] == 'return':
162 ret_type = self.parse_type(fields[1])
163 elif fields[0] == 'param':
164 arg_name, arg_type = fields[1:3]
165 arg_types[fields[1]] = self.parse_arg(function_name, arg_name, arg_type)
166 elif fields[0] == 'category':
171 line = self.lookahead()
173 args = [arg_types[arg_name] for arg_name in arg_names]
175 if category is not None:
176 if category == self.prefix:
177 category = self.prefix.upper()
179 category = self.prefix.upper() + '_' + category
180 if category != self.category:
181 if self.category is not None:
183 print ' # %s' % category
184 self.category = category
186 if self.prefix == 'wgl':
187 constructor = 'StdFunction'
189 constructor = 'GlFunction'
191 print ' %s(%s, "%s", [%s]%s),' % (constructor, ret_type, function_name, ', '.join(args), extra)
193 array_re = re.compile(r'^array\s+\[(.*)\]$')
196 'GLchar': 'GLstring',
197 'GLcharARB': 'GLstringARB',
200 def parse_arg(self, function_name, arg_name, arg_type):
201 orig_type, inout, kind = arg_type.split(' ', 2)
203 base_type = self.parse_type(orig_type)
207 elif kind == 'reference':
208 arg_type = 'Pointer(%s)' % base_type
210 arg_type = 'Const(%s)' % arg_type
211 elif kind.startswith("array"):
212 arg_type = 'OpaquePointer(%s)' % base_type
214 mo = self.array_re.match(kind)
216 length = mo.group(1).strip()
219 arg_type = self.string_typemap[base_type]
223 arg_type = 'Pointer(%s)' % base_type
224 elif length.find("COMPSIZE") == -1:
225 arg_type = 'Array(%s, "%s")' % (base_type, length)
227 # XXX: Handle COMPSIZE better
228 length = length.replace("COMPSIZE", "__%s_size" % function_name)
229 length = length.replace("/", ",")
230 arg_type = 'Array(%s, "%s")' % (base_type, length)
232 arg_type = 'Const(%s)' % arg_type
236 arg = '(%s, "%s")' % (arg_type, arg_name)
243 'Texture': 'GLtexture',
252 def parse_type(self, type):
254 return self.semantic_typemap[type]
257 type = self.typemap.get(type, type)
258 type = self.post_typemap.get(type, type)
261 def match_comment(self):
262 line = self.lookahead()
263 return line.startswith('#')
269 parser = TypemapParser(open(sys.argv[2], 'rt'))
270 typemap = parser.parse()
272 for arg in sys.argv[3:]:
273 parser = SpecParser(open(arg, 'rt'), prefix=prefix, typemap=typemap)
277 if __name__ == '__main__':