Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | |
Ian Romanick | 5f1f229 | 2005-01-07 02:39:09 +0000 | [diff] [blame] | 3 | # (C) Copyright IBM Corporation 2004, 2005 |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | # copy of this software and associated documentation files (the "Software"), |
| 8 | # to deal in the Software without restriction, including without limitation |
| 9 | # on the rights to use, copy, modify, merge, publish, distribute, sub |
| 10 | # license, and/or sell copies of the Software, and to permit persons to whom |
| 11 | # the Software is furnished to do so, subject to the following conditions: |
| 12 | # |
| 13 | # The above copyright notice and this permission notice (including the next |
| 14 | # paragraph) shall be included in all copies or substantial portions of the |
| 15 | # Software. |
| 16 | # |
| 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 NON-INFRINGEMENT. IN NO EVENT SHALL |
| 20 | # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 23 | # IN THE SOFTWARE. |
| 24 | # |
| 25 | # Authors: |
| 26 | # Ian Romanick <idr@us.ibm.com> |
| 27 | |
| 28 | from xml.sax import saxutils |
| 29 | from xml.sax import make_parser |
| 30 | from xml.sax.handler import feature_namespaces |
| 31 | |
Brian Paul | 98fa2bf | 2004-10-28 21:11:02 +0000 | [diff] [blame] | 32 | import re |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 33 | |
| 34 | class glItem: |
| 35 | """Generic class on which all other API entity types are based.""" |
| 36 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 37 | def __init__(self, tag_name, name, context): |
| 38 | self.name = name |
| 39 | self.category = context.get_category_define() |
| 40 | self.context = context |
| 41 | self.tag_name = tag_name |
| 42 | |
| 43 | context.append(tag_name, self) |
| 44 | return |
| 45 | |
| 46 | def startElement(self, name, attrs): |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 47 | """Generic startElement handler. |
| 48 | |
| 49 | The startElement handler is called for all elements except |
| 50 | the one that starts the object. For a foo element, the |
| 51 | XML "<foo><bar/></foo>" would cause the startElement handler |
| 52 | to be called once, but the endElement handler would be called |
| 53 | twice.""" |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 54 | return |
| 55 | |
| 56 | def endElement(self, name): |
| 57 | """Generic endElement handler. |
| 58 | |
| 59 | Generic endElement handler. Returns 1 if the tag containing |
| 60 | the object is complete. Otherwise 0 is returned. All |
| 61 | derived class endElement handlers should call this method. If |
| 62 | the name of the ending tag is the same as the tag that |
| 63 | started this object, the object is assumed to be complete. |
| 64 | |
| 65 | This fails if a tag can contain another tag with the same |
| 66 | name. The XML "<foo><foo/><bar/></foo>" would fail. The |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 67 | object would end before the bar tag was processed. |
| 68 | |
| 69 | The endElement handler is called for every end element |
| 70 | associated with an object, even the element that started the |
| 71 | object. See the description of startElement an example.""" |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 72 | |
| 73 | if name == self.tag_name: |
| 74 | return 1 |
| 75 | else: |
| 76 | return 0 |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 77 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 78 | def get_category_define(self): |
| 79 | return self.category |
| 80 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 81 | |
| 82 | class glEnum( glItem ): |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 83 | """Subclass of glItem for representing GL enumerants. |
| 84 | |
| 85 | This class is not complete, and is not really used yet.""" |
| 86 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 87 | def __init__(self, context, name, attrs): |
| 88 | self.value = int(attrs.get('value', "0x0000"), 0) |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 89 | |
| 90 | enum_name = "GL_" + attrs.get('name', None) |
| 91 | glItem.__init__(self, name, enum_name, context) |
| 92 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 93 | |
Ian Romanick | 5ff2b94 | 2005-01-24 21:29:13 +0000 | [diff] [blame^] | 94 | def process_attributes(self, attrs): |
| 95 | name = attrs.get('name', None) |
| 96 | |
| 97 | temp = attrs.get('count', None) |
| 98 | try: |
| 99 | c = int(temp) |
| 100 | except Exception,e: |
| 101 | raise RuntimeError('Invalid count value "%s" for enum "%s" in function "%s" when an integer was expected.' % (temp, self.name, n)) |
| 102 | |
| 103 | return [name, c] |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | class glType( glItem ): |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 107 | """Subclass of glItem for representing GL types.""" |
| 108 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 109 | def __init__(self, context, name, attrs): |
| 110 | self.size = int(attrs.get('size', "0")) |
Ian Romanick | a285acb | 2005-01-07 03:22:56 +0000 | [diff] [blame] | 111 | self.glx_name = attrs.get('glx_name', "") |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 112 | |
| 113 | type_name = "GL" + attrs.get('name', None) |
| 114 | glItem.__init__(self, name, type_name, context) |
| 115 | |
| 116 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 117 | class glParameter( glItem ): |
| 118 | """Parameter of a glFunction.""" |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 119 | p_type = None |
| 120 | p_type_string = "" |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 121 | p_count = 0 |
| 122 | p_count_parameters = None |
| 123 | counter = None |
| 124 | is_output = 0 |
| 125 | is_counter = 0 |
| 126 | is_pointer = 0 |
| 127 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 128 | def __init__(self, context, name, attrs): |
| 129 | p_name = attrs.get('name', None) |
| 130 | self.p_type_string = attrs.get('type', None) |
| 131 | self.p_count_parameters = attrs.get('variable_param', None) |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 132 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 133 | self.p_type = context.context.find_type(self.p_type_string) |
| 134 | if self.p_type == None: |
| 135 | raise RuntimeError("Unknown type '%s' in function '%s'." % (self.p_type_string, context.name)) |
| 136 | |
| 137 | |
| 138 | # The count tag can be either a numeric string or the name of |
| 139 | # a variable. If it is the name of a variable, the int(c) |
| 140 | # statement will throw an exception, and the except block will |
| 141 | # take over. |
| 142 | |
| 143 | c = attrs.get('count', "0") |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 144 | try: |
| 145 | self.p_count = int(c) |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 146 | self.counter = None |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 147 | except Exception,e: |
| 148 | self.p_count = 0 |
| 149 | self.counter = c |
| 150 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 151 | if attrs.get('counter', "false") == "true": |
| 152 | self.is_counter = 1 |
| 153 | else: |
| 154 | self.is_counter = 0 |
| 155 | |
| 156 | if attrs.get('output', "false") == "true": |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 157 | self.is_output = 1 |
| 158 | else: |
| 159 | self.is_output = 0 |
| 160 | |
Ian Romanick | 5f1f229 | 2005-01-07 02:39:09 +0000 | [diff] [blame] | 161 | |
| 162 | # Pixel data has special parameters. |
| 163 | |
| 164 | self.width = attrs.get('img_width', None) |
| 165 | self.height = attrs.get('img_height', None) |
| 166 | self.depth = attrs.get('img_depth', None) |
| 167 | self.extent = attrs.get('img_extent', None) |
| 168 | |
| 169 | self.img_xoff = attrs.get('img_xoff', None) |
| 170 | self.img_yoff = attrs.get('img_yoff', None) |
| 171 | self.img_zoff = attrs.get('img_zoff', None) |
| 172 | self.img_woff = attrs.get('img_woff', None) |
| 173 | |
| 174 | self.img_format = attrs.get('img_format', None) |
| 175 | self.img_type = attrs.get('img_type', None) |
| 176 | self.img_target = attrs.get('img_target', None) |
| 177 | |
| 178 | pad = attrs.get('img_pad_dimensions', "false") |
| 179 | if pad == "true": |
| 180 | self.img_pad_dimensions = 1 |
| 181 | else: |
| 182 | self.img_pad_dimensions = 0 |
| 183 | |
| 184 | |
| 185 | null_flag = attrs.get('img_null_flag', "false") |
| 186 | if null_flag == "true": |
| 187 | self.img_null_flag = 1 |
| 188 | else: |
| 189 | self.img_null_flag = 0 |
| 190 | |
| 191 | send_null = attrs.get('img_send_null', "false") |
| 192 | if send_null == "true": |
| 193 | self.img_send_null = 1 |
| 194 | else: |
| 195 | self.img_send_null = 0 |
| 196 | |
| 197 | |
| 198 | |
| 199 | if self.p_count > 0 or self.counter or self.p_count_parameters: |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 200 | has_count = 1 |
| 201 | else: |
| 202 | has_count = 0 |
| 203 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 204 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 205 | # If there is a * anywhere in the parameter's type, then it |
| 206 | # is a pointer. |
| 207 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 208 | if re.compile("[*]").search(self.p_type_string): |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 209 | # We could do some other validation here. For |
| 210 | # example, an output parameter should not be const, |
| 211 | # but every non-output parameter should. |
| 212 | |
| 213 | self.is_pointer = 1; |
| 214 | else: |
| 215 | # If a parameter is not a pointer, then there cannot |
| 216 | # be an associated count (either fixed size or |
| 217 | # variable) and the parameter cannot be an output. |
| 218 | |
| 219 | if has_count or self.is_output: |
| 220 | raise RuntimeError("Non-pointer type has count or is output.") |
| 221 | self.is_pointer = 0; |
| 222 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 223 | glItem.__init__(self, name, p_name, context) |
| 224 | return |
| 225 | |
| 226 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 227 | def is_variable_length_array(self): |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 228 | """Determine if a parameter is a variable length array. |
| 229 | |
| 230 | A parameter is considered to be a variable length array if |
| 231 | its size depends on the value of another parameter that is |
| 232 | an enumerant. The params parameter to glTexEnviv is an |
| 233 | example of a variable length array parameter. Arrays whose |
| 234 | size depends on a count variable, such as the lists parameter |
| 235 | to glCallLists, are not variable length arrays in this |
| 236 | sense.""" |
| 237 | |
Ian Romanick | 5f1f229 | 2005-01-07 02:39:09 +0000 | [diff] [blame] | 238 | return self.p_count_parameters or self.counter or self.width |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 239 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 240 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 241 | def is_array(self): |
| 242 | return self.is_pointer |
| 243 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 244 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 245 | def count_string(self): |
| 246 | """Return a string representing the number of items |
| 247 | |
| 248 | Returns a string representing the number of items in a |
| 249 | parameter. For scalar types this will always be "1". For |
| 250 | vector types, it will depend on whether or not it is a |
| 251 | fixed length vector (like the parameter of glVertex3fv), |
| 252 | a counted length (like the vector parameter of |
| 253 | glDeleteTextures), or a general variable length vector.""" |
| 254 | |
| 255 | if self.is_array(): |
Ian Romanick | 4f0a75e | 2004-12-01 00:29:48 +0000 | [diff] [blame] | 256 | if self.p_count_parameters != None: |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 257 | return "compsize" |
| 258 | elif self.counter != None: |
| 259 | return self.counter |
| 260 | else: |
| 261 | return str(self.p_count) |
| 262 | else: |
| 263 | return "1" |
| 264 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 265 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 266 | def size(self): |
Ian Romanick | 5f1f229 | 2005-01-07 02:39:09 +0000 | [diff] [blame] | 267 | if self.p_count_parameters or self.counter or self.width or self.is_output: |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 268 | return 0 |
| 269 | elif self.p_count == 0: |
| 270 | return self.p_type.size |
| 271 | else: |
| 272 | return self.p_type.size * self.p_count |
| 273 | |
Ian Romanick | 4f0a75e | 2004-12-01 00:29:48 +0000 | [diff] [blame] | 274 | def size_string(self): |
| 275 | s = self.size() |
| 276 | if s == 0: |
| 277 | a_prod = "compsize" |
| 278 | b_prod = self.p_type.size |
| 279 | |
| 280 | if self.p_count_parameters == None and self.counter != None: |
| 281 | a_prod = self.counter |
| 282 | elif self.p_count_parameters != None and self.counter == None: |
| 283 | pass |
| 284 | elif self.p_count_parameters != None and self.counter != None: |
| 285 | b_prod = self.counter |
Ian Romanick | 5f1f229 | 2005-01-07 02:39:09 +0000 | [diff] [blame] | 286 | elif self.width: |
| 287 | return "compsize" |
Ian Romanick | 4f0a75e | 2004-12-01 00:29:48 +0000 | [diff] [blame] | 288 | else: |
| 289 | raise RuntimeError("Parameter '%s' to function '%s' has size 0." % (self.name, self.context.name)) |
| 290 | |
| 291 | return "(%s * %s)" % (a_prod, b_prod) |
| 292 | else: |
| 293 | return str(s) |
| 294 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 295 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 296 | class glParameterIterator: |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 297 | """Class to iterate over a list of glParameters. |
| 298 | |
Ian Romanick | 1d27084 | 2004-12-21 21:26:36 +0000 | [diff] [blame] | 299 | Objects of this class are returned by the parameterIterator method of |
| 300 | the glFunction class. They are used to iterate over the list of |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 301 | parameters to the function.""" |
| 302 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 303 | def __init__(self, data): |
| 304 | self.data = data |
| 305 | self.index = 0 |
Ian Romanick | 1d27084 | 2004-12-21 21:26:36 +0000 | [diff] [blame] | 306 | |
| 307 | def __iter__(self): |
| 308 | return self |
| 309 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 310 | def next(self): |
| 311 | if self.index == len( self.data ): |
| 312 | raise StopIteration |
| 313 | i = self.index |
| 314 | self.index += 1 |
| 315 | return self.data[i] |
| 316 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 317 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 318 | class glFunction( glItem ): |
| 319 | real_name = "" |
| 320 | fn_alias = None |
| 321 | fn_offset = -1 |
| 322 | fn_return_type = "void" |
| 323 | fn_parameters = [] |
| 324 | |
| 325 | def __init__(self, context, name, attrs): |
| 326 | self.fn_alias = attrs.get('alias', None) |
| 327 | self.fn_parameters = [] |
Ian Romanick | 5f1f229 | 2005-01-07 02:39:09 +0000 | [diff] [blame] | 328 | self.image = None |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 329 | |
| 330 | temp = attrs.get('offset', None) |
| 331 | if temp == None or temp == "?": |
| 332 | self.fn_offset = -1 |
| 333 | else: |
| 334 | self.fn_offset = int(temp) |
| 335 | |
| 336 | fn_name = attrs.get('name', None) |
| 337 | if self.fn_alias != None: |
| 338 | self.real_name = self.fn_alias |
| 339 | else: |
| 340 | self.real_name = fn_name |
| 341 | |
| 342 | glItem.__init__(self, name, fn_name, context) |
| 343 | return |
| 344 | |
| 345 | |
Ian Romanick | 1d27084 | 2004-12-21 21:26:36 +0000 | [diff] [blame] | 346 | def parameterIterator(self): |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 347 | return glParameterIterator(self.fn_parameters) |
| 348 | |
| 349 | |
| 350 | def startElement(self, name, attrs): |
| 351 | if name == "param": |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 352 | try: |
Ian Romanick | 4f0a75e | 2004-12-01 00:29:48 +0000 | [diff] [blame] | 353 | self.context.factory.create(self, name, attrs) |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 354 | except RuntimeError: |
| 355 | print "Error with parameter '%s' in function '%s'." \ |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 356 | % (attrs.get('name','(unknown)'), self.name) |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 357 | raise |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 358 | elif name == "return": |
| 359 | self.set_return_type(attrs.get('type', None)) |
| 360 | |
| 361 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 362 | def append(self, tag_name, p): |
| 363 | if tag_name != "param": |
| 364 | raise RuntimeError("Trying to append '%s' to parameter list of function '%s'." % (tag_name, self.name)) |
| 365 | |
Ian Romanick | 5f1f229 | 2005-01-07 02:39:09 +0000 | [diff] [blame] | 366 | if p.width: |
| 367 | self.image = p |
| 368 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 369 | self.fn_parameters.append(p) |
| 370 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 371 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 372 | def set_return_type(self, t): |
| 373 | self.fn_return_type = t |
| 374 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 375 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 376 | def get_parameter_string(self): |
| 377 | arg_string = "" |
| 378 | comma = "" |
Ian Romanick | 1d27084 | 2004-12-21 21:26:36 +0000 | [diff] [blame] | 379 | for p in glFunction.parameterIterator(self): |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 380 | arg_string = arg_string + comma + p.p_type_string + " " + p.name |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 381 | comma = ", " |
| 382 | |
| 383 | if arg_string == "": |
| 384 | arg_string = "void" |
| 385 | |
| 386 | return arg_string |
| 387 | |
| 388 | |
| 389 | class glItemFactory: |
| 390 | """Factory to create objects derived from glItem.""" |
| 391 | |
| 392 | def create(self, context, name, attrs): |
| 393 | if name == "function": |
| 394 | return glFunction(context, name, attrs) |
| 395 | elif name == "type": |
| 396 | return glType(context, name, attrs) |
| 397 | elif name == "enum": |
| 398 | return glEnum(context, name, attrs) |
Ian Romanick | 4f0a75e | 2004-12-01 00:29:48 +0000 | [diff] [blame] | 399 | elif name == "param": |
| 400 | return glParameter(context, name, attrs) |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 401 | else: |
| 402 | return None |
| 403 | |
| 404 | |
| 405 | class FilterGLAPISpecBase(saxutils.XMLFilterBase): |
| 406 | name = "a" |
| 407 | license = "The license for this file is unspecified." |
| 408 | functions = {} |
| 409 | next_alias = -2 |
| 410 | types = {} |
| 411 | xref = {} |
| 412 | current_object = None |
| 413 | factory = None |
| 414 | current_category = "" |
| 415 | |
| 416 | def __init__(self): |
| 417 | saxutils.XMLFilterBase.__init__(self) |
| 418 | self.functions = {} |
| 419 | self.types = {} |
| 420 | self.xref = {} |
| 421 | self.factory = glItemFactory() |
| 422 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 423 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 424 | def find_type(self,type_name): |
| 425 | for t in self.types: |
| 426 | if re.compile(t).search(type_name): |
| 427 | return self.types[t] |
| 428 | print "Unable to find base type matching \"%s\"." % (type_name) |
| 429 | return None |
| 430 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 431 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 432 | def find_function(self,function_name): |
| 433 | index = self.xref[function_name] |
| 434 | return self.functions[index] |
| 435 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 436 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 437 | def printFunctions(self): |
| 438 | keys = self.functions.keys() |
| 439 | keys.sort() |
| 440 | prevk = -1 |
| 441 | for k in keys: |
| 442 | if k < 0: continue |
| 443 | |
| 444 | if self.functions[k].fn_alias == None: |
| 445 | if k != prevk + 1: |
| 446 | #print 'Missing offset %d' % (prevk) |
| 447 | pass |
| 448 | prevk = int(k) |
| 449 | self.printFunction(self.functions[k]) |
| 450 | |
| 451 | keys.reverse() |
| 452 | for k in keys: |
| 453 | if self.functions[k].fn_alias != None: |
| 454 | self.printFunction(self.functions[k]) |
| 455 | |
| 456 | return |
| 457 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 458 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 459 | def printHeader(self): |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 460 | """Print the header associated with all files and call the printRealHeader method.""" |
| 461 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 462 | print '/* DO NOT EDIT - This file generated automatically by %s script */' \ |
| 463 | % (self.name) |
| 464 | print '' |
| 465 | print '/*' |
| 466 | print ' * ' + self.license.replace('\n', '\n * ') |
| 467 | print ' */' |
| 468 | print '' |
| 469 | self.printRealHeader(); |
| 470 | return |
| 471 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 472 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 473 | def printFooter(self): |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 474 | """Print the header associated with all files and call the printRealFooter method.""" |
| 475 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 476 | self.printFunctions() |
| 477 | self.printRealFooter() |
| 478 | |
| 479 | |
| 480 | def get_category_define(self): |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 481 | """Convert the category name to the #define that would be found in glext.h""" |
| 482 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 483 | if re.compile("[1-9][0-9]*[.][0-9]+").match(self.current_category): |
| 484 | s = self.current_category |
| 485 | return "GL_VERSION_" + s.replace(".", "_") |
| 486 | else: |
| 487 | return self.current_category |
| 488 | |
| 489 | |
| 490 | def append(self, object_type, obj): |
| 491 | if object_type == "function": |
| 492 | # If the function is not an alias and has a negative |
| 493 | # offset, then we do not need to track it. These are |
| 494 | # functions that don't have an assigned offset |
| 495 | |
| 496 | if obj.fn_offset >= 0 or obj.fn_alias != None: |
| 497 | if obj.fn_offset >= 0: |
| 498 | index = obj.fn_offset |
| 499 | else: |
| 500 | index = self.next_alias |
| 501 | self.next_alias -= 1 |
| 502 | |
| 503 | self.functions[index] = obj |
| 504 | self.xref[obj.name] = index |
| 505 | elif object_type == "type": |
| 506 | self.types[obj.name] = obj |
| 507 | |
| 508 | return |
| 509 | |
| 510 | |
| 511 | def startElement(self, name, attrs): |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 512 | """Start a new element in the XML stream. |
| 513 | |
| 514 | Starts a new element. There are three types of elements that |
| 515 | are specially handled by this function. When a "category" |
| 516 | element is encountered, the name of the category is saved. |
| 517 | If an element is encountered and no API object is |
| 518 | in-progress, a new object is created using the API factory. |
| 519 | Any future elements, until that API object is closed, are |
| 520 | passed to the current objects startElement method. |
| 521 | |
| 522 | This paradigm was chosen becuase it allows subclasses of the |
| 523 | basic API types (i.e., glFunction, glEnum, etc.) to handle |
| 524 | additional XML data, GLX protocol information, that the base |
| 525 | classes do not know about.""" |
| 526 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 527 | if self.current_object != None: |
| 528 | self.current_object.startElement(name, attrs) |
| 529 | elif name == "category": |
| 530 | self.current_category = attrs.get('name', "") |
| 531 | else: |
| 532 | self.current_object = self.factory.create(self, name, attrs) |
| 533 | return |
| 534 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 535 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 536 | def endElement(self, name): |
| 537 | if self.current_object != None: |
| 538 | if self.current_object.endElement(name): |
| 539 | self.current_object = None |
| 540 | return |
| 541 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 542 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 543 | def printFunction(self,offset): |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 544 | """Print a single function. |
| 545 | |
| 546 | In the base class, this function is empty. All derived |
| 547 | classes should over-ride this function.""" |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 548 | return |
| 549 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 550 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 551 | def printRealHeader(self): |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 552 | """Print the "real" header for the created file. |
| 553 | |
| 554 | In the base class, this function is empty. All derived |
| 555 | classes should over-ride this function.""" |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 556 | return |
| 557 | |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 558 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 559 | def printRealFooter(self): |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 560 | """Print the "real" footer for the created file. |
| 561 | |
| 562 | In the base class, this function is empty. All derived |
| 563 | classes should over-ride this function.""" |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 564 | return |