public void postVisit(ASTNode node) { if (!(node instanceof ParenthesizedExpression)) { return; } ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) node; Expression expression = parenthesizedExpression.getExpression(); while (expression instanceof ParenthesizedExpression) { expression = ((ParenthesizedExpression) expression).getExpression(); } // if this is part of another expression, check for this and parent precedences if (parenthesizedExpression.getParent() instanceof Expression) { Expression parentExpression = (Expression) parenthesizedExpression.getParent(); int expressionPrecedence = getExpressionPrecedence(expression); int parentPrecedence = getExpressionPrecedence(parentExpression); if ((expressionPrecedence > parentPrecedence) && !(parenthesizedExpression.getParent() instanceof ParenthesizedExpression)) { return; } // check for case when precedences for expression and parent are same if ((expressionPrecedence == parentPrecedence) && (parentExpression instanceof InfixExpression)) { // we have expr infix (expr infix expr) removing the parenthesis is equal to (expr infix // expr) infix expr InfixExpression parentInfix = (InfixExpression) parentExpression; Operator parentOperator = parentInfix.getOperator(); if (parentInfix.getLeftOperand() == parenthesizedExpression) { fNodes.add(node); } else if (isAssoziative(parentOperator)) { if (parentOperator == InfixExpression.Operator.PLUS) { if (isStringExpression(parentInfix.getLeftOperand()) || isStringExpression(parentInfix.getRightOperand())) { return; } for (Iterator J = parentInfix.extendedOperands().iterator(); J.hasNext(); ) { Expression operand = (Expression) J.next(); if (isStringExpression(operand)) { return; } } } fNodes.add(node); } return; } else if (expressionPrecedence == parentPrecedence && parentExpression instanceof ConditionalExpression) { if (((ConditionalExpression) parentExpression).getElseExpression() != parenthesizedExpression) return; } } fNodes.add(node); }
/* * @see ASTVisitor#visit(InfixExpression) */ public boolean visit(InfixExpression node) { node.getLeftOperand().accept(this); this.fBuffer.append(' '); // for cases like x= i - -1; or x= i++ + ++i; this.fBuffer.append(node.getOperator().toString()); this.fBuffer.append(' '); node.getRightOperand().accept(this); final List extendedOperands = node.extendedOperands(); if (extendedOperands.size() != 0) { this.fBuffer.append(' '); for (Iterator it = extendedOperands.iterator(); it.hasNext(); ) { this.fBuffer.append(node.getOperator().toString()).append(' '); Expression e = (Expression) it.next(); e.accept(this); } } return false; }
/** @deprecated using deprecated code */ public void testInfixExpression() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= 1 + 2;\n"); buf.append(" j= 1 + 2 + 3 + 4 + 5;\n"); buf.append(" k= 1 + 2 + 3 + 4 + 5;\n"); buf.append(" }\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); // TypeDeclaration type= findTypeDeclaration(astRoot, "E"); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 3", statements.size() == 3); { // change left side & right side & operand ExpressionStatement stmt = (ExpressionStatement) statements.get(0); Assignment assignment = (Assignment) stmt.getExpression(); InfixExpression expr = (InfixExpression) assignment.getRightHandSide(); SimpleName leftOp = ast.newSimpleName("k"); rewrite.replace(expr.getLeftOperand(), leftOp, null); SimpleName rightOp = ast.newSimpleName("j"); rewrite.replace(expr.getRightOperand(), rightOp, null); // change operand rewrite.set(expr, InfixExpression.OPERATOR_PROPERTY, InfixExpression.Operator.MINUS, null); } { // remove an ext. operand, add one and replace one ExpressionStatement stmt = (ExpressionStatement) statements.get(1); Assignment assignment = (Assignment) stmt.getExpression(); InfixExpression expr = (InfixExpression) assignment.getRightHandSide(); List extendedOperands = expr.extendedOperands(); assertTrue("Number of extendedOperands not 3", extendedOperands.size() == 3); rewrite.remove((ASTNode) extendedOperands.get(0), null); SimpleName newOp1 = ast.newSimpleName("k"); rewrite.replace((ASTNode) extendedOperands.get(1), newOp1, null); SimpleName newOp2 = ast.newSimpleName("n"); rewrite .getListRewrite(expr, InfixExpression.EXTENDED_OPERANDS_PROPERTY) .insertLast(newOp2, null); } { // change operand ExpressionStatement stmt = (ExpressionStatement) statements.get(2); Assignment assignment = (Assignment) stmt.getExpression(); InfixExpression expr = (InfixExpression) assignment.getRightHandSide(); rewrite.set(expr, InfixExpression.OPERATOR_PROPERTY, InfixExpression.Operator.TIMES, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= k - j;\n"); buf.append(" j= 1 + 2 + k + 5 + n;\n"); buf.append(" k= 1 * 2 * 3 * 4 * 5;\n"); buf.append(" }\n"); assertEqualString(preview, buf.toString()); }