Skip to content

Commit

Permalink
load_modules: factor out __get_token_resolutions_from_struct()
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Szakacs <szakacs.attila96@gmail.com>
  • Loading branch information
alltilla committed Sep 17, 2024
1 parent 9f95376 commit 942dcfe
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 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,28 @@ def __remove_ifdef(grammar: DCFG) -> None:
grammar.remove_symbol(symbol)


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"{[^{}]+,[^{}]+}")

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 struct_match in struct_regex.finditer(file_content):
resolutions.update(__get_token_resolutions_from_struct(struct_match.group(1)))

return resolutions

Expand Down

0 comments on commit 942dcfe

Please sign in to comment.