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
402
403
404
405
406
407
408
409
410
411
|
#!/usr/bin/python
import sys
from lxml import etree as ET
# this does not recognize octal unlike int(s, 0)
def intdh(s):
if s.startswith("0x"):
return int(s[2:], 16)
else:
return int(s)
root = ET.Element("database")
if sys.argv[1:2]:
filename = sys.argv[1]
rnn = open(filename)
else:
rnn = sys.stdin
s = "\n" + rnn.read()
default_reg = "reg32"
comments = {}
token = ""
i = 0
tokens = []
attrvalues = []
comment = ""
linenum = 0
errors = 0
linestart = 0
brief = None
indent = 0
def str_width(s):
w = 0
for i in s:
if i == '\t':
w = (w + 8) & ~7
else:
w = w + 1
return w
def hexa(v):
if v <= 9:
return str(v)
else:
return hex(v)
def indent_str(indent):
return ("\t" * (indent >> 3) + " " * (indent & 7)) if indent > 0 else ""
def error(msg):
global errors
print >> sys.stderr, filename + ":" + str(linenum) + ":" + str(str_width(s[linestart:i]) + 1) + ": " + msg
errors += 1
def isident(c):
return c.isalnum() or c == "_" or c == "-" or c == "#" or c == ","
def reduce_stack(target):
global stack
while len(stack) > target:
stack[-1][1].tail = "\n" + indent_str(stack[-2][0])
stack = stack[:-1]
special_chars = ":=[]{}"
stack = [(-1, root)]
last_attachment = None
empty_line = False
while True:
c = s[i:i+1]
if s[i:i + 2] == "//":
i += 2
begin = i
while s[i] and s[i] != "\n":
i += 1
comment = s[begin:i]
c = "\n"
if not c or c == "\n":
if len(tokens) == 0:
empty_line = True
else:
last_closed_elem = None
if last_attachment is not None:
last_closed_elem = last_attachment
last_attachment = None
for j in xrange(len(stack)):
if indent <= stack[j][0]:
if indent < stack[j][0]:
error("line indentation does not match any previous indentation")
last_closed_elem = stack[j][1]
reduce_stack(j)
stack = stack[0:j]
break
elem = None
parent = stack[-1][1]
ptag = parent.tag
parent_is_reg = ptag in ("reg8", "reg16", "reg32")
reglike_tag = None
while tokens:
if tokens[0] in ("bare", "inline"):
attrvalues.append((tokens[0], "yes"))
tokens = tokens[1:]
else:
break
for j in xrange(len(tokens)):
if not tokens[j]:
continue
if tokens[j][0] == "!":
attrvalues.append(("access", tokens[j][1:]))
tokens[j] = None
if j >= 2 and tokens[j] == ":" and (j + 1) < len(tokens) and tokens[j + 1].isdigit():
reglike_tag = "reg" + tokens[j + 1]
tokens[j] = tokens[j + 1] = None
if (j + 2) < len(tokens):
if tokens[j] == "{" and tokens[j + 2] == "}":
attrvalues.append(("stride", tokens[j + 1]))
tokens[j] = tokens[j + 1] = tokens[j + 2] = None
if tokens[j] == "[" and tokens[j + 2] == "]":
attrvalues.append(("length", tokens[j + 1]))
tokens[j] = tokens[j + 1] = tokens[j + 2] = None
tokens = [token for token in tokens if token]
if tokens and tokens[-1] == "=":
if len(tokens) >= 2 and tokens[-2] == ":":
reglike_tag = "stripe"
tokens = tokens[:-2]
else:
reglike_tag = "array"
tokens = tokens[:-1]
if not tokens:
error("line with no tokens")
elif tokens[0] == "#import":
elem = ET.Element("import")
if brief:
elem.attrib["file"] = brief
brief = None
else:
error("#include without file argument")
if len(tokens) > 1:
error("too many tokens in #include line")
elif tokens[0] == "#pragma":
if len(tokens) == 1:
error("pragma with no arguments")
elif tokens[1][:3] == "reg":
default_reg = tokens[1]
else:
error("unrecognized pragma")
elem = None
elif tokens[0] == "@":
elem = ET.Element(tokens[0][1:])
if len(tokens) >= 2:
elem.attrib["name"] = tokens[1]
elif len(tokens) >= 3:
error("too many tokens in generic tag line")
elif tokens[0] == ":":
doc = parent.find("doc")
if doc is not None:
prev = doc.getprevious()
if prev is not None:
docindent = prev.tail
else:
prev = doc.getparent()
if prev is not None:
docindent = prev.text
else:
docindent = ""
if "\n" not in doc.text:
doc.text = docindent + "\t" + doc.text + docindent
nl = doc.text.rfind("\n")
doc.text = doc.text[:nl] + docindent + "\t" + (tokens[1] if len(tokens) >= 2 else "") + docindent
elem = None
else:
elem = ET.Element("doc")
if len(tokens) >= 2:
elem.text = tokens[1]
elif tokens[-1] == ":":
elem = ET.Element(tokens[0])
if len(tokens) >= 3:
elem.attrib["name"] = tokens[1]
elif len(tokens) >= 4:
error("too many tokens in generic tag line")
elif len(tokens) == 1 and (parent_is_reg or ptag == "enum"):
elem = ET.Element("value")
if tokens[0][0].isdigit():
elem.attrib["value"] = tokens[0]
else:
elem.attrib["name"] = tokens[0]
elif len(tokens) >= 3 and tokens[1] == "=":
elem = ET.Element("value")
elem.attrib["value"] = tokens[0]
elem.attrib["name"] = tokens[2]
if len(tokens) >= 4:
error("too many tokens in value line")
elif len(tokens) >= 3 and tokens[0] == "use":
elem = ET.Element("use-" + tokens[1])
elem.attrib["name"] = tokens[2]
if len(tokens) >= 4:
error("too many tokens in use line")
elif parent_is_reg or ptag == "bitset":
elem = ET.Element("bitfield")
sl = tokens[0].find("-")
if sl >= 0:
low = tokens[0][:sl]
high = tokens[0][sl + 1:]
else:
low = high = tokens[0]
if low == high:
elem.attrib["pos"] = str(intdh(low))
else:
elem.attrib["low"] = str(intdh(low))
elem.attrib["high"] = str(intdh(high))
if len(tokens) >= 2:
elem.attrib["name"] = tokens[1]
if len(tokens) >= 3:
elem.attrib["type"] = tokens[2]
if len(tokens) >= 4:
error("too many tokens in bitfield line")
else:
elem = ET.Element(reglike_tag if reglike_tag else default_reg)
elem.attrib["offset"] = hexa(int(tokens[0], 16))
if len(tokens) >= 2:
elem.attrib["name"] = tokens[1]
if len(tokens) >= 3:
elem.attrib["type"] = tokens[2].replace(",", " ")
if len(tokens) >= 4:
error("too many tokens in reg line")
reglike_tag = None
if elem is not None:
for attr, value in attrvalues:
elem.attrib[attr] = value
if elem.attrib.get("type") == "reg":
del elem.attrib["type"]
# if elem.tag == "bitfield":
# if parent.tag.startswith("reg"):
# parent.attrib["type"] = "bitfield"
# if elem.tag == "value":
# if parent.tag.startswith("reg"):
# parent.attrib["type"] = "enum"
if brief:
briefelem = ET.Element("brief")
briefelem.tag = "brief"
briefelem.text = brief
elem.append(briefelem)
last_attachment = briefelem
parent.append(elem)
xmlindent = "\n" +indent_str(indent)
if last_closed_elem is not None:
last_closed_elem.tail = ("\n" if empty_line else "") + xmlindent
else:
parent.text = xmlindent
stack.append((indent, elem))
empty_line = False
if reglike_tag:
error("line specified to be a " + reglike_tag + " but the line format does not match it")
if comment:
commentelem = ET.Comment(comment)
comments.setdefault(stack[-1][1], []).append(commentelem)
tokens = []
attrvalues = []
comment = None
brief = None
linenum += 1
linestart = i
indent = 0
if not c:
break
i += 1
while s[i:i + 1]:
if s[i:i+1] == " ":
indent += 1
if s[i:i+1] == "\t":
indent = (indent + 8) & ~7
else:
break
i += 1
indent &= ~7
if s[i:i + 1] == ":":
begin = i + 1
while s[i:i+1] and s[i] != "\n" and s[i:i+2] != "//":
i += 1
tokens.append(":")
tokens.append(s[begin:i].strip())
elif c.isspace():
i += 1
pass
elif c in special_chars:
tokens.append(c)
i += 1
elif c == "\"":
i += 1
begin = i
while True:
if not s[i:i+1] or s[i] == "\"":
break
if s[i:i+1] == "\\":
i += 2
else:
i += 1
if brief:
error("brief specified multiple times")
else:
if not tokens:
error("brief cannot be the start of a line")
else:
brief = s[begin:i]
i += 1
else:
begin = i
paren = -1
while True:
d = s[i:i+1]
if d == "(":
paren = i
i += 1
while True:
if not s[i:i+1]:
error("unterminated parenthesis")
break
elif s[i] == ')':
break
else:
i += 1
break
elif d.isspace() or d in special_chars:
break
else:
i += 1
if paren >= 0:
if paren == begin:
variants = s[paren + 1:i]
equals = variants.rfind("=")
if equals >= 0:
attrvalues.append(("varset", variants[:equals]))
variants = variants[equals + 1:]
attrvalues.append(("variants", variants))
else:
attrvalues.append((s[begin:paren], s[paren + 1:i]))
if s[i] == ")":
i += 1
else:
token = s[begin:i]
tokens.append(token)
reduce_stack(1)
# TODO: this takes in ordinate amount of code... can it be shortened?
for elem, commentelems in comments.items():
for action, subelem in ET.iterwalk(elem, events = ("start", "end")):
if action == "start":
if subelem.text and "\n" in subelem.text:
if len(commentelems) == 1:
subelem.insert(0, commentelems[0])
commentelems[0].tail = subelem.text
subelem.text = " "
else:
for i in xrange(len(commentelems)):
subelem.insert(i, commentelems[i])
commentelems[i].tail = subelem.text
break
elif action == "end":
if subelem.tail and "\n" in subelem.tail:
parent = subelem.getparent()
i = parent.index(subelem)
i += 1
if len(commentelems) == 1:
parent.insert(i, commentelems[0])
commentelems[0].tail = subelem.tail
subelem.tail = " "
else:
prev = elem.getprevious()
if prev is not None:
indent = prev.tail
else:
prev = elem.getparent()
if prev is not None:
indent = prev.text
else:
indent = "\n"
indent += "\t"
for j in xrange(len(commentelems)):
parent.insert(i + j, commentelems[j])
commentelems[j].tail = indent
commentelems[-1].tail = subelem.tail
subelem.tail = indent
break
# lxml etree refuses attributes with colons, and using namespaces "properly" is harder and has no advantages
# this is a sequence from /dev/urandom
colon = "39584ac56e6d0976692778b80915f94b61767814a8704982"
root.attrib["xmlns"] = "http://nouveau.freedesktop.org/"
root.attrib["xmlns" + colon + "xsi"] = "http://www.w3.org/2001/XMLSchema-instance"
root.attrib["xsi" + colon + "schemaLocation"] = "http://nouveau.freedesktop.org/ rules-ng.xsd"
print ET.tostring(root).replace(colon, ":")
sys.exit(0 if errors == 0 else 1)
|