01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.JsCatch;
19: import com.google.gwt.dev.js.ast.JsContext;
20: import com.google.gwt.dev.js.ast.JsExpression;
21: import com.google.gwt.dev.js.ast.JsFunction;
22: import com.google.gwt.dev.js.ast.JsNameRef;
23: import com.google.gwt.dev.js.ast.JsProgram;
24: import com.google.gwt.dev.js.ast.JsScope;
25: import com.google.gwt.dev.js.ast.JsVisitor;
26:
27: import java.util.Stack;
28:
29: /**
30: * Base class for any recursive resolver classes.
31: */
32: public abstract class JsAbstractSymbolResolver extends JsVisitor {
33:
34: private final Stack<JsScope> scopeStack = new Stack<JsScope>();
35:
36: @Override
37: public void endVisit(JsCatch x, JsContext<JsCatch> ctx) {
38: popScope();
39: }
40:
41: @Override
42: public void endVisit(JsFunction x, JsContext<JsExpression> ctx) {
43: popScope();
44: }
45:
46: @Override
47: public void endVisit(JsNameRef x, JsContext<JsExpression> ctx) {
48: if (x.isResolved()) {
49: return;
50: }
51:
52: resolve(x);
53: }
54:
55: @Override
56: public void endVisit(JsProgram x, JsContext<JsProgram> ctx) {
57: popScope();
58: }
59:
60: @Override
61: public boolean visit(JsCatch x, JsContext<JsCatch> ctx) {
62: pushScope(x.getScope());
63: return true;
64: }
65:
66: @Override
67: public boolean visit(JsFunction x, JsContext<JsExpression> ctx) {
68: pushScope(x.getScope());
69: return true;
70: }
71:
72: @Override
73: public boolean visit(JsProgram x, JsContext<JsProgram> ctx) {
74: pushScope(x.getScope());
75: return true;
76: }
77:
78: protected JsScope getScope() {
79: return scopeStack.peek();
80: }
81:
82: protected abstract void resolve(JsNameRef x);
83:
84: private void popScope() {
85: scopeStack.pop();
86: }
87:
88: private void pushScope(JsScope scope) {
89: scopeStack.push(scope);
90: }
91: }
|