Java Code Examples for com.sun.tools.javac.tree.TreeInfo#opPrec()

The following examples show how to use com.sun.tools.javac.tree.TreeInfo#opPrec() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VeryPretty.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
   public void visitUnary(JCUnary tree) {
int ownprec = TreeInfo.opPrec(tree.getTag());
Name opname;
       switch (tree.getTag()) {
           case POS: opname = names.fromString("+"); break;
           case NEG: opname = names.fromString("-"); break;
           default: opname = operators.operatorName(tree.getTag()); break;
       }
if (tree.getTag().ordinal() <= JCTree.Tag.PREDEC.ordinal()) { //XXX: comparing ordinals!
           if (cs.spaceAroundUnaryOps()) {
               needSpace();
               print(opname);
               print(' ');
           } else {
               print(opname);
               if (   (tree.getTag() == JCTree.Tag.POS && (tree.arg.getTag() == JCTree.Tag.POS || tree.arg.getTag() == JCTree.Tag.PREINC))
                   || (tree.getTag() == JCTree.Tag.NEG && (tree.arg.getTag() == JCTree.Tag.NEG || tree.arg.getTag() == JCTree.Tag.PREDEC))) {
                   print(' ');
               }
           }
    printExpr(tree.arg, ownprec);
} else {
    printExpr(tree.arg, ownprec);
           if (cs.spaceAroundUnaryOps()) {
               print(' ');
               print(opname);
               print(' ');
           } else {
               print(opname);
           }
}
   }
 
Example 2
Source File: VeryPretty.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
   public void visitBinary(JCBinary tree) {
int ownprec = TreeInfo.opPrec(tree.getTag());
Name opname = operators.operatorName(tree.getTag());
       int col = out.col;
printExpr(tree.lhs, ownprec);
if(cs.spaceAroundBinaryOps())
           print(' ');
print(opname);
       boolean needsSpace =    cs.spaceAroundBinaryOps()
                            || (tree.getTag() == JCTree.Tag.PLUS  && (tree.rhs.getTag() == JCTree.Tag.POS || tree.rhs.getTag() == JCTree.Tag.PREINC))
                            || (tree.getTag() == JCTree.Tag.MINUS && (tree.rhs.getTag() == JCTree.Tag.NEG || tree.rhs.getTag() == JCTree.Tag.PREDEC));
int rm = cs.getRightMargin();
       switch(cs.wrapBinaryOps()) {
       case WRAP_IF_LONG:
           if (widthEstimator.estimateWidth(tree.rhs, rm - out.col) + out.col <= cs.getRightMargin()) {
               if(needsSpace)
                   print(' ');
               break;
           }
       case WRAP_ALWAYS:
           newline();
           toColExactly(cs.alignMultilineBinaryOp() ? col : out.leftMargin + cs.getContinuationIndentSize());
           break;
       case WRAP_NEVER:
           if(needsSpace)
               print(' ');
           break;
       }
printExpr(tree.rhs, ownprec + 1);
   }
 
Example 3
Source File: WidthEstimator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void visitUnary(JCUnary tree) {
int ownprec = TreeInfo.opPrec(tree.getTag());
Name opname = operators.operatorName(tree.getTag());
open(prec, ownprec);
width(opname);
width(tree.arg, ownprec);
   }
 
Example 4
Source File: WidthEstimator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void visitBinary(JCBinary tree) {
int ownprec = TreeInfo.opPrec(tree.getTag());
Name opname = operators.operatorName(tree.getTag());
open(prec, ownprec);
width(opname);
width+=2;
width(tree.lhs, ownprec);
width(tree.rhs, ownprec + 1);
   }
 
Example 5
Source File: Trees.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the precedence of an expression's operator.
 */
static int precedence(ExpressionTree expression) {
    return TreeInfo.opPrec(((JCTree) expression).getTag());
}
 
Example 6
Source File: Trees.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns the precedence of an expression's operator.
 */
static int precedence(ExpressionTree expression) {
    return TreeInfo.opPrec(((JCTree) expression).getTag());
}
 
Example 7
Source File: Trees.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
/** Returns the precedence of an expression's operator. */
static int precedence(ExpressionTree expression) {
  return TreeInfo.opPrec(((JCTree) expression).getTag());
}
 
Example 8
Source File: ExpressionTemplate.java    From Refaster with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the precedence level appropriate for unambiguously printing
 * leaf as a subexpression of its parent.
 */
private static int getPrecedence(JCTree leaf, Context context) {
  JCCompilationUnit comp = context.get(JCCompilationUnit.class);
  JCTree parent = TreeInfo.pathFor(leaf, comp).get(1);

  // In general, this should match the logic in com.sun.tools.javac.tree.Pretty.
  //
  // TODO(mdempsky): There are probably cases where we could omit parentheses
  // by tweaking the returned precedence, but they need careful review.
  // For example, consider a template to replace "add(a, b)" with "a + b",
  // which applied to "x + add(y, z)" would result in "x + (y + z)".
  // In most cases, we'd likely prefer "x + y + z" instead, but those aren't
  // always equivalent: "0L + (Integer.MIN_VALUE + Integer.MIN_VALUE)" yields
  // a different value than "0L + Integer.MIN_VALUE + Integer.MIN_VALUE" due
  // to integer promotion rules.

  if (parent instanceof JCConditional) {
    // This intentionally differs from Pretty, because Pretty appears buggy:
    // http://mail.openjdk.java.net/pipermail/compiler-dev/2013-September/007303.html
    JCConditional conditional = (JCConditional) parent;
    return TreeInfo.condPrec + ((conditional.cond == leaf) ? 1 : 0);
  } else if (parent instanceof JCAssign) {
    JCAssign assign = (JCAssign) parent;
    return TreeInfo.assignPrec + ((assign.lhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCAssignOp) {
    JCAssignOp assignOp = (JCAssignOp) parent;
    return TreeInfo.assignopPrec + ((assignOp.lhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCUnary) {
    return TreeInfo.opPrec(parent.getTag());
  } else if (parent instanceof JCBinary) {
    JCBinary binary = (JCBinary) parent;
    return TreeInfo.opPrec(parent.getTag()) + ((binary.rhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCTypeCast) {
    JCTypeCast typeCast = (JCTypeCast) parent;
    return (typeCast.expr == leaf) ? TreeInfo.prefixPrec : TreeInfo.noPrec;
  } else if (parent instanceof JCInstanceOf) {
    JCInstanceOf instanceOf = (JCInstanceOf) parent;
    return TreeInfo.ordPrec + ((instanceOf.clazz == leaf) ? 1 : 0);
  } else if (parent instanceof JCArrayAccess) {
    JCArrayAccess arrayAccess = (JCArrayAccess) parent;
    return (arrayAccess.indexed == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec;
  } else if (parent instanceof JCFieldAccess) {
    JCFieldAccess fieldAccess = (JCFieldAccess) parent;
    return (fieldAccess.selected == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec;
  } else {
    return TreeInfo.noPrec;
  }
}