Updated parser.
This commit is contained in:
parent
6a6c32b562
commit
5672ce8542
76
main.py
76
main.py
|
@ -1,5 +1,6 @@
|
|||
import re
|
||||
import sys
|
||||
import json
|
||||
|
||||
def parse_table_entries(fields):
|
||||
"""Parse aligned table entries"""
|
||||
|
@ -19,10 +20,11 @@ def parse_table_entries(fields):
|
|||
line = yield
|
||||
if line == None:
|
||||
break
|
||||
line = line.rstrip()
|
||||
if len(line.strip()) == 0:
|
||||
flush()
|
||||
continue
|
||||
match = pattern.fullmatch(line.strip())
|
||||
match = pattern.fullmatch(line)
|
||||
if match is not None:
|
||||
flush()
|
||||
starts = [match.span(v)[0] for v in fields]
|
||||
|
@ -42,18 +44,6 @@ def parse_table_entries(fields):
|
|||
flush()
|
||||
return entries
|
||||
|
||||
# def parse_register_list(lines):
|
||||
# """Parse register entry from `pdftotext -layout output` from TRM table"""
|
||||
# fields = [
|
||||
# r'id: \w+',
|
||||
# r'address: 0x[0-9A-F]+',
|
||||
# r'size: \d+',
|
||||
# r'type: \w+',
|
||||
# r'reset_value: 0x[0-9A-F]+',
|
||||
# r'description: .+'
|
||||
# ]
|
||||
# return parse_table_entries(fields, lines)
|
||||
|
||||
def parse_register_fields():
|
||||
fields = [
|
||||
r'name: \w+',
|
||||
|
@ -73,28 +63,53 @@ def end_iterator(it):
|
|||
return e.value
|
||||
|
||||
def parse_registers():
|
||||
def two_column(width):
|
||||
fields = [
|
||||
f'key: \\w.{{1,{width-2}}}\\s',
|
||||
r'value: .+'
|
||||
]
|
||||
it = parse_table_entries(fields)
|
||||
next(it)
|
||||
return it
|
||||
|
||||
def inner():
|
||||
pattern = re.compile(r'Name\s+(\w+)')
|
||||
def_start = re.compile(r'Name\s+(\w+)\s+')
|
||||
field_start = re.compile(r'\s+Field Name\s+Bits\s+Type\s+Reset '
|
||||
r'Value\s+Description\s+')
|
||||
|
||||
state = 0
|
||||
name = ''
|
||||
it = None
|
||||
results = {}
|
||||
results = []
|
||||
while True:
|
||||
line = yield
|
||||
if line == None:
|
||||
break
|
||||
line = line.strip()
|
||||
m = pattern.fullmatch(line)
|
||||
line = line
|
||||
if state == 0:
|
||||
m = def_start.fullmatch(line)
|
||||
if m is not None:
|
||||
if it is not None:
|
||||
results[name] = end_iterator(it)
|
||||
name = m.group(1)
|
||||
it = parse_register_fields()
|
||||
results[-1]['fields']= end_iterator(it)
|
||||
it = two_column(m.span(1)[0])
|
||||
it.send(line)
|
||||
state = 1
|
||||
else:
|
||||
if it is not None:
|
||||
it.send(line)
|
||||
elif state == 1:
|
||||
m = field_start.fullmatch(line)
|
||||
if m is not None:
|
||||
if it is not None:
|
||||
results[name] = end_iterator(it)
|
||||
results.append({'def': end_iterator(it)})
|
||||
it = parse_register_fields()
|
||||
state = 0
|
||||
else:
|
||||
if it is not None:
|
||||
it.send(line)
|
||||
|
||||
if it is not None:
|
||||
if state == 0:
|
||||
results[-1]['fields']= end_iterator(it)
|
||||
return results
|
||||
it = inner()
|
||||
next(it)
|
||||
|
@ -139,21 +154,8 @@ def emit_rust(base_addr, ending_addr, registers):
|
|||
code.append(line)
|
||||
return code
|
||||
|
||||
|
||||
|
||||
# if len(sys.argv) != 3:
|
||||
# print("Pipe pdftotext to stdin, and give starting and ending address"
|
||||
# "(inclusive) for the registers")
|
||||
# exit()
|
||||
|
||||
# for line in emit_rust(int(sys.argv[1], 0), int(sys.argv[2], 0), parse_register_list(sys.stdin)):
|
||||
# print(line)
|
||||
|
||||
parser = parse_registers()
|
||||
for line in sys.stdin:
|
||||
parser.send(line)
|
||||
for k, v in end_iterator(parser).items():
|
||||
print(k)
|
||||
for entry in v:
|
||||
print(entry)
|
||||
print('------------')
|
||||
v = end_iterator(parser)
|
||||
print(json.dumps(v, indent = 4))
|
||||
|
|
Loading…
Reference in New Issue