custom func arg only arg

This commit is contained in:
Jesse Bannon 2023-11-22 14:47:00 -08:00
parent 23c7b3acbf
commit 1d953a8270
2 changed files with 20 additions and 1 deletions

View file

@ -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:

View file

@ -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",
[