KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > js > JsPrecedenceVisitor


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.js;
17
18 import com.google.gwt.dev.js.ast.JsArrayAccess;
19 import com.google.gwt.dev.js.ast.JsArrayLiteral;
20 import com.google.gwt.dev.js.ast.JsBinaryOperation;
21 import com.google.gwt.dev.js.ast.JsBlock;
22 import com.google.gwt.dev.js.ast.JsBooleanLiteral;
23 import com.google.gwt.dev.js.ast.JsBreak;
24 import com.google.gwt.dev.js.ast.JsCase;
25 import com.google.gwt.dev.js.ast.JsCatch;
26 import com.google.gwt.dev.js.ast.JsConditional;
27 import com.google.gwt.dev.js.ast.JsContext;
28 import com.google.gwt.dev.js.ast.JsContinue;
29 import com.google.gwt.dev.js.ast.JsDebugger;
30 import com.google.gwt.dev.js.ast.JsDecimalLiteral;
31 import com.google.gwt.dev.js.ast.JsDefault;
32 import com.google.gwt.dev.js.ast.JsDoWhile;
33 import com.google.gwt.dev.js.ast.JsEmpty;
34 import com.google.gwt.dev.js.ast.JsExprStmt;
35 import com.google.gwt.dev.js.ast.JsExpression;
36 import com.google.gwt.dev.js.ast.JsFor;
37 import com.google.gwt.dev.js.ast.JsForIn;
38 import com.google.gwt.dev.js.ast.JsFunction;
39 import com.google.gwt.dev.js.ast.JsIf;
40 import com.google.gwt.dev.js.ast.JsIntegralLiteral;
41 import com.google.gwt.dev.js.ast.JsInvocation;
42 import com.google.gwt.dev.js.ast.JsLabel;
43 import com.google.gwt.dev.js.ast.JsNameRef;
44 import com.google.gwt.dev.js.ast.JsNew;
45 import com.google.gwt.dev.js.ast.JsNullLiteral;
46 import com.google.gwt.dev.js.ast.JsObjectLiteral;
47 import com.google.gwt.dev.js.ast.JsParameter;
48 import com.google.gwt.dev.js.ast.JsPostfixOperation;
49 import com.google.gwt.dev.js.ast.JsPrefixOperation;
50 import com.google.gwt.dev.js.ast.JsProgram;
51 import com.google.gwt.dev.js.ast.JsPropertyInitializer;
52 import com.google.gwt.dev.js.ast.JsRegExp;
53 import com.google.gwt.dev.js.ast.JsReturn;
54 import com.google.gwt.dev.js.ast.JsStringLiteral;
55 import com.google.gwt.dev.js.ast.JsSwitch;
56 import com.google.gwt.dev.js.ast.JsThisRef;
57 import com.google.gwt.dev.js.ast.JsThrow;
58 import com.google.gwt.dev.js.ast.JsTry;
59 import com.google.gwt.dev.js.ast.JsVars;
60 import com.google.gwt.dev.js.ast.JsVisitor;
61 import com.google.gwt.dev.js.ast.JsWhile;
62 import com.google.gwt.dev.js.ast.JsVars.JsVar;
63
64 /**
65  * Precedence indices from "JavaScript - The Definitive Guide" 4th Edition (page
66  * 57)
67  *
68  * Precedence 16 is for indivisible primaries that either don't have children,
69  * or provide their own delimiters.
70  *
71  * Precedence 15 is for really important things that have their own AST classes.
72  *
73  * Precedence 14 is for unary operators.
74  *
75  * Precedences 12 through 4 are for non-assigning binary operators.
76  *
77  * Precedence 3 is for the tertiary conditional.
78  *
79  * Precedence 2 is for assignments.
80  *
81  * Precedence 1 is for comma operations.
82  */

