/** * Extracts an identifier from the Antlr AST */ protected String identifier(AST node) { assertNodeType(IDENT, node); return node.getText(); }
protected boolean setAccessTrue(AST node, boolean access) { if (!access) { return true; } else { throw new ASTRuntimeException(node, "Cannot specify modifier: " + node.getText() + " when access scope has already been defined"); } }
protected int setModifierBit(AST node, int answer, int bit) { if ((answer & bit) != 0) { throw new ASTRuntimeException(node, "Cannot repeat modifier: " + node.getText()); } return answer | bit; }
private void dump(AST node, String offset) { System.out.println(offset+"Type: " + getTokenName(node) + " text: " + node.getText()); }
protected void dump(AST node) { System.out.println("Type: " + getTokenName(node) + " text: " + node.getText()); } }
protected String getFirstChildText(AST node) { AST child = node.getFirstChild(); return child != null ? child.getText() : null; }
@Override public void visitMultiCatch(final GroovySourceAST t, final int visit) { if (visit == CLOSING_VISIT) { final AST child = t.getFirstChild(); if ("MULTICATCH_TYPES".equals(child.getText())) { print(t, visit, null, null, " "+child.getNextSibling().getText()); } else { print(t, visit, null, null, " "+child.getFirstChild().getText()); } } }
protected Expression variableExpression(AST node) { String text = node.getText(); // TODO we might wanna only try to resolve the name if we are // on the left hand side of an expression or before a dot? VariableExpression variableExpression = new VariableExpression(text); configureAST(variableExpression, node); return variableExpression; }
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()); }
public static String qualifiedName(AST qualifiedNameNode) { if (isType(IDENT, qualifiedNameNode)) { return qualifiedNameNode.getText(); } if (isType(DOT, qualifiedNameNode)) { AST node = qualifiedNameNode.getFirstChild(); StringBuilder buffer = new StringBuilder(); boolean first = true; while (node != null && !isType(TYPE_ARGUMENTS, node)) { if (first) { first = false; } else { buffer.append("."); } buffer.append(qualifiedName(node)); node = node.getNextSibling(); } return buffer.toString(); } else { return qualifiedNameNode.getText(); } }
protected ConstantExpression integerExpression(AST node) { String text = node.getText(); Object number = Numbers.parseInteger(node, text); boolean keepPrimitive = number instanceof Integer || number instanceof Long; ConstantExpression constantExpression = new ConstantExpression(number, keepPrimitive); configureAST(constantExpression, node); return constantExpression; }
protected ConstantExpression decimalExpression(AST node) { String text = node.getText(); Object number = Numbers.parseDecimal(text); ConstantExpression constantExpression = new ConstantExpression(number, number instanceof Double || number instanceof Float); configureAST(constantExpression, node); return constantExpression; }
if (isPrevString) assertNodeType(IDENT, node); // parser bug isPrevString = true; text = node.getText(); ConstantExpression constantExpression = new ConstantExpression(text); configureAST(constantExpression, node);
private TupleExpression tupleExpression(AST node) { TupleExpression exp = new TupleExpression(); configureAST(exp, node); node = node.getFirstChild(); while (node != null) { assertNodeType(VARIABLE_DEF, node); AST nameNode = node.getFirstChild().getNextSibling(); VariableExpression varExp = new VariableExpression(nameNode.getText()); configureAST(varExp, nameNode); exp.addExpression(varExp); node = node.getNextSibling(); } return exp; }
throw new ASTRuntimeException(selector, "Primitive type literal: " + selector.getText() + " cannot be used as a method name"); } else if (isType(SELECT_SLOT, selector)) { if (selector.getText().equals("this") || selector.getText().equals("super")) { if (!(annotationBeingDef && selector.getText().equals("super"))) { throw new ASTRuntimeException(elist, "Constructor call must be the first statement in a constructor.");
answer = ClassHelper.make(qualifiedName(node)); } else if (isPrimitiveTypeLiteral(node)) { answer = ClassHelper.make(node.getText()); } else if (isType(INDEX_OP, node) || isType(ARRAY_DECLARATOR, node)) { AST child = node.getFirstChild(); return answer; } else { String identifier = node.getText(); answer = ClassHelper.make(identifier);
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; } }
/** * Extracts an identifier from the Antlr AST */ protected String identifier(AST node) { assertNodeType(IDENT, node); return node.getText(); }
protected boolean setAccessTrue(AST node, boolean access) { if (!access) { return true; } else { throw new ASTRuntimeException(node, "Cannot specify modifier: " + node.getText() + " when access scope has already been defined"); } }
return literalExpression(node, null); case STRING_LITERAL: return literalExpression(node, node.getText());