From 621e950c82595b23207807e1fe2ce35ebc5349e0 Mon Sep 17 00:00:00 2001 From: Attila Szakacs Date: Tue, 3 Oct 2023 09:38:45 +0200 Subject: [PATCH] parse_sentence: support arrowed option with type hint Signed-off-by: Attila Szakacs --- .../module_loader/parse_sentence.py | 23 ++++++++++++++++++ tests/module_loader/test_parse_sentence.py | 24 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/syslog_ng_cfg_helper/module_loader/parse_sentence.py b/syslog_ng_cfg_helper/module_loader/parse_sentence.py index a8529ed..627a34e 100644 --- a/syslog_ng_cfg_helper/module_loader/parse_sentence.py +++ b/syslog_ng_cfg_helper/module_loader/parse_sentence.py @@ -30,6 +30,16 @@ def __is_block(sentence: Tuple[str, ...]) -> bool: raise ParseError(f"Block is not closed: {sentence}") +def __is_arrowed_option(sentence: Tuple[str, ...]) -> bool: + if len(sentence) < 3: + return False + + if sentence[1] != "=>": + return False + + return True + + def __is_named_option(sentence: Tuple[str, ...]) -> bool: if len(sentence) < 3: return False @@ -82,6 +92,16 @@ def __parse_block(sentence: Tuple[str, ...]) -> Tuple[Block, int]: return (block, number_of_processed_symbols) +def __parse_arrowed_option(sentence: Tuple[str, ...]) -> Tuple[Option, int]: + if len(sentence) < 4 or sentence[3] != "(": + option_params = sentence[0:2] + else: + option_params = sentence[0 : sentence.index(")") + 1] + + option = Option(params={option_params}) + return (option, len(option_params)) + + def __parse_named_option(sentence: Tuple[str, ...]) -> Tuple[Option, int]: option_name = __get_named_option_name(sentence) option_params = __get_named_option_params(sentence) @@ -115,6 +135,9 @@ def __parse_options_in_block(sentence: Tuple[str, ...], target_block: Block) -> if __is_block(rest_of_sentence): block, number_of_parsed_symbols = __parse_block(rest_of_sentence) target_block.add_block(block) + elif __is_arrowed_option(rest_of_sentence): + arrowed_option, number_of_parsed_symbols = __parse_arrowed_option(rest_of_sentence) + target_block.add_option(arrowed_option) elif __is_named_option(rest_of_sentence): named_option, number_of_parsed_symbols = __parse_named_option(rest_of_sentence) target_block.add_option(named_option) diff --git a/tests/module_loader/test_parse_sentence.py b/tests/module_loader/test_parse_sentence.py index f9f954a..a4174a6 100644 --- a/tests/module_loader/test_parse_sentence.py +++ b/tests/module_loader/test_parse_sentence.py @@ -223,6 +223,30 @@ def get_test_params() -> List[Tuple[Tuple[str, ...], Driver]]: ) ) + expected_7 = Driver("ctx", "driver") + expected_7.add_block(Block("block")) + expected_7.get_block("block").add_option(Option(params={("opt", "=>", "hint", "(", "", ")")})) + test_params.append( + ( + ( + "LL_CONTEXT_CTX", + "driver", + "(", + "block", + "(", + "opt", + "=>", + "hint", + "(", + "", + ")", + ")", + ")", + ), + expected_7, + ) + ) + return test_params