Interpret dictionary from parsed table.
This commit is contained in:
parent
5672ce8542
commit
fd78ec3960
36
main.py
36
main.py
|
@ -2,6 +2,8 @@ import re
|
|||
import sys
|
||||
import json
|
||||
|
||||
ADDRESS_PATTERN = r'0x[0-9A-F]+'
|
||||
|
||||
def parse_table_entries(fields):
|
||||
"""Parse aligned table entries"""
|
||||
pattern = r'\s+'.join([f"(?P<{v[:v.find(':')]}>{v[v.find(':') + 1:].strip()})"
|
||||
|
@ -49,7 +51,7 @@ def parse_register_fields():
|
|||
r'name: \w+',
|
||||
r'bits: \d+(:\d+)?',
|
||||
r'type: \w+',
|
||||
r'reset: 0x[0-9A-F]+',
|
||||
f'reset: {ADDRESS_PATTERN}',
|
||||
r'description: .+'
|
||||
]
|
||||
it = parse_table_entries(fields)
|
||||
|
@ -64,9 +66,11 @@ def end_iterator(it):
|
|||
|
||||
def parse_registers():
|
||||
def two_column(width):
|
||||
# only require the leftmost column starts with a \w, and the a space
|
||||
# separator between the two columns...
|
||||
fields = [
|
||||
f'key: \\w.{{1,{width-2}}}\\s',
|
||||
r'value: .+'
|
||||
r'value: \w.+'
|
||||
]
|
||||
it = parse_table_entries(fields)
|
||||
next(it)
|
||||
|
@ -115,6 +119,30 @@ def parse_registers():
|
|||
next(it)
|
||||
return it
|
||||
|
||||
def interpret(reg):
|
||||
pattern = re.compile(r'(.+\w)\d+')
|
||||
result = {}
|
||||
name_pattern = None
|
||||
expected = ['Name', 'Relative Address', 'Absolute Address', 'Width',
|
||||
'Access Type', 'Reset Value', 'Description']
|
||||
for v in reg['def']:
|
||||
a = v['key']
|
||||
b = v['value']
|
||||
key = a[0].strip()
|
||||
if len(expected) > 0 and key == expected[0]:
|
||||
result[key] = b
|
||||
expected = expected[1:]
|
||||
if key == 'Name':
|
||||
m = pattern.fullmatch(b[0].strip())
|
||||
if m is not None:
|
||||
name_pattern = m.group(1)
|
||||
elif name_pattern != None and key.startswith(name_pattern):
|
||||
if 'similar' not in result:
|
||||
result['similar'] = []
|
||||
result['similar'].append({'name': key, 'address': b[0].strip()})
|
||||
result['fields'] = reg['fields']
|
||||
return result
|
||||
|
||||
def emit_rust(base_addr, ending_addr, registers):
|
||||
current_addr = base_addr
|
||||
reserved_id = 0
|
||||
|
@ -158,4 +186,6 @@ parser = parse_registers()
|
|||
for line in sys.stdin:
|
||||
parser.send(line)
|
||||
v = end_iterator(parser)
|
||||
print(json.dumps(v, indent = 4))
|
||||
for reg in v:
|
||||
reg = interpret(reg)
|
||||
print(json.dumps(reg, indent = 4))
|
||||
|
|
Loading…
Reference in New Issue