PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/utils/ParenthesesUtils.java

http://github.com/JetBrains/intellij-community
Java | 153 lines | 122 code | 16 blank | 15 comment | 12 complexity | 3521dce1ba84766ad300d3567371b1dc MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. /*
  2. * Copyright 2000-2012 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jetbrains.plugins.groovy.intentions.utils;
  17. import com.intellij.psi.tree.IElementType;
  18. import org.jetbrains.annotations.NotNull;
  19. import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
  20. import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
  21. import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.*;
  22. import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral;
  23. import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. public class ParenthesesUtils {
  27. private ParenthesesUtils() {
  28. }
  29. private static final int PARENTHESIZED_PRECEDENCE;
  30. private static final int LITERAL_PRECEDENCE;
  31. public static final int METHOD_CALL_PRECEDENCE;
  32. private static final int POSTFIX_PRECEDENCE;
  33. public static final int PREFIX_PRECEDENCE;
  34. public static final int TYPE_CAST_PRECEDENCE;
  35. public static final int EXPONENTIAL_PRECEDENCE;
  36. public static final int MULTIPLICATIVE_PRECEDENCE;
  37. private static final int ADDITIVE_PRECEDENCE;
  38. private static final int SHIFT_PRECEDENCE;
  39. public static final int INSTANCEOF_PRECEDENCE;
  40. private static final int RELATIONAL_PRECEDENCE;
  41. public static final int EQUALITY_PRECEDENCE;
  42. private static final int BINARY_AND_PRECEDENCE;
  43. private static final int BINARY_XOR_PRECEDENCE;
  44. private static final int BINARY_OR_PRECEDENCE;
  45. public static final int AND_PRECEDENCE;
  46. public static final int OR_PRECEDENCE;
  47. public static final int CONDITIONAL_PRECEDENCE;
  48. private static final int ASSIGNMENT_PRECEDENCE;
  49. private static final int NUM_PRECEDENCES;
  50. static {
  51. int i = 0;
  52. PARENTHESIZED_PRECEDENCE = 0;
  53. LITERAL_PRECEDENCE = i++;
  54. METHOD_CALL_PRECEDENCE = i++;
  55. POSTFIX_PRECEDENCE = i++;
  56. PREFIX_PRECEDENCE = i++;
  57. TYPE_CAST_PRECEDENCE = i++;
  58. EXPONENTIAL_PRECEDENCE = i++;
  59. MULTIPLICATIVE_PRECEDENCE = i++;
  60. ADDITIVE_PRECEDENCE = i++;
  61. SHIFT_PRECEDENCE = i++;
  62. INSTANCEOF_PRECEDENCE = i++;
  63. RELATIONAL_PRECEDENCE = i++;
  64. EQUALITY_PRECEDENCE = i++;
  65. BINARY_AND_PRECEDENCE = i++;
  66. BINARY_XOR_PRECEDENCE = i++;
  67. BINARY_OR_PRECEDENCE = i++;
  68. AND_PRECEDENCE = i++;
  69. OR_PRECEDENCE = i++;
  70. CONDITIONAL_PRECEDENCE = i++;
  71. ASSIGNMENT_PRECEDENCE = i++;
  72. NUM_PRECEDENCES = i;
  73. }
  74. private static final Map<IElementType, Integer> s_binaryOperatorPrecedence = new HashMap<IElementType, Integer>(NUM_PRECEDENCES);
  75. static {
  76. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mPLUS, ADDITIVE_PRECEDENCE);
  77. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mMINUS, ADDITIVE_PRECEDENCE);
  78. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mSTAR, MULTIPLICATIVE_PRECEDENCE);
  79. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mDIV, MULTIPLICATIVE_PRECEDENCE);
  80. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mMOD, MULTIPLICATIVE_PRECEDENCE);
  81. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mSTAR_STAR, EXPONENTIAL_PRECEDENCE);
  82. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mLAND, AND_PRECEDENCE);
  83. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mLOR, OR_PRECEDENCE);
  84. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mBAND, BINARY_AND_PRECEDENCE);
  85. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mBOR, BINARY_OR_PRECEDENCE);
  86. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mBXOR, BINARY_XOR_PRECEDENCE);
  87. s_binaryOperatorPrecedence.put(GroovyElementTypes.COMPOSITE_LSHIFT_SIGN, SHIFT_PRECEDENCE);
  88. s_binaryOperatorPrecedence.put(GroovyElementTypes.COMPOSITE_RSHIFT_SIGN, SHIFT_PRECEDENCE);
  89. s_binaryOperatorPrecedence.put(GroovyElementTypes.COMPOSITE_TRIPLE_SHIFT_SIGN, SHIFT_PRECEDENCE);
  90. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mGT, RELATIONAL_PRECEDENCE);
  91. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mGE, RELATIONAL_PRECEDENCE);
  92. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mLT, RELATIONAL_PRECEDENCE);
  93. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mLE, RELATIONAL_PRECEDENCE);
  94. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mEQUAL, EQUALITY_PRECEDENCE);
  95. s_binaryOperatorPrecedence.put(GroovyTokenTypes.kIN, RELATIONAL_PRECEDENCE);
  96. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mNOT_EQUAL, EQUALITY_PRECEDENCE);
  97. s_binaryOperatorPrecedence.put(GroovyTokenTypes.mCOMPARE_TO, EQUALITY_PRECEDENCE);
  98. }
  99. public static int getPrecedence(GrExpression expression) {
  100. if (expression instanceof GrLiteral) {
  101. return LITERAL_PRECEDENCE;
  102. }
  103. if (expression instanceof GrReferenceExpression) {
  104. final GrReferenceExpression referenceExpression = (GrReferenceExpression)expression;
  105. return referenceExpression.getQualifierExpression() == null ? LITERAL_PRECEDENCE : METHOD_CALL_PRECEDENCE;
  106. }
  107. if (expression instanceof GrMethodCallExpression) {
  108. return METHOD_CALL_PRECEDENCE;
  109. }
  110. if (expression instanceof GrTypeCastExpression || expression instanceof GrNewExpression) {
  111. return TYPE_CAST_PRECEDENCE;
  112. }
  113. if (expression instanceof GrUnaryExpression) {
  114. return ((GrUnaryExpression)expression).isPostfix() ? POSTFIX_PRECEDENCE : PREFIX_PRECEDENCE;
  115. }
  116. if (expression instanceof GrBinaryExpression) {
  117. final IElementType sign = ((GrBinaryExpression)expression).getOperationTokenType();
  118. return precedenceForBinaryOperator(sign);
  119. }
  120. if (expression instanceof GrConditionalExpression) {
  121. return CONDITIONAL_PRECEDENCE;
  122. }
  123. if (expression instanceof GrAssignmentExpression) {
  124. return ASSIGNMENT_PRECEDENCE;
  125. }
  126. if (expression instanceof GrParenthesizedExpression) {
  127. return PARENTHESIZED_PRECEDENCE;
  128. }
  129. if (expression instanceof GrInstanceOfExpression) {
  130. return INSTANCEOF_PRECEDENCE;
  131. }
  132. return -1;
  133. }
  134. private static int precedenceForBinaryOperator(@NotNull IElementType sign) {
  135. return s_binaryOperatorPrecedence.get(sign);
  136. }
  137. }