Search in sources :

Example 66 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project SQLWindowing by hbutani.

the class ParseUtils method parseSelect.

public static SelectSpec parseSelect(String selectExprStr) throws WindowingException {
    Windowing2Lexer lexer;
    CommonTokenStream tokens;
    Windowing2Parser parser = null;
    CommonTree t;
    CommonTreeNodeStream nodes;
    QSpecBuilder2 qSpecBldr = null;
    String err;
    try {
        lexer = new Windowing2Lexer(new ANTLRStringStream(selectExprStr));
        tokens = new CommonTokenStream(lexer);
        parser = new Windowing2Parser(tokens);
        parser.setTreeAdaptor(TranslateUtils.adaptor);
        t = (CommonTree) parser.select().getTree();
        err = parser.getWindowingParseErrors();
        if (err != null) {
            throw new WindowingException(err);
        }
    } catch (WindowingException we) {
        throw we;
    } catch (Throwable te) {
        err = parser.getWindowingParseErrors();
        if (err != null) {
            throw new WindowingException(err);
        }
        throw new WindowingException("Parse Error:" + te.toString(), te);
    }
    TranslateUtils.unescapeStringLiterals((ASTNode) t);
    try {
        nodes = new CommonTreeNodeStream(t);
        nodes.setTokenStream(tokens);
        qSpecBldr = new QSpecBuilder2(nodes);
        SelectSpec selectSpec = qSpecBldr.select();
        err = qSpecBldr.getWindowingParseErrors();
        if (err != null) {
            throw new WindowingException(err);
        }
        return selectSpec;
    } catch (WindowingException we) {
        throw we;
    } catch (Throwable te) {
        err = qSpecBldr.getWindowingParseErrors();
        if (err != null) {
            throw new WindowingException(err);
        }
        throw new WindowingException("Parse Error:" + te.toString(), te);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) WindowingException(com.sap.hadoop.windowing.WindowingException) SelectSpec(com.sap.hadoop.windowing.query2.specification.SelectSpec) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 67 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project drill by apache.

the class PhysicalOpUnitTestBase method parseExpr.

@Override
protected LogicalExpression parseExpr(String expr) {
    ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ExprParser parser = new ExprParser(tokens);
    try {
        return parser.parse().e;
    } catch (RecognitionException e) {
        throw new RuntimeException("Error parsing expression: " + expr);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ExprLexer(org.apache.drill.common.expression.parser.ExprLexer) ExprParser(org.apache.drill.common.expression.parser.ExprParser) RecognitionException(org.antlr.runtime.RecognitionException)

Example 68 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project drill by apache.

the class TestEvaluationVisitor method getExpr.

private LogicalExpression getExpr(String expr) throws Exception {
    ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    //    tokens.fill();
    //    for(Token t : (List<Token>) tokens.getTokens()){
    //      System.out.println(t + "" + t.getType());
    //    }
    //    tokens.rewind();
    ExprParser parser = new ExprParser(tokens);
    parse_return ret = parser.parse();
    return ret.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)

Example 69 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project drill by apache.

the class ExecTest method parseExpr.

protected LogicalExpression parseExpr(String expr) throws RecognitionException {
    final ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final ExprParser parser = new ExprParser(tokens);
    final ExprParser.parse_return ret = parser.parse();
    return ret.e;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ExprLexer(org.apache.drill.common.expression.parser.ExprLexer) ExprParser(org.apache.drill.common.expression.parser.ExprParser)

Example 70 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project nifi by apache.

the class RecordPath method compile.

/**
 * Compiles a RecordPath from the given text
 *
 * @param path the textual representation of the RecordPath
 * @return the compiled RecordPath
 * @throws RecordPathException if the given text is not a valid RecordPath
 */
public static RecordPath compile(final String path) throws RecordPathException {
    try {
        final CharStream input = new ANTLRStringStream(path);
        final RecordPathLexer lexer = new RecordPathLexer(input);
        final CommonTokenStream lexerTokenStream = new CommonTokenStream(lexer);
        final RecordPathParser parser = new RecordPathParser(lexerTokenStream);
        final Tree tree = (Tree) parser.pathExpression().getTree();
        // We look at the first child, because 'tree' is a PATH_EXPRESSION and we really
        // want the underlying PATH token.
        final Tree firstChild = tree.getChild(0);
        final int childType = firstChild.getType();
        final boolean absolute;
        final RecordPathSegment rootPath;
        if (childType == PATH || childType == CHILD_REFERENCE) {
            rootPath = new RootPath();
            absolute = true;
        } else {
            rootPath = null;
            absolute = false;
        }
        return RecordPathCompiler.compile(firstChild, rootPath, absolute);
    } catch (final RecordPathException e) {
        throw e;
    } catch (final Exception e) {
        throw new RecordPathException(e);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) RecordPathSegment(org.apache.nifi.record.path.paths.RecordPathSegment) CommonTokenStream(org.antlr.runtime.CommonTokenStream) RootPath(org.apache.nifi.record.path.paths.RootPath) Tree(org.antlr.runtime.tree.Tree) RecordPathException(org.apache.nifi.record.path.exception.RecordPathException) CharStream(org.antlr.runtime.CharStream) RecordPathException(org.apache.nifi.record.path.exception.RecordPathException)

Aggregations

ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)117 CommonTokenStream (org.antlr.runtime.CommonTokenStream)63 Token (org.antlr.runtime.Token)30 Test (org.junit.Test)28 CharStream (org.antlr.runtime.CharStream)24 CommonToken (org.antlr.runtime.CommonToken)20 RecognitionException (org.antlr.runtime.RecognitionException)18 Lexer (org.eclipse.xtext.parser.antlr.Lexer)16 ActionSplitter (org.antlr.v4.parse.ActionSplitter)12 TokenStream (org.antlr.runtime.TokenStream)10 CommonTree (org.antlr.runtime.tree.CommonTree)10 ExprLexer (org.apache.drill.common.expression.parser.ExprLexer)10 ExprParser (org.apache.drill.common.expression.parser.ExprParser)10 InternalSimpleExpressionsTestLanguageLexer (org.eclipse.xtext.testlanguages.parser.antlr.internal.InternalSimpleExpressionsTestLanguageLexer)8 File (java.io.File)4 ArrayList (java.util.ArrayList)4 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)4 Tree (org.antlr.runtime.tree.Tree)4 ExprParser.parse_return (org.apache.drill.common.expression.parser.ExprParser.parse_return)4 ApexLexer (apex.jorje.parser.impl.ApexLexer)3