1 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 ; 28 29 32 public class JsSymbolResolver { 33 34 37 private class JsResolveSymbolsVisitor extends JsVisitor { 38 39 private final Stack scopeStack = new Stack (); 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 ident = x.getIdent(); 56 if (x.getQualifier() == null) { 57 name = getScope().findExistingName(ident); 58 if (name == null) { 59 name = program.getRootScope().declareName(ident); 61 name.setObfuscatable(false); 62 } 63 } else { 64 name = program.getObjectScope().findExistingName(ident); 65 if (name == null) { 66 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 |