/**
   * Helper for getOperandProtectedForLowerPrec() and getOperandProtectedForLowerOrEqualPrec().
   *
   * @param index The index of the operand to get.
   * @param shouldProtectEqualPrec Whether to proect the operand if it is an operator with equal
   *     precedence to this operator.
   * @return The source string for the operand at the given index, possibly protected by surrounding
   *     parentheses.
   */
  private String getOperandProtectedForPrecHelper(int index, boolean shouldProtectEqualPrec) {

    int thisOpPrec = this.getOperator().getPrecedence();

    ExprNode child = getChild(index);

    boolean shouldProtect;
    if (child instanceof OperatorNode) {
      int childOpPrec = ((OperatorNode) child).getOperator().getPrecedence();
      shouldProtect = shouldProtectEqualPrec ? childOpPrec <= thisOpPrec : childOpPrec < thisOpPrec;
    } else {
      shouldProtect = false;
    }

    if (shouldProtect) {
      return "(" + child.toSourceString() + ")";
    } else {
      return child.toSourceString();
    }
  }
예제 #2
0
  @Override
  public String toSourceString() {

    StringBuilder sourceSb = new StringBuilder();
    sourceSb.append('[');

    boolean isFirst = true;
    for (ExprNode child : getChildren()) {
      if (isFirst) {
        isFirst = false;
      } else {
        sourceSb.append(", ");
      }
      sourceSb.append(child.toSourceString());
    }

    sourceSb.append(']');
    return sourceSb.toString();
  }