Class SkriptParser

java.lang.Object
ch.njol.skript.lang.SkriptParser

public class SkriptParser extends Object
Used for parsing my custom patterns.

Note: All parse methods print one error at most xor any amount of warnings and lower level log messages. If the given string doesn't match any pattern then nothing is printed.
  • Field Details

    • PARSE_EXPRESSIONS

      public static final int PARSE_EXPRESSIONS
      See Also:
      Constant Field Values
    • PARSE_LITERALS

      public static final int PARSE_LITERALS
      See Also:
      Constant Field Values
    • ALL_FLAGS

      public static final int ALL_FLAGS
      See Also:
      Constant Field Values
    • context

      public final ParseContext context
    • wildcard

      public static final String wildcard
      See Also:
      Constant Field Values
    • stringMatcher

      public static final String stringMatcher
      See Also:
      Constant Field Values
    • listSplitPattern

      public static final Pattern listSplitPattern
      Matches ',', 'and', 'or', etc. as well as surrounding whitespace.

      group 1 is null for ',', otherwise it's one of and/or/nor (not necessarily lowercase).

  • Constructor Details

    • SkriptParser

      public SkriptParser(String expr)
    • SkriptParser

      public SkriptParser(String expr, int flags)
    • SkriptParser

      public SkriptParser(String expr, int flags, ParseContext context)
      Constructs a new SkriptParser object that can be used to parse the given expression.

      A SkriptParser can be re-used indefinitely for the given expression, but to parse a new expression a new SkriptParser has to be created.

      Parameters:
      expr - The expression to parse
      flags - Some parse flags (PARSE_EXPRESSIONS, PARSE_LITERALS)
      context - The parse context
    • SkriptParser

      public SkriptParser(SkriptParser other, String expr)
  • Method Details

    • parseLiteral

      public static <T> @Nullable Literal<? extends T> parseLiteral(String expr, Class<T> c, ParseContext context)
      Parses a single literal, i.e. not lists of literals.

      Prints errors.

    • parse

      public static <T extends SyntaxElement> @Nullable T parse(String expr, Iterator<? extends SyntaxElementInfo<T>> source, @Nullable String defaultError)
      Parses a string as one of the given syntax elements.

      Can print an error.

    • parseStatic

      public static <T extends SyntaxElement> @Nullable T parseStatic(String expr, Iterator<? extends SyntaxElementInfo<? extends T>> source, @Nullable String defaultError)
    • parseExpression

      public final <T> @Nullable Expression<? extends T> parseExpression(Class<? extends T>... types)
    • parseExpression

      public final @Nullable Expression<?> parseExpression(SkriptParser.ExprInfo vi)
    • parseFunction

      public final <T> @Nullable FunctionReference<T> parseFunction(@Nullable Class<? extends T>... types)
      Parameters:
      types - The required return type or null if it is not used (e.g. when calling a void function)
      Returns:
      The parsed function, or null if the given expression is not a function call or is an invalid function call (check for an error to differentiate these two)
    • parseArguments

      public static boolean parseArguments(String args, ScriptCommand command, ScriptCommandEvent event)
      Prints parse errors (i.e. must start a ParseLog before calling this method)
    • parse

      public static @Nullable SkriptParser.ParseResult parse(String text, String pattern)
      Parses the text as the given pattern as ParseContext.COMMAND.

      Prints parse errors (i.e. must start a ParseLog before calling this method)

    • parseEvent

      public static @Nullable NonNullPair<SkriptEventInfo<?>,​SkriptEvent> parseEvent(String event, String defaultError)
    • nextBracket

      public static int nextBracket(String pattern, char closingBracket, char openingBracket, int start, boolean isGroup) throws MalformedPatternException
      Finds the closing bracket of the group at start (i.e. start has to be in a group).
      Parameters:
      pattern -
      closingBracket - The bracket to look for, e.g. ')'
      openingBracket - A bracket that opens another group, e.g. '('
      start - This must not be the index of the opening bracket!
      isGroup - Whether start is assumed to be in a group (will print an error if this is not the case, otherwise it returns pattern.length())
      Returns:
      The index of the next bracket
      Throws:
      MalformedPatternException - If the group is not closed
    • notOfType

      public static String notOfType(Class<?>... cs)
      Parameters:
      cs -
      Returns:
      "not an x" or "neither an x, a y nor a z"
    • notOfType

      public static String notOfType(ClassInfo<?>... cs)
    • next

      public static int next(String expr, int i, ParseContext context)
      Returns the next character in the expression, skipping strings, variables and parentheses (unless context is ParseContext.COMMAND).
      Parameters:
      expr - The expression
      i - The last index
      Returns:
      The next index (can be expr.length()), or -1 if an invalid string, variable or bracket is found or if i >= expr.length().
      Throws:
      StringIndexOutOfBoundsException - if i < 0
    • validatePattern

      public static @Nullable NonNullPair<String,​boolean[]> validatePattern(String pattern)
      Validates a user-defined pattern (used in ExprParse).
      Parameters:
      pattern -
      Returns:
      The pattern with %codenames% and a boolean array that contains whether the expressions are plural or not
    • validateLine

      public static boolean validateLine(String line)