Unverified Commit d28855d1 authored by Robin Mueller's avatar Robin Mueller
Browse files

refactored returnvalue parser

parent 8016ac97
import re
from typing import List
import sys
from typing import List, Tuple
from modgen.parserbase.parser import FileParser
from modgen.utility.printer import PrettyPrinter
......@@ -18,10 +19,13 @@ DEFAULT_MOVING_WINDOWS_SIZE = 7
class InterfaceParser(FileParser):
def __init__(self, file_list, print_table):
def __init__(self, file_list: list, print_table: bool = False):
super().__init__(file_list)
self.count = 0
self.print_table = print_table
self.file_table_list = []
self.file_name_table = []
self.start_name_list = []
self.end_name_list = []
def _handle_file_parsing_moving_window(self, file_name: str, current_line: int,
moving_window_size: int, moving_window: list, *args,
......@@ -29,46 +33,93 @@ class InterfaceParser(FileParser):
pass
def _handle_file_parsing(self, file_name: str, *args, **kwargs):
self.file_name_table.append(file_name)
try:
file = open(file_name, 'r', encoding='utf-8')
all_lines = file.readlines()
except UnicodeDecodeError:
file = open(file_name, 'r', encoding='cp1252')
all_lines = file.readlines()
if "FwClassIds.h" in file_name:
count_matched = False
# Parses first entry, which has explicit value 1
for line in all_lines:
if not count_matched:
match = re.search(r'[\s]*([A-Z_0-9]*) = ([0-9]*),[\s]*//([A-Z]{1,3})', line)
else:
match = re.search(r'[\s]*([A-Z_0-9]*),[\s]*//([A-Z]{1,3})', line)
if match and not count_matched:
self.count = int(match.group(2))
self.mib_table.update({match.group(1): [1, match.group(3)]})
self.count += 1
count_matched = True
elif match:
self.mib_table.update({match.group(1): [self.count, match.group(2)]})
self.count += 1
elif "classIds.h" in file_name:
file = open(file_name, "r")
all_lines = file.readlines()
for line in all_lines:
match = re.search(r'[\s]*([\w]*) = FW_CLASS_ID_COUNT,[\s]*(//([A-Z]{1,3}))?', line)
self.__handle_regular_class_id_parsing(file_name=file_name, all_lines=all_lines)
def __handle_regular_class_id_parsing(self, file_name: str, all_lines: List[str]):
count = 0
current_file_table = dict()
start_matched = False
start_name = ""
target_end_name = ""
for line in all_lines:
if not start_matched:
match = re.search(r'[\s]*([\w]*) = [\s]*([\w]*)', line)
if match:
self.mib_table.update({match.group(1): [self.count, match.group(2)]})
self.count += 1
for line in all_lines:
match = re.search(r'^[\s]*([\w]*)[,]*[\s]*//[!<]*[\s]*([^\n]*)', line)
# current_file_table.update({count: [match.group(1), match.group(2)]})
start_name = match.group(1)
target_end_name = match.group(2)
start_matched = True
else:
match = re.search(r'[\s]*([A-Z_0-9]*),?[\s]*//([^\n]*)?', line)
if match:
self.mib_table.update({match.group(1): [self.count, match.group(2)]})
self.count += 1
count += 1
if re.search(r"\[EXPORT\][\s]*:[\s]*\[END\]", match.group(2)):
last_entry_name = match.group(1)
self.end_name_list.append([last_entry_name, None])
else:
current_file_table.update({count: [match.group(1), match.group(2)]})
if not start_matched:
print("No start match detected when parsing interface files..")
sys.exit(1)
self.start_name_list.append([start_name, target_end_name, None, count])
self.file_name_table.append(file_name)
self.file_table_list.append(current_file_table)
def _post_parsing_operation(self):
self.start_name_list, self.end_name_list = self.__assign_start_end_indexes(
self.start_name_list, self.end_name_list
)
for idx, file_table in enumerate(self.file_table_list):
self.__build_mod_interface_table(self.start_name_list[idx][2], file_table)
if self.print_table:
PrettyPrinter.pprint(self.mib_table)
@staticmethod
def __assign_start_end_indexes(start_name_list_list, end_name_list_list) -> Tuple[List, List]:
start_list_list_completed = start_name_list_list
end_list_list_completed = end_name_list_list
all_indexes_filled = False
max_outer_iterations = 15
current_iteration = 0
while not all_indexes_filled:
for idx, start_name_list in enumerate(start_list_list_completed):
if start_name_list[1].isdigit():
start_list_list_completed[idx][2] = int(start_name_list[1])
end_list_list_completed[idx][1] = \
start_list_list_completed[idx][2] + start_list_list_completed[idx][3]
target_end_name = start_name_list[1]
for end_name_list in end_list_list_completed:
end_name = end_name_list[0]
end_value = end_name_list[1]
if end_name == target_end_name and end_value is not None:
start_list_list_completed[idx][2] = end_value
end_list_list_completed[idx][1] = end_value + start_list_list_completed[idx][3]
all_indexes_filled = True
for idx, start_name_list in enumerate(start_list_list_completed):
if start_name_list[2] is None or end_name_list_list[idx][1] is None:
all_indexes_filled = False
current_iteration += 1
if current_iteration >= max_outer_iterations:
print("Could not fill out start and end index list in given number of maximum outer iterations!")
sys.exit(1)
return start_list_list_completed, end_list_list_completed
def __build_mod_interface_table(self, count_start: int, interface_dict: dict):
dict_to_build = dict()
for local_count, interface_name_and_shortname in interface_dict.items():
dict_to_build.update(
{interface_name_and_shortname[0]: [local_count + count_start, interface_name_and_shortname[1]]}
)
self.mib_table.update(dict_to_build)
class ReturnValueParser(FileParser):
"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment