Skip to content

Commit

Permalink
parse_sentence: support arrowed option with type hint
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 Oct 3, 2023
1 parent 0942df6 commit 621e950
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
23 changes: 23 additions & 0 deletions syslog_ng_cfg_helper/module_loader/parse_sentence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions tests/module_loader/test_parse_sentence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "(", "<arg>", ")")}))
test_params.append(
(
(
"LL_CONTEXT_CTX",
"driver",
"(",
"block",
"(",
"opt",
"=>",
"hint",
"(",
"<arg>",
")",
")",
")",
),
expected_7,
)
)

return test_params


Expand Down

0 comments on commit 621e950

Please sign in to comment.