1 16 package com.google.gwt.dev.js.ast; 17 18 import com.google.gwt.dev.js.JsKeywords; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.TreeMap ; 25 26 51 public class JsScope { 52 53 56 private static String maybeMangleKeyword(String ident) { 57 if (JsKeywords.isKeyword(ident)) { 58 ident = ident + "_$"; 59 } 60 return ident; 61 } 62 63 private final List children = new ArrayList (); 64 private final String description; 65 private final Map names = new TreeMap (); 66 private final JsScope parent; 67 68 71 public JsScope(JsScope parent, String description) { 72 assert (parent != null); 73 this.description = description; 74 this.parent = parent; 75 this.parent.children.add(this); 76 } 77 78 81 protected JsScope(String description) { 82 this.description = description; 83 this.parent = null; 84 } 85 86 92 public JsName declareName(String ident) { 93 ident = maybeMangleKeyword(ident); 94 JsName name = findExistingNameNoRecurse(ident); 95 if (name != null) { 96 return name; 97 } 98 return doCreateName(ident, ident); 99 } 100 101 110 public JsName declareName(String ident, String shortIdent) { 111 ident = maybeMangleKeyword(ident); 112 shortIdent = maybeMangleKeyword(shortIdent); 113 JsName name = findExistingNameNoRecurse(ident); 114 if (name != null) { 115 if (!name.getShortIdent().equals(shortIdent)) { 116 throw new IllegalArgumentException ("Requested short name " + shortIdent 117 + " conflicts with preexisting short name " + name.getShortIdent() 118 + " for identifier " + ident); 119 } 120 return name; 121 } 122 return doCreateName(ident, shortIdent); 123 } 124 125 131 public final JsName findExistingName(String ident) { 132 ident = maybeMangleKeyword(ident); 133 JsName name = findExistingNameNoRecurse(ident); 134 if (name == null && parent != null) { 135 return parent.findExistingName(ident); 136 } 137 return name; 138 } 139 140 146 public final JsName findExistingUnobfuscatableName(String ident) { 147 ident = maybeMangleKeyword(ident); 148 JsName name = findExistingNameNoRecurse(ident); 149 if (name != null && name.isObfuscatable()) { 150 name = null; 151 } 152 if (name == null && parent != null) { 153 return parent.findExistingUnobfuscatableName(ident); 154 } 155 return name; 156 } 157 158 161 public Iterator getAllNames() { 162 return names.values().iterator(); 163 } 164 165 168 public final List getChildren() { 169 return children; 170 } 171 172 176 public final JsScope getParent() { 177 return parent; 178 } 179 180 183 public JsProgram getProgram() { 184 assert (parent != null) : "Subclasses must override getProgram() if they do not set a parent"; 185 return parent.getProgram(); 186 } 187 188 public final String toString() { 189 if (parent != null) { 190 return description + "->" + parent; 191 } else { 192 return description; 193 } 194 } 195 196 199 protected JsName doCreateName(String ident, String shortIdent) { 200 JsName name = new JsName(ident, shortIdent); 201 names.put(ident, name); 202 return name; 203 } 204 205 211 protected JsName findExistingNameNoRecurse(String ident) { 212 return (JsName) names.get(ident); 213 } 214 215 } 216 | Popular Tags |