1 16 package com.google.gwt.dev.jjs.impl; 17 18 import com.google.gwt.dev.jjs.InternalCompilerException; 19 import com.google.gwt.dev.jjs.ast.Context; 20 import com.google.gwt.dev.jjs.ast.JAbsentArrayDimension; 21 import com.google.gwt.dev.jjs.ast.JArrayRef; 22 import com.google.gwt.dev.jjs.ast.JBinaryOperation; 23 import com.google.gwt.dev.jjs.ast.JBooleanLiteral; 24 import com.google.gwt.dev.jjs.ast.JCastOperation; 25 import com.google.gwt.dev.jjs.ast.JCharLiteral; 26 import com.google.gwt.dev.jjs.ast.JClassLiteral; 27 import com.google.gwt.dev.jjs.ast.JConditional; 28 import com.google.gwt.dev.jjs.ast.JDoubleLiteral; 29 import com.google.gwt.dev.jjs.ast.JExpression; 30 import com.google.gwt.dev.jjs.ast.JFieldRef; 31 import com.google.gwt.dev.jjs.ast.JFloatLiteral; 32 import com.google.gwt.dev.jjs.ast.JInstanceOf; 33 import com.google.gwt.dev.jjs.ast.JIntLiteral; 34 import com.google.gwt.dev.jjs.ast.JLocalRef; 35 import com.google.gwt.dev.jjs.ast.JLongLiteral; 36 import com.google.gwt.dev.jjs.ast.JMethodCall; 37 import com.google.gwt.dev.jjs.ast.JNewArray; 38 import com.google.gwt.dev.jjs.ast.JNewInstance; 39 import com.google.gwt.dev.jjs.ast.JNullLiteral; 40 import com.google.gwt.dev.jjs.ast.JParameterRef; 41 import com.google.gwt.dev.jjs.ast.JPostfixOperation; 42 import com.google.gwt.dev.jjs.ast.JPrefixOperation; 43 import com.google.gwt.dev.jjs.ast.JStringLiteral; 44 import com.google.gwt.dev.jjs.ast.JThisRef; 45 import com.google.gwt.dev.jjs.ast.JVisitor; 46 47 54 class JavaPrecedenceVisitor extends JVisitor { 55 56 public static int exec(JExpression expression) { 57 JavaPrecedenceVisitor visitor = new JavaPrecedenceVisitor(); 58 visitor.accept(expression); 59 if (visitor.answer < 0) { 60 throw new InternalCompilerException("Precedence must be >= 0!"); 61 } 62 return visitor.answer; 63 } 64 65 private int answer = -1; 66 67 private JavaPrecedenceVisitor() { 68 } 69 70 public boolean visit(JAbsentArrayDimension x, Context ctx) { 72 answer = 0; 73 return false; 74 } 75 76 public boolean visit(JArrayRef x, Context ctx) { 78 answer = 0; 79 return false; 80 } 81 82 public boolean visit(JBinaryOperation operation, Context ctx) { 84 answer = operation.getOp().getPrecedence(); 85 return false; 86 } 87 88 public boolean visit(JBooleanLiteral x, Context ctx) { 90 answer = 0; 91 return false; 92 } 93 94 public boolean visit(JCastOperation operation, Context ctx) { 96 answer = 2; 97 return false; 98 } 99 100 public boolean visit(JCharLiteral x, Context ctx) { 102 answer = 0; 103 return false; 104 } 105 106 public boolean visit(JClassLiteral x, Context ctx) { 108 answer = 0; 109 return false; 110 } 111 112 public boolean visit(JConditional conditional, Context ctx) { 114 answer = 13; 115 return false; 116 } 117 118 public boolean visit(JDoubleLiteral x, Context ctx) { 120 answer = 0; 121 return false; 122 } 123 124 public boolean visit(JFieldRef x, Context ctx) { 126 answer = 0; 127 return false; 128 } 129 130 public boolean visit(JFloatLiteral x, Context ctx) { 132 answer = 0; 133 return false; 134 } 135 136 public boolean visit(JInstanceOf of, Context ctx) { 138 answer = 6; 139 return false; 140 } 141 142 public boolean visit(JIntLiteral x, Context ctx) { 144 answer = 0; 145 return false; 146 } 147 148 public boolean visit(JLocalRef x, Context ctx) { 150 answer = 0; 151 return false; 152 } 153 154 public boolean visit(JLongLiteral x, Context ctx) { 156 answer = 0; 157 return false; 158 } 159 160 public boolean visit(JMethodCall x, Context ctx) { 162 answer = 0; 163 return false; 164 } 165 166 public boolean visit(JNewArray array, Context ctx) { 168 answer = 2; 169 return false; 170 } 171 172 public boolean visit(JNewInstance instance, Context ctx) { 174 answer = 2; 175 return false; 176 } 177 178 public boolean visit(JNullLiteral x, Context ctx) { 180 answer = 0; 181 return false; 182 } 183 184 public boolean visit(JParameterRef x, Context ctx) { 186 answer = 0; 187 return false; 188 } 189 190 public boolean visit(JPostfixOperation operation, Context ctx) { 192 answer = 0; 193 return false; 194 } 195 196 public boolean visit(JPrefixOperation operation, Context ctx) { 198 answer = 1; 199 return false; 200 } 201 202 public boolean visit(JStringLiteral x, Context ctx) { 204 answer = 0; 205 return false; 206 } 207 208 public boolean visit(JThisRef x, Context ctx) { 210 answer = 0; 211 return false; 212 } 213 214 } | Popular Tags |