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

new parser appears to work

parent 28eaf556
import re
from typing import List
from modgen.parserbase.parser import FileParser
from modgen.utility.mib_printer import PrettyPrinter
# Intermediate solution
MAX_STRING_LEN = 80
DEBUG_FOR_FILE_NAME = True
PRINT_TRUNCATED_ENTRIES = False
DEBUG_INTERFACE_ID_COMPARISON = False
DEBUG_FOR_FILE_NAME = False
DEBUG_FILE_NAME = "RingBufferAnalyzer"
CLASS_ID_NAMESPACE = "CLASS_ID"
DEFAULT_MOVING_WINDOWS_SIZE = 7
class InterfaceParser(FileParser):
......@@ -75,6 +82,7 @@ class ReturnValueParser(FileParser):
self.count = 0
# Stores last three lines
self.last_lines = ["", "", ""]
self.moving_window_center_idx = 3
self.current_interface_id_entries = {
"Name": "",
"ID": 0,
......@@ -86,6 +94,14 @@ class ReturnValueParser(FileParser):
'RETURN_FAILED', 'HasReturnvaluesIF.h',
'HasReturnvaluesIF')})
def set_moving_window_mode(self, moving_window_size: int):
"""
Set moving window parsing mode
:param moving_window_size:
:return:
"""
super().set_moving_window_mode(DEFAULT_MOVING_WINDOWS_SIZE)
def _handle_file_parsing(self, file_name: str, *args, **kwargs):
"""
Former way to parse returnvalues. Not recommended anymore.
......@@ -106,13 +122,91 @@ class ReturnValueParser(FileParser):
def _handle_file_parsing_moving_window(self, file_name: str, current_line: int,
moving_window_size: int, moving_window: list, *args,
**kwargs):
# print(file_name)
# Generic regex to match full returnvalue expression up until semicolon
# returnvalue_match = re.search(
# r"^[\s]*static const(?:expr)? ReturnValue_t.+?(?=;);", two_lines, re.DOTALL
# )
interface_id_match = re.search(
f"{CLASS_ID_NAMESPACE}::([a-zA-Z_0-9]*)", moving_window[self.moving_window_center_idx]
)
if interface_id_match:
self.__handle_interfaceid_match(interface_id_match=interface_id_match, file_name=file_name)
returnvalue_match = re.search(
r"^[\s]*static const(?:expr)?[\s]*ReturnValue_t[\s]*([\w]*)[\s]*=[\s]*((?!;).*$)",
moving_window[self.moving_window_center_idx], re.DOTALL
)
full_returnvalue_string = ""
if returnvalue_match:
if ";" in returnvalue_match.group(0):
full_returnvalue_string = returnvalue_match.group(0)
else:
full_returnvalue_string = self.__build_multi_line_returnvalue_string(
moving_window=moving_window, first_line=moving_window[self.moving_window_center_idx]
)
returnvalue_match = re.search(
r"^[\s]*static const(?:expr)? ReturnValue_t[\s]*([\w] *)[\s]*=[\s]*.*::[\w]*\(([\w]*),[\s]*([\d]*)\)",
full_returnvalue_string
)
if not returnvalue_match:
returnvalue_match = re.search(
r'^[\s]*static const(?:expr)? ReturnValue_t[\s]*([a-zA-Z_0-9]*)[\s]*=[\s]*'
r'MAKE_RETURN_CODE[\s]*\([\s]*([\w]*)[\s]*\)',
full_returnvalue_string
)
if returnvalue_match:
description = self.__search_for_descrip_string(moving_window=moving_window)
self.__handle_returnvalue_match(
name_match=returnvalue_match.group(1), file_name=file_name, number_match=returnvalue_match.group(2),
description=description
)
pass
def __build_multi_line_returnvalue_string(
self, first_line: str, moving_window: List[str]
) -> str:
all_lines = first_line.rstrip()
end_found = False
current_idx = self.moving_window_center_idx
while not end_found and current_idx < len(moving_window) - 1:
current_idx += 1
string_to_add = moving_window[current_idx].lstrip()
if ";" in moving_window[current_idx]:
all_lines += string_to_add
break
else:
string_to_add.rstrip()
all_lines += string_to_add
return all_lines
def __search_for_descrip_string(self, moving_window: List[str]) -> str:
current_idx = self.moving_window_center_idx - 1
# Look at the line above first
descrip_match = re.search(
r"\[EXPORT\][\s]*:[\s]*\[COMMENT\]", moving_window[current_idx]
)
if not descrip_match:
while current_idx > 0:
current_idx -= 1
if re.search(r"^[\s]*static const(?:expr)? ReturnValue_t", moving_window[current_idx]):
break
descrip_match = re.search(
r"\[EXPORT\][\s]*:[\s]*\[COMMENT\]", moving_window[current_idx]
)
if descrip_match:
break
if descrip_match:
current_build_idx = current_idx
descrip_string = ""
while current_build_idx < self.moving_window_center_idx:
string_to_add = moving_window[current_build_idx].lstrip()
string_to_add = string_to_add.rstrip()
descrip_string += string_to_add
current_build_idx += 1
else:
return ""
print(descrip_string)
resulting_description = re.search(
r"\[EXPORT\][\s]*:[\s]*\[COMMENT\](.*)", descrip_string
)
return resulting_description.group(1)
def __handle_line_reading(self, line, file_name, print_truncated_entries: bool):
newline = line
if self.last_lines[0] != '\n':
......@@ -122,7 +216,7 @@ class ReturnValueParser(FileParser):
interface_id_match = re.search(r'INTERFACE_ID[\s]*=[\s]*CLASS_ID::([a-zA-Z_0-9]*)',
two_lines)
if interface_id_match:
self.__handle_interfaceid_match(interface_id_match)
self.__handle_interfaceid_match(interface_id_match, file_name=file_name)
returnvalue_match = re.search(
r'^[\s]*static const(?:expr)? ReturnValue_t[\s]*([a-zA-Z_0-9]*)[\s]*=[\s]*'
......@@ -134,33 +228,36 @@ class ReturnValueParser(FileParser):
self.last_lines[1] = self.last_lines[0]
self.last_lines[0] = newline
def __handle_interfaceid_match(self, interface_id_match):
# print("Interface ID" + str(match1.group(1)) + "found in " + fileName)
def __handle_interfaceid_match(self, interface_id_match, file_name: str):
if DEBUG_INTERFACE_ID_COMPARISON:
print(f"Interface ID {interface_id_match.group(1)} found in {file_name}")
self.current_interface_id_entries["ID"] = \
self.interfaces[interface_id_match.group(1)][0]
self.current_interface_id_entries["Name"] = \
self.interfaces[interface_id_match.group(1)][1]
self.current_interface_id_entries["FullName"] = interface_id_match.group(1)
# print( "Current ID: " + str(self.current_interface_id_entries["ID"]) )
if DEBUG_INTERFACE_ID_COMPARISON:
current_id = self.current_interface_id_entries["ID"]
print(f"Current ID: {current_id}")
def __handle_returnvalue_match(self, returnvalue_match, file_name: str,
print_truc_entries: bool):
# valueTable.append([])
description = self.clean_up_description(returnvalue_match.group(4))
def __handle_returnvalue_match(
self, name_match: str, number_match: str, file_name: str, description: str
):
string_to_add = self.build_checked_string(
self.current_interface_id_entries["Name"], returnvalue_match.group(1), MAX_STRING_LEN,
print_truc_entries)
full_id = (self.current_interface_id_entries["ID"] << 8) + return_number_from_string(
returnvalue_match.group(2))
self.current_interface_id_entries["Name"], name_match, MAX_STRING_LEN,
PRINT_TRUNCATED_ENTRIES
)
full_id = (self.current_interface_id_entries["ID"] << 8) + return_number_from_string(number_match)
if full_id in self.return_value_dict:
# print('Duplicate returncode ' + hex(full_id) + ' from ' + file_name +
# ' was already in ' + self.return_value_dict[full_id][3])
pass
self.return_value_dict.update(
{full_id: (string_to_add, description, returnvalue_match.group(1),
file_name, self.current_interface_id_entries["FullName"])})
# valueTable[count].append(fullId)
# valueTable[count].append(stringToAdd)
dict_tuple = (
string_to_add, description, number_match, file_name, self.current_interface_id_entries["FullName"]
)
self.return_value_dict.update({
full_id: dict_tuple
})
self.count = self.count + 1
def _post_parsing_operation(self):
......
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