KickJava   Java API By Example, From Geeks To Geeks.

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


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.JsCatch;
19 import com.google.gwt.dev.js.ast.JsContext;
20 import com.google.gwt.dev.js.ast.JsFunction;
21 import com.google.gwt.dev.js.ast.JsName;
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 JavaDoc;
28
29 /**
30  * Resolves any unresolved JsNameRefs.
31  */

32 public class JsSymbolResolver {
33
34   /**
35    * Resolves any unresolved JsNameRefs.
36    */

37   private class JsResolveSymbolsVisitor extends JsVisitor {
38
39     private final Stack JavaDoc scopeStack = new Stack JavaDoc();
40
41     public void endVisit(JsCatch x, JsContext ctx) {
42       popScope();
43     }
44
45     public void endVisit(JsFunction x, JsContext ctx) {
46       popScope();
47     }
48
49     public void endVisit(JsNameRef x, JsContext ctx) {
50       if (x.isResolved()) {
51         return;
52       }
53
54       JsName name;
55       String JavaDoc ident = x.getIdent();
56       if (x.getQualifier() == null) {
57         name = getScope().findExistingName(ident);
58         if (name == null) {
59           // No clue what this is; create a new unobfuscatable name
60
name = program.getRootScope().declareName(ident);
61           name.setObfuscatable(false);
62         }
63       } else {
64         name = program.getObjectScope().findExistingName(ident);
65         if (name == null) {
66           // No clue what this is; create a new unobfuscatable name
67
name = program.getObjectScope().declareName(ident);
68           name.setObfuscatable(false);
69         }
70       }
71       x.resolve(name);
72     }
73
74     public void endVisit(JsProgram x, JsContext ctx) {
75       popScope();
76     }
77
78     public boolean visit(JsCatch x, JsContext ctx) {
79       pushScope(x.getScope());
80       return true;
81     }
82
83     public boolean visit(JsFunction x, JsContext ctx) {
84       pushScope(x.getScope());
85       return true;
86     }
87
88     public boolean visit(JsProgram x, JsContext ctx) {
89       pushScope(x.getScope());
90       return true;
91     }
92
93     private JsScope getScope() {
94       return (JsScope) scopeStack.peek();
95     }
96
97     private void popScope() {
98       scopeStack.pop();
99     }
100
101     private void pushScope(JsScope scope) {
102       scopeStack.push(scope);
103     }
104   }
105
106   public static void exec(JsProgram program) {
107     new JsSymbolResolver(program).execImpl();
108   }
109
110   private final JsProgram program;
111
112   private JsSymbolResolver(JsProgram program) {
113     this.program = program;
114   }
115
116   private void execImpl() {
117     JsResolveSymbolsVisitor resolver = new JsResolveSymbolsVisitor();
118     resolver.accept(program);
119   }
120 }
121
Popular Tags