Search in sources :

Example 1 with ExprLexer

use of org.apache.druid.math.expr.antlr.ExprLexer in project druid by druid-io.

the class Parser method parse.

@VisibleForTesting
public static Expr parse(String in, ExprMacroTable macroTable, boolean withFlatten) {
    ExprLexer lexer = new ExprLexer(new ANTLRInputStream(in));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ExprParser parser = new ExprParser(tokens);
    parser.setBuildParseTree(true);
    ParseTree parseTree = parser.expr();
    ParseTreeWalker walker = new ParseTreeWalker();
    ExprListenerImpl listener = new ExprListenerImpl(parseTree, macroTable);
    walker.walk(listener, parseTree);
    Expr parsed = listener.getAST();
    if (parsed == null) {
        throw new RE("Failed to parse expression: %s", in);
    }
    return withFlatten ? flatten(parsed) : parsed;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) RE(org.apache.druid.java.util.common.RE) ExprLexer(org.apache.druid.math.expr.antlr.ExprLexer) ExprParser(org.apache.druid.math.expr.antlr.ExprParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)1 RE (org.apache.druid.java.util.common.RE)1 ExprLexer (org.apache.druid.math.expr.antlr.ExprLexer)1 ExprParser (org.apache.druid.math.expr.antlr.ExprParser)1