summaryrefslogtreecommitdiff
path: root/gendoc.py
blob: e1c7057884f1db4e5aa45249750cf5b94d4837bd (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from lxml import etree
import getopt
import os
import sys
import errno
import re
import io
import subprocess
import collections

lentag = 0
isextension = False
indents = collections.defaultdict(int)
tymap = {
    'char':     'byte',
    'BYTE':     'byte',
    'INT8':     'int8',
    'INT16':    'int16',
    'INT32':    'int32',
    'INT64':    'int64',
    'CARD8':    'byte',
    'CARD16':   'uint16',
    'CARD32':   'uint32',
    'CARD64':   'uint64',
    'BOOL':     'bool',
    'xid':      'xid',
    'void':     'void',
}

renames = {
    'render': {
        'CreateCursor': 'RenderCreateCursor'
    }
}

def writeln(f, s, *args):
    s = s.format(*args)
    if len(s) == 0:
        f.write('\n')
    for ln in s.splitlines():
        if len(ln):
            f.write('\t'*indents[f] + ln + '\n')
        else:
            f.write('\n')

def indent(f):
    indents[f] += 1

def outdent(f):
    indents[f] -= 1

def myrtype(ty):
    if ty in tymap:
        return tymap[ty]
    else:
        return ty.lower()

def isprimitive(ty):
    return ty in tymap

def load(mod, path):
    with open(path) as f:
        t = etree.parse(f)

    elts = collections.defaultdict(dict)
    for e in t.getroot():
        if e.tag is etree.Comment:
            continue
        name = e.get('name')
        if mod in renames and name in renames[mod]:
            print('rename {} => {}'.format(name, renames[mod][name]))
            e.set('name', renames[mod][name])
        if e.tag == 'enum':         elts['enums'][e.get('name')] = e
        elif e.tag == 'error':      elts['errors'][e.get('name')] = e
        elif e.tag == 'errorcopy':  elts['errors'][e.get('name')] = e
        elif e.tag == 'event':      elts['events'][e.get('name')] = e
        elif e.tag == 'eventcopy':  elts['events'][e.get('name')] = e
        elif e.tag == 'struct':     elts['structs'][e.get('name')] = e
        elif e.tag == 'request':    elts['requests'][e.get('name')] = e
        elif e.tag == 'union':      elts['unions'][e.get('name')] = e
        elif e.tag == 'import':     elts['imports'][e.text] = e.text
        elif e.tag == 'xidtype':    tymap[e.get('name')] = tymap['xid']
        elif e.tag == 'xidunion':   tymap[e.get('name')] = tymap['xid']
        elif e.tag == 'typedef':    tymap[e.get('newname')] = myrtype(e.get('oldname'))
        elif e.tag == 'generate':   raise Exception('unimplemented')
        else:
            raise Exception('unknown tag {}'.format(e.tag))
    return elts

def enumval(e):
    if e.tag == 'bit':
        return 1 << int(e.text)
    elif e.tag == 'value':
        return int(e.text)
    else:
        print etree.tostring(e)
        raise Exception('unknown tag {}'.format(e.tag))

def genenum(f, name, elts):
    ename = name.lower()
    writeln(f, 'type {} = int32', ename)
    for elt in elts:
        if elt.tag is etree.Comment:
            continue
        if elt.tag == 'item':
            val = enumval(elt[0])
            writeln(f, 'const {}{}\t: {} = {}',
                name, elt.get('name'), ename, val)
    writeln(f, '')

def lenexpr(elt):
    if elt.tag == 'fieldref':
        return '(val.{} : std.size)'.format(elt.text)
    elif elt.tag == 'op':
        return '({} {} {})'.format(
                lenexpr(elt[0]), elt.get('op'), lenexpr(elt[1]))
    elif elt.tag == 'value':
        return elt.text
    else:
        print etree.tostring(elt)
        raise Exception('unknown tag {}'.format(elt.tag))

def fieldname(elt):
    name = elt.get('name')
    if name == 'type':
        name = 'kind'
    return name

def genstructif(f, elt, suff=''):
    if elt.tag == 'field':
        writeln(f, '{}\t: {}{}',
            fieldname(elt), myrtype(elt.get('type')), suff)
    elif elt.tag == 'list':
        etype = elt.get('type')
        if etype == 'void':
            etype = 'BYTE'
        listlen = ':'
        if elt.find('value') is not None:
            listlen = elt.find('value').text
        writeln(f, '{}\t: {}[{}]{}',
            fieldname(elt), myrtype(etype), listlen, suff)
    elif elt.tag == 'pad':
        pass
    elif elt.tag == 'switch':
        for case in elt.findall('bitcase'):
            if case.find('field') is None:
                print etree.tostring(case)
                raise Exception('unknown tag {}'.format(elt.tag))
            writeln(f, '{}\t: {}',
                fieldname(case.find('field')), myrtype(case.find('field').get('type')))
    elif elt.tag == 'exprfield':
        # synthesized, we can ignore for now
        pass
    else:
        print etree.tostring(elt)
        raise Exception('unknown tag {}'.format(elt.tag))

def packfirst(elts, isreq, isevent):
    if isreq or (isevent and not elts.get('xge')):
        for (idx, elt) in enumerate(elts):
            if elt.tag is etree.Comment or elt.tag in {'reply', 'doc'}:
                continue
            if elt.tag == 'pad' and elt.get('bytes') == '1':
                    return (True, elt, elts[idx+1:])
            if elt.tag == 'field':
                if myrtype(elt.get('type')) in {'byte', 'int8', 'uint8', 'bool'}:
                    return (True, elt, elts[idx+1:])
    return (False, None, elts)

def genstruct(f, name, selt, isreq=False, isresp=False, isevent=False):
    suff = ''
    if isreq:
        suff = 'req'
    elif isresp:
        suff = 'resp'
    (pack, first, elts) = packfirst(selt, isreq, isevent)
    sname = name.lower()
    writeln(f, 'type {}{} = struct', sname, suff)
    # generate interface
    indent(f)
    if isreq:
        writeln(f, 'major\t: byte')
        if pack:
            genstructif(f, first)
        else:
            writeln(f, 'minor\t: byte')
        writeln(f, 'length\t: uint16')
    elif isresp:
        writeln(f, 'response_type\t: byte')
        writeln(f, 'sequence\t: uint16')
        writeln(f, 'length\t: uint32')
    elif isevent:
        writeln(f, 'issent\t: bool')
        if selt.get('xge') is not None:
            writeln(f, 'ext\t: byte')
            writeln(f, 'sequence\t:uint16')
            writeln(f, 'length\t: uint32')
            writeln(f, 'event_type\t: uint16')
        else:
            if pack:
                genstructif(f, first)
            if selt.get('no-sequence-number') is None:
                writeln(f, 'sequence\t:uint16')

    for elt in elts:
        if elt.tag is etree.Comment or elt.tag in {'reply', 'doc'}:
            continue
        genstructif(f, elt)


    outdent(f)
    writeln(f, ';;')
    writeln(f, '')

    sw = selt.find('switch')
    if sw is not None:
        writeln(f, 'const No{}vals = (0 : {}vals#)', sname, sname)
        writeln(f, 'type {}vals = struct', sname)
        indent(f)
        for elt in sw.findall('bitcase'):
            if elt.tag is etree.Comment or elt.tag in {'reply', 'doc'}:
                continue
            genstructif(f, elt.find('field'))
        outdent(f)
        writeln(f, ';;')
        writeln(f, '')
    writeln(f, '')


def eltsz(elt):
    return 20


# ugh, C style
def genunion(f, name, elts, suff=''):
    sz = 0
    uname = name.lower()
    writeln(f, 'type {} = struct', uname)
    indent(f)
    for u in elts:
        if u.tag is etree.Comment:
            continue
        genstructif(f, u, '[...]')
        sz = max(sz, eltsz(u))
    outdent(f)
    writeln(f, ';;')
    writeln(f, '')

    writeln(f, 'const pack{}{} : (c : display#, val : {}{}# -> void)', uname, suff, uname, suff)
    writeln(f, 'const unpack{}{} : (buf : byte[:], off : std.size, val : {}{}# -> std.size)', uname, suff, uname, suff)
    writeln(f, '')


def gencallproto(f, name, elts):
    f.write(indents[summary]*'\t')
    f.write('const {} : (dpy : display#'.format(name.lower()))
    resp = elts.find('reply')
    for elt in elts:
        if elt.tag is etree.Comment or elt.tag in {'pad', 'reply', 'doc', 'reply', 'exprfield'}:
            continue

        f.write(', ')
        if elt.tag == 'field':
            f.write('{} : {}'.format(
                fieldname(elt), myrtype(elt.get('type'))))
        elif elt.tag == 'list':
            etype = elt.get('type')
            if etype == 'void':
                etype = 'BYTE'
            f.write('{}: {}[:]'.format(
                fieldname(elt), myrtype(etype)))
        elif elt.tag == 'switch':
            f.write('vals : {}vals#'.format(name.lower()))
        elif elt.tag == 'exprfield':
            # synthesized, we can ignore for now
            pass
        else:
            print etree.tostring(elt)
            raise Exception('unknown tag {}'.format(elt.tag))
    if resp is not None:
        writeln(f, '-> {}seq)\n', name.lower())
        writeln(f, 'const wait{} : (dpy : display#, tag : {}seq -> {}resp#)',
            name.lower(), name.lower(), name.lower())
    else:
        f.write('-> void)\n')

def genrequest(f, name, elts):
    writeln(f, 'const {}\t: byte = {}', name, elts.get('opcode'))
    genstruct(f, name, elts, True, False)
    resp = elts.find('reply')
    if resp is not None:
        writeln(f, 'type {}seq = uint16', name.lower())
        genstruct(f, name, resp, False, True)
    gencallproto(f, name, elts)

def genevent(f, name, elts):
    writeln(f, 'const {} : byte = {}', name, elts.get('number'))
    genstruct(f, name, elts, isevent=True)

def geneventunion(f, mods, elts):
    for mod in mods:
        writeln(f, 'use "{}-gen"', mod)
    writeln(f, 'pkg xmyrb =')
    indent(f)
    writeln(f, 'type event = union')
    indent(f)
    for (k, v) in elts.items():
        writeln(f, '`{} {}', k, k.lower())
    outdent(f)
    writeln(f, ';;')
    outdent(f)
    writeln(f, ';;')

def finddoc(doc, n):
    for d in doc:
        if d.get('name') == n:
            if d.text:
                return d.text.strip()

def gendoc(f, fn, name, elt, doc):
    indent(f)
    fn(f, name, elt)
    outdent(f)
    if not doc:
        return
    b = doc.find('brief')
    if b:
        print(b.text)
    for sub in elt:
        if sub.tag == 'field':
            n = sub.get('name')
            desc = finddoc(doc, n)
            if desc:
                writeln(f, '`{}`: {}', n, finddoc(doc, n))
            writeln(f, '')

def generate(elts):
    writeln(summary, 'pkg xmyrb =')
    indent(summary)

    writeln(summary, '/* enums */')
    for (k, v) in elts['enums'].items():
        genenum(summary, k, v)
        gendoc(fulldoc, genenum, k, v, v.find('doc'))
    writeln(summary, '/* structs */')
    for (k, v) in elts['structs'].items():
        genstruct(summary, k, v)
        gendoc(fulldoc, genstruct, k, v, v.find('doc'))
    writeln(summary, '/* requests */')
    for (k, v) in elts['requests'].items():
        genrequest(summary, k, v)
        gendoc(fulldoc, genrequest, k, v, v.find('doc'))
    writeln(summary, '/* unions */')
    for (k, v) in elts['unions'].items():
        genunion(summary, k, v)
        gendoc(fulldoc, genunion, k, v, v.find('doc'))
    writeln(summary, '/* events */')
    for (k, v) in elts['events'].items():
        genevent(summary, k, v)
        gendoc(fulldoc, genevent, k, v, v.find('doc'))

    outdent(summary)
    writeln(summary, ';;')

def addevents(events, elts):
    for (k, v) in elts['events'].items():
        if k in events:
            raise Exception('event {} found in two modules'.format(k))
        events[k] = v

def main():
    global summary, fulldoc
    cmd = ['pkg-config', '--variable', 'xcbincludedir', 'xcb-proto']
    xcbdir = subprocess.check_output(cmd).strip()
    events = {}
    for mod in sys.argv[1:]:
        summary = io.BytesIO()
        fulldoc = io.BytesIO()
        indent(summary)
        path =  os.path.join(xcbdir, mod + '.xml')
        print('generate {}...'.format(mod))

        t = load(mod, path)
        addevents(events, t)
        generate(t)

        out = '{}-doc.txt'.format(mod)
        with open(out, 'w') as f:
            f.write(summary.getvalue())
            f.write(fulldoc.getvalue())
        outdent(summary)
        outdent(fulldoc)
        summary.close()

    summary = io.BytesIO()
    geneventunion(summary, sys.argv[1:], events)
    with open('events-merged.txt', 'w') as f:
        f.write(summary.getvalue())

if __name__ == '__main__':
    main()