Class ParserUtils

java.lang.Object
org.elasticsearch.xpack.kql.parser.ParserUtils

public final class ParserUtils extends Object
Utility class for parsing and processing KQL expressions. Provides methods for type-safe parsing, text extraction, and string escaping/unescaping.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    escapeLuceneQueryString(String queryText, boolean preserveWildcards)
    Escapes special characters in a query string for use in Lucene queries.
    static String
    extractText(org.antlr.v4.runtime.ParserRuleContext ctx)
    Extracts text from a parser tree context by joining all terminal nodes with a space delimiter.
    static boolean
    hasWildcard(org.antlr.v4.runtime.ParserRuleContext ctx)
    Checks if the given context contains any unescaped wildcard characters.
    static <T> T
    typedParsing(org.antlr.v4.runtime.tree.ParseTreeVisitor<?> visitor, org.antlr.v4.runtime.ParserRuleContext ctx, Class<T> type)
    Performs type-safe parsing using the provided visitor.

    Methods inherited from class java.lang.Object

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

    • typedParsing

      public static <T> T typedParsing(org.antlr.v4.runtime.tree.ParseTreeVisitor<?> visitor, org.antlr.v4.runtime.ParserRuleContext ctx, Class<T> type)
      Performs type-safe parsing using the provided visitor.
      Parameters:
      visitor - The visitor to use to do the parsing
      ctx - The parser tree context to visit
      type - The expected return type class
      Returns:
      The parsed result, casted to the expected type
    • extractText

      public static String extractText(org.antlr.v4.runtime.ParserRuleContext ctx)
      Extracts text from a parser tree context by joining all terminal nodes with a space delimiter.
      Parameters:
      ctx - The parser tree context
      Returns:
      The extracted text
    • hasWildcard

      public static boolean hasWildcard(org.antlr.v4.runtime.ParserRuleContext ctx)
      Checks if the given context contains any unescaped wildcard characters.
      Parameters:
      ctx - The tree context to check
      Returns:
      true if wildcards are present, false otherwise
    • escapeLuceneQueryString

      public static String escapeLuceneQueryString(String queryText, boolean preserveWildcards)
      Escapes special characters in a query string for use in Lucene queries.
      Parameters:
      queryText - The query text to escape
      preserveWildcards - If true, does not escape wildcard characters (*)
      Returns:
      The escaped query string