1d4401354Smrg'''
2d4401354SmrgXML parser.  One function for each top-level element in the schema.
3d4401354Smrg
4d4401354SmrgMost functions just declare a new object and add it to the module.
5d4401354SmrgFor typedefs, eventcopies, xidtypes, and other aliases though,
6d4401354Smrgwe do not create a new type object, we just record the existing one under a new name.
7d4401354Smrg'''
8d4401354Smrg
9d4401354Smrgfrom os.path import join
10d5c9b07bSmrgfrom sys import version_info
11d5c9b07bSmrg
12d5c9b07bSmrgif version_info[:2] >= (3, 3):
13d5c9b07bSmrg    from xml.etree.ElementTree import parse
14d5c9b07bSmrgelse:
15d5c9b07bSmrg    from xml.etree.cElementTree import parse
16d4401354Smrg
17986c8b3dSmrgfrom xcbgen.xtypes import *
18d4401354Smrg
19d4401354Smrgdef import_(node, module, namespace):
20d4401354Smrg    '''
21d4401354Smrg    For imports, we load the file, create a new namespace object,
22d4401354Smrg    execute recursively, then record the import (for header files, etc.)
23d4401354Smrg    '''
24986c8b3dSmrg    # To avoid circular import error
25986c8b3dSmrg    from xcbgen import state
26b26960f7Smrg    module.import_level = module.import_level + 1
27d4401354Smrg    new_file = join(namespace.dir, '%s.xml' % node.text)
28d4401354Smrg    new_root = parse(new_file).getroot()
29d4401354Smrg    new_namespace = state.Namespace(new_file)
30d4401354Smrg    execute(module, new_namespace)
31b26960f7Smrg    module.import_level = module.import_level - 1
32d4401354Smrg    if not module.has_import(node.text):
33d4401354Smrg        module.add_import(node.text, new_namespace)
34d4401354Smrg
35d4401354Smrgdef typedef(node, module, namespace):
36d4401354Smrg    id = node.get('newname')
37d4401354Smrg    name = namespace.prefix + (id,)
38d4401354Smrg    type = module.get_type(node.get('oldname'))
39d4401354Smrg    module.add_type(id, namespace.ns, name, type)
40d4401354Smrg
41d4401354Smrgdef xidtype(node, module, namespace):
42d4401354Smrg    id = node.get('name')
43d4401354Smrg    name = namespace.prefix + (id,)
44d4401354Smrg    type = module.get_type('CARD32')
45d4401354Smrg    module.add_type(id, namespace.ns, name, type)
46d4401354Smrg
47d4401354Smrgdef xidunion(node, module, namespace):
48d4401354Smrg    id = node.get('name')
49d4401354Smrg    name = namespace.prefix + (id,)
50d4401354Smrg    type = module.get_type('CARD32')
51d4401354Smrg    module.add_type(id, namespace.ns, name, type)
52d4401354Smrg
53d4401354Smrgdef enum(node, module, namespace):
54d4401354Smrg    id = node.get('name')
55d4401354Smrg    name = namespace.prefix + (id,)
56d4401354Smrg    type = Enum(name, node)
57d4401354Smrg    module.add_type(id, namespace.ns, name, type)
58d4401354Smrg
59d4401354Smrgdef struct(node, module, namespace):
60d4401354Smrg    id = node.get('name')
61d4401354Smrg    name = namespace.prefix + (id,)
62d4401354Smrg    type = Struct(name, node)
63d4401354Smrg    module.add_type(id, namespace.ns, name, type)
64d4401354Smrg
65a27842ffSmrgdef eventstruct(node, module, namespace):
66a27842ffSmrg    id = node.get('name')
67a27842ffSmrg    name = namespace.prefix + (id,)
68a27842ffSmrg    type = EventStruct(name, node)
69a27842ffSmrg    module.add_type(id, namespace.ns, name, type)
70a27842ffSmrg
71d4401354Smrgdef union(node, module, namespace):
72d4401354Smrg    id = node.get('name')
73d4401354Smrg    name = namespace.prefix + (id,)
74d4401354Smrg    type = Union(name, node)
75d4401354Smrg    module.add_type(id, namespace.ns, name, type)
76d4401354Smrg
77d4401354Smrgdef request(node, module, namespace):
78d4401354Smrg    id = node.get('name')
79d4401354Smrg    name = namespace.prefix + (id,)
80d4401354Smrg    type = Request(name, node)
81d4401354Smrg    module.add_request(id, name, type)
82d4401354Smrg
83d4401354Smrgdef event(node, module, namespace):
84d4401354Smrg    id = node.get('name')
85d4401354Smrg    name = namespace.prefix + (id,)
86d4401354Smrg    event = Event(name, node)
87d4401354Smrg    event.add_opcode(node.get('number'), name, True)
88d4401354Smrg    module.add_event(id, name, event)
89d4401354Smrg
90d4401354Smrgdef eventcopy(node, module, namespace):
91d4401354Smrg    id = node.get('name')
92d4401354Smrg    name = namespace.prefix + (id,)
93d4401354Smrg    event = module.get_event(node.get('ref'))
94d4401354Smrg    event.add_opcode(node.get('number'), name, False)
95d4401354Smrg    module.add_event(id, name, event)
96d4401354Smrg
97d4401354Smrgdef error(node, module, namespace):
98d4401354Smrg    id = node.get('name')
99d4401354Smrg    name = namespace.prefix + (id,)
100d4401354Smrg    error = Error(name, node)
101d4401354Smrg    error.add_opcode(node.get('number'), name, True)
102d4401354Smrg    module.add_error(id, name, error)
103d4401354Smrg
104d4401354Smrgdef errorcopy(node, module, namespace):
105d4401354Smrg    id = node.get('name')
106d4401354Smrg    name = namespace.prefix + (id,)
107d4401354Smrg    error = module.get_error(node.get('ref'))
108d4401354Smrg    error.add_opcode(node.get('number'), name, False)
109d4401354Smrg    module.add_error(id, name, error)
110d4401354Smrg
111d4401354Smrgfuncs = {'import' : import_,
112d4401354Smrg         'typedef' : typedef,
113d4401354Smrg         'xidtype' : xidtype,
114d4401354Smrg         'xidunion' : xidunion,
115d4401354Smrg         'enum' : enum,
116d4401354Smrg         'struct' : struct,
117a27842ffSmrg         'eventstruct' : eventstruct,
118d4401354Smrg         'union' : union,
119d4401354Smrg         'request' : request,
120d4401354Smrg         'event' : event,
121d4401354Smrg         'eventcopy' : eventcopy,
122d4401354Smrg         'error' : error,
123d4401354Smrg         'errorcopy' : errorcopy}
124d4401354Smrg
125d4401354Smrgdef execute(module, namespace):
126d4401354Smrg    for elt in list(namespace.root):
127d4401354Smrg        funcs[elt.tag](elt, module, namespace)
128