Class ExpressionParser

java.lang.Object
org.cyclops.integrateddynamics.core.evaluate.expression.tree.ExpressionParser

public final class ExpressionParser extends Object
Parses the text form of expressions, as written by ExpressionPrinter.printExact(Expression, ValueDeseralizationContext, IntFunction, Function, IntFunction).
expression     := [parameters "=>"] ternary
parameters     := NAME | "(" parameter ("," parameter)* ")"
parameter      := NAME [":" [NAME ":"] NAME]
ternary        := or ["?" ternary ":" ternary]
or             := and ("||" and)*
and            := equality ("&&" equality)*
equality       := relational (("==" | "!=") relational)*
relational     := additive (("<" | "<=" | ">" | ">=") additive)*
additive       := multiplicative (("+" | "-") multiplicative)*
multiplicative := unary (("*" | "/" | "%") unary)*
unary          := "!" unary | ("-" NUMBER | primary) ("." NAME arguments)*
primary        := NUMBER | STRING | "true" | "false" | "@" TYPE SNBT | "#" (ID | NAME | STRING) | "?"
                | "[" [ternary ("," ternary)* [","]] "]" | NAME arguments | NAME | "(" (NAME | SYMBOL) ")" | "(" ternary ")"
arguments      := "(" [ternary ("," ternary)*] ")"
Operator names are resolved by OperatorNames, using the type of the first argument. A ? is an empty slot, and (name) an operator used as a value. As in Integrated Scripting, a.f(b) is f(a, b), calling a parameter as in f(a) is apply(f, a), + joins strings, strings can be in single quotes, and ->, === and !== can be written for =>, == and !=.
  • Field Details

    • INFIX_SYMBOLS

      public static final Set<String> INFIX_SYMBOLS
    • PREFIX_SYMBOLS

      public static final Set<String> PREFIX_SYMBOLS
  • Method Details

    • parseLiteral

      @Nullable public static IValue parseLiteral(String text)
      Parameters:
      text - Text.
      Returns:
      The value if the text is a single number or boolean, as in 5, -5L, 1.5 or true, read as in the text of an expression, or null otherwise.
    • getMaxLength

      public static int getMaxLength()
      Returns:
      The maximum length of the text of an expression, from the server config, which clients get when they join.
    • parse

      public static Expression parse(String text, ExpressionParser.Context context) throws ExpressionParseException
      Parameters:
      text - The text of an expression.
      context - What the parser needs to know about the world.
      Returns:
      The expression.
      Throws:
      ExpressionParseException - If the text is not a valid expression.
    • tokenize

      Parse the text of an expression, and split the text into tokens that tell what node each part belongs to, as ExpressionPrinter.tokenizeExact(Expression, ValueDeseralizationContext, IntFunction, Function, ExpressionTypeChecker.Result) does for printed text, so typed text can be shown as typed.
      Parameters:
      text - The text of an expression.
      context - What the parser needs to know about the world.
      Returns:
      The expression and the tokens of the text.
      Throws:
      ExpressionParseException - If the text is not a valid expression.
    • complete

      public static ExpressionParser.Completion complete(String text, int cursor, ExpressionParser.Context context)
      Find what can be completed at a cursor, as far as the text before it tells.
      Parameters:
      text - The text of an expression.
      cursor - The position of the cursor.
      context - What the parser needs to know about the world.
      Returns:
      What can be completed.
    • getParameterList

      @Nullable public static ExpressionParser.ParameterList getParameterList(String text, ExpressionParser.Context context)
      Find the parameters in front of a text, which does not need to parse as a whole.
      Parameters:
      text - The text of an expression.
      context - What the parser needs to know about the world.
      Returns:
      The parameters, or null if there are none.
    • getTypes

      public static List<IValueType<?>> getTypes(ExpressionParser.Types types, ExpressionParser.Context context)
      Parameters:
      types - A type that is being typed.
      context - What the parser needs to know about the world.
      Returns:
      The types whose name starts with what is typed, or whose name without the namespace of an add-on does, ordered by name, those of Integrated Dynamics first. For parameters, only the types they can have.
    • isValidParameterName

      public static boolean isValidParameterName(String name)
      Parameters:
      name - A parameter name.
      Returns:
      If the name can be written in the text form.
    • isIdentifier

      public static boolean isIdentifier(String text)
      Parameters:
      text - A text.
      Returns:
      If the text is a single name.
    • getTypeName

      public static String getTypeName(IValueType<?> valueType)
      Parameters:
      valueType - A value type.
      Returns:
      The name of the type in the text form, see IValueType.getExpressionName().
    • getValueType

      @Nullable public static IValueType<?> getValueType(String name)
      Parameters:
      name - The name of a type in the text form, in any case, as in Integer or integer. Unique names are read too, as in itemstack or integratedmekanism:chemicalstack, where the namespace can be left out when no other type has that name.
      Returns:
      The type, or null if unknown or ambiguous.
    • isTypeNamePart

      public static boolean isTypeNamePart(char c)
      Parameters:
      c - A character.
      Returns:
      If it can be part of the name of a type after @, which can contain a namespace.
    • getListType

      public static IValueType<?> getListType(List<? extends IValue> values)
      Parameters:
      values - The values of a list.
      Returns:
      The type of a list literal with these values: the type they share, or Any if they differ or there are none.