Search in sources :

Example 16 with TreeVisitor

use of org.antlr.runtime.tree.TreeVisitor in project cuba by cuba-platform.

the class QueryParserAstBased method getAllEntityNames.

@Override
public Set<String> getAllEntityNames() {
    TreeVisitor visitor = new TreeVisitor();
    EntitiesFinder finder = new EntitiesFinder();
    visitor.visit(getQueryAnalyzer().getTree(), finder);
    return finder.resolveEntityNames(model, getQueryAnalyzer().getRootQueryVariableContext());
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor)

Example 17 with TreeVisitor

use of org.antlr.runtime.tree.TreeVisitor in project cuba by cuba-platform.

the class QueryParserAstBased method getParamNames.

@Override
public Set<String> getParamNames() {
    TreeVisitor visitor = new TreeVisitor();
    ParameterCounter parameterCounter = new ParameterCounter(true);
    visitor.visit(getQueryAnalyzer().getTree(), parameterCounter);
    return parameterCounter.getParameterNames();
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) ParameterCounter(com.haulmont.cuba.core.sys.jpql.transform.ParameterCounter)

Example 18 with TreeVisitor

use of org.antlr.runtime.tree.TreeVisitor in project cuba by cuba-platform.

the class QueryAnalyzerTest method mixinJoinOnIntoTree.

@Test
public void mixinJoinOnIntoTree() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTreeTransformer qa = new QueryTreeTransformer();
    qa.prepare(model, "select c from Car c");
    CommonTree tree = qa.getTree();
    CommonTree sources = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES);
    assertEquals(1, sources.getChildCount());
    assertTrue(sources.getChild(0) instanceof SelectionSourceNode);
    CommonTree source = (CommonTree) sources.getFirstChildWithType(JPA2Lexer.T_SOURCE);
    assertTrue(source.getChild(0) instanceof IdentificationVariableNode);
    JoinVariableNode join = Parser.parseJoinClause("join Driver d on d.car.id = c.id").get(0);
    qa.mixinJoinIntoTree(join, new VariableEntityReference("Car", "c"), true);
    tree = qa.getTree();
    sources = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES);
    assertEquals(1, sources.getChildCount());
    SelectionSourceNode sourceNode = (SelectionSourceNode) sources.getChild(0);
    assertEquals(2, sourceNode.getChildCount());
    assertTrue(sourceNode.getChild(0) instanceof IdentificationVariableNode);
    assertTrue(sourceNode.getChild(1) instanceof JoinVariableNode);
    JoinVariableNode joinNode = (JoinVariableNode) sourceNode.getChild(1);
    TreeVisitor visitor = new TreeVisitor();
    TreeToQuery treeToQuery = new TreeToQuery();
    visitor.visit(joinNode, treeToQuery);
    assertEquals("d", joinNode.getVariableName());
    assertEquals("join Driver d on d.car.id = c.id", treeToQuery.getQueryString().trim());
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) CommonTree(org.antlr.runtime.tree.CommonTree) TreeToQuery(com.haulmont.cuba.core.sys.jpql.TreeToQuery) QueryTreeTransformer(com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer) VariableEntityReference(com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference) Test(org.junit.Test)

Example 19 with TreeVisitor

use of org.antlr.runtime.tree.TreeVisitor in project antlr4 by tunnelvisionlabs.

the class GrammarTransformPipeline method setGrammarPtr.

/**
 * Utility visitor that sets grammar ptr in each node
 */
public static void setGrammarPtr(final Grammar g, GrammarAST tree) {
    if (tree == null)
        return;
    // ensure each node has pointer to surrounding grammar
    TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor());
    v.visit(tree, new TreeVisitorAction() {

        @Override
        public Object pre(Object t) {
            ((GrammarAST) t).g = g;
            return t;
        }

        @Override
        public Object post(Object t) {
            return t;
        }
    });
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) TreeVisitorAction(org.antlr.runtime.tree.TreeVisitorAction)

Example 20 with TreeVisitor

use of org.antlr.runtime.tree.TreeVisitor in project flink by apache.

the class HiveParserBaseSemanticAnalyzer method validateNoHavingReferenceToAlias.

// We support having referring alias just as in hive's semantic analyzer. This check only prints
// a warning now.
public static void validateNoHavingReferenceToAlias(HiveParserQB qb, HiveParserASTNode havingExpr, HiveParserRowResolver inputRR, HiveParserSemanticAnalyzer semanticAnalyzer) throws SemanticException {
    HiveParserQBParseInfo qbPI = qb.getParseInfo();
    Map<HiveParserASTNode, String> exprToAlias = qbPI.getAllExprToColumnAlias();
    for (Map.Entry<HiveParserASTNode, String> exprAndAlias : exprToAlias.entrySet()) {
        final HiveParserASTNode expr = exprAndAlias.getKey();
        final String alias = exprAndAlias.getValue();
        // put the alias in input RR so that we can generate ExprNodeDesc with it
        if (inputRR.getExpression(expr) != null) {
            inputRR.put("", alias, inputRR.getExpression(expr));
        }
        final Set<Object> aliasReferences = new HashSet<>();
        TreeVisitorAction action = new TreeVisitorAction() {

            @Override
            public Object pre(Object t) {
                if (HiveASTParseDriver.ADAPTOR.getType(t) == HiveASTParser.TOK_TABLE_OR_COL) {
                    Object c = HiveASTParseDriver.ADAPTOR.getChild(t, 0);
                    if (c != null && HiveASTParseDriver.ADAPTOR.getType(c) == HiveASTParser.Identifier && HiveASTParseDriver.ADAPTOR.getText(c).equals(alias)) {
                        aliasReferences.add(t);
                    }
                }
                return t;
            }

            @Override
            public Object post(Object t) {
                return t;
            }
        };
        new TreeVisitor(HiveASTParseDriver.ADAPTOR).visit(havingExpr, action);
        if (aliasReferences.size() > 0) {
            String havingClause = semanticAnalyzer.ctx.getTokenRewriteStream().toString(havingExpr.getTokenStartIndex(), havingExpr.getTokenStopIndex());
            String msg = String.format("Encountered Select alias '%s' in having clause '%s'" + " This is non standard behavior.", alias, havingClause);
            LOG.warn(msg);
        }
    }
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) TreeVisitorAction(org.antlr.runtime.tree.TreeVisitorAction) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet)

Aggregations

TreeVisitor (org.antlr.runtime.tree.TreeVisitor)20 TreeVisitorAction (org.antlr.runtime.tree.TreeVisitorAction)8 CommonTree (org.antlr.runtime.tree.CommonTree)4 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)4 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 TreeToQuery (com.haulmont.cuba.core.sys.jpql.TreeToQuery)2 NodesFinder (com.haulmont.cuba.core.sys.jpql.transform.NodesFinder)2 DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)1 JPA2Lexer (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer)1 JPA2Parser (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser)1 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)1 ParameterCounter (com.haulmont.cuba.core.sys.jpql.transform.ParameterCounter)1 QueryTreeTransformer (com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer)1 VariableEntityReference (com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference)1 JoinVariableNode (com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode)1 PathNode (com.haulmont.cuba.core.sys.jpql.tree.PathNode)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1