Search in sources :

Example 6 with TreeVisitorAction

use of org.antlr.runtime.tree.TreeVisitorAction in project antlr4 by antlr.

the class GrammarTransformPipeline method expandParameterizedLoops.

/**
 * Find and replace
 *      ID*[','] with ID (',' ID)*
 *      ID+[','] with ID (',' ID)+
 *      (x {action} y)+[','] with x {action} y (',' x {action} y)+
 *
 *  Parameter must be a token.
 *  todo: do we want?
 */
public void expandParameterizedLoops(GrammarAST root) {
    TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor());
    v.visit(root, new TreeVisitorAction() {

        @Override
        public Object pre(Object t) {
            if (((GrammarAST) t).getType() == 3) {
                return expandParameterizedLoop((GrammarAST) t);
            }
            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 7 with TreeVisitorAction

use of org.antlr.runtime.tree.TreeVisitorAction 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 8 with TreeVisitorAction

use of org.antlr.runtime.tree.TreeVisitorAction 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)8 TreeVisitorAction (org.antlr.runtime.tree.TreeVisitorAction)8 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)4 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CommonToken (org.antlr.runtime.CommonToken)1 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)1 HiveParserASTNode (org.apache.flink.table.planner.delegation.hive.copy.HiveParserASTNode)1 SQLCheckConstraint (org.apache.hadoop.hive.metastore.api.SQLCheckConstraint)1 SQLDefaultConstraint (org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint)1 SQLNotNullConstraint (org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint)1 SQLUniqueConstraint (org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint)1 DefaultConstraint (org.apache.hadoop.hive.ql.metadata.DefaultConstraint)1 NotNullConstraint (org.apache.hadoop.hive.ql.metadata.NotNullConstraint)1 SemanticException (org.apache.hadoop.hive.ql.parse.SemanticException)1 HivePrivilegeObject (org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObject)1