Interpret dictionary from parsed table.

This commit is contained in:
pca006132 2020-07-29 10:49:41 +08:00
parent 5672ce8542
commit fd78ec3960
1 changed files with 33 additions and 3 deletions

36
main.py
View File

@ -2,6 +2,8 @@ import re
import sys import sys
import json import json
ADDRESS_PATTERN = r'0x[0-9A-F]+'
def parse_table_entries(fields): def parse_table_entries(fields):
"""Parse aligned table entries""" """Parse aligned table entries"""
pattern = r'\s+'.join([f"(?P<{v[:v.find(':')]}>{v[v.find(':') + 1:].strip()})" 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'name: \w+',
r'bits: \d+(:\d+)?', r'bits: \d+(:\d+)?',
r'type: \w+', r'type: \w+',
r'reset: 0x[0-9A-F]+', f'reset: {ADDRESS_PATTERN}',
r'description: .+' r'description: .+'
] ]
it = parse_table_entries(fields) it = parse_table_entries(fields)
@ -64,9 +66,11 @@ def end_iterator(it):
def parse_registers(): def parse_registers():
def two_column(width): def two_column(width):
# only require the leftmost column starts with a \w, and the a space
# separator between the two columns...
fields = [ fields = [
f'key: \\w.{{1,{width-2}}}\\s', f'key: \\w.{{1,{width-2}}}\\s',
r'value: .+' r'value: \w.+'
] ]
it = parse_table_entries(fields) it = parse_table_entries(fields)
next(it) next(it)
@ -115,6 +119,30 @@ def parse_registers():
next(it) next(it)
return 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): def emit_rust(base_addr, ending_addr, registers):
current_addr = base_addr current_addr = base_addr
reserved_id = 0 reserved_id = 0
@ -158,4 +186,6 @@ parser = parse_registers()
for line in sys.stdin: for line in sys.stdin:
parser.send(line) parser.send(line)
v = end_iterator(parser) v = end_iterator(parser)
print(json.dumps(v, indent = 4)) for reg in v:
reg = interpret(reg)
print(json.dumps(reg, indent = 4))