def parse(): try: tree = ET.parse( GL_FILE ) except FileNotFound: error( "Can't find file \"%s\"." % GL_FILE ) except: error( "An unexpected exception occured: %s" % sys.exc_info()[1] ) root = tree.getroot() if root.tag != "registry": error( "Expected root node to be \"registry\", got \"%s\" instead" % root.tag ) for child in root: if child.tag == "comment": print( "/*%s*/" % child.text ) #ignore groups elif child.tag == "groups": pass elif child.tag == "types": parseTypes( child ) elif child.tag == "enums": parseEnums( child ) elif child.tag == "commands": parseCommands( child ) elif child.tag == "feature": parseFeature( child ) elif child.tag == "extensions": parseExtensions( child ) else: tagError( child ) print( "Parsed %d enums." % len( enums ) ) print( "Parsed %d commands." % len( commands ) ) print( "Parsed %d features." % len( features ) ) print( "Parsed %d extensions." % len( extensions ) )
def parseExtensions( node ): for child in node: if child.tag == "extension": parseExtension( child ) else: tagError( child ) #Make sure list of extensions is sorted alphabetically by name extensions.sort( key = lambda x: x.name )
def parseEnums( node ): global enums for child in node: if child.tag == "enum": name = child.attrib["name"] value = child.attrib["value"] enums[name] = Enum( name, value ) #ignore "unused" tags elif child.tag == "unused": pass else: tagError( child )
def parseTypes( node ): global includeTypes global types for child in node: if child.tag == "type": #Note: "requires" attribute is ignored if it exists text = innerText( child ) t = Type( text, child.get( "name" ), child.get( "comment" ), child.get( "api" ) ) #Types that include headers need to go outside of namespaces if "#include" in text: includeTypes.append( t ) else: types.append( t ) else: tagError( child )
def parseCommands( node ): global commands for child in node: if child.tag == "command": proto = child.find( "proto" ) rvnode = proto.find( "ptype" ) rv = (rvnode.text if rvnode != None else proto.text).strip() name = proto.find( "name" ).text params = [ innerText( paramnode ).strip() for paramnode in child if paramnode.tag == "param" ] commands[ name ] = Command( rv, name, params ) else: tagError( child )
def parseModule( node, module ): for child in node: if child.tag == "require": for child2 in child: if child2.tag == "enum": module.require( enums[ child2.attrib["name"] ] ) elif child2.tag == "command": module.require( commands[ child2.attrib["name"] ] ) #ignore "type" tags elif child2.tag == "type": pass else: tagError( child2 ) elif child.tag == "remove": for child2 in child: if child2.tag == "enum": module.remove( enums[ child2.attrib["name"] ] ) elif child2.tag == "command": module.remove( commands[ child2.attrib["name"] ] ) else: tagError( child2 ) else: tagError( child )