| 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.List; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public class ScriptNode extends Scope { |
| 21 |
|
| 22 |
private int encodedSourceStart = -1; |
| 23 |
private int encodedSourceEnd = -1; |
| 24 |
private String sourceName; |
| 25 |
private String encodedSource; |
| 26 |
private int endLineno = -1; |
| 27 |
|
| 28 |
private List<FunctionNode> functions; |
| 29 |
private List<RegExpLiteral> regexps; |
| 30 |
private List<FunctionNode> EMPTY_LIST = Collections.emptyList(); |
| 31 |
|
| 32 |
private List<Symbol> symbols = new ArrayList<Symbol>(4); |
| 33 |
private int paramCount = 0; |
| 34 |
private String[] variableNames; |
| 35 |
private boolean[] isConsts; |
| 36 |
|
| 37 |
private Object compilerData; |
| 38 |
private int tempNumber = 0; |
| 39 |
private boolean inStrictMode; |
| 40 |
|
| 41 |
{ |
| 42 |
|
| 43 |
this.top = this; |
| 44 |
this.type = Token.SCRIPT; |
| 45 |
} |
| 46 |
|
| 47 |
public ScriptNode() { |
| 48 |
} |
| 49 |
|
| 50 |
public ScriptNode(int pos) { |
| 51 |
super(pos); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public String getSourceName() { |
| 59 |
return sourceName; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public void setSourceName(String sourceName) { |
| 67 |
this.sourceName = sourceName; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public int getEncodedSourceStart() { |
| 75 |
return encodedSourceStart; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public void setEncodedSourceStart(int start) { |
| 83 |
this.encodedSourceStart = start; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public int getEncodedSourceEnd() { |
| 91 |
return encodedSourceEnd; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public void setEncodedSourceEnd(int end) { |
| 99 |
this.encodedSourceEnd = end; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public void setEncodedSourceBounds(int start, int end) { |
| 107 |
this.encodedSourceStart = start; |
| 108 |
this.encodedSourceEnd = end; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public void setEncodedSource(String encodedSource) { |
| 116 |
this.encodedSource = encodedSource; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public String getEncodedSource() { |
| 135 |
return encodedSource; |
| 136 |
} |
| 137 |
|
| 138 |
public int getBaseLineno() { |
| 139 |
return lineno; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public void setBaseLineno(int lineno) { |
| 148 |
if (lineno < 0 || this.lineno >= 0) codeBug(); |
| 149 |
this.lineno = lineno; |
| 150 |
} |
| 151 |
|
| 152 |
public int getEndLineno() { |
| 153 |
return endLineno; |
| 154 |
} |
| 155 |
|
| 156 |
public void setEndLineno(int lineno) { |
| 157 |
|
| 158 |
if (lineno < 0 || endLineno >= 0) codeBug(); |
| 159 |
endLineno = lineno; |
| 160 |
} |
| 161 |
|
| 162 |
public int getFunctionCount() { |
| 163 |
return functions == null ? 0 : functions.size(); |
| 164 |
} |
| 165 |
|
| 166 |
public FunctionNode getFunctionNode(int i) { |
| 167 |
return functions.get(i); |
| 168 |
} |
| 169 |
|
| 170 |
public List<FunctionNode> getFunctions() { |
| 171 |
return functions == null ? EMPTY_LIST : functions; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public int addFunction(FunctionNode fnNode) { |
| 180 |
if (fnNode == null) codeBug(); |
| 181 |
if (functions == null) |
| 182 |
functions = new ArrayList<FunctionNode>(); |
| 183 |
functions.add(fnNode); |
| 184 |
return functions.size() - 1; |
| 185 |
} |
| 186 |
|
| 187 |
public int getRegexpCount() { |
| 188 |
return regexps == null ? 0 : regexps.size(); |
| 189 |
} |
| 190 |
|
| 191 |
public String getRegexpString(int index) { |
| 192 |
return regexps.get(index).getValue(); |
| 193 |
} |
| 194 |
|
| 195 |
public String getRegexpFlags(int index) { |
| 196 |
return regexps.get(index).getFlags(); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public void addRegExp(RegExpLiteral re) { |
| 203 |
if (re == null) codeBug(); |
| 204 |
if (regexps == null) |
| 205 |
regexps = new ArrayList<RegExpLiteral>(); |
| 206 |
regexps.add(re); |
| 207 |
re.putIntProp(REGEXP_PROP, regexps.size() - 1); |
| 208 |
} |
| 209 |
|
| 210 |
public int getIndexForNameNode(Node nameNode) { |
| 211 |
if (variableNames == null) codeBug(); |
| 212 |
Scope node = nameNode.getScope(); |
| 213 |
Symbol symbol = node == null |
| 214 |
? null |
| 215 |
: node.getSymbol(((Name)nameNode).getIdentifier()); |
| 216 |
return (symbol == null) ? -1 : symbol.getIndex(); |
| 217 |
} |
| 218 |
|
| 219 |
public String getParamOrVarName(int index) { |
| 220 |
if (variableNames == null) codeBug(); |
| 221 |
return variableNames[index]; |
| 222 |
} |
| 223 |
|
| 224 |
public int getParamCount() { |
| 225 |
return paramCount; |
| 226 |
} |
| 227 |
|
| 228 |
public int getParamAndVarCount() { |
| 229 |
if (variableNames == null) codeBug(); |
| 230 |
return symbols.size(); |
| 231 |
} |
| 232 |
|
| 233 |
public String[] getParamAndVarNames() { |
| 234 |
if (variableNames == null) codeBug(); |
| 235 |
return variableNames; |
| 236 |
} |
| 237 |
|
| 238 |
public boolean[] getParamAndVarConst() { |
| 239 |
if (variableNames == null) codeBug(); |
| 240 |
return isConsts; |
| 241 |
} |
| 242 |
|
| 243 |
void addSymbol(Symbol symbol) { |
| 244 |
if (variableNames != null) codeBug(); |
| 245 |
if (symbol.getDeclType() == Token.LP) { |
| 246 |
paramCount++; |
| 247 |
} |
| 248 |
symbols.add(symbol); |
| 249 |
} |
| 250 |
|
| 251 |
public List<Symbol> getSymbols() { |
| 252 |
return symbols; |
| 253 |
} |
| 254 |
|
| 255 |
public void setSymbols(List<Symbol> symbols) { |
| 256 |
this.symbols = symbols; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public void flattenSymbolTable(boolean flattenAllTables) { |
| 268 |
if (!flattenAllTables) { |
| 269 |
List<Symbol> newSymbols = new ArrayList<Symbol>(); |
| 270 |
if (this.symbolTable != null) { |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
for (int i = 0; i < symbols.size(); i++) { |
| 275 |
Symbol symbol = symbols.get(i); |
| 276 |
if (symbol.getContainingTable() == this) { |
| 277 |
newSymbols.add(symbol); |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
symbols = newSymbols; |
| 282 |
} |
| 283 |
variableNames = new String[symbols.size()]; |
| 284 |
isConsts = new boolean[symbols.size()]; |
| 285 |
for (int i = 0; i < symbols.size(); i++) { |
| 286 |
Symbol symbol = symbols.get(i); |
| 287 |
variableNames[i] = symbol.getName(); |
| 288 |
isConsts[i] = symbol.getDeclType() == Token.CONST; |
| 289 |
symbol.setIndex(i); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
public Object getCompilerData() { |
| 294 |
return compilerData; |
| 295 |
} |
| 296 |
|
| 297 |
public void setCompilerData(Object data) { |
| 298 |
assertNotNull(data); |
| 299 |
|
| 300 |
if (compilerData != null) |
| 301 |
throw new IllegalStateException(); |
| 302 |
compilerData = data; |
| 303 |
} |
| 304 |
|
| 305 |
public String getNextTempName() { |
| 306 |
return "$" + tempNumber++; |
| 307 |
} |
| 308 |
|
| 309 |
public void setInStrictMode(boolean inStrictMode) { |
| 310 |
this.inStrictMode = inStrictMode; |
| 311 |
} |
| 312 |
|
| 313 |
public boolean isInStrictMode() { |
| 314 |
return inStrictMode; |
| 315 |
} |
| 316 |
|
| 317 |
@Override |
| 318 |
public void visit(NodeVisitor v) { |
| 319 |
if (v.visit(this)) { |
| 320 |
for (Node kid : this) { |
| 321 |
((AstNode)kid).visit(v); |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
} |