more custom regex functions

This commit is contained in:
Jesse Bannon 2023-12-19 09:52:51 -08:00
parent 1f2331c00a
commit e069afb8b8
3 changed files with 56 additions and 3 deletions

View file

@ -41,7 +41,8 @@ CUSTOM_FUNCTION_SCRIPTS: Dict[str, str] = {
# $0 - input variable
# $1 - regex array
# $2 - defaults
"%regex_capture": """{
# $3 - error message
"%regex_capture_inner": """{
%assert_then(
%array_reduce(
%array_apply_fixed(
@ -63,7 +64,35 @@ CUSTOM_FUNCTION_SCRIPTS: Dict[str, str] = {
%array_extend( ['using all defaults'], $2 ),
True
),
'Number of regex capture groups must be less than or equal to the number of defaults'
$3
)
}""",
"%regex_capture_many_required": """{
%assert_ne(
%regex_capture_inner(
$0, $1, ['', '', '', '', '', '', '', '', '', ''],
'When using %regex_capture_many, number of regex capture groups must be less than or equal to the number of defaults'
),
['using all defaults', '', '', '', '', '', '', '', '', '', ''],
'When running %regex_capture_many_required, no regex strings captured'
)
}""",
"%regex_capture_many_with_defaults": """{
%regex_capture_inner(
$0, $1, $2,
'When using %regex_capture_with_defaults, number of regex capture groups must be less than or equal to the number of defaults'
)
}""",
"%regex_search_any": """{
%ne(
%array_at(
%regex_capture_inner(
$0, $1, [],
'When using %regex_search_many, all regex strings must contain no capture groups'
),
0
),
'using all defaults'
)
}""",
}

View file

@ -33,3 +33,27 @@ class ErrorFunctions:
if not bool(value.value):
raise UserThrownRuntimeError(assert_message)
return ret
@staticmethod
def assert_eq(
value: ReturnableArgument, equals: AnyArgument, assert_message: String
) -> ReturnableArgument:
"""
Explicitly throw an error with the provided assert message if ``value`` does not equal
``equals``. If they do equal, then return ``value``.
"""
if not value.value == equals.value:
raise UserThrownRuntimeError(assert_message)
return value
@staticmethod
def assert_ne(
value: ReturnableArgument, equals: AnyArgument, assert_message: String
) -> ReturnableArgument:
"""
Explicitly throw an error with the provided assert message if ``value`` equals
``equals``. If they do equal, then return ``value``.
"""
if value.value == equals.value:
raise UserThrownRuntimeError(assert_message)
return value

View file

@ -77,7 +77,7 @@ def regex_subscription_dict(regex_subscription_dict_base, output_directory):
},
"overrides": {
"title_capture_list": f"""{{
%regex_capture(
%regex_capture_many_with_defaults(
title,
[
"should not cap (.+) - (.+)",