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;
}
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());
}
}
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;
}
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;
}
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;
}
Aggregations