From 1d953a8270a08a802e8408b7cc1c75bcf2659df6 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Wed, 22 Nov 2023 14:47:00 -0800 Subject: [PATCH] custom func arg only arg --- src/ytdl_sub/script/parser.py | 12 +++++++++++- tests/unit/script/types/test_custom_function.py | 9 +++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ytdl_sub/script/parser.py b/src/ytdl_sub/script/parser.py index 3fa53a75..08c3aa37 100644 --- a/src/ytdl_sub/script/parser.py +++ b/src/ytdl_sub/script/parser.py @@ -66,6 +66,10 @@ BOOLEAN_ONLY_ARGS = InvalidSyntaxException( "Booleans can only be used as arguments to functions, maps, or arrays" ) +CUSTOM_FUNCTION_ARGUMENTS_ONLY_ARGS = InvalidSyntaxException( + "Custom function arguments can only be used as arguments to functions, maps, or arrays" +) + FUNCTION_INVALID_CHAR = InvalidSyntaxException("Invalid value when parsing a function") @@ -115,6 +119,10 @@ def _is_boolean_false(string: Optional[str]) -> bool: return string == "False" +def _is_custom_function_argument_start(char: str) -> bool: + return char == "$" + + class _Parser: def __init__( self, @@ -303,7 +311,7 @@ class _Parser: if self._read(increment_pos=False) == "{": self._pos += 1 return self._parse_map() - if self._read(increment_pos=False) == "$": + if _is_custom_function_argument_start(self._read(increment_pos=False)): self._pos += 1 return self._parse_custom_function_argument() if _is_variable_start(self._read(increment_pos=False)): @@ -520,6 +528,8 @@ class _Parser: self._read(increment_pos=False, length=4) ) or _is_boolean_false(self._read(increment_pos=False, length=5)): raise BOOLEAN_ONLY_ARGS + elif _is_custom_function_argument_start(self._read(increment_pos=False)): + raise CUSTOM_FUNCTION_ARGUMENTS_ONLY_ARGS else: raise _UNEXPECTED_CHAR_ARGUMENT(arg_type=ParsedArgType.SCRIPT) elif bracket_counter == 0: diff --git a/tests/unit/script/types/test_custom_function.py b/tests/unit/script/types/test_custom_function.py index 649df654..7b478d8f 100644 --- a/tests/unit/script/types/test_custom_function.py +++ b/tests/unit/script/types/test_custom_function.py @@ -2,12 +2,14 @@ import re import pytest +from ytdl_sub.script.parser import CUSTOM_FUNCTION_ARGUMENTS_ONLY_ARGS from ytdl_sub.script.script import Script from ytdl_sub.script.types.resolvable import Integer from ytdl_sub.script.utils.exceptions import CycleDetected from ytdl_sub.script.utils.exceptions import FunctionDoesNotExist from ytdl_sub.script.utils.exceptions import InvalidCustomFunctionArgumentName from ytdl_sub.script.utils.exceptions import InvalidCustomFunctionArguments +from ytdl_sub.script.utils.exceptions import InvalidSyntaxException class TestCustomFunction: @@ -105,6 +107,13 @@ class TestCustomFunction: } ).resolve() + def test_custom_function_function_argument_usage_in_brackets(self): + with pytest.raises( + InvalidSyntaxException, + match=re.escape(str(CUSTOM_FUNCTION_ARGUMENTS_ONLY_ARGS)), + ): + Script({"%func1": "{$0}"}).resolve() + @pytest.mark.parametrize( "argument", [