summaryrefslogtreecommitdiff
path: root/tools/c-constants-gen.py
blob: c74d4fdc978556e5c6649400a7808d47c972ff08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python

from sys import argv, stdout, stderr
import xml.dom.minidom

from libtpcodegen import file_set_contents
from libglibcodegen import NS_TP, get_docstring, \
        get_descendant_text, get_by_path

class Generator(object):
    def __init__(self, prefix, dom, output_base):
        self.prefix = prefix + '_'
        self.spec = get_by_path(dom, "spec")[0]

	self.output_base = output_base
        self.__header = []
        self.__docs = []

    def __call__(self):
        self.do_header()
        self.do_body()
        self.do_footer()

        file_set_contents(self.output_base + '.h', ''.join(self.__header))
        file_set_contents(self.output_base + '-gtk-doc.h', ''.join(self.__docs))

    def write(self, code):
        self.__header.append(code.encode('utf-8'))

    def d(self, code):
        self.__docs.append(code.encode('utf-8'))

    # Header
    def do_header(self):
        self.write('/* Generated from ')
        self.write(get_descendant_text(get_by_path(self.spec, 'title')))
        version = get_by_path(self.spec, "version")
        if version:
            self.write(', version ' + get_descendant_text(version))
        self.write('\n\n')
        for copyright in get_by_path(self.spec, 'copyright'):
            self.write(get_descendant_text(copyright))
            self.write('\n')
        self.write(get_descendant_text(get_by_path(self.spec, 'license')))
        self.write('\n')
        self.write(get_descendant_text(get_by_path(self.spec, 'docstring')))
        self.write("""
 */

#ifdef __cplusplus
extern "C" {
#endif
\n""")

    # Body
    def do_body(self):
        for elem in self.spec.getElementsByTagNameNS(NS_TP, '*'):
            if elem.localName == 'flags':
                self.do_flags(elem)
            elif elem.localName == 'enum':
                self.do_enum(elem)

    def do_flags(self, flags):
        name = flags.getAttribute('plural') or flags.getAttribute('name')
        value_prefix = flags.getAttribute('singular') or \
                       flags.getAttribute('value-prefix') or \
                       flags.getAttribute('name')
        self.d("""\
/**
 * %s:
""" % (self.prefix + name).replace('_', ''))
        for flag in get_by_path(flags, 'flag'):
            self.do_gtkdoc(flag, value_prefix)
        self.d(' *\n')
        docstrings = get_by_path(flags, 'docstring')
        if docstrings:
            self.d("""\
 * <![CDATA[%s]]>
 *
""" % get_descendant_text(docstrings).replace('\n', ' '))
        self.d("""\
 * Bitfield/set of flags generated from the Telepathy specification.
 */
""")

        self.write("typedef enum /*< flags >*/ {\n")

        for flag in get_by_path(flags, 'flag'):
            self.do_val(flag, value_prefix)
        self.write("""\
} %s;

""" % (self.prefix + name).replace('_', ''))

    def do_enum(self, enum):
        name = enum.getAttribute('singular') or enum.getAttribute('name')
        value_prefix = enum.getAttribute('singular') or \
                       enum.getAttribute('value-prefix') or \
                       enum.getAttribute('name')
        name_plural = enum.getAttribute('plural') or \
                      enum.getAttribute('name') + 's'
        self.d("""\
/**
 *
%s:
""" % (self.prefix + name).replace('_', ''))
        vals = get_by_path(enum, 'enumvalue')
        for val in vals:
            self.do_gtkdoc(val, value_prefix)
        self.d(' *\n')
        docstrings = get_by_path(enum, 'docstring')
        if docstrings:
            self.d("""\
 * <![CDATA[%s]]>
 *
""" % get_descendant_text(docstrings).replace('\n', ' '))
        self.d("""\
 * Bitfield/set of flags generated from the Telepathy specification.
 */
""")

        self.write("typedef enum {\n")

        for val in vals:
            self.do_val(val, value_prefix)
        self.write("} %s;\n" % (self.prefix + name).replace('_', ''))

        self.d("""\
/**
 * NUM_%(upper-plural)s: (skip)
 *
 * 1 higher than the highest valid value of #%(mixed-name)s.
 */
""" % {'mixed-name' : (self.prefix + name).replace('_', ''),
       'upper-plural' : (self.prefix + name_plural).upper(),
       'last-val' : vals[-1].getAttribute('value')})

        self.write("""\
#define NUM_%(upper-plural)s (%(last-val)s+1)

""" % {'mixed-name' : (self.prefix + name).replace('_', ''),
       'upper-plural' : (self.prefix + name_plural).upper(),
       'last-val' : vals[-1].getAttribute('value')})

    def do_val(self, val, value_prefix):
        name = val.getAttribute('name')
        suffix = val.getAttribute('suffix')
        use_name = (self.prefix + value_prefix + '_' + \
                (suffix or name)).upper()
        assert not (name and suffix) or name == suffix, \
                'Flag/enumvalue name %s != suffix %s' % (name, suffix)
        self.write('    %s = %s,\n' % (use_name, val.getAttribute('value')))

    def do_gtkdoc(self, node, value_prefix):
        self.d(' * @')
        self.d((self.prefix + value_prefix + '_' +
            node.getAttribute('suffix')).upper())
        self.d(': <![CDATA[')
        docstring = get_by_path(node, 'docstring')
        self.d(get_descendant_text(docstring).replace('\n', ' '))
        self.d(']]>\n')

    # Footer
    def do_footer(self):
        self.write("""
#ifdef __cplusplus
}
#endif
""")

if __name__ == '__main__':
    argv = argv[1:]
    Generator(argv[0], xml.dom.minidom.parse(argv[1]), argv[2])()