public static boolean isType(int typeCode, AST node) { return node != null && node.getType() == typeCode; }
protected boolean isPrimitiveTypeLiteral(AST node) { int type = node.getType(); switch (type) { case LITERAL_boolean: case LITERAL_byte: case LITERAL_char: case LITERAL_double: case LITERAL_float: case LITERAL_int: case LITERAL_long: case LITERAL_short: return true; default: return false; } }
private String getTokenName(AST node) { if (node == null) return "null"; return getTokenName(node.getType()); }
private String getTokenName(AST node) { if (node == null) return "null"; return getTokenName(node.getType()); }
public List<GroovySourceAST> childrenOfType(int type) { List<GroovySourceAST> result = new ArrayList<GroovySourceAST>(); AST child = this.getFirstChild(); while (child != null) { if (child.getType() == type) { result.add((GroovySourceAST) child); } child = child.getNextSibling(); } return result; }
public GroovySourceAST childOfType(int type) { AST child = this.getFirstChild(); while (child != null) { if (child.getType() == type) { return (GroovySourceAST)child; } child = child.getNextSibling(); } return null; }
protected void unknownAST(AST node) { if (node.getType() == CLASS_DEF) { throw new ASTRuntimeException(node, "Class definition not expected here. Please define the class at an appropriate place or perhaps try using a block/Closure instead."); } if (node.getType() == METHOD_DEF) { throw new ASTRuntimeException(node, "Method definition not expected here. Please define the method at an appropriate place or perhaps try using a block/Closure instead."); } throw new ASTRuntimeException(node, "Unknown type: " + getTokenName(node)); }
private boolean isConstructorIdent(Token x) { if (currentClass == null) return false; if (currentClass.getType() != IDENT) return false; // cannot happen? String cname = currentClass.getText(); if (x == null || x.getType() != IDENT) return false; // cannot happen? return cname.equals(x.getText()); }
protected boolean addArgumentExpression(AST node, List<Expression> expressionList) { if (node.getType() == SPREAD_MAP_ARG) { AST rightNode = node.getFirstChild(); Expression keyExpression = spreadMapExpression(node); Expression rightExpression = expression(rightNode); MapEntryExpression mapEntryExpression = new MapEntryExpression(keyExpression, rightExpression); expressionList.add(mapEntryExpression); return true; } else { Expression expression = expression(node); expressionList.add(expression); return expression instanceof MapEntryExpression; } }
int type = node.getType(); String text = null; switch (type) {
protected void assertNodeType(int type, AST node) { if (node == null) { throw new ASTRuntimeException(node, "No child node available in AST when expecting type: " + getTokenName(type)); } if (node.getType() != type) { throw new ASTRuntimeException(node, "Unexpected node type: " + getTokenName(node) + " found when expecting type: " + getTokenName(type)); } }
protected Expression unaryPlusExpression(AST unaryPlusExpr) { AST node = unaryPlusExpr.getFirstChild(); switch (node.getType()) { case NUM_DOUBLE: case NUM_FLOAT: case NUM_BIG_DECIMAL: case NUM_BIG_INT: case NUM_INT: case NUM_LONG: return expression(node); default: UnaryPlusExpression unaryPlusExpression = new UnaryPlusExpression(expression(node)); configureAST(unaryPlusExpression, unaryPlusExpr); return unaryPlusExpression; } }
protected Expression listExpression(AST listNode) { List<Expression> expressions = new ArrayList<Expression>(); AST elist = listNode.getFirstChild(); assertNodeType(ELIST, elist); for (AST node = elist.getFirstChild(); node != null; node = node.getNextSibling()) { // check for stray labeled arguments: switch (node.getType()) { case LABELED_ARG: assertNodeType(COMMA, node); break; // helpful error? case SPREAD_MAP_ARG: assertNodeType(SPREAD_ARG, node); break; // helpful error } expressions.add(expression(node)); } ListExpression listExpression = new ListExpression(expressions); configureAST(listExpression, listNode); return listExpression; }
/** * Typically only used for map constructors I think? */ protected Expression mapExpression(AST mapNode) { List expressions = new ArrayList(); AST elist = mapNode.getFirstChild(); if (elist != null) { // totally empty in the case of [:] assertNodeType(ELIST, elist); for (AST node = elist.getFirstChild(); node != null; node = node.getNextSibling()) { switch (node.getType()) { case LABELED_ARG: case SPREAD_MAP_ARG: break; // legal cases case SPREAD_ARG: assertNodeType(SPREAD_MAP_ARG, node); break; // helpful error default: assertNodeType(LABELED_ARG, node); break; // helpful error } expressions.add(mapEntryExpression(node)); } } MapExpression mapExpression = new MapExpression(expressions); configureAST(mapExpression, mapNode); return mapExpression; }
protected Expression blockExpression(AST node) { AST codeNode = node.getFirstChild(); if (codeNode == null) return ConstantExpression.NULL; if (codeNode.getType() == EXPR && codeNode.getNextSibling() == null) { // Simplify common case of {expr} to expr. return expression(codeNode); } Parameter[] parameters = Parameter.EMPTY_ARRAY; Statement code = statementListNoChild(codeNode, node); ClosureExpression closureExpression = new ClosureExpression(parameters, code); configureAST(closureExpression, node); // Call it immediately. String callName = "call"; Expression noArguments = new ArgumentListExpression(); MethodCallExpression call = new MethodCallExpression(closureExpression, callName, noArguments); configureAST(call, node); return call; }
protected MapEntryExpression mapEntryExpression(AST node) { if (node.getType() == SPREAD_MAP_ARG) { AST rightNode = node.getFirstChild(); Expression keyExpression = spreadMapExpression(node); Expression rightExpression = expression(rightNode); MapEntryExpression mapEntryExpression = new MapEntryExpression(keyExpression, rightExpression); configureAST(mapEntryExpression, node); return mapEntryExpression; } else { AST keyNode = node.getFirstChild(); Expression keyExpression = expression(keyNode); AST valueNode = keyNode.getNextSibling(); Expression valueExpression = expression(valueNode); MapEntryExpression mapEntryExpression = new MapEntryExpression(keyExpression, valueExpression); configureAST(mapEntryExpression, node); return mapEntryExpression; } }
protected Expression unaryMinusExpression(AST unaryMinusExpr) { AST node = unaryMinusExpr.getFirstChild(); // if we are a number literal then let's just parse it // as the negation operator on MIN_INT causes rounding to a long String text = node.getText(); switch (node.getType()) { case NUM_DOUBLE: case NUM_FLOAT: case NUM_BIG_DECIMAL: ConstantExpression constantExpression = new ConstantExpression(Numbers.parseDecimal("-" + text)); configureAST(constantExpression, unaryMinusExpr); return constantExpression; case NUM_BIG_INT: case NUM_INT: case NUM_LONG: ConstantExpression constantLongExpression = new ConstantExpression(Numbers.parseInteger(unaryMinusExpr,"-" + text)); configureAST(constantLongExpression, unaryMinusExpr); return constantLongExpression; default: UnaryMinusExpression unaryMinusExpression = new UnaryMinusExpression(expression(node)); configureAST(unaryMinusExpression, unaryMinusExpr); return unaryMinusExpression; } }
int type = node.getType(); switch (type) { case PACKAGE_DEF:
if (callee != null && callee.getType() == METHOD_CALL) { appendedBlock_AST = (AST)astFactory.make( (new ASTArray(3)).add(create(METHOD_CALL,"(",callee,LT(1))).add(callee.getFirstChild()).add(cb_AST)); } else {