|
| 1 | +/** |
| 2 | + * SPDX-License-Identifier: (MIT OR CECILL-C) |
| 3 | + * |
| 4 | + * Copyright (C) 2006-2019 INRIA and contributors |
| 5 | + * |
| 6 | + * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. |
| 7 | + */ |
| 8 | +package spoon.reflect.visitor; |
| 9 | + |
| 10 | +import spoon.reflect.code.CtBinaryOperator; |
| 11 | +import spoon.reflect.code.CtExpression; |
| 12 | +import spoon.reflect.code.CtUnaryOperator; |
| 13 | +import spoon.reflect.declaration.CtElement; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class for determining whether or not an expression requires round brackets in order to preserve |
| 17 | + * AST structure (and consequently semantics). |
| 18 | + */ |
| 19 | +class RoundBracketAnalyzer { |
| 20 | + |
| 21 | + enum EncloseInRoundBrackets { |
| 22 | + YES, NO, UNKNOWN; |
| 23 | + } |
| 24 | + |
| 25 | + private RoundBracketAnalyzer() { |
| 26 | + } |
| 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 | + } |
| 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 | + } |
| 64 | + |
| 65 | + OperatorHelper.OperatorAssociativity associativity = getOperatorAssociativity(nestedOperator); |
| 66 | + OperatorHelper.OperatorAssociativity positionInParent = getPositionInParent(nestedOperator); |
| 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 | + } |
| 75 | + |
| 76 | + private static boolean isNestedOperator(CtElement e) { |
| 77 | + return e.isParentInitialized() && isOperator(e) && isOperator(e.getParent()); |
| 78 | + } |
| 79 | + |
| 80 | + private static boolean isOperator(CtElement e) { |
| 81 | + return e instanceof CtBinaryOperator || e instanceof CtUnaryOperator; |
| 82 | + } |
| 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 | + } |
| 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 | + } |
| 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 | + } |
| 114 | +} |
0 commit comments