|
18 | 18 | */
|
19 | 19 | class RoundBracketAnalyzer {
|
20 | 20 |
|
21 |
| - enum EncloseInRoundBrackets { |
22 |
| - YES, NO, UNKNOWN; |
23 |
| - } |
| 21 | + enum EncloseInRoundBrackets { |
| 22 | + YES, NO, UNKNOWN; |
| 23 | + } |
24 | 24 |
|
25 |
| - private RoundBracketAnalyzer() { |
26 |
| - } |
| 25 | + private RoundBracketAnalyzer() { |
| 26 | + } |
27 | 27 |
|
28 |
| - /** |
29 |
| - * @param expr A unary or binary expr. |
30 |
| - * @return true if the expr should be enclosed in round brackets. |
31 |
| - */ |
32 |
| - static EncloseInRoundBrackets requiresRoundBrackets(CtExpression<?> expr) { |
33 |
| - return isNestedOperator(expr) |
34 |
| - ? nestedOperatorRequiresRoundBrackets(expr) |
35 |
| - : EncloseInRoundBrackets.UNKNOWN; |
36 |
| - } |
| 28 | + /** |
| 29 | + * @param expr A unary or binary expr. |
| 30 | + * @return true if the expr should be enclosed in round brackets. |
| 31 | + */ |
| 32 | + static EncloseInRoundBrackets requiresRoundBrackets(CtExpression<?> expr) { |
| 33 | + return isNestedOperator(expr) |
| 34 | + ? nestedOperatorRequiresRoundBrackets(expr) |
| 35 | + : EncloseInRoundBrackets.UNKNOWN; |
| 36 | + } |
37 | 37 |
|
38 |
| - /** |
39 |
| - * Assuming that operator is a nested operator (i.e. both operator and its parent are |
40 |
| - * {@link CtUnaryOperator} or {@link CtBinaryOperator}), determine whether or not it must be |
41 |
| - * enclosed in round brackets. |
42 |
| - * |
43 |
| - * Given an element <code>e</code> with a parent <code>p</code>, we must parenthesize |
44 |
| - * <code>e</code> if any of the following are true. |
45 |
| - * |
46 |
| - * <ul> |
47 |
| - * <li>The parent p is a unary operator</li> |
48 |
| - * <li>The parent p is a binary operator, and <code>precedence(p) > precedence(e></code></li> |
49 |
| - * <li>The parent p is a binary operator, <code>precedence(p) == precedence(e)</code>, |
50 |
| - * e appears as the X-hand-side operand of p, and e's operator is Y-associative, where |
51 |
| - * <code>X != Y</code></li> |
52 |
| - * </ul> |
53 |
| - * |
54 |
| - * Note that the final rule is necessary to preserve syntactical structure, but it is not |
55 |
| - * required for preserving semantics. |
56 |
| - * |
57 |
| - * @param nestedOperator A nested operator. |
58 |
| - * @return Whether or not to enclose the nested operator in round brackets. |
59 |
| - */ |
60 |
| - private static EncloseInRoundBrackets nestedOperatorRequiresRoundBrackets(CtExpression<?> nestedOperator) { |
61 |
| - if (nestedOperator.getParent() instanceof CtUnaryOperator) { |
62 |
| - return EncloseInRoundBrackets.YES; |
63 |
| - } |
| 38 | + /** |
| 39 | + * Assuming that operator is a nested operator (i.e. both operator and its parent are |
| 40 | + * {@link CtUnaryOperator} or {@link CtBinaryOperator}), determine whether or not it must be |
| 41 | + * enclosed in round brackets. |
| 42 | + * |
| 43 | + * Given an element <code>e</code> with a parent <code>p</code>, we must parenthesize |
| 44 | + * <code>e</code> if any of the following are true. |
| 45 | + * |
| 46 | + * <ul> |
| 47 | + * <li>The parent p is a unary operator</li> |
| 48 | + * <li>The parent p is a binary operator, and <code>precedence(p) > precedence(e></code></li> |
| 49 | + * <li>The parent p is a binary operator, <code>precedence(p) == precedence(e)</code>, |
| 50 | + * e appears as the X-hand-side operand of p, and e's operator is Y-associative, where |
| 51 | + * <code>X != Y</code></li> |
| 52 | + * </ul> |
| 53 | + * |
| 54 | + * Note that the final rule is necessary to preserve syntactical structure, but it is not |
| 55 | + * required for preserving semantics. |
| 56 | + * |
| 57 | + * @param nestedOperator A nested operator. |
| 58 | + * @return Whether or not to enclose the nested operator in round brackets. |
| 59 | + */ |
| 60 | + private static EncloseInRoundBrackets nestedOperatorRequiresRoundBrackets(CtExpression<?> nestedOperator) { |
| 61 | + if (nestedOperator.getParent() instanceof CtUnaryOperator) { |
| 62 | + return EncloseInRoundBrackets.YES; |
| 63 | + } |
64 | 64 |
|
65 |
| - OperatorHelper.OperatorAssociativity associativity = getOperatorAssociativity(nestedOperator); |
66 |
| - OperatorHelper.OperatorAssociativity positionInParent = getPositionInParent(nestedOperator); |
| 65 | + OperatorHelper.OperatorAssociativity associativity = getOperatorAssociativity(nestedOperator); |
| 66 | + OperatorHelper.OperatorAssociativity positionInParent = getPositionInParent(nestedOperator); |
67 | 67 |
|
68 |
| - int parentPrecedence = getOperatorPrecedence(nestedOperator.getParent()); |
69 |
| - int precedence = getOperatorPrecedence(nestedOperator); |
70 |
| - return precedence < parentPrecedence |
71 |
| - || (precedence == parentPrecedence && associativity != positionInParent) |
72 |
| - ? EncloseInRoundBrackets.YES |
73 |
| - : EncloseInRoundBrackets.NO; |
74 |
| - } |
| 68 | + int parentPrecedence = getOperatorPrecedence(nestedOperator.getParent()); |
| 69 | + int precedence = getOperatorPrecedence(nestedOperator); |
| 70 | + return precedence < parentPrecedence |
| 71 | + || (precedence == parentPrecedence && associativity != positionInParent) |
| 72 | + ? EncloseInRoundBrackets.YES |
| 73 | + : EncloseInRoundBrackets.NO; |
| 74 | + } |
75 | 75 |
|
76 |
| - private static boolean isNestedOperator(CtElement e) { |
77 |
| - return e.isParentInitialized() && isOperator(e) && isOperator(e.getParent()); |
78 |
| - } |
| 76 | + private static boolean isNestedOperator(CtElement e) { |
| 77 | + return e.isParentInitialized() && isOperator(e) && isOperator(e.getParent()); |
| 78 | + } |
79 | 79 |
|
80 |
| - private static boolean isOperator(CtElement e) { |
81 |
| - return e instanceof CtBinaryOperator || e instanceof CtUnaryOperator; |
82 |
| - } |
| 80 | + private static boolean isOperator(CtElement e) { |
| 81 | + return e instanceof CtBinaryOperator || e instanceof CtUnaryOperator; |
| 82 | + } |
83 | 83 |
|
84 |
| - private static int getOperatorPrecedence(CtElement e) { |
85 |
| - if (e instanceof CtBinaryOperator) { |
86 |
| - return OperatorHelper.getOperatorPrecedence(((CtBinaryOperator<?>) e).getKind()); |
87 |
| - } else if (e instanceof CtUnaryOperator) { |
88 |
| - return OperatorHelper.getOperatorPrecedence(((CtUnaryOperator<?>) e).getKind()); |
89 |
| - } else { |
90 |
| - return 0; |
91 |
| - } |
92 |
| - } |
| 84 | + private static int getOperatorPrecedence(CtElement e) { |
| 85 | + if (e instanceof CtBinaryOperator) { |
| 86 | + return OperatorHelper.getOperatorPrecedence(((CtBinaryOperator<?>) e).getKind()); |
| 87 | + } else if (e instanceof CtUnaryOperator) { |
| 88 | + return OperatorHelper.getOperatorPrecedence(((CtUnaryOperator<?>) e).getKind()); |
| 89 | + } else { |
| 90 | + return 0; |
| 91 | + } |
| 92 | + } |
93 | 93 |
|
94 |
| - private static OperatorHelper.OperatorAssociativity getOperatorAssociativity(CtElement e) { |
95 |
| - if (e instanceof CtBinaryOperator) { |
96 |
| - return OperatorHelper.getOperatorAssociativity(((CtBinaryOperator<?>) e).getKind()); |
97 |
| - } else if (e instanceof CtUnaryOperator) { |
98 |
| - return OperatorHelper.getOperatorAssociativity(((CtUnaryOperator<?>) e).getKind()); |
99 |
| - } else { |
100 |
| - return OperatorHelper.OperatorAssociativity.NONE; |
101 |
| - } |
102 |
| - } |
| 94 | + private static OperatorHelper.OperatorAssociativity getOperatorAssociativity(CtElement e) { |
| 95 | + if (e instanceof CtBinaryOperator) { |
| 96 | + return OperatorHelper.getOperatorAssociativity(((CtBinaryOperator<?>) e).getKind()); |
| 97 | + } else if (e instanceof CtUnaryOperator) { |
| 98 | + return OperatorHelper.getOperatorAssociativity(((CtUnaryOperator<?>) e).getKind()); |
| 99 | + } else { |
| 100 | + return OperatorHelper.OperatorAssociativity.NONE; |
| 101 | + } |
| 102 | + } |
103 | 103 |
|
104 |
| - private static OperatorHelper.OperatorAssociativity getPositionInParent(CtElement e) { |
105 |
| - CtElement parent = e.getParent(); |
106 |
| - if (parent instanceof CtBinaryOperator) { |
107 |
| - return ((CtBinaryOperator<?>) parent).getLeftHandOperand() == e |
108 |
| - ? OperatorHelper.OperatorAssociativity.LEFT |
109 |
| - : OperatorHelper.OperatorAssociativity.RIGHT; |
110 |
| - } else { |
111 |
| - return OperatorHelper.OperatorAssociativity.NONE; |
112 |
| - } |
113 |
| - } |
| 104 | + private static OperatorHelper.OperatorAssociativity getPositionInParent(CtElement e) { |
| 105 | + CtElement parent = e.getParent(); |
| 106 | + if (parent instanceof CtBinaryOperator) { |
| 107 | + return ((CtBinaryOperator<?>) parent).getLeftHandOperand() == e |
| 108 | + ? OperatorHelper.OperatorAssociativity.LEFT |
| 109 | + : OperatorHelper.OperatorAssociativity.RIGHT; |
| 110 | + } else { |
| 111 | + return OperatorHelper.OperatorAssociativity.NONE; |
| 112 | + } |
| 113 | + } |
114 | 114 | }
|
0 commit comments