| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript.ast; |
| 8 |
|
| 9 |
import org.mozilla.javascript.Node; |
| 10 |
import org.mozilla.javascript.Token; |
| 11 |
|
| 12 |
import java.util.ArrayList; |
| 13 |
import java.util.Collections; |
| 14 |
import java.util.LinkedHashMap; |
| 15 |
import java.util.List; |
| 16 |
import java.util.Map; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class Scope extends Jump { |
| 23 |
|
| 24 |
|
| 25 |
protected Map<String,Symbol> symbolTable; |
| 26 |
protected Scope parentScope; |
| 27 |
protected ScriptNode top; |
| 28 |
|
| 29 |
private List<Scope> childScopes; |
| 30 |
|
| 31 |
{ |
| 32 |
this.type = Token.BLOCK; |
| 33 |
} |
| 34 |
|
| 35 |
public Scope() { |
| 36 |
} |
| 37 |
|
| 38 |
public Scope(int pos) { |
| 39 |
this.position = pos; |
| 40 |
} |
| 41 |
|
| 42 |
public Scope(int pos, int len) { |
| 43 |
this(pos); |
| 44 |
this.length = len; |
| 45 |
} |
| 46 |
|
| 47 |
public Scope getParentScope() { |
| 48 |
return parentScope; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public void setParentScope(Scope parentScope) { |
| 55 |
this.parentScope = parentScope; |
| 56 |
this.top = parentScope == null ? (ScriptNode)this : parentScope.top; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public void clearParentScope() { |
| 63 |
this.parentScope = null; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public List<Scope> getChildScopes() { |
| 71 |
return childScopes; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public void addChildScope(Scope child) { |
| 81 |
if (childScopes == null) { |
| 82 |
childScopes = new ArrayList<Scope>(); |
| 83 |
} |
| 84 |
childScopes.add(child); |
| 85 |
child.setParentScope(this); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public void replaceWith(Scope newScope) { |
| 98 |
if (childScopes != null) { |
| 99 |
for (Scope kid : childScopes) { |
| 100 |
newScope.addChildScope(kid); |
| 101 |
} |
| 102 |
childScopes.clear(); |
| 103 |
childScopes = null; |
| 104 |
} |
| 105 |
if (symbolTable != null && !symbolTable.isEmpty()) { |
| 106 |
joinScopes(this, newScope); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public ScriptNode getTop() { |
| 114 |
return top; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public void setTop(ScriptNode top) { |
| 121 |
this.top = top; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static Scope splitScope(Scope scope) { |
| 131 |
Scope result = new Scope(scope.getType()); |
| 132 |
result.symbolTable = scope.symbolTable; |
| 133 |
scope.symbolTable = null; |
| 134 |
result.parent = scope.parent; |
| 135 |
result.setParentScope(scope.getParentScope()); |
| 136 |
result.setParentScope(result); |
| 137 |
scope.parent = result; |
| 138 |
result.top = scope.top; |
| 139 |
return result; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public static void joinScopes(Scope source, Scope dest) { |
| 146 |
Map<String,Symbol> src = source.ensureSymbolTable(); |
| 147 |
Map<String,Symbol> dst = dest.ensureSymbolTable(); |
| 148 |
if (!Collections.disjoint(src.keySet(), dst.keySet())) { |
| 149 |
codeBug(); |
| 150 |
} |
| 151 |
for (Map.Entry<String, Symbol> entry: src.entrySet()) { |
| 152 |
Symbol sym = entry.getValue(); |
| 153 |
sym.setContainingTable(dest); |
| 154 |
dst.put(entry.getKey(), sym); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public Scope getDefiningScope(String name) { |
| 165 |
for (Scope s = this; s != null; s = s.parentScope) { |
| 166 |
Map<String,Symbol> symbolTable = s.getSymbolTable(); |
| 167 |
if (symbolTable != null && symbolTable.containsKey(name)) { |
| 168 |
return s; |
| 169 |
} |
| 170 |
} |
| 171 |
return null; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public Symbol getSymbol(String name) { |
| 180 |
return symbolTable == null ? null : symbolTable.get(name); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public void putSymbol(Symbol symbol) { |
| 187 |
if (symbol.getName() == null) |
| 188 |
throw new IllegalArgumentException("null symbol name"); |
| 189 |
ensureSymbolTable(); |
| 190 |
symbolTable.put(symbol.getName(), symbol); |
| 191 |
symbol.setContainingTable(this); |
| 192 |
top.addSymbol(symbol); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public Map<String,Symbol> getSymbolTable() { |
| 200 |
return symbolTable; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public void setSymbolTable(Map<String, Symbol> table) { |
| 207 |
symbolTable = table; |
| 208 |
} |
| 209 |
|
| 210 |
private Map<String,Symbol> ensureSymbolTable() { |
| 211 |
if (symbolTable == null) { |
| 212 |
symbolTable = new LinkedHashMap<String,Symbol>(5); |
| 213 |
} |
| 214 |
return symbolTable; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public List<AstNode> getStatements() { |
| 225 |
List<AstNode> stmts = new ArrayList<AstNode>(); |
| 226 |
Node n = getFirstChild(); |
| 227 |
while (n != null) { |
| 228 |
stmts.add((AstNode)n); |
| 229 |
n = n.getNext(); |
| 230 |
} |
| 231 |
return stmts; |
| 232 |
} |
| 233 |
|
| 234 |
@Override |
| 235 |
public String toSource(int depth) { |
| 236 |
StringBuilder sb = new StringBuilder(); |
| 237 |
sb.append(makeIndent(depth)); |
| 238 |
sb.append("{\n"); |
| 239 |
for (Node kid : this) { |
| 240 |
sb.append(((AstNode)kid).toSource(depth+1)); |
| 241 |
} |
| 242 |
sb.append(makeIndent(depth)); |
| 243 |
sb.append("}\n"); |
| 244 |
return sb.toString(); |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public void visit(NodeVisitor v) { |
| 249 |
if (v.visit(this)) { |
| 250 |
for (Node kid : this) { |
| 251 |
((AstNode)kid).visit(v); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |