Search in sources :

Example 1 with CommonErrorNode

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

the class IdVarSelector method pre.

@Override
public Object pre(Object t) {
    if (!(t instanceof CommonTree))
        return t;
    if (t instanceof CommonErrorNode) {
        return t;
    }
    CommonTree node = (CommonTree) t;
    if (node instanceof QueryNode) {
        QueryVariableContext newCurrent = new QueryVariableContext(model, (QueryNode) node);
        if (root == null) {
            root = newCurrent;
        }
        QueryVariableContext last = stack.peekLast();
        if (last != null) {
            last.addChild(newCurrent);
        }
        stack.addLast(newCurrent);
    }
    return t;
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) QueryNode(com.haulmont.cuba.core.sys.jpql.tree.QueryNode) QueryVariableContext(com.haulmont.cuba.core.sys.jpql.QueryVariableContext) CommonErrorNode(org.antlr.runtime.tree.CommonErrorNode)

Example 2 with CommonErrorNode

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

the class IdVarSelector method post.

@Override
public Object post(Object t) {
    if (!(t instanceof CommonTree))
        return t;
    if (t instanceof CommonErrorNode) {
        return t;
    }
    CommonTree node = (CommonTree) t;
    if (node.token == null)
        return t;
    if ((node instanceof QueryNode) && node.getParent() != null && "T_CONDITION".equals(((CommonTree) node.getParent()).token.getText())) {
        stack.peekLast().setPropagateVariablesUp(false);
        return t;
    }
    if (node instanceof IdentificationVariableNode) {
        IdentificationVariableNode vnode = (IdentificationVariableNode) node;
        vnode.identifyVariableEntity(model, stack, invalidIdVarNodes);
        return t;
    }
    if (node instanceof JoinVariableNode) {
        JoinVariableNode vnode = (JoinVariableNode) node;
        vnode.identifyVariableEntity(model, stack, invalidIdVarNodes);
        return t;
    }
    return t;
}
Also used : IdentificationVariableNode(com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode) CommonTree(org.antlr.runtime.tree.CommonTree) QueryNode(com.haulmont.cuba.core.sys.jpql.tree.QueryNode) JoinVariableNode(com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode) CommonErrorNode(org.antlr.runtime.tree.CommonErrorNode)

Example 3 with CommonErrorNode

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

the class TreeToQuery method pre.

@Override
public Object pre(Object t) {
    if (!(t instanceof CommonTree)) {
        return t;
    }
    if (t instanceof CommonErrorNode) {
        invalidNodes.add(new ErrorRec((CommonErrorNode) t, "Error node"));
        return t;
    }
    CommonTree node = (CommonTree) t;
    if (node.token == null)
        return t;
    if (node.getType() == JPA2Lexer.HAVING || node.parent != null && node.parent.getType() == JPA2Lexer.T_SIMPLE_CONDITION && !parentNodeHasPreviousLparen(node) || node.parent != null && node.parent.getType() == JPA2Lexer.T_GROUP_BY || node.parent != null && node.parent.getType() == JPA2Lexer.T_ORDER_BY && node.getType() != JPA2Lexer.T_ORDER_BY_FIELD || node.parent != null && node.parent.getType() == JPA2Lexer.T_CONDITION && node.getType() == JPA2Lexer.LPAREN && (node.childIndex == 0 || node.parent.getChild(node.childIndex - 1).getType() != JPA2Lexer.LPAREN) || node.getType() == JPA2Lexer.AND || node.parent != null && node.parent.getType() == JPA2Lexer.T_ORDER_BY_FIELD || node.parent != null && node.parent.getType() == JPA2Lexer.T_SELECTED_ITEM && node.getType() == JPA2Lexer.AS || node.getType() == JPA2Lexer.OR || node.getType() == JPA2Lexer.NOT || node.getType() == JPA2Lexer.DISTINCT && node.childIndex == 0 || node.getType() == JPA2Lexer.JOIN || node.getType() == JPA2Lexer.LEFT || node.getType() == JPA2Lexer.OUTER || node.getType() == JPA2Lexer.INNER || node.getType() == JPA2Lexer.FETCH || node.getType() == JPA2Lexer.CASE || node.getType() == JPA2Lexer.WHEN || node.getType() == JPA2Lexer.THEN || node.getType() == JPA2Lexer.ELSE || node.getType() == JPA2Lexer.END) {
        sb.appendSpace();
    }
    if (node.getType() == JPA2Lexer.T_ORDER_BY_FIELD && node.childIndex > 0 && node.parent.getChild(node.childIndex - 1).getType() == JPA2Lexer.T_ORDER_BY_FIELD) {
        sb.appendString(", ");
    }
    if (isGroupByItem(node)) {
        if (node.childIndex > 0 && isGroupByItem((CommonTree) node.parent.getChild(node.childIndex - 1))) {
            sb.appendString(", ");
        }
    }
    if (node instanceof TreeToQueryCapable) {
        return ((TreeToQueryCapable) t).treeToQueryPre(sb, invalidNodes);
    }
    if (node.getType() == JPA2Lexer.T_SELECTED_ITEMS) {
        return t;
    }
    if (node.getType() == JPA2Lexer.T_SOURCES) {
        sb.appendString("from ");
        return t;
    }
    sb.appendString(node.toString());
    return t;
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) CommonErrorNode(org.antlr.runtime.tree.CommonErrorNode) TreeToQueryCapable(com.haulmont.cuba.core.sys.jpql.tree.TreeToQueryCapable)

Example 4 with CommonErrorNode

use of org.antlr.runtime.tree.CommonErrorNode in project alfresco-remote-api by Alfresco.

the class RecognizedParamsExtractor method getClause.

/**
 * Gets the clause specificed in paramName
 *
 * @param param
 * @param paramName
 * @return bean property names potentially using JSON Pointer syntax
 */
default List<String> getClause(String param, String paramName) {
    if (param == null)
        return Collections.emptyList();
    try {
        CommonTree selectedPropsTree = WhereCompiler.compileSelectClause(param);
        if (selectedPropsTree instanceof CommonErrorNode) {
            rpeLogger().debug("Error parsing the " + paramName + " clause " + selectedPropsTree);
            throw new InvalidSelectException(paramName, selectedPropsTree);
        }
        if (selectedPropsTree.getChildCount() == 0 && !selectedPropsTree.getText().isEmpty()) {
            return Arrays.asList(selectedPropsTree.getText());
        }
        List<Tree> children = (List<Tree>) selectedPropsTree.getChildren();
        if (children != null && !children.isEmpty()) {
            List<String> properties = new ArrayList<String>(children.size());
            for (Tree child : children) {
                properties.add(child.getText());
            }
            return properties;
        }
    } catch (RewriteCardinalityException re) {
        // Catch any error so it doesn't get thrown up the stack
        rpeLogger().debug("Unhandled Error parsing the " + paramName + " clause: " + re);
    } catch (RecognitionException e) {
        rpeLogger().debug("Error parsing the \"+paramName+\" clause: " + param);
    } catch (InvalidQueryException iqe) {
        throw new InvalidSelectException(paramName, iqe.getQueryParam());
    }
    // Default to throw out an invalid query
    throw new InvalidSelectException(paramName, param);
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) ArrayList(java.util.ArrayList) CommonTree(org.antlr.runtime.tree.CommonTree) Tree(org.antlr.runtime.tree.Tree) ArrayList(java.util.ArrayList) List(java.util.List) InvalidSelectException(org.alfresco.rest.framework.resource.parameters.InvalidSelectException) RewriteCardinalityException(org.antlr.runtime.tree.RewriteCardinalityException) RecognitionException(org.antlr.runtime.RecognitionException) InvalidQueryException(org.alfresco.rest.framework.resource.parameters.where.InvalidQueryException) CommonErrorNode(org.antlr.runtime.tree.CommonErrorNode)

Example 5 with CommonErrorNode

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

the class Jpa2GrammarTest method isValid.

protected boolean isValid(CommonTree tree) {
    TreeVisitor visitor = new TreeVisitor();
    ErrorNodesFinder errorNodesFinder = new ErrorNodesFinder();
    visitor.visit(tree, errorNodesFinder);
    List<CommonErrorNode> errorNodes = errorNodesFinder.getErrorNodes();
    if (!errorNodes.isEmpty()) {
        System.err.println(errorNodes);
    }
    return errorNodes.isEmpty();
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) CommonErrorNode(org.antlr.runtime.tree.CommonErrorNode)

Aggregations

CommonErrorNode (org.antlr.runtime.tree.CommonErrorNode)8 CommonTree (org.antlr.runtime.tree.CommonTree)7 QueryNode (com.haulmont.cuba.core.sys.jpql.tree.QueryNode)3 List (java.util.List)3 QueryVariableContext (com.haulmont.cuba.core.sys.jpql.QueryVariableContext)2 IdentificationVariableNode (com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode)2 ErrorRec (com.haulmont.cuba.core.sys.jpql.ErrorRec)1 UnknownEntityNameException (com.haulmont.cuba.core.sys.jpql.UnknownEntityNameException)1 BaseJoinNode (com.haulmont.cuba.core.sys.jpql.tree.BaseJoinNode)1 JoinVariableNode (com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode)1 TreeToQueryCapable (com.haulmont.cuba.core.sys.jpql.tree.TreeToQueryCapable)1 ArrayList (java.util.ArrayList)1 InvalidSelectException (org.alfresco.rest.framework.resource.parameters.InvalidSelectException)1 InvalidQueryException (org.alfresco.rest.framework.resource.parameters.where.InvalidQueryException)1 RecognitionException (org.antlr.runtime.RecognitionException)1 RewriteCardinalityException (org.antlr.runtime.tree.RewriteCardinalityException)1 Tree (org.antlr.runtime.tree.Tree)1 TreeVisitor (org.antlr.runtime.tree.TreeVisitor)1