1 /******************************************************************************* 2 * Copyright (c) 2000, 2018 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jdt.internal.core.manipulation.dom; 15 16 import org.eclipse.jdt.core.dom.ArrayAccess; 17 import org.eclipse.jdt.core.dom.Assignment; 18 import org.eclipse.jdt.core.dom.CastExpression; 19 import org.eclipse.jdt.core.dom.ClassInstanceCreation; 20 import org.eclipse.jdt.core.dom.ConditionalExpression; 21 import org.eclipse.jdt.core.dom.Expression; 22 import org.eclipse.jdt.core.dom.FieldAccess; 23 import org.eclipse.jdt.core.dom.InfixExpression; 24 import org.eclipse.jdt.core.dom.InfixExpression.Operator; 25 import org.eclipse.jdt.core.dom.InstanceofExpression; 26 import org.eclipse.jdt.core.dom.MethodInvocation; 27 import org.eclipse.jdt.core.dom.PostfixExpression; 28 import org.eclipse.jdt.core.dom.PrefixExpression; 29 30 public class OperatorPrecedence { 31 32 private static final int ASSIGNMENT= 0; 33 private static final int CONDITIONAL= 1; 34 private static final int CONDITIONAL_OR= 2; 35 private static final int CONDITIONAL_AND= 3; 36 private static final int BITWISE_INCLUSIVE_OR= 4; 37 private static final int BITWISE_EXCLUSIVE_OR= 5; 38 private static final int BITWISE_AND= 6; 39 private static final int EQUALITY= 7; 40 private static final int RELATIONAL= 8; 41 private static final int SHIFT= 9; 42 private static final int ADDITIVE= 10; 43 private static final int MULTIPLICATIVE= 11; 44 private static final int TYPEGENERATION= 12; 45 private static final int PREFIX= 13; 46 private static final int POSTFIX= 14; 47 48 /** 49 * Returns the precedence of the expression. Expression 50 * with higher precedence are executed before expressions 51 * with lower precedence. 52 * i.e. in: 53 * <br><code> int a= ++3--;</code></br> 54 * 55 * the precedence order is 56 * <ul> 57 * <li>3</li> 58 * <li>++</li> 59 * <li>--</li> 60 * <li>=</li> 61 * </ul> 62 * 1. 3 -(++)-> 4<br> 63 * 2. 4 -(--)-> 3<br> 64 * 3. 3 -(=)-> a<br> 65 * 66 * @param expression the expression to determine the precedence for 67 * @return the precedence the higher to stronger the binding to its operand(s) 68 */ getExpressionPrecedence(Expression expression)69 public static int getExpressionPrecedence(Expression expression) { 70 if (expression instanceof InfixExpression) { 71 return getOperatorPrecedence(((InfixExpression)expression).getOperator()); 72 } else if (expression instanceof Assignment) { 73 return ASSIGNMENT; 74 } else if (expression instanceof ConditionalExpression) { 75 return CONDITIONAL; 76 } else if (expression instanceof InstanceofExpression) { 77 return RELATIONAL; 78 } else if (expression instanceof CastExpression) { 79 return TYPEGENERATION; 80 } else if (expression instanceof ClassInstanceCreation) { 81 return POSTFIX; 82 } else if (expression instanceof PrefixExpression) { 83 return PREFIX; 84 } else if (expression instanceof FieldAccess) { 85 return POSTFIX; 86 } else if (expression instanceof MethodInvocation) { 87 return POSTFIX; 88 } else if (expression instanceof ArrayAccess) { 89 return POSTFIX; 90 } else if (expression instanceof PostfixExpression) { 91 return POSTFIX; 92 } 93 return Integer.MAX_VALUE; 94 } 95 96 /** 97 * Returns the precedence of an infix operator. Operators 98 * with higher precedence are executed before expressions 99 * with lower precedence. 100 * <br> 101 * i.e. in: <br> 102 * <code>3 + 4 - 5 * 6;</code><br> 103 * the precedence order is 104 * <ul> 105 * <li>*</li> 106 * <li>+</li> 107 * <li>-</li> 108 * </ul> 109 * 1. 5,6 -(*)-> 30<br> 110 * 2. 3,4 -(+)-> 7<br> 111 * 3. 7,30 -(-)-> -23<br> 112 * 113 * @param operator the expression to determine the precedence for 114 * @return the precedence the higher to stronger the binding to its operands 115 */ getOperatorPrecedence(Operator operator)116 public static int getOperatorPrecedence(Operator operator) { 117 if (operator == Operator.CONDITIONAL_OR) { 118 return CONDITIONAL_OR; 119 } else if (operator == Operator.CONDITIONAL_AND) { 120 return CONDITIONAL_AND; 121 } else if (operator == Operator.OR) { 122 return BITWISE_INCLUSIVE_OR; 123 } else if (operator == Operator.XOR) { 124 return BITWISE_EXCLUSIVE_OR; 125 } else if (operator == Operator.AND) { 126 return BITWISE_AND; 127 } else if (operator == Operator.EQUALS || operator == Operator.NOT_EQUALS) { 128 return EQUALITY; 129 } else if (operator == Operator.LESS || operator == Operator.LESS_EQUALS || operator == Operator.GREATER || operator == Operator.GREATER_EQUALS) { 130 return RELATIONAL; 131 } else if (operator == Operator.LEFT_SHIFT || operator == Operator.RIGHT_SHIFT_SIGNED || operator == Operator.RIGHT_SHIFT_UNSIGNED) { 132 return SHIFT; 133 } else if (operator == Operator.PLUS || operator == Operator.MINUS) { 134 return ADDITIVE; 135 } else if (operator == Operator.REMAINDER || operator == Operator.DIVIDE || operator == Operator.TIMES) { 136 return MULTIPLICATIVE; 137 } 138 return Integer.MAX_VALUE; 139 } 140 OperatorPrecedence()141 private OperatorPrecedence() { 142 } 143 } 144