Search in sources :

Example 11 with ExprLexer

use of org.apache.drill.common.expression.parser.ExprLexer in project drill by axbaretto.

the class SchemaPath method parseFromString.

/**
 * Parses input string using the same rules which are used for the field in the query.
 * If a string contains dot outside back-ticks, or there are no backticks in the string,
 * will be created {@link SchemaPath} with the {@link NameSegment}
 * which contains one else {@link NameSegment}, etc.
 * If a string contains [] then {@link ArraySegment} will be created.
 *
 * @param expr input string to be parsed
 * @return {@link SchemaPath} instance
 */
public static SchemaPath parseFromString(String expr) {
    if (expr == null || expr.isEmpty()) {
        return null;
    }
    try {
        ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ExprParser parser = new ExprParser(tokens);
        parse_return ret = parser.parse();
        if (ret.e instanceof SchemaPath) {
            return (SchemaPath) ret.e;
        } else {
            throw new IllegalStateException("Schema path is not a valid format.");
        }
    } catch (RecognitionException e) {
        throw new RuntimeException(e);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ExprLexer(org.apache.drill.common.expression.parser.ExprLexer) ExprParser.parse_return(org.apache.drill.common.expression.parser.ExprParser.parse_return) ExprParser(org.apache.drill.common.expression.parser.ExprParser) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

ExprLexer (org.apache.drill.common.expression.parser.ExprLexer)11 ExprParser (org.apache.drill.common.expression.parser.ExprParser)11 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)10 CommonTokenStream (org.antlr.runtime.CommonTokenStream)10 RecognitionException (org.antlr.runtime.RecognitionException)6 SchemaPath (org.apache.drill.common.expression.SchemaPath)3 ExprParser.parse_return (org.apache.drill.common.expression.parser.ExprParser.parse_return)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1