83 class JsPrecedenceVisitor extends JsVisitor {
84
85   public static int exec(JsExpression expression) {
86     JsPrecedenceVisitor visitor = new JsPrecedenceVisitor();
87     visitor.accept(expression);
88     if (visitor.answer < 0) {
89       throw new RuntimeException JavaDoc("Precedence must be >= 0!");
90     }
91     return visitor.answer;
92   }
93
94   private int answer = -1;
95
96   private JsPrecedenceVisitor() {
97   }
98
99   public boolean visit(JsArrayAccess x, JsContext ctx) {
100     answer = 15;
101     return false;
102   }
103
104   public boolean visit(JsArrayLiteral x, JsContext ctx) {
105     answer = 16; // primary
106
return false;
107   }
108
109   public boolean visit(JsBinaryOperation x, JsContext ctx) {
110     answer = x.getOperator().getPrecedence();
111     return false;
112   }
113
114   public boolean visit(JsBlock x, JsContext ctx) {
115     throw new RuntimeException JavaDoc("Only expressions have precedence.");
116   }
117
118   public boolean visit(JsBooleanLiteral x, JsContext ctx) {
119     answer = 16; // primary
120
return false;
121   }
122
123   public boolean visit(JsBreak x, JsContext ctx) {
124     throw new RuntimeException JavaDoc("Only expressions have precedence.");
125   }
126
127   public boolean visit(JsCase x, JsContext ctx) {
128     throw new RuntimeException JavaDoc("Only expressions have precedence.");
129   }
130
131   public boolean visit(JsCatch x, JsContext ctx) {
132     throw new RuntimeException JavaDoc("Only expressions have precedence.");
133   }
134
135   public boolean visit(JsConditional x, JsContext ctx) {
136     answer = 3;
137     return false;
138   }
139
140   public boolean visit(JsContinue x, JsContext ctx) {
141     throw new RuntimeException JavaDoc("Only expressions have precedence.");
142   }
143
144   public boolean visit(JsDebugger x, JsContext ctx) {
145     throw new RuntimeException JavaDoc("Only expressions have precedence.");
146   }
147
148   public boolean visit(JsDecimalLiteral x, JsContext ctx) {
149     answer = 16; // primary
150
return false;
151   }
152
153   public boolean visit(JsDefault x, JsContext ctx) {
154     throw new RuntimeException JavaDoc("Only expressions have precedence.");
155   }
156
157   public boolean visit(JsDoWhile x, JsContext ctx) {
158     throw new RuntimeException JavaDoc("Only expressions have precedence.");
159   }
160
161   public boolean visit(JsEmpty x, JsContext ctx) {
162     throw new RuntimeException JavaDoc("Only expressions have precedence.");
163   }
164
165   public boolean visit(JsExprStmt x, JsContext ctx) {
166     throw new RuntimeException JavaDoc("Only expressions have precedence.");
167   }
168
169   public boolean visit(JsFor x, JsContext ctx) {
170     throw new RuntimeException JavaDoc("Only expressions have precedence.");
171   }
172
173   public boolean visit(JsForIn x, JsContext ctx) {
174     throw new RuntimeException JavaDoc("Only expressions have precedence.");
175   }
176
177   public boolean visit(JsFunction x, JsContext ctx) {
178     answer = 16; // primary
179
return false;
180   }
181
182   public boolean visit(JsIf x, JsContext ctx) {
183     throw new RuntimeException JavaDoc("Only expressions have precedence.");
184   }
185
186   public boolean visit(JsIntegralLiteral x, JsContext ctx) {
187     answer = 16; // primary
188
return false;
189   }
190
191   public boolean visit(JsInvocation x, JsContext ctx) {
192     answer = 15;
193     return false;
194   }
195
196   public boolean visit(JsLabel x, JsContext ctx) {
197     throw new RuntimeException JavaDoc("Only expressions have precedence.");
198   }
199
200   public boolean visit(JsNameRef x, JsContext ctx) {
201     if (x.isLeaf()) {
202       answer = 16; // primary
203
} else {
204       answer = 15; // property access
205
}
206     return false;
207   }
208
209   public boolean visit(JsNew x, JsContext ctx) {
210     answer = 15;
211     return false;
212   }
213
214   public boolean visit(JsNullLiteral x, JsContext ctx) {
215     answer = 16; // primary
216
return false;
217   }
218
219   public boolean visit(JsObjectLiteral x, JsContext ctx) {
220     answer = 16; // primary
221
return false;
222   }
223
224   public boolean visit(JsParameter x, JsContext ctx) {
225     throw new RuntimeException JavaDoc("Only expressions have precedence.");
226   }
227
228   public boolean visit(JsPostfixOperation x, JsContext ctx) {
229     answer = x.getOperator().getPrecedence();
230     return false;
231   }
232
233   public boolean visit(JsPrefixOperation x, JsContext ctx) {
234     answer = x.getOperator().getPrecedence();
235     return false;
236   }
237
238   public boolean visit(JsProgram x, JsContext ctx) {
239     throw new RuntimeException JavaDoc("Only expressions have precedence.");
240   }
241
242   public boolean visit(JsPropertyInitializer x, JsContext ctx) {
243     answer = 16; // primary
244
return false;
245   }
246
247   public boolean visit(JsRegExp x, JsContext ctx) {
248     answer = 16; // primary
249
return false;
250   }
251
252   public boolean visit(JsReturn x, JsContext ctx) {
253     throw new RuntimeException JavaDoc("Only expressions have precedence.");
254   }
255
256   public boolean visit(JsStringLiteral x, JsContext ctx) {
257     answer = 16; // primary
258
return false;
259   }
260
261   public boolean visit(JsSwitch x, JsContext ctx) {
262     throw new RuntimeException JavaDoc("Only expressions have precedence.");
263   }
264
265   public boolean visit(JsThisRef x, JsContext ctx) {
266     answer = 16; // primary
267
return false;
268   }
269
270   public boolean visit(JsThrow x, JsContext ctx) {
271     throw new RuntimeException JavaDoc("Only expressions have precedence.");
272   }
273
274   public boolean visit(JsTry x, JsContext ctx) {
275     throw new RuntimeException JavaDoc("Only expressions have precedence.");
276   }
277
278   public boolean visit(JsVar x, JsContext ctx) {
279     throw new RuntimeException JavaDoc("Only expressions have precedence.");
280   }
281
282   public boolean visit(JsVars x, JsContext ctx) {
283     throw new RuntimeException JavaDoc("Only expressions have precedence.");
284   }
285
286   public boolean visit(JsWhile x, JsContext ctx) {
287     throw new RuntimeException JavaDoc("Only expressions have precedence.");
288   }
289
290 }
291
Popular Tags