Class TokenRules

java.lang.Object
net.luis.utils.io.token.rule.rules.TokenRules

public final class TokenRules extends Object
A utility class for creating token rules.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor to prevent instantiation.
    This is a static helper class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static @NotNull TokenRule
    Provides a token rule that always matches.
    static @NotNull TokenRule
    any(@NotNull Set<TokenRule> tokenRules)
    Creates a token rule that matches any of the given token rules.
    static @NotNull TokenRule
    any(TokenRule @NotNull ... tokenRules)
    Creates a token rule that matches any of the given token rules.
    static @NotNull TokenRule
    boundary(@NotNull TokenRule startTokenRule, @NotNull TokenRule endTokenRule)
    Creates a token rule that matches a boundary between the given start and end token rules.
    static @NotNull TokenRule
    boundary(@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 TokenRule
    end()
    Provides a token rule that matches the end of the input.
    private static boolean
    endsWithSpecialChar(@NotNull String regex)
    Checks if the given regex ends with a special character.
    The following characters are considered special characters: ?, *, +,
    private static boolean
    isLastChar(@NotNull String regex, char c)
    Checks if the given regex ends with the given character.
    private static boolean
    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 TokenRule
    optional(@NotNull TokenRule tokenRule)
    Creates an optional token rule with the given token rule.
    static @NotNull TokenRule
    pattern(@NotNull String pattern)
    Creates a pattern token rule with the given pattern in string format.
    static @NotNull TokenRule
    pattern(@NotNull Pattern pattern)
    Creates a pattern token rule with the given pattern.
    static @NotNull TokenRule
    repeatAtLeast(@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 to repeatBetween(TokenRule, int, int) with the maximum number of occurrences set to Integer.MAX_VALUE.
    static @NotNull TokenRule
    repeatAtMost(@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 to repeatBetween(TokenRule, int, int) with the minimum number of occurrences set to 0.
    static @NotNull TokenRule
    repeatBetween(@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 TokenRule
    repeatExactly(@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 to repeatBetween(TokenRule, int, int) with the minimum and maximum number of occurrences set to the same value.
    static @NotNull TokenRule
    repeatInfinitely(@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 to repeatBetween(TokenRule, int, int) with the minimum number of occurrences set to 0 and the maximum number of occurrences set to Integer.MAX_VALUE.
    static @NotNull TokenRule
    sequence(@NotNull List<TokenRule> tokenRules)
    Creates a token rule that matches a sequence of token rules.
    static @NotNull TokenRule
    sequence(TokenRule @NotNull ... tokenRules)
    Creates a token rule that matches a sequence of token rules.
    private static @NotNull String
    toBaseRegex(@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 String
    toBaseRegex(@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 String
    toRegex(@NotNull TokenRule tokenRule)
    Converts the given token rule to a regex string.
    This method will remove the surrounding brackets if they are present.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TokenRules

      private TokenRules()
      Private constructor to prevent instantiation.
      This is a static helper class.
  • Method Details

    • alwaysMatch

      @NotNull public static @NotNull TokenRule 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.INSTANCE instance
    • 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

      @NotNull public static @NotNull TokenRule pattern(@NotNull @NotNull Pattern 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

      @NotNull public static @NotNull TokenRule optional(@NotNull @NotNull TokenRule tokenRule)
      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 to repeatBetween(TokenRule, int, int) with the maximum number of occurrences set to Integer.MAX_VALUE.
      Parameters:
      tokenRule - The token rule to match
      min - 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 null
      IllegalArgumentException - 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 to repeatBetween(TokenRule, int, int) with the minimum and maximum number of occurrences set to the same value.
      Parameters:
      tokenRule - The token rule to match
      repeats - 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 null
      IllegalArgumentException - 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 to repeatBetween(TokenRule, int, int) with the minimum number of occurrences set to 0.
      Parameters:
      tokenRule - The token rule to match
      max - 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 null
      IllegalArgumentException - If the maximum number of occurrences is lower than 0
      See Also:
    • repeatInfinitely

      @NotNull public static @NotNull TokenRule repeatInfinitely(@NotNull @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 to repeatBetween(TokenRule, int, int) with the minimum number of occurrences set to 0 and the maximum number of occurrences set to Integer.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 match
      min - The minimum number of occurrences
      max - 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 null
      IllegalArgumentException - 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

      @NotNull public static @NotNull TokenRule sequence(TokenRule @NotNull ... tokenRules)
      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 null
      IllegalArgumentException - If the token rule array is empty
      See Also:
    • sequence

      @NotNull public static @NotNull TokenRule sequence(@NotNull @NotNull List<TokenRule> tokenRules)
      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 null
      IllegalArgumentException - If the token rule list is empty
      See Also:
    • any

      @NotNull public static @NotNull TokenRule any(TokenRule @NotNull ... tokenRules)
      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 null
      IllegalArgumentException - If the token rule array is empty
      See Also:
    • any

      @NotNull public static @NotNull TokenRule any(@NotNull @NotNull Set<TokenRule> tokenRules)
      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 null
      IllegalArgumentException - 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 sequence
      endTokenRule - 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 null
      IllegalArgumentException - 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 sequence
      betweenTokenRule - The token rule that matches the content between the start and end token rules
      endTokenRule - 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 null
      IllegalArgumentException - If the start or between token rule is invalid
      See Also:
    • end

      @NotNull public static @NotNull TokenRule 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.INSTANCE instance
    • toRegex

      @NotNull public static @NotNull String toRegex(@NotNull @NotNull TokenRule tokenRule)
      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

      @NotNull private static @NotNull String toBaseRegex(@NotNull @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.
      Parameters:
      tokenRule - The token rule to convert
      Returns:
      The regex string
      Throws:
      NullPointerException - If the token rule is null
      See Also:
    • toBaseRegex

      @NotNull private static @NotNull String toBaseRegex(@NotNull @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.
      Parameters:
      definition - The token definition to convert
      Returns:
      The regex string
      Throws:
      NullPointerException - If the token definition is null
    • isSurroundedByBrackets

      private static boolean isSurroundedByBrackets(@NotNull @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.
      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

      private static boolean endsWithSpecialChar(@NotNull @NotNull String regex)
      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

      private static boolean isLastChar(@NotNull @NotNull String regex, char c)
      Checks if the given regex ends with the given character.
      Parameters:
      regex - The regex to check
      c - The character to check
      Returns:
      True if the regex ends with the given character, false otherwise
      Throws:
      NullPointerException - If the regex is null