Skip to content

Commit

Permalink
Fix indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
slarse committed Mar 9, 2021
1 parent db058e1 commit 73338f7
Showing 1 changed file with 83 additions and 83 deletions.
166 changes: 83 additions & 83 deletions src/main/java/spoon/reflect/visitor/RoundBracketAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,97 +18,97 @@
*/
class RoundBracketAnalyzer {

enum EncloseInRoundBrackets {
YES, NO, UNKNOWN;
}
enum EncloseInRoundBrackets {
YES, NO, UNKNOWN;
}

private RoundBracketAnalyzer() {
}
private RoundBracketAnalyzer() {
}

/**
* @param expr A unary or binary expr.
* @return true if the expr should be enclosed in round brackets.
*/
static EncloseInRoundBrackets requiresRoundBrackets(CtExpression<?> expr) {
return isNestedOperator(expr)
? nestedOperatorRequiresRoundBrackets(expr)
: EncloseInRoundBrackets.UNKNOWN;
}
/**
* @param expr A unary or binary expr.
* @return true if the expr should be enclosed in round brackets.
*/
static EncloseInRoundBrackets requiresRoundBrackets(CtExpression<?> expr) {
return isNestedOperator(expr)
? nestedOperatorRequiresRoundBrackets(expr)
: EncloseInRoundBrackets.UNKNOWN;
}

/**
* Assuming that operator is a nested operator (i.e. both operator and its parent are
* {@link CtUnaryOperator} or {@link CtBinaryOperator}), determine whether or not it must be
* enclosed in round brackets.
*
* Given an element <code>e</code> with a parent <code>p</code>, we must parenthesize
* <code>e</code> if any of the following are true.
*
* <ul>
* <li>The parent p is a unary operator</li>
* <li>The parent p is a binary operator, and <code>precedence(p) > precedence(e></code></li>
* <li>The parent p is a binary operator, <code>precedence(p) == precedence(e)</code>,
* e appears as the X-hand-side operand of p, and e's operator is Y-associative, where
* <code>X != Y</code></li>
* </ul>
*
* Note that the final rule is necessary to preserve syntactical structure, but it is not
* required for preserving semantics.
*
* @param nestedOperator A nested operator.
* @return Whether or not to enclose the nested operator in round brackets.
*/
private static EncloseInRoundBrackets nestedOperatorRequiresRoundBrackets(CtExpression<?> nestedOperator) {
if (nestedOperator.getParent() instanceof CtUnaryOperator) {
return EncloseInRoundBrackets.YES;
}
/**
* Assuming that operator is a nested operator (i.e. both operator and its parent are
* {@link CtUnaryOperator} or {@link CtBinaryOperator}), determine whether or not it must be
* enclosed in round brackets.
*
* Given an element <code>e</code> with a parent <code>p</code>, we must parenthesize
* <code>e</code> if any of the following are true.
*
* <ul>
* <li>The parent p is a unary operator</li>
* <li>The parent p is a binary operator, and <code>precedence(p) > precedence(e></code></li>
* <li>The parent p is a binary operator, <code>precedence(p) == precedence(e)</code>,
* e appears as the X-hand-side operand of p, and e's operator is Y-associative, where
* <code>X != Y</code></li>
* </ul>
*
* Note that the final rule is necessary to preserve syntactical structure, but it is not
* required for preserving semantics.
*
* @param nestedOperator A nested operator.
* @return Whether or not to enclose the nested operator in round brackets.
*/
private static EncloseInRoundBrackets nestedOperatorRequiresRoundBrackets(CtExpression<?> nestedOperator) {
if (nestedOperator.getParent() instanceof CtUnaryOperator) {
return EncloseInRoundBrackets.YES;
}

OperatorHelper.OperatorAssociativity associativity = getOperatorAssociativity(nestedOperator);
OperatorHelper.OperatorAssociativity positionInParent = getPositionInParent(nestedOperator);
OperatorHelper.OperatorAssociativity associativity = getOperatorAssociativity(nestedOperator);
OperatorHelper.OperatorAssociativity positionInParent = getPositionInParent(nestedOperator);

int parentPrecedence = getOperatorPrecedence(nestedOperator.getParent());
int precedence = getOperatorPrecedence(nestedOperator);
return precedence < parentPrecedence
|| (precedence == parentPrecedence && associativity != positionInParent)
? EncloseInRoundBrackets.YES
: EncloseInRoundBrackets.NO;
}
int parentPrecedence = getOperatorPrecedence(nestedOperator.getParent());
int precedence = getOperatorPrecedence(nestedOperator);
return precedence < parentPrecedence
|| (precedence == parentPrecedence && associativity != positionInParent)
? EncloseInRoundBrackets.YES
: EncloseInRoundBrackets.NO;
}

private static boolean isNestedOperator(CtElement e) {
return e.isParentInitialized() && isOperator(e) && isOperator(e.getParent());
}
private static boolean isNestedOperator(CtElement e) {
return e.isParentInitialized() && isOperator(e) && isOperator(e.getParent());
}

private static boolean isOperator(CtElement e) {
return e instanceof CtBinaryOperator || e instanceof CtUnaryOperator;
}
private static boolean isOperator(CtElement e) {
return e instanceof CtBinaryOperator || e instanceof CtUnaryOperator;
}

private static int getOperatorPrecedence(CtElement e) {
if (e instanceof CtBinaryOperator) {
return OperatorHelper.getOperatorPrecedence(((CtBinaryOperator<?>) e).getKind());
} else if (e instanceof CtUnaryOperator) {
return OperatorHelper.getOperatorPrecedence(((CtUnaryOperator<?>) e).getKind());
} else {
return 0;
}
}
private static int getOperatorPrecedence(CtElement e) {
if (e instanceof CtBinaryOperator) {
return OperatorHelper.getOperatorPrecedence(((CtBinaryOperator<?>) e).getKind());
} else if (e instanceof CtUnaryOperator) {
return OperatorHelper.getOperatorPrecedence(((CtUnaryOperator<?>) e).getKind());
} else {
return 0;
}
}

private static OperatorHelper.OperatorAssociativity getOperatorAssociativity(CtElement e) {
if (e instanceof CtBinaryOperator) {
return OperatorHelper.getOperatorAssociativity(((CtBinaryOperator<?>) e).getKind());
} else if (e instanceof CtUnaryOperator) {
return OperatorHelper.getOperatorAssociativity(((CtUnaryOperator<?>) e).getKind());
} else {
return OperatorHelper.OperatorAssociativity.NONE;
}
}
private static OperatorHelper.OperatorAssociativity getOperatorAssociativity(CtElement e) {
if (e instanceof CtBinaryOperator) {
return OperatorHelper.getOperatorAssociativity(((CtBinaryOperator<?>) e).getKind());
} else if (e instanceof CtUnaryOperator) {
return OperatorHelper.getOperatorAssociativity(((CtUnaryOperator<?>) e).getKind());
} else {
return OperatorHelper.OperatorAssociativity.NONE;
}
}

private static OperatorHelper.OperatorAssociativity getPositionInParent(CtElement e) {
CtElement parent = e.getParent();
if (parent instanceof CtBinaryOperator) {
return ((CtBinaryOperator<?>) parent).getLeftHandOperand() == e
? OperatorHelper.OperatorAssociativity.LEFT
: OperatorHelper.OperatorAssociativity.RIGHT;
} else {
return OperatorHelper.OperatorAssociativity.NONE;
}
}
private static OperatorHelper.OperatorAssociativity getPositionInParent(CtElement e) {
CtElement parent = e.getParent();
if (parent instanceof CtBinaryOperator) {
return ((CtBinaryOperator<?>) parent).getLeftHandOperand() == e
? OperatorHelper.OperatorAssociativity.LEFT
: OperatorHelper.OperatorAssociativity.RIGHT;
} else {
return OperatorHelper.OperatorAssociativity.NONE;
}
}
}

0 comments on commit 73338f7

Please sign in to comment.