Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load_modules: try to include extra parser files #50

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions axosyslog_cfg_helper/module_loader/load_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,54 @@ def __remove_ifdef(grammar: DCFG) -> None:
grammar.remove_symbol(symbol)


def __find_file_upwards(file_to_find: Path, relative_to: Path) -> Optional[Path]:
current_dir = relative_to

while True:
current_dir = current_dir.parent
if current_dir == Path("/"):
return None

for found_file in current_dir.rglob(file_to_find.name):
return found_file


def __get_token_resolutions_from_struct(struct: str) -> Dict[str, Set[str]]:
resolutions: Dict[str, Set[str]] = {}
entry_regex = re.compile(r"{[^{}]+,[^{}]+}")

for entry_match in entry_regex.finditer(struct):
entry = entry_match.group(0)[1:-1].replace(" ", "").split(",")
token = entry[1]
keyword = entry[0][1:-1].replace("_", "-")
resolutions.setdefault(token, set()).add(keyword)

return resolutions


def __get_token_resolutions(parser_file: Path) -> Dict[str, Set[str]]:
resolutions: Dict[str, Set[str]] = {}

struct_regex = re.compile(r"CfgLexerKeyword(.*?)};")
entry_regex = re.compile(r"{[^{}]+,[^{}]+}")
include_regex = re.compile(r'#include (<[^>]*|"[^"]*)')

with parser_file.open("r") as file:
for struct_match in struct_regex.finditer(file.read().replace("\n", "")):
for entry_match in entry_regex.finditer(struct_match.group(0)):
entry = entry_match.group(0)[1:-1].replace(" ", "").split(",")
token = entry[1]
keyword = entry[0][1:-1].replace("_", "-")
resolutions.setdefault(token, set()).add(keyword)
file_content = file.read().replace("\n", "")
for include_match in include_regex.finditer(file_content):
included_file = Path(include_match.group(1)[1:])
if not included_file.name.endswith("-parser.h"):
continue

extra_parser_file = __find_file_upwards(included_file, parser_file.parent)
if extra_parser_file is None:
print(f" Cannot find extra parser file: {str(extra_parser_file)}.")
continue

extra_parser_file_content = extra_parser_file.read_text().replace("\n", "")
resolutions.update(__get_token_resolutions_from_struct(extra_parser_file_content))

for struct_match in struct_regex.finditer(file_content):
resolutions.update(__get_token_resolutions_from_struct(struct_match.group(1)))

return resolutions

Expand Down
Loading