Created
August 26, 2019 21:42
-
-
Save rojer/4fa173442fb00e24dc7b9d120c2e30af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Copyright 2016-2019 the u-root Authors. All rights reserved | |
# Use of this source code is governed by a BSD-style | |
# license that can be found in the LICENSE file. | |
import os | |
import re | |
import sys | |
type_name = sys.argv[1] | |
bits, extra = [], [] | |
for line in sys.stdin: | |
line = line.strip() | |
bit, desc = None, None | |
if line.startswith("Bit"): | |
parts = line.strip().split(None, 2) | |
if len(parts) == 3: | |
bit, desc = parts[1], parts[2] | |
else: | |
parts = line.strip().split(None, 1) | |
if len(parts) == 2 and re.match(r'\d+$', parts[0]): | |
bit, desc = parts[0], parts[1] | |
if bit is None: | |
extra.append("// %s" % line.strip()) | |
continue # some junk, skip | |
if parts[0] == "Bit": | |
parts = parts[1:] | |
v = int(parts[0], 10) | |
descr = parts[1] | |
ident_words = [] | |
for w in descr.split(): | |
w = re.sub(r"[^a-zA-Z0-9_]", "", w) | |
if w and w[0].islower(): | |
w = w.capitalize() | |
ident_words.append(w) | |
ident = type_name + "".join(ident_words) | |
bits.append((v, ident, descr)) | |
print("""\ | |
// %s fields are defined in DSP0134 x.x.x | |
const (""" % type_name) | |
i = 0 | |
for v, ident, descr in bits: | |
i += 1 | |
if i == 1: | |
print("\t%s %s = (1 << %d) // %s" % (ident, type_name, v, descr)) | |
else: | |
print("\t%s = (1 << %d) // %s" % (ident, v, descr)) | |
print("""\ | |
) | |
func (v %s) String() string { | |
\tvar lines []string""" % type_name) | |
for v, ident, descr in bits: | |
print('\tif (v & %s != 0) { lines = append(lines, "%s") }' % (ident, descr)) | |
print("""\ | |
\treturn "\\t\\t" + strings.Join(lines, "\\n\\t\\t") | |
}""") | |
print("\n".join(extra)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Copyright 2016-2019 the u-root Authors. All rights reserved | |
# Use of this source code is governed by a BSD-style | |
# license that can be found in the LICENSE file. | |
import os | |
import re | |
import sys | |
type_name = sys.argv[1] | |
enum_values, extra = [], [] | |
for line in sys.stdin: | |
parts = line.strip().split(None, 1) | |
if len(parts) != 2 or not re.match(r"[0-9A-Fa-f]{2}h", parts[0]): | |
extra.append("// %s" % line.strip()) | |
continue | |
try: | |
v = int(parts[0][:-1], 16) | |
except ValueError: | |
extra.append("// %s" % line.strip()) | |
continue | |
descr = parts[1] | |
if descr.startswith("%d " % v): | |
descr = descr[descr.index(" ")+1:] | |
""" | |
v = 0 | |
for line in ff.splitlines(): | |
#parts = line.strip().split(None, 2) | |
#if len(parts) < 2: | |
# continue | |
#print(parts) | |
v += 1 #int(parts[1].replace(",", ""), 0) | |
descr = line.strip().replace('"', "").replace(',', '') | |
""" | |
ident_words = [] | |
for w in descr.split(): | |
w = re.sub(r"[^a-zA-Z0-9_]", "", w) | |
if not w: | |
continue | |
if w[0].islower(): | |
w = w.capitalize() | |
ident_words.append(w) | |
ident = type_name + "".join(ident_words) | |
enum_values.append((v, ident, descr)) | |
print("""\ | |
// %s values are defined in DSP0134 x.x.x. | |
const (""" % type_name) | |
i = 0 | |
for v, ident, descr in enum_values: | |
i += 1 | |
if i == 1: | |
print("\t%s %s = 0x%02x // %s" % (ident, type_name, v, descr)) | |
else: | |
print("\t%s = 0x%02x // %s" % (ident, v, descr)) | |
print("""\ | |
) | |
func (v %s) String() string { | |
\tswitch v {""" % type_name) | |
for v, ident, descr in enum_values: | |
print('\tcase %s: return "%s"' % (ident, descr)) | |
print("""\ | |
\t} | |
\treturn fmt.Sprintf("%d", v) | |
}""") | |
print("\n".join(extra)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import re | |
import sys | |
type_name = sys.argv[1] | |
length_values = {"BYTE": "uint8", "WORD": "uint16", "DWORD": "uint32", "QWORD": "uint64"} | |
fields = [] | |
enums = [] | |
def ProcessRow(row): | |
if not row: | |
return | |
parts = row.strip().split(None, 1) | |
off = int(parts[0][:-1], 16) | |
if off < 4: # Header | |
return | |
rem = parts[1] | |
ident, go_type, comment = "", "", "" | |
for w in rem.split(): | |
if not go_type: | |
len_col = False | |
for lv, ft in length_values.items(): | |
if lv in w: | |
# This is the length column which marks the end of identifier | |
# and is our first indication of type | |
# (may be corrected later for enums and strings). | |
go_type = ft | |
len_col = True | |
if len_col: | |
continue | |
# Words before the type comprise the identifier. | |
if w.endswith("+"): | |
continue # Version, skip it. | |
# Sanitize the identifier. | |
w = re.sub(r"[^a-zA-Z0-9_]", "", w) | |
if w and w[0].islower(): | |
w = w.capitalize() | |
ident += w | |
elif not comment: # Value type column. | |
if w == "STRING": | |
go_type = "string" | |
elif w == "ENUM": | |
enums.append((ident, go_type)) | |
go_type = ident | |
elif w in ("Bit", "Field"): | |
if go_type != ident: | |
enums.append((ident, go_type)) | |
go_type = ident | |
elif w == "Varies": | |
pass | |
else: | |
comment = w | |
else: | |
if len(comment) < 50: | |
comment += " " + w | |
elif not comment.endswith(" ..."): | |
comment += " ..." | |
fields.append((off, ident, go_type, comment)) | |
row = "" | |
for line in sys.stdin: | |
if re.search(r"^[0-9A-Fa-f]{2}h", line): | |
ProcessRow(row) | |
row = line.strip() | |
else: | |
row += " " + line.strip() | |
ProcessRow(row) | |
print("""\ | |
// %s is defined in DSP0134 x.x. | |
type %s struct {""" % (type_name,type_name)) | |
i = 0 | |
for off, ident, go_type, comment in fields: | |
i += 1 | |
if i == 1: | |
print("\tTable") | |
print("\t%s\t%s\t// %02Xh" % (ident, go_type, off)) | |
print("}") | |
for n, t in enums: | |
print("// %s is defined in DSP0134 x.x.x." % n) | |
print("type %s %s" % (n, t)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment