java.lang.Object
net.luis.utils.io.token.rule.rules.TokenRules
A utility class for creating token rules.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation.
This is a static helper class. -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull TokenRuleProvides a token rule that always matches.static @NotNull TokenRuleCreates a token rule that matches any of the given token rules.static @NotNull TokenRuleCreates a token rule that matches any of the given token rules.static @NotNull TokenRuleCreates a token rule that matches a boundary between the given start and end token rules.static @NotNull TokenRuleboundary(@NotNull TokenRule startTokenRule, @NotNull TokenRule betweenTokenRule, @NotNull TokenRule endTokenRule) Creates a token rule that matches a boundary between the given start, between, and end token rules.static @NotNull TokenRuleend()Provides a token rule that matches the end of the input.private static booleanendsWithSpecialChar(@NotNull String regex) Checks if the given regex ends with a special character.
The following characters are considered special characters:?, *, +,private static booleanisLastChar(@NotNull String regex, char c) Checks if the given regex ends with the given character.private static booleanisSurroundedByBrackets(@NotNull String regex) Checks if the given regex is surrounded by brackets.
This method will check if the first character is a bracket and if the last character is a closing bracket.static @NotNull TokenRuleCreates an optional token rule with the given token rule.static @NotNull TokenRuleCreates a pattern token rule with the given pattern in string format.static @NotNull TokenRuleCreates a pattern token rule with the given pattern.static @NotNull TokenRulerepeatAtLeast(@NotNull TokenRule tokenRule, int min) Creates a repeated token rule with the given token rule and the minimum number of occurrences.
This method is equivalent torepeatBetween(TokenRule, int, int)with the maximum number of occurrences set toInteger.MAX_VALUE.static @NotNull TokenRulerepeatAtMost(@NotNull TokenRule tokenRule, int max) Creates a repeated token rule with the given token rule and the maximum number of occurrences.
This method is equivalent torepeatBetween(TokenRule, int, int)with the minimum number of occurrences set to 0.static @NotNull TokenRulerepeatBetween(@NotNull TokenRule tokenRule, int min, int max) Creates a repeated token rule with the given token rule and the minimum and maximum number of occurrences.static @NotNull TokenRulerepeatExactly(@NotNull TokenRule tokenRule, int repeats) Creates a repeated token rule with the given token rule and the exact number of occurrences.
This method is equivalent torepeatBetween(TokenRule, int, int)with the minimum and maximum number of occurrences set to the same value.static @NotNull TokenRulerepeatInfinitely(@NotNull TokenRule tokenRule) Creates a repeated token rule with the given token rule and no limit on the number of occurrences.
This method is equivalent torepeatBetween(TokenRule, int, int)with the minimum number of occurrences set to 0 and the maximum number of occurrences set toInteger.MAX_VALUE.static @NotNull TokenRuleCreates a token rule that matches a sequence of token rules.static @NotNull TokenRuleCreates a token rule that matches a sequence of token rules.private static @NotNull StringtoBaseRegex(@NotNull TokenDefinition definition) Converts the given token definition to a regex string.
A word token definition is converted to a regex that matches any alphanumeric and numeric character.
All special characters are escaped.private static @NotNull StringtoBaseRegex(@NotNull TokenRule tokenRule) Converts the given token rule to a regex string without removing the surrounding brackets.
The rules are converted to regex strings and joined together with the appropriate regex operators.static @NotNull StringConverts the given token rule to a regex string.
This method will remove the surrounding brackets if they are present.
-
Constructor Details
-
TokenRules
private TokenRules()Private constructor to prevent instantiation.
This is a static helper class.
-
-
Method Details
-
alwaysMatch
Provides a token rule that always matches.- Returns:
- The token rule
- See Also:
- API Note:
- This is the preferred way to access the
AlwaysMatchTokenRule.INSTANCEinstance
-
pattern
@NotNull public static @NotNull TokenRule pattern(@Language("RegExp") @NotNull @NotNull String pattern) Creates a pattern token rule with the given pattern in string format.- Parameters:
pattern- The pattern to match- Returns:
- The created token rule
- Throws:
NullPointerException- If the pattern is null- See Also:
-
pattern
Creates a pattern token rule with the given pattern.- Parameters:
pattern- The pattern to match- Returns:
- The created token rule
- Throws:
NullPointerException- If the pattern is null- See Also:
-
optional
Creates an optional token rule with the given token rule.- Parameters:
tokenRule- The token rule to match optionally- Returns:
- The created token rule that matches the given token rule optionally
- Throws:
NullPointerException- If the token rule is null- See Also:
-
repeatAtLeast
@NotNull public static @NotNull TokenRule repeatAtLeast(@NotNull @NotNull TokenRule tokenRule, int min) Creates a repeated token rule with the given token rule and the minimum number of occurrences.
This method is equivalent torepeatBetween(TokenRule, int, int)with the maximum number of occurrences set toInteger.MAX_VALUE.- Parameters:
tokenRule- The token rule to matchmin- The minimum number of occurrences- Returns:
- The created token rule that matches the given token rule at least the given number of times
- Throws:
NullPointerException- If the token rule is nullIllegalArgumentException- If the minimum number of occurrences is less than 0- See Also:
-
repeatExactly
@NotNull public static @NotNull TokenRule repeatExactly(@NotNull @NotNull TokenRule tokenRule, int repeats) Creates a repeated token rule with the given token rule and the exact number of occurrences.
This method is equivalent torepeatBetween(TokenRule, int, int)with the minimum and maximum number of occurrences set to the same value.- Parameters:
tokenRule- The token rule to matchrepeats- The exact number of occurrences- Returns:
- The created token rule that matches the given token rule exactly the given number of times
- Throws:
NullPointerException- If the token rule is nullIllegalArgumentException- If the number of occurrences is lower than 0- See Also:
-
repeatAtMost
@NotNull public static @NotNull TokenRule repeatAtMost(@NotNull @NotNull TokenRule tokenRule, int max) Creates a repeated token rule with the given token rule and the maximum number of occurrences.
This method is equivalent torepeatBetween(TokenRule, int, int)with the minimum number of occurrences set to 0.- Parameters:
tokenRule- The token rule to matchmax- The maximum number of occurrences- Returns:
- The created token rule that matches the given token rule at most the given number of times
- Throws:
NullPointerException- If the token rule is nullIllegalArgumentException- If the maximum number of occurrences is lower than 0- See Also:
-
repeatInfinitely
Creates a repeated token rule with the given token rule and no limit on the number of occurrences.
This method is equivalent torepeatBetween(TokenRule, int, int)with the minimum number of occurrences set to 0 and the maximum number of occurrences set toInteger.MAX_VALUE.- Parameters:
tokenRule- The token rule to match- Returns:
- The created token rule that matches the given token rule any number of times
- Throws:
NullPointerException- If the token rule is null- See Also:
-
repeatBetween
@NotNull public static @NotNull TokenRule repeatBetween(@NotNull @NotNull TokenRule tokenRule, int min, int max) Creates a repeated token rule with the given token rule and the minimum and maximum number of occurrences.- Parameters:
tokenRule- The token rule to matchmin- The minimum number of occurrencesmax- The maximum number of occurrences- Returns:
- The created token rule that matches the given token rule between the given number of times
- Throws:
NullPointerException- If the token rule is nullIllegalArgumentException- If the minimum or maximum number of occurrences is lower than 0, or if the maximum number of occurrences is lower than the minimum number of occurrences, or if both are 0- See Also:
-
sequence
Creates a token rule that matches a sequence of token rules.- Parameters:
tokenRules- The token rules to match in sequence- Returns:
- The created token rule
- Throws:
NullPointerException- If the token rule array or any of its elements are nullIllegalArgumentException- If the token rule array is empty- See Also:
-
sequence
Creates a token rule that matches a sequence of token rules.- Parameters:
tokenRules- The token rules to match in sequence- Returns:
- The created token rule
- Throws:
NullPointerException- If the token rule list or any of its elements are nullIllegalArgumentException- If the token rule list is empty- See Also:
-
any
Creates a token rule that matches any of the given token rules.- Parameters:
tokenRules- The token rules to match any of- Returns:
- The created token rule
- Throws:
NullPointerException- If the token rule array or any of its elements are nullIllegalArgumentException- If the token rule array is empty- See Also:
-
any
Creates a token rule that matches any of the given token rules.- Parameters:
tokenRules- The token rules to match any of- Returns:
- The created token rule
- Throws:
NullPointerException- If the token rule set or any of its elements are nullIllegalArgumentException- If the token rule set is empty- See Also:
-
boundary
@NotNull public static @NotNull TokenRule boundary(@NotNull @NotNull TokenRule startTokenRule, @NotNull @NotNull TokenRule endTokenRule) Creates a token rule that matches a boundary between the given start and end token rules.- Parameters:
startTokenRule- The token rule that marks the start of the sequenceendTokenRule- The token rule that marks the end of the sequence- Returns:
- The created token rule
- Throws:
NullPointerException- If the start or end token rule is nullIllegalArgumentException- If the start token rule is invalid- See Also:
-
boundary
@NotNull public static @NotNull TokenRule boundary(@NotNull @NotNull TokenRule startTokenRule, @NotNull @NotNull TokenRule betweenTokenRule, @NotNull @NotNull TokenRule endTokenRule) Creates a token rule that matches a boundary between the given start, between, and end token rules.- Parameters:
startTokenRule- The token rule that marks the start of the sequencebetweenTokenRule- The token rule that matches the content between the start and end token rulesendTokenRule- The token rule that marks the end of the sequence- Returns:
- The created token rule
- Throws:
NullPointerException- If the start, between, or end token rule is nullIllegalArgumentException- If the start or between token rule is invalid- See Also:
-
end
Provides a token rule that matches the end of the input.- Returns:
- The token rule
- See Also:
- API Note:
- This is the preferred way to access the
EndTokenRule.INSTANCEinstance
-
toRegex
Converts the given token rule to a regex string.
This method will remove the surrounding brackets if they are present.- Parameters:
tokenRule- The token rule to convert- Returns:
- The regex string
- Throws:
NullPointerException- If the token rule is null- See Also:
-
toBaseRegex
Converts the given token rule to a regex string without removing the surrounding brackets.
The rules are converted to regex strings and joined together with the appropriate regex operators.- Parameters:
tokenRule- The token rule to convert- Returns:
- The regex string
- Throws:
NullPointerException- If the token rule is null- See Also:
-
toBaseRegex
Converts the given token definition to a regex string.
A word token definition is converted to a regex that matches any alphanumeric and numeric character.
All special characters are escaped.- Parameters:
definition- The token definition to convert- Returns:
- The regex string
- Throws:
NullPointerException- If the token definition is null
-
isSurroundedByBrackets
Checks if the given regex is surrounded by brackets.
This method will check if the first character is a bracket and if the last character is a closing bracket.- Parameters:
regex- The regex to check- Returns:
- True if the regex is surrounded by brackets, false otherwise
- Throws:
NullPointerException- If the regex is null
-
endsWithSpecialChar
Checks if the given regex ends with a special character.
The following characters are considered special characters:?, *, +,- Parameters:
regex- The regex to check- Returns:
- True if the regex ends with a special character, false otherwise
- Throws:
NullPointerException- If the regex is null
-
isLastChar
Checks if the given regex ends with the given character.- Parameters:
regex- The regex to checkc- The character to check- Returns:
- True if the regex ends with the given character, false otherwise
- Throws:
NullPointerException- If the regex is null
-