Search in sources :

Example 1 with TreeVisitor

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

the class Parser method checkTreeForExceptions.

private static void checkTreeForExceptions(String input, CommonTree tree) {
    TreeVisitor visitor = new TreeVisitor();
    ErrorNodesFinder errorNodesFinder = new ErrorNodesFinder();
    visitor.visit(tree, errorNodesFinder);
    List<ErrorRec> errors = errorNodesFinder.getErrorNodes().stream().map(node -> new ErrorRec(node, "CommonErrorNode")).collect(Collectors.toList());
    if (!errors.isEmpty()) {
        throw new JpqlSyntaxException(String.format("Errors found for input jpql:[%s]", StringUtils.strip(input)), errors);
    }
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) StringUtils(org.apache.commons.lang.StringUtils) TreeVisitor(org.antlr.runtime.tree.TreeVisitor) Collectors(java.util.stream.Collectors) List(java.util.List) RecognitionException(org.antlr.runtime.RecognitionException) JPA2Lexer(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer) JPA2Parser(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser) CharStream(org.antlr.runtime.CharStream) Collections(java.util.Collections) JoinVariableNode(com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode)

Example 2 with TreeVisitor

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

the class QueryTreeAnalyzer method prepare.

public void prepare(DomainModel model, String query, boolean failOnErrors) throws RecognitionException {
    Preconditions.checkNotNull(query, "query is null");
    this.model = model;
    query = query.replace("\n", " ");
    query = query.replace("\r", " ");
    query = query.replace("\t", " ");
    tree = Parser.parse(query, failOnErrors);
    TreeVisitor visitor = new TreeVisitor();
    idVarSelector = new IdVarSelector(model);
    visitor.visit(tree, idVarSelector);
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor)

Example 3 with TreeVisitor

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

the class QueryTreeAnalyzer method findAllConditions.

public List<SimpleConditionNode> findAllConditions() {
    NodesFinder<SimpleConditionNode> nodesFinder = new NodesFinder<>(SimpleConditionNode.class);
    TreeVisitor treeVisitor = new TreeVisitor();
    treeVisitor.visit(tree, nodesFinder);
    return nodesFinder.getFoundNodes();
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) NodesFinder(com.haulmont.cuba.core.sys.jpql.transform.NodesFinder)

Example 4 with TreeVisitor

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

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 5 with TreeVisitor

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

the class HiveParserUtils method rewriteGroupingFunctionAST.

public static HiveParserASTNode rewriteGroupingFunctionAST(final List<HiveParserASTNode> grpByAstExprs, HiveParserASTNode targetNode, final boolean noneSet) throws SemanticException {
    final MutableBoolean visited = new MutableBoolean(false);
    final MutableBoolean found = new MutableBoolean(false);
    final boolean legacyGrouping = legacyGrouping();
    TreeVisitorAction action = new TreeVisitorAction() {

        @Override
        public Object pre(Object t) {
            return t;
        }

        @Override
        public Object post(Object t) {
            HiveParserASTNode current = (HiveParserASTNode) t;
            // rewrite grouping function
            if (current.getType() == HiveASTParser.TOK_FUNCTION && current.getChildCount() >= 2) {
                HiveParserASTNode func = (HiveParserASTNode) current.getChild(0);
                if (func.getText().equals("grouping")) {
                    visited.setValue(true);
                    convertGrouping(current, grpByAstExprs, noneSet, legacyGrouping, found);
                }
            } else if (legacyGrouping && current.getType() == HiveASTParser.TOK_TABLE_OR_COL && current.getChildCount() == 1) {
                // rewrite grouping__id
                HiveParserASTNode child = (HiveParserASTNode) current.getChild(0);
                if (child.getText().equalsIgnoreCase(VirtualColumn.GROUPINGID.getName())) {
                    return convertToLegacyGroupingId(current, grpByAstExprs.size());
                }
            }
            return t;
        }
    };
    HiveParserASTNode newTargetNode = (HiveParserASTNode) new TreeVisitor(HiveASTParseDriver.ADAPTOR).visit(targetNode, action);
    if (visited.booleanValue() && !found.booleanValue()) {
        throw new SemanticException("Expression in GROUPING function not present in GROUP BY");
    }
    return newTargetNode;
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) HiveParserASTNode(org.apache.flink.table.planner.delegation.hive.copy.HiveParserASTNode) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) TreeVisitorAction(org.antlr.runtime.tree.TreeVisitorAction) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException)

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