git.s-ol.nu subv / 9d02be6
add optional strings stage s-ol 1 year, 11 months ago
4 changed file(s) with 74 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
0 #!/usr/bin/env python3
1 """ strings.py
2 Replaces literal strings with byte-slices
3
4 >>> from io import StringIO
5 >>> # doctest: +REPORT_NDIFF
6 ... print(subv.join_all(strings(StringIO('''
7 ... == data 0x80500000
8 ... E_MAGIC:
9 ... @@/string "Invalid DTB magic!\\\\n\\\\0"
10 ... S_TOTALSZ:
11 ... @@/string "Total Size: \\\\0"
12 ... S_DASH:
13 ... @@/string " - \\\\0"
14 ... '''[1:-1]))))
15 == data 0x80500000
16 E_MAGIC:
17 49/8 6e/8 76/8 61/8 6c/8 69/8 64/8 20/8 44/8 54/8 42/8 20/8 6d/8 61/8 67/8 69/8 63/8 21/8 a/8 0/8
18 S_TOTALSZ:
19 54/8 6f/8 74/8 61/8 6c/8 20/8 53/8 69/8 7a/8 65/8 3a/8 20/8 0/8
20 S_DASH:
21 20/8 2d/8 20/8 0/8
22 """
23
24 import subv
25 from ast import literal_eval
26
27 @subv.with_parsed_lines
28 def strings(iter):
29 for segment, line in iter:
30 if line["type"] == "special" and line["special"][0] == ('@@', 'string'):
31 string = literal_eval(line["special"][1])
32 result = {
33 "type": "instr",
34 "instr": [(byte, 8) for byte in string.encode('utf-8')],
35 "comment": None,
36 }
37
38 yield subv.format(result)
39 else:
40 yield line["raw"]
41
42 if __name__ == "__main__":
43 import sys
44
45 for line in strings(sys.stdin):
46 print(line)
7474 return (parts[1],)
7575 else:
7676 raise ValueError("invalid segment line")
77
78
79 def parse_special(line):
80 """parse a special-line.
81
82 >>> parse_special('@@/test "hello/123 asd"')
83 [('@@', 'test'), '"hello/123 asd"']
84 >>> parse_special('@@/test @@/123 @@/uuu')
85 [('@@', 'test'), '@@/123 @@/uuu']
86 """
87 parts = white.split(line, maxsplit=1)
88 parts[0] = parse_part(parts[0])
89 return parts
7790
7891
7992 def parse_label(line):
155168 'instr'
156169 >>> classify('ff/8 0/3 2/5')
157170 'instr'
171 >>> classify('@@/8')
172 'special'
173 >>> classify('@@/test 123 "asdx"')
174 'special'
158175 """
159176 if line == "":
160177 return "empty"
162179 return "segment"
163180 elif line.endswith(":"): # label
164181 return "label"
182 elif line.startswith("@@/"):
183 return "special"
165184 else:
166185 return "instr"
167186
184203 parsed = parse_label(clean)
185204 elif type == "instr":
186205 parsed = parse_instr(clean)
206 elif type == "special":
207 parsed = parse_special(clean)
187208 else:
188209 parsed = None
189210
323344 if line["comment"]:
324345 packed = packed + " # " + line["comment"]
325346 return packed
326 elif type == "empty":
347 elif type == "empty" or type == "special":
327348 return line["raw"]
328349 else:
329350 raise NotImplementedError("type {}".format(type))
1919
2020 msg "OUTPUTTING TO $BASE.*"
2121
22 msg UNSTRINGING...
23 ./strings.py <"$INPUT" >"$BASE.string"
2224 msg VALIDATING...
23 ./validate.py <"$INPUT" >"$BASE.valid"
25 ./validate.py <"$BASE.string" >"$BASE.valid"
2426 msg SURVEYING...
2527 ./survey.py <"$BASE.valid" >"$BASE.survey"
2628 msg FORMATTING...
612612 )
613613
614614 line["instr"] = validator(line["instr"][:])
615 line = yield subv.format(line)
615 yield subv.format(line)
616616 else:
617 line = yield line["raw"]
617 yield line["raw"]
618618
619619
620620 if __name__ == "__main__":