Search in sources :

Example 1 with AST

use of antlr.collections.AST in project checkstyle by checkstyle.

the class SimplifyBooleanReturnCheck method isBooleanLiteralReturnStatement.

/**
     * Returns if an AST is a return statement with a boolean literal.
     *
     * <p>Returns {@code true} iff ast represents
     * <br/>
     * <pre>
     * return true/false;
     * </pre>
     *
     * @param ast the syntax tree to check
     * @return if ast is a return statement with a boolean literal.
     */
private static boolean isBooleanLiteralReturnStatement(AST ast) {
    boolean booleanReturnStatement = false;
    if (ast != null && ast.getType() == TokenTypes.LITERAL_RETURN) {
        final AST expr = ast.getFirstChild();
        if (expr.getType() != TokenTypes.SEMI) {
            final AST value = expr.getFirstChild();
            booleanReturnStatement = isBooleanLiteralType(value.getType());
        }
    }
    return booleanReturnStatement;
}
Also used : AST(antlr.collections.AST) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 2 with AST

use of antlr.collections.AST in project checkstyle by checkstyle.

the class StringLiteralEqualityCheck method visitToken.

@Override
public void visitToken(DetailAST ast) {
    // no need to check for nulls here, == and != always have two children
    final AST firstChild = ast.getFirstChild();
    final AST secondChild = firstChild.getNextSibling();
    if (firstChild.getType() == TokenTypes.STRING_LITERAL || secondChild.getType() == TokenTypes.STRING_LITERAL) {
        log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY, ast.getText());
    }
}
Also used : AST(antlr.collections.AST) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 3 with AST

use of antlr.collections.AST in project checkstyle by checkstyle.

the class DetailAST method getChildCount.

/**
     * Returns the number of child nodes one level below this node. That is is
     * does not recurse down the tree.
     * @return the number of child nodes
     */
public int getChildCount() {
    // lazy init
    if (childCount == NOT_INITIALIZED) {
        childCount = 0;
        AST child = getFirstChild();
        while (child != null) {
            childCount += 1;
            child = child.getNextSibling();
        }
    }
    return childCount;
}
Also used : AST(antlr.collections.AST)

Example 4 with AST

use of antlr.collections.AST in project groovy by apache.

the class SimpleGroovyClassDocAssembler method getTypeNodeAsText.

private String getTypeNodeAsText(GroovySourceAST typeNode, String defaultText) {
    // TODO refactor to retain richer type information rather than converting to String
    if (typeNode == null) {
        return defaultText;
    }
    if (typeNode.getType() == TYPE) {
        return getAsText(typeNode, defaultText);
    } else if (typeNode.getType() == TYPE_ARGUMENT) {
        return getTypeNodeAsText((GroovySourceAST) typeNode.getFirstChild(), defaultText);
    } else if (typeNode.getType() == WILDCARD_TYPE) {
        AST next = typeNode.getNextSibling();
        if (next == null && typeNode.getFirstChild() != null) {
            // Java2Groovy produces a slightly different tree structure (TODO fix converter or java.g instead?)
            next = typeNode.getFirstChild();
        }
        if (next == null)
            return "?";
        String boundType = getTypeNodeAsText((GroovySourceAST) next.getFirstChild(), defaultText);
        if (next.getType() == TYPE_UPPER_BOUNDS)
            return "? extends " + boundType;
        if (next.getType() == TYPE_LOWER_BOUNDS)
            return "? super " + boundType;
    } else if (typeNode.getType() == IDENT) {
        String ident = getAsTextCurrent(typeNode, defaultText);
        AST next = typeNode.getNextSibling();
        if (next == null && typeNode.getFirstChild() != null) {
            // Java2Groovy produces a slightly different tree structure (TODO fix converter or java.g instead?)
            next = typeNode.getFirstChild();
        }
        if (next == null)
            return ident;
        String boundType = getTypeNodeAsText((GroovySourceAST) next.getFirstChild(), defaultText);
        if (next.getType() == TYPE_UPPER_BOUNDS)
            return ident + " extends " + boundType;
        if (next.getType() == TYPE_LOWER_BOUNDS)
            return ident + " super " + boundType;
    }
    return defaultText;
}
Also used : GroovySourceAST(org.codehaus.groovy.antlr.GroovySourceAST) AST(antlr.collections.AST) GroovySourceAST(org.codehaus.groovy.antlr.GroovySourceAST)

Example 5 with AST

use of antlr.collections.AST in project groovy by apache.

the class AntlrParserPlugin method castExpression.

protected Expression castExpression(AST castNode) {
    AST node = castNode.getFirstChild();
    ClassNode type = makeTypeWithArguments(node);
    assertTypeNotNull(type, node);
    AST expressionNode = node.getNextSibling();
    Expression expression = expression(expressionNode);
    CastExpression castExpression = new CastExpression(type, expression);
    configureAST(castExpression, castNode);
    return castExpression;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST)

Aggregations

AST (antlr.collections.AST)421 RecognitionException (antlr.RecognitionException)157 ASTPair (antlr.ASTPair)155 NoViableAltException (antlr.NoViableAltException)68 ArrayList (java.util.ArrayList)61 ClassNode (org.codehaus.groovy.ast.ClassNode)25 EnumConstantClassNode (org.codehaus.groovy.ast.EnumConstantClassNode)25 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)25 LinkedList (java.util.LinkedList)24 List (java.util.List)20 AntlrASTProcessor (org.codehaus.groovy.antlr.AntlrASTProcessor)16 AssertStatement (org.codehaus.groovy.ast.stmt.AssertStatement)15 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)15 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)15 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)15 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)14 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)14 BreakStatement (org.codehaus.groovy.ast.stmt.BreakStatement)14 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)14 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)14