2019-05-18 08:42:04 +00:00
|
|
|
import re
|
2019-06-03 12:22:03 +00:00
|
|
|
from typing import Dict, Tuple, NewType
|
2019-03-18 16:18:57 +00:00
|
|
|
|
|
|
|
|
2019-05-18 08:42:04 +00:00
|
|
|
FUNCTION_REGEX = r'^def [\w_]*'
|
2019-05-24 14:51:21 +00:00
|
|
|
BEGIN_INSERT_REGEX = r'# begin insert '
|
|
|
|
END_INSERT_REGEX = r'# end insert'
|
2019-05-18 08:42:04 +00:00
|
|
|
|
2019-06-03 12:22:03 +00:00
|
|
|
SpecObject = NewType('SpecObjects', Tuple[Dict[str, str], Dict[str, str], Dict[str, str], Dict[str, str]])
|
2019-05-18 08:42:04 +00:00
|
|
|
|
2019-06-03 12:22:03 +00:00
|
|
|
|
|
|
|
def get_spec(file_name: str) -> SpecObject:
|
|
|
|
"""
|
|
|
|
Takes in the file name of a spec.md file, opens it and returns the following objects:
|
|
|
|
functions = {function_name: function_code}
|
|
|
|
constants= {constant_name: constant_code}
|
|
|
|
ssz_objects= {object_name: object}
|
|
|
|
inserts= {insert_tag: code to be inserted}
|
|
|
|
|
|
|
|
Note: This function makes heavy use of the inherent ordering of dicts,
|
|
|
|
if this is not supported by your python version, it will not work.
|
|
|
|
"""
|
2019-05-24 14:51:21 +00:00
|
|
|
pulling_from = None # line number of start of latest object
|
|
|
|
current_name = None # most recent section title
|
|
|
|
insert_name = None # stores the label of the current insert object
|
2019-05-20 12:00:54 +00:00
|
|
|
functions = {}
|
2019-05-18 08:42:04 +00:00
|
|
|
constants = {}
|
2019-05-20 12:00:54 +00:00
|
|
|
ssz_objects = {}
|
2019-05-24 14:51:21 +00:00
|
|
|
inserts = {}
|
2019-05-18 08:42:04 +00:00
|
|
|
function_matcher = re.compile(FUNCTION_REGEX)
|
2019-05-24 14:51:21 +00:00
|
|
|
inserts_matcher = re.compile(BEGIN_INSERT_REGEX)
|
2019-05-07 11:13:22 +00:00
|
|
|
for linenum, line in enumerate(open(file_name).readlines()):
|
2019-03-18 16:18:57 +00:00
|
|
|
line = line.rstrip()
|
|
|
|
if pulling_from is None and len(line) > 0 and line[0] == '#' and line[-1] == '`':
|
2019-03-19 03:39:19 +00:00
|
|
|
current_name = line[line[:-1].rfind('`') + 1: -1]
|
2019-03-18 16:18:57 +00:00
|
|
|
if line[:9] == '```python':
|
|
|
|
assert pulling_from is None
|
|
|
|
pulling_from = linenum + 1
|
|
|
|
elif line[:3] == '```':
|
2019-05-18 08:42:04 +00:00
|
|
|
pulling_from = None
|
2019-05-24 14:51:21 +00:00
|
|
|
elif inserts_matcher.match(line) is not None:
|
2019-06-03 12:22:03 +00:00
|
|
|
# Find @insert names
|
2019-05-24 14:51:21 +00:00
|
|
|
insert_name = re.search(r'@[\w]*', line).group(0)
|
|
|
|
elif insert_name is not None:
|
2019-06-03 12:22:03 +00:00
|
|
|
# In insert mode, either the next line is more code, or the end of the insert
|
2019-05-24 14:51:21 +00:00
|
|
|
if re.match(END_INSERT_REGEX, line) is not None:
|
|
|
|
insert_name = None
|
|
|
|
else:
|
|
|
|
inserts[insert_name] = inserts.get(insert_name, '') + line + '\n'
|
2019-03-18 16:18:57 +00:00
|
|
|
else:
|
2019-06-03 12:22:03 +00:00
|
|
|
# Handle function definitions & ssz_objects
|
2019-05-18 08:42:04 +00:00
|
|
|
if pulling_from is not None:
|
2019-05-24 14:51:21 +00:00
|
|
|
func_match = function_matcher.match(line)
|
|
|
|
if func_match is not None:
|
|
|
|
current_name = func_match.group(0)
|
2019-06-03 12:22:03 +00:00
|
|
|
if function_matcher.match(current_name) is None: # The current line is an SSZ Object
|
2019-05-20 12:00:54 +00:00
|
|
|
ssz_objects[current_name] = ssz_objects.get(current_name, '') + line + '\n'
|
2019-06-03 12:22:03 +00:00
|
|
|
else: # The current line is code
|
2019-05-20 12:00:54 +00:00
|
|
|
functions[current_name] = functions.get(current_name, '') + line + '\n'
|
2019-05-18 08:42:04 +00:00
|
|
|
# Handle constant table entries
|
2019-03-18 16:18:57 +00:00
|
|
|
elif pulling_from is None and len(line) > 0 and line[0] == '|':
|
|
|
|
row = line[1:].split('|')
|
|
|
|
if len(row) >= 2:
|
|
|
|
for i in range(2):
|
|
|
|
row[i] = row[i].strip().strip('`')
|
|
|
|
if '`' in row[i]:
|
|
|
|
row[i] = row[i][:row[i].find('`')]
|
|
|
|
eligible = True
|
|
|
|
if row[0][0] not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_':
|
|
|
|
eligible = False
|
|
|
|
for c in row[0]:
|
|
|
|
if c not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789':
|
|
|
|
eligible = False
|
|
|
|
if eligible:
|
2019-05-18 08:42:04 +00:00
|
|
|
constants[row[0]] = row[1].replace('**TBD**', '0x1234567890123456789012345678901234567890')
|
2019-05-24 14:51:21 +00:00
|
|
|
return functions, constants, ssz_objects, inserts
|