org.antlr.runtime.tree.CommonErrorNode Java Examples

The following examples show how to use org.antlr.runtime.tree.CommonErrorNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParsingEnvironment.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public String getErrorHeader(RecognitionException e) {
	if (e.node != null) {
		try {
			e = ((CommonErrorNode) e.node).trappedException;
		}
		catch (ClassCastException cce) {
			// ignore for now
		}
	}
	Location location = locator.getLocation(e.line);
	if (location == null) {
		return "UNKNOWN LOCATION (uncorrelated parser line " + e.line + ")";
	}
	if (location.lineno < 0) {
		System.out.println("whoa, line < 0");
	}
	return location.filename + " line " + location.lineno + ":";
}
 
Example #2
Source File: IdVarSelector.java    From cuba with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: RecognizedParamsExtractor.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Takes the "where" parameter and turns it into a Java Object that can be used for querying
 *
 * @param whereParam String
 * @return Query a parsed version of the where clause, represented in Java
 */
default Query getWhereClause(String whereParam) throws InvalidQueryException
{
    if (whereParam == null)
        return QueryImpl.EMPTY;

    try
    {
        CommonTree whereTree = WhereCompiler.compileWhereClause(whereParam);
        if (whereTree instanceof CommonErrorNode)
        {
            rpeLogger().debug("Error parsing the WHERE clause " + whereTree);
            throw new InvalidQueryException(whereTree);
        }
        return new QueryImpl(whereTree);
    }
    catch (RewriteCardinalityException re)
    {  //Catch any error so it doesn't get thrown up the stack
        rpeLogger().info("Unhandled Error parsing the WHERE clause: " + re);
    }
    catch (RecognitionException e)
    {
        whereParam += ", " + WhereCompiler.resolveMessage(e);
        rpeLogger().info("Error parsing the WHERE clause: " + whereParam);
    }
    //Default to throw out an invalid query
    throw new InvalidQueryException(whereParam);
}
 
Example #4
Source File: ErrorNodesFinder.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public Object pre(Object t) {
    if (t instanceof CommonErrorNode) {
        errorNodes.add((CommonErrorNode) t);
        return t;
    }
    return t;
}
 
Example #5
Source File: TreeToQuery.java    From cuba with Apache License 2.0 5 votes vote down vote up
@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.getType() == JPA2Lexer.DISTINCT ||
            node.getType() == JPA2Lexer.FETCH ||
            node.getType() == JPA2Lexer.THEN ||
            node.getType() == JPA2Lexer.ELSE ||
            node.parent != null && (node.parent.getType() == JPA2Lexer.T_SELECTED_ITEM || node.parent.getType() == JPA2Lexer.T_SOURCE) && node.getType() == JPA2Lexer.AS ||
            isExtractFromNode(node)) {
        sb.appendSpace();
    }

    if (node instanceof TreeToQueryCapable) {
        return ((TreeToQueryCapable) t).treeToQueryPost(sb, invalidNodes);
    }

    return t;
}
 
Example #6
Source File: Jpa2GrammarTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #7
Source File: RecognizedParamsExtractor.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 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);
}
 
Example #8
Source File: GrammarASTErrorNode.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public GrammarASTErrorNode(TokenStream input, Token start, Token stop,
                           org.antlr.runtime.RecognitionException e)
{
    delegate = new CommonErrorNode(input,start,stop,e);
}
 
Example #9
Source File: BaseJoinNode.java    From cuba with Apache License 2.0 4 votes vote down vote up
public void identifyVariableEntity(DomainModel model,
                                   Deque<QueryVariableContext> stack,
                                   List<ErrorRec> invalidNodes) {
    String variableName = getVariableName();
    if (variableName == null) {
        invalidNodes.add(new ErrorRec(this, "No variable name found"));
        return;
    }

    List children = getChildren();
    if (children == null || children.size() == 0) {
        invalidNodes.add(new ErrorRec(this, "No children found"));
        return;
    }

    CommonTree child0 = (CommonTree) children.get(0);
    if (child0 instanceof CommonErrorNode) {
        invalidNodes.add(new ErrorRec(this, "Child 0 is an error node"));
        return;
    }

    QueryVariableContext variableContext = stack.peekLast();

    if (child0 instanceof PathNode) {
        PathNode pathNode = (PathNode) child0;
        Pointer pointer = pathNode.resolvePointer(model, variableContext);
        if (pointer instanceof NoPointer) {
            invalidNodes.add(new ErrorRec(this, "Cannot resolve joined entity"));
        } else if (pointer instanceof SimpleAttributePointer) {
            invalidNodes.add(new ErrorRec(this, "Joined entity resolved to a non-entity attribute"));
        } else if (pointer instanceof EntityPointer) {
            variableContext.addEntityVariable(variableName, ((EntityPointer) pointer).getEntity());
        } else if (pointer instanceof CollectionPointer) {
            variableContext.addEntityVariable(variableName, ((CollectionPointer) pointer).getEntity());
        } else {
            invalidNodes.add(new ErrorRec(this,
                            "Unexpected pointer variable type: " + pointer.getClass())
            );
        }
    } else {//this special case is for "join X on X.a = Y.b" query. Entity name would be just text in the child node
        try {
            variableContext.addEntityVariable(variableName, model.getEntityByName(child0.getText()));
        } catch (UnknownEntityNameException e) {
            invalidNodes.add(new ErrorRec(this,
                            "Could not find entity for name " + child0.getText())
            );
        }
    }
}
 
Example #10
Source File: ErrorNodesFinder.java    From cuba with Apache License 2.0 4 votes vote down vote up
public List<CommonErrorNode> getErrorNodes() {
    return Collections.unmodifiableList(errorNodes);
}
 
Example #11
Source File: TreeToQuery.java    From cuba with Apache License 2.0 4 votes vote down vote up
@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) && !isDecimal(node) ||
            node.parent != null && node.parent.getType() == JPA2Lexer.T_GROUP_BY && !isExtractDatePartNode(node) ||
            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 && !isExtractDatePartNode(node) ||
            node.parent != null && (node.parent.getType() == JPA2Lexer.T_SELECTED_ITEM || node.parent.getType() == JPA2Lexer.T_SOURCE) && 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 ||
            isExtractFromNode(node) ||
            isCastTypeNode(node) ||
            isSubQueryAliasWithoutAs(node)
    ) {
        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))) {
            if (!isExtractFromNode(node) && !isExtractDatePartNode(node)
                    && !isExtractArgNode(node) && !isExtractEnd(node)) {
                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;
}