| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
import org.mozilla.javascript.ast.ArrayComprehension; |
| 10 |
import org.mozilla.javascript.ast.ArrayComprehensionLoop; |
| 11 |
import org.mozilla.javascript.ast.ArrayLiteral; |
| 12 |
import org.mozilla.javascript.ast.Assignment; |
| 13 |
import org.mozilla.javascript.ast.AstNode; |
| 14 |
import org.mozilla.javascript.ast.AstRoot; |
| 15 |
import org.mozilla.javascript.ast.Block; |
| 16 |
import org.mozilla.javascript.ast.BreakStatement; |
| 17 |
import org.mozilla.javascript.ast.CatchClause; |
| 18 |
import org.mozilla.javascript.ast.Comment; |
| 19 |
import org.mozilla.javascript.ast.ConditionalExpression; |
| 20 |
import org.mozilla.javascript.ast.ContinueStatement; |
| 21 |
import org.mozilla.javascript.ast.DestructuringForm; |
| 22 |
import org.mozilla.javascript.ast.DoLoop; |
| 23 |
import org.mozilla.javascript.ast.ElementGet; |
| 24 |
import org.mozilla.javascript.ast.EmptyExpression; |
| 25 |
import org.mozilla.javascript.ast.EmptyStatement; |
| 26 |
import org.mozilla.javascript.ast.ErrorNode; |
| 27 |
import org.mozilla.javascript.ast.ExpressionStatement; |
| 28 |
import org.mozilla.javascript.ast.ForInLoop; |
| 29 |
import org.mozilla.javascript.ast.ForLoop; |
| 30 |
import org.mozilla.javascript.ast.FunctionCall; |
| 31 |
import org.mozilla.javascript.ast.FunctionNode; |
| 32 |
import org.mozilla.javascript.ast.GeneratorExpression; |
| 33 |
import org.mozilla.javascript.ast.GeneratorExpressionLoop; |
| 34 |
import org.mozilla.javascript.ast.IdeErrorReporter; |
| 35 |
import org.mozilla.javascript.ast.IfStatement; |
| 36 |
import org.mozilla.javascript.ast.InfixExpression; |
| 37 |
import org.mozilla.javascript.ast.Jump; |
| 38 |
import org.mozilla.javascript.ast.KeywordLiteral; |
| 39 |
import org.mozilla.javascript.ast.Label; |
| 40 |
import org.mozilla.javascript.ast.LabeledStatement; |
| 41 |
import org.mozilla.javascript.ast.LetNode; |
| 42 |
import org.mozilla.javascript.ast.Loop; |
| 43 |
import org.mozilla.javascript.ast.Name; |
| 44 |
import org.mozilla.javascript.ast.NewExpression; |
| 45 |
import org.mozilla.javascript.ast.NumberLiteral; |
| 46 |
import org.mozilla.javascript.ast.ObjectLiteral; |
| 47 |
import org.mozilla.javascript.ast.ObjectProperty; |
| 48 |
import org.mozilla.javascript.ast.ParenthesizedExpression; |
| 49 |
import org.mozilla.javascript.ast.PropertyGet; |
| 50 |
import org.mozilla.javascript.ast.RegExpLiteral; |
| 51 |
import org.mozilla.javascript.ast.ReturnStatement; |
| 52 |
import org.mozilla.javascript.ast.Scope; |
| 53 |
import org.mozilla.javascript.ast.ScriptNode; |
| 54 |
import org.mozilla.javascript.ast.StringLiteral; |
| 55 |
import org.mozilla.javascript.ast.SwitchCase; |
| 56 |
import org.mozilla.javascript.ast.SwitchStatement; |
| 57 |
import org.mozilla.javascript.ast.Symbol; |
| 58 |
import org.mozilla.javascript.ast.ThrowStatement; |
| 59 |
import org.mozilla.javascript.ast.TryStatement; |
| 60 |
import org.mozilla.javascript.ast.UnaryExpression; |
| 61 |
import org.mozilla.javascript.ast.VariableDeclaration; |
| 62 |
import org.mozilla.javascript.ast.VariableInitializer; |
| 63 |
import org.mozilla.javascript.ast.WhileLoop; |
| 64 |
import org.mozilla.javascript.ast.WithStatement; |
| 65 |
import org.mozilla.javascript.ast.XmlDotQuery; |
| 66 |
import org.mozilla.javascript.ast.XmlElemRef; |
| 67 |
import org.mozilla.javascript.ast.XmlExpression; |
| 68 |
import org.mozilla.javascript.ast.XmlLiteral; |
| 69 |
import org.mozilla.javascript.ast.XmlMemberGet; |
| 70 |
import org.mozilla.javascript.ast.XmlPropRef; |
| 71 |
import org.mozilla.javascript.ast.XmlRef; |
| 72 |
import org.mozilla.javascript.ast.XmlString; |
| 73 |
import org.mozilla.javascript.ast.Yield; |
| 74 |
|
| 75 |
import java.io.BufferedReader; |
| 76 |
import java.io.IOException; |
| 77 |
import java.io.Reader; |
| 78 |
|
| 79 |
import java.util.ArrayList; |
| 80 |
import java.util.HashMap; |
| 81 |
import java.util.List; |
| 82 |
import java.util.Map; |
| 83 |
import java.util.Set; |
| 84 |
import java.util.HashSet; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public class Parser |
| 106 |
{ |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public static final int ARGC_LIMIT = 1 << 16; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
final static int |
| 116 |
CLEAR_TI_MASK = 0xFFFF, |
| 117 |
TI_AFTER_EOL = 1 << 16, |
| 118 |
TI_CHECK_LABEL = 1 << 17; |
| 119 |
|
| 120 |
CompilerEnvirons compilerEnv; |
| 121 |
private ErrorReporter errorReporter; |
| 122 |
private IdeErrorReporter errorCollector; |
| 123 |
private String sourceURI; |
| 124 |
private char[] sourceChars; |
| 125 |
|
| 126 |
boolean calledByCompileFunction; |
| 127 |
private boolean parseFinished; |
| 128 |
|
| 129 |
private TokenStream ts; |
| 130 |
private int currentFlaggedToken = Token.EOF; |
| 131 |
private int currentToken; |
| 132 |
private int syntaxErrorCount; |
| 133 |
|
| 134 |
private List<Comment> scannedComments; |
| 135 |
private Comment currentJsDocComment; |
| 136 |
|
| 137 |
protected int nestingOfFunction; |
| 138 |
private LabeledStatement currentLabel; |
| 139 |
private boolean inDestructuringAssignment; |
| 140 |
protected boolean inUseStrictDirective; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
ScriptNode currentScriptOrFn; |
| 145 |
Scope currentScope; |
| 146 |
private int endFlags; |
| 147 |
private boolean inForInit; |
| 148 |
private Map<String,LabeledStatement> labelSet; |
| 149 |
private List<Loop> loopSet; |
| 150 |
private List<Jump> loopAndSwitchSet; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
private int prevNameTokenStart; |
| 157 |
private String prevNameTokenString = ""; |
| 158 |
private int prevNameTokenLineno; |
| 159 |
|
| 160 |
private boolean defaultUseStrictDirective; |
| 161 |
|
| 162 |
|
| 163 |
private static class ParserException extends RuntimeException |
| 164 |
{ |
| 165 |
static final long serialVersionUID = 5882582646773765630L; |
| 166 |
} |
| 167 |
|
| 168 |
public Parser() { |
| 169 |
this(new CompilerEnvirons()); |
| 170 |
} |
| 171 |
|
| 172 |
public Parser(CompilerEnvirons compilerEnv) { |
| 173 |
this(compilerEnv, compilerEnv.getErrorReporter()); |
| 174 |
} |
| 175 |
|
| 176 |
public Parser(CompilerEnvirons compilerEnv, ErrorReporter errorReporter) { |
| 177 |
this.compilerEnv = compilerEnv; |
| 178 |
this.errorReporter = errorReporter; |
| 179 |
if (errorReporter instanceof IdeErrorReporter) { |
| 180 |
errorCollector = (IdeErrorReporter)errorReporter; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
void addStrictWarning(String messageId, String messageArg) { |
| 186 |
int beg = -1, end = -1; |
| 187 |
if (ts != null) { |
| 188 |
beg = ts.tokenBeg; |
| 189 |
end = ts.tokenEnd - ts.tokenBeg; |
| 190 |
} |
| 191 |
addStrictWarning(messageId, messageArg, beg, end); |
| 192 |
} |
| 193 |
|
| 194 |
void addStrictWarning(String messageId, String messageArg, |
| 195 |
int position, int length) { |
| 196 |
if (compilerEnv.isStrictMode()) |
| 197 |
addWarning(messageId, messageArg, position, length); |
| 198 |
} |
| 199 |
|
| 200 |
void addWarning(String messageId, String messageArg) { |
| 201 |
int beg = -1, end = -1; |
| 202 |
if (ts != null) { |
| 203 |
beg = ts.tokenBeg; |
| 204 |
end = ts.tokenEnd - ts.tokenBeg; |
| 205 |
} |
| 206 |
addWarning(messageId, messageArg, beg, end); |
| 207 |
} |
| 208 |
|
| 209 |
void addWarning(String messageId, int position, int length) { |
| 210 |
addWarning(messageId, null, position, length); |
| 211 |
} |
| 212 |
|
| 213 |
void addWarning(String messageId, String messageArg, |
| 214 |
int position, int length) |
| 215 |
{ |
| 216 |
String message = lookupMessage(messageId, messageArg); |
| 217 |
if (compilerEnv.reportWarningAsError()) { |
| 218 |
addError(messageId, messageArg, position, length); |
| 219 |
} else if (errorCollector != null) { |
| 220 |
errorCollector.warning(message, sourceURI, position, length); |
| 221 |
} else { |
| 222 |
errorReporter.warning(message, sourceURI, ts.getLineno(), |
| 223 |
ts.getLine(), ts.getOffset()); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
void addError(String messageId) { |
| 228 |
addError(messageId, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg); |
| 229 |
} |
| 230 |
|
| 231 |
void addError(String messageId, int position, int length) { |
| 232 |
addError(messageId, null, position, length); |
| 233 |
} |
| 234 |
|
| 235 |
void addError(String messageId, String messageArg) { |
| 236 |
addError(messageId, messageArg, ts.tokenBeg, |
| 237 |
ts.tokenEnd - ts.tokenBeg); |
| 238 |
} |
| 239 |
|
| 240 |
void addError(String messageId, String messageArg, int position, int length) |
| 241 |
{ |
| 242 |
++syntaxErrorCount; |
| 243 |
String message = lookupMessage(messageId, messageArg); |
| 244 |
if (errorCollector != null) { |
| 245 |
errorCollector.error(message, sourceURI, position, length); |
| 246 |
} else { |
| 247 |
int lineno = 1, offset = 1; |
| 248 |
String line = ""; |
| 249 |
if (ts != null) { |
| 250 |
lineno = ts.getLineno(); |
| 251 |
line = ts.getLine(); |
| 252 |
offset = ts.getOffset(); |
| 253 |
} |
| 254 |
errorReporter.error(message, sourceURI, lineno, line, offset); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
private void addStrictWarning(String messageId, String messageArg, |
| 259 |
int position, int length, |
| 260 |
int line, String lineSource, int lineOffset) { |
| 261 |
if (compilerEnv.isStrictMode()) { |
| 262 |
addWarning(messageId, messageArg, position, length, line, lineSource, lineOffset); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
private void addWarning(String messageId, String messageArg, |
| 267 |
int position, int length, |
| 268 |
int line, String lineSource, int lineOffset) { |
| 269 |
String message = lookupMessage(messageId, messageArg); |
| 270 |
if (compilerEnv.reportWarningAsError()) { |
| 271 |
addError(messageId, messageArg, position, length, line, lineSource, lineOffset); |
| 272 |
} else if (errorCollector != null) { |
| 273 |
errorCollector.warning(message, sourceURI, position, length); |
| 274 |
} else { |
| 275 |
errorReporter.warning(message, sourceURI, line, lineSource, lineOffset); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
private void addError(String messageId, String messageArg, |
| 280 |
int position, int length, |
| 281 |
int line, String lineSource, int lineOffset) { |
| 282 |
++syntaxErrorCount; |
| 283 |
String message = lookupMessage(messageId, messageArg); |
| 284 |
if (errorCollector != null) { |
| 285 |
errorCollector.error(message, sourceURI, position, length); |
| 286 |
} else { |
| 287 |
errorReporter.error(message, sourceURI, line, lineSource, lineOffset); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
String lookupMessage(String messageId) { |
| 292 |
return lookupMessage(messageId, null); |
| 293 |
} |
| 294 |
|
| 295 |
String lookupMessage(String messageId, String messageArg) { |
| 296 |
return messageArg == null |
| 297 |
? ScriptRuntime.getMessage0(messageId) |
| 298 |
: ScriptRuntime.getMessage1(messageId, messageArg); |
| 299 |
} |
| 300 |
|
| 301 |
void reportError(String messageId) { |
| 302 |
reportError(messageId, null); |
| 303 |
} |
| 304 |
|
| 305 |
void reportError(String messageId, String messageArg) { |
| 306 |
if (ts == null) { |
| 307 |
reportError(messageId, messageArg, 1, 1); |
| 308 |
} else { |
| 309 |
reportError(messageId, messageArg, ts.tokenBeg, |
| 310 |
ts.tokenEnd - ts.tokenBeg); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
void reportError(String messageId, int position, int length) |
| 315 |
{ |
| 316 |
reportError(messageId, null, position, length); |
| 317 |
} |
| 318 |
|
| 319 |
void reportError(String messageId, String messageArg, int position, |
| 320 |
int length) |
| 321 |
{ |
| 322 |
addError(messageId, messageArg, position, length); |
| 323 |
|
| 324 |
if (!compilerEnv.recoverFromErrors()) { |
| 325 |
throw new ParserException(); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
private int getNodeEnd(AstNode n) { |
| 333 |
return n.getPosition() + n.getLength(); |
| 334 |
} |
| 335 |
|
| 336 |
private void recordComment(int lineno, String comment) { |
| 337 |
if (scannedComments == null) { |
| 338 |
scannedComments = new ArrayList<Comment>(); |
| 339 |
} |
| 340 |
Comment commentNode = new Comment(ts.tokenBeg, |
| 341 |
ts.getTokenLength(), |
| 342 |
ts.commentType, |
| 343 |
comment); |
| 344 |
if (ts.commentType == Token.CommentType.JSDOC && |
| 345 |
compilerEnv.isRecordingLocalJsDocComments()) { |
| 346 |
currentJsDocComment = commentNode; |
| 347 |
} |
| 348 |
commentNode.setLineno(lineno); |
| 349 |
scannedComments.add(commentNode); |
| 350 |
} |
| 351 |
|
| 352 |
private Comment getAndResetJsDoc() { |
| 353 |
Comment saved = currentJsDocComment; |
| 354 |
currentJsDocComment = null; |
| 355 |
return saved; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
private int getNumberOfEols(String comment) { |
| 360 |
int lines = 0; |
| 361 |
for (int i = comment.length()-1; i >= 0; i--) { |
| 362 |
if (comment.charAt(i) == '\n') { |
| 363 |
lines++; |
| 364 |
} |
| 365 |
} |
| 366 |
return lines; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
private int peekToken() |
| 386 |
throws IOException |
| 387 |
{ |
| 388 |
|
| 389 |
|
| 390 |
if (currentFlaggedToken != Token.EOF) { |
| 391 |
return currentToken; |
| 392 |
} |
| 393 |
|
| 394 |
int lineno = ts.getLineno(); |
| 395 |
int tt = ts.getToken(); |
| 396 |
boolean sawEOL = false; |
| 397 |
|
| 398 |
|
| 399 |
while (tt == Token.EOL || tt == Token.COMMENT) { |
| 400 |
if (tt == Token.EOL) { |
| 401 |
lineno++; |
| 402 |
sawEOL = true; |
| 403 |
} else { |
| 404 |
if (compilerEnv.isRecordingComments()) { |
| 405 |
String comment = ts.getAndResetCurrentComment(); |
| 406 |
recordComment(lineno, comment); |
| 407 |
|
| 408 |
|
| 409 |
lineno += getNumberOfEols(comment); |
| 410 |
} |
| 411 |
} |
| 412 |
tt = ts.getToken(); |
| 413 |
} |
| 414 |
|
| 415 |
currentToken = tt; |
| 416 |
currentFlaggedToken = tt | (sawEOL ? TI_AFTER_EOL : 0); |
| 417 |
return currentToken; |
| 418 |
} |
| 419 |
|
| 420 |
private int peekFlaggedToken() |
| 421 |
throws IOException |
| 422 |
{ |
| 423 |
peekToken(); |
| 424 |
return currentFlaggedToken; |
| 425 |
} |
| 426 |
|
| 427 |
private void consumeToken() { |
| 428 |
currentFlaggedToken = Token.EOF; |
| 429 |
} |
| 430 |
|
| 431 |
private int nextToken() |
| 432 |
throws IOException |
| 433 |
{ |
| 434 |
int tt = peekToken(); |
| 435 |
consumeToken(); |
| 436 |
return tt; |
| 437 |
} |
| 438 |
|
| 439 |
private int nextFlaggedToken() |
| 440 |
throws IOException |
| 441 |
{ |
| 442 |
peekToken(); |
| 443 |
int ttFlagged = currentFlaggedToken; |
| 444 |
consumeToken(); |
| 445 |
return ttFlagged; |
| 446 |
} |
| 447 |
|
| 448 |
private boolean matchToken(int toMatch) |
| 449 |
throws IOException |
| 450 |
{ |
| 451 |
if (peekToken() != toMatch) { |
| 452 |
return false; |
| 453 |
} |
| 454 |
consumeToken(); |
| 455 |
return true; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
private int peekTokenOrEOL() |
| 464 |
throws IOException |
| 465 |
{ |
| 466 |
int tt = peekToken(); |
| 467 |
|
| 468 |
if ((currentFlaggedToken & TI_AFTER_EOL) != 0) { |
| 469 |
tt = Token.EOL; |
| 470 |
} |
| 471 |
return tt; |
| 472 |
} |
| 473 |
|
| 474 |
private boolean mustMatchToken(int toMatch, String messageId) |
| 475 |
throws IOException |
| 476 |
{ |
| 477 |
return mustMatchToken(toMatch, messageId, ts.tokenBeg, |
| 478 |
ts.tokenEnd - ts.tokenBeg); |
| 479 |
} |
| 480 |
|
| 481 |
private boolean mustMatchToken(int toMatch, String msgId, int pos, int len) |
| 482 |
throws IOException |
| 483 |
{ |
| 484 |
if (matchToken(toMatch)) { |
| 485 |
return true; |
| 486 |
} |
| 487 |
reportError(msgId, pos, len); |
| 488 |
return false; |
| 489 |
} |
| 490 |
|
| 491 |
private void mustHaveXML() { |
| 492 |
if (!compilerEnv.isXmlAvailable()) { |
| 493 |
reportError("msg.XML.not.available"); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
public boolean eof() { |
| 498 |
return ts.eof(); |
| 499 |
} |
| 500 |
|
| 501 |
boolean insideFunction() { |
| 502 |
return nestingOfFunction != 0; |
| 503 |
} |
| 504 |
|
| 505 |
void pushScope(Scope scope) { |
| 506 |
Scope parent = scope.getParentScope(); |
| 507 |
|
| 508 |
|
| 509 |
if (parent != null) { |
| 510 |
if (parent != currentScope) |
| 511 |
codeBug(); |
| 512 |
} else { |
| 513 |
currentScope.addChildScope(scope); |
| 514 |
} |
| 515 |
currentScope = scope; |
| 516 |
} |
| 517 |
|
| 518 |
void popScope() { |
| 519 |
currentScope = currentScope.getParentScope(); |
| 520 |
} |
| 521 |
|
| 522 |
private void enterLoop(Loop loop) { |
| 523 |
if (loopSet == null) |
| 524 |
loopSet = new ArrayList<Loop>(); |
| 525 |
loopSet.add(loop); |
| 526 |
if (loopAndSwitchSet == null) |
| 527 |
loopAndSwitchSet = new ArrayList<Jump>(); |
| 528 |
loopAndSwitchSet.add(loop); |
| 529 |
pushScope(loop); |
| 530 |
if (currentLabel != null) { |
| 531 |
currentLabel.setStatement(loop); |
| 532 |
currentLabel.getFirstLabel().setLoop(loop); |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
loop.setRelative(-currentLabel.getPosition()); |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
private void exitLoop() { |
| 542 |
Loop loop = loopSet.remove(loopSet.size() - 1); |
| 543 |
loopAndSwitchSet.remove(loopAndSwitchSet.size() - 1); |
| 544 |
if (loop.getParent() != null) { |
| 545 |
loop.setRelative(loop.getParent().getPosition()); |
| 546 |
} |
| 547 |
popScope(); |
| 548 |
} |
| 549 |
|
| 550 |
private void enterSwitch(SwitchStatement node) { |
| 551 |
if (loopAndSwitchSet == null) |
| 552 |
loopAndSwitchSet = new ArrayList<Jump>(); |
| 553 |
loopAndSwitchSet.add(node); |
| 554 |
} |
| 555 |
|
| 556 |
private void exitSwitch() { |
| 557 |
loopAndSwitchSet.remove(loopAndSwitchSet.size() - 1); |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
public AstRoot parse(String sourceString, String sourceURI, int lineno) |
| 569 |
{ |
| 570 |
if (parseFinished) throw new IllegalStateException("parser reused"); |
| 571 |
this.sourceURI = sourceURI; |
| 572 |
if (compilerEnv.isIdeMode()) { |
| 573 |
this.sourceChars = sourceString.toCharArray(); |
| 574 |
} |
| 575 |
this.ts = new TokenStream(this, null, sourceString, lineno); |
| 576 |
try { |
| 577 |
return parse(); |
| 578 |
} catch (IOException iox) { |
| 579 |
|
| 580 |
throw new IllegalStateException(); |
| 581 |
} finally { |
| 582 |
parseFinished = true; |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
public AstRoot parse(Reader sourceReader, String sourceURI, int lineno) |
| 592 |
throws IOException |
| 593 |
{ |
| 594 |
if (parseFinished) throw new IllegalStateException("parser reused"); |
| 595 |
if (compilerEnv.isIdeMode()) { |
| 596 |
return parse(readFully(sourceReader), sourceURI, lineno); |
| 597 |
} |
| 598 |
try { |
| 599 |
this.sourceURI = sourceURI; |
| 600 |
ts = new TokenStream(this, sourceReader, null, lineno); |
| 601 |
return parse(); |
| 602 |
} finally { |
| 603 |
parseFinished = true; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
private AstRoot parse() throws IOException |
| 608 |
{ |
| 609 |
int pos = 0; |
| 610 |
AstRoot root = new AstRoot(pos); |
| 611 |
currentScope = currentScriptOrFn = root; |
| 612 |
|
| 613 |
int baseLineno = ts.lineno; |
| 614 |
int end = pos; |
| 615 |
|
| 616 |
boolean inDirectivePrologue = true; |
| 617 |
boolean savedStrictMode = inUseStrictDirective; |
| 618 |
|
| 619 |
inUseStrictDirective = defaultUseStrictDirective; |
| 620 |
if (inUseStrictDirective) { |
| 621 |
root.setInStrictMode(true); |
| 622 |
} |
| 623 |
|
| 624 |
try { |
| 625 |
for (;;) { |
| 626 |
int tt = peekToken(); |
| 627 |
if (tt <= Token.EOF) { |
| 628 |
break; |
| 629 |
} |
| 630 |
|
| 631 |
AstNode n; |
| 632 |
if (tt == Token.FUNCTION) { |
| 633 |
consumeToken(); |
| 634 |
try { |
| 635 |
n = function(calledByCompileFunction |
| 636 |
? FunctionNode.FUNCTION_EXPRESSION |
| 637 |
: FunctionNode.FUNCTION_STATEMENT); |
| 638 |
} catch (ParserException e) { |
| 639 |
break; |
| 640 |
} |
| 641 |
} else { |
| 642 |
n = statement(); |
| 643 |
if (inDirectivePrologue) { |
| 644 |
String directive = getDirective(n); |
| 645 |
if (directive == null) { |
| 646 |
inDirectivePrologue = false; |
| 647 |
} else if (directive.equals("use strict")) { |
| 648 |
inUseStrictDirective = true; |
| 649 |
root.setInStrictMode(true); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
} |
| 654 |
end = getNodeEnd(n); |
| 655 |
root.addChildToBack(n); |
| 656 |
n.setParent(root); |
| 657 |
} |
| 658 |
} catch (StackOverflowError ex) { |
| 659 |
String msg = lookupMessage("msg.too.deep.parser.recursion"); |
| 660 |
if (!compilerEnv.isIdeMode()) |
| 661 |
throw Context.reportRuntimeError(msg, sourceURI, |
| 662 |
ts.lineno, null, 0); |
| 663 |
} finally { |
| 664 |
inUseStrictDirective = savedStrictMode; |
| 665 |
} |
| 666 |
|
| 667 |
if (this.syntaxErrorCount != 0) { |
| 668 |
String msg = String.valueOf(this.syntaxErrorCount); |
| 669 |
msg = lookupMessage("msg.got.syntax.errors", msg); |
| 670 |
if (!compilerEnv.isIdeMode()) |
| 671 |
throw errorReporter.runtimeError(msg, sourceURI, baseLineno, |
| 672 |
null, 0); |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
if (scannedComments != null) { |
| 677 |
|
| 678 |
|
| 679 |
int last = scannedComments.size() - 1; |
| 680 |
end = Math.max(end, getNodeEnd(scannedComments.get(last))); |
| 681 |
for (Comment c : scannedComments) { |
| 682 |
root.addComment(c); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
root.setLength(end - pos); |
| 687 |
root.setSourceName(sourceURI); |
| 688 |
root.setBaseLineno(baseLineno); |
| 689 |
root.setEndLineno(ts.lineno); |
| 690 |
return root; |
| 691 |
} |
| 692 |
|
| 693 |
private AstNode parseFunctionBody(int type, FunctionNode fnNode) |
| 694 |
throws IOException |
| 695 |
{ |
| 696 |
boolean isExpressionClosure = false; |
| 697 |
if (!matchToken(Token.LC)) { |
| 698 |
if (compilerEnv.getLanguageVersion() < Context.VERSION_1_8 && type != FunctionNode.ARROW_FUNCTION) { |
| 699 |
reportError("msg.no.brace.body"); |
| 700 |
} else { |
| 701 |
isExpressionClosure = true; |
| 702 |
} |
| 703 |
} |
| 704 |
boolean isArrow = type == FunctionNode.ARROW_FUNCTION; |
| 705 |
++nestingOfFunction; |
| 706 |
int pos = ts.tokenBeg; |
| 707 |
Block pn = new Block(pos); |
| 708 |
|
| 709 |
boolean inDirectivePrologue = true; |
| 710 |
boolean savedStrictMode = inUseStrictDirective; |
| 711 |
|
| 712 |
|
| 713 |
pn.setLineno(ts.lineno); |
| 714 |
try { |
| 715 |
if (isExpressionClosure) { |
| 716 |
ReturnStatement n = new ReturnStatement(ts.lineno); |
| 717 |
n.setReturnValue(assignExpr()); |
| 718 |
|
| 719 |
n.putProp(Node.EXPRESSION_CLOSURE_PROP, Boolean.TRUE); |
| 720 |
pn.putProp(Node.EXPRESSION_CLOSURE_PROP, Boolean.TRUE); |
| 721 |
if (isArrow) { |
| 722 |
n.putProp(Node.ARROW_FUNCTION_PROP, Boolean.TRUE); |
| 723 |
} |
| 724 |
pn.addStatement(n); |
| 725 |
} else { |
| 726 |
bodyLoop: for (;;) { |
| 727 |
AstNode n; |
| 728 |
int tt = peekToken(); |
| 729 |
switch (tt) { |
| 730 |
case Token.ERROR: |
| 731 |
case Token.EOF: |
| 732 |
case Token.RC: |
| 733 |
break bodyLoop; |
| 734 |
|
| 735 |
case Token.FUNCTION: |
| 736 |
consumeToken(); |
| 737 |
n = function(FunctionNode.FUNCTION_STATEMENT); |
| 738 |
break; |
| 739 |
default: |
| 740 |
n = statement(); |
| 741 |
if (inDirectivePrologue) { |
| 742 |
String directive = getDirective(n); |
| 743 |
if (directive == null) { |
| 744 |
inDirectivePrologue = false; |
| 745 |
} else if (directive.equals("use strict")) { |
| 746 |
inUseStrictDirective = true; |
| 747 |
fnNode.setInStrictMode(true); |
| 748 |
if (!savedStrictMode) { |
| 749 |
setRequiresActivation(); |
| 750 |
} |
| 751 |
} |
| 752 |
} |
| 753 |
break; |
| 754 |
} |
| 755 |
pn.addStatement(n); |
| 756 |
} |
| 757 |
} |
| 758 |
} catch (ParserException e) { |
| 759 |
|
| 760 |
} finally { |
| 761 |
--nestingOfFunction; |
| 762 |
inUseStrictDirective = savedStrictMode; |
| 763 |
} |
| 764 |
|
| 765 |
int end = ts.tokenEnd; |
| 766 |
getAndResetJsDoc(); |
| 767 |
if (!isExpressionClosure && mustMatchToken(Token.RC, "msg.no.brace.after.body")) |
| 768 |
end = ts.tokenEnd; |
| 769 |
pn.setLength(end - pos); |
| 770 |
return pn; |
| 771 |
} |
| 772 |
|
| 773 |
private String getDirective(AstNode n) { |
| 774 |
if (n instanceof ExpressionStatement) { |
| 775 |
AstNode e = ((ExpressionStatement) n).getExpression(); |
| 776 |
if (e instanceof StringLiteral) { |
| 777 |
return ((StringLiteral) e).getValue(); |
| 778 |
} |
| 779 |
} |
| 780 |
return null; |
| 781 |
} |
| 782 |
|
| 783 |
private void parseFunctionParams(FunctionNode fnNode) |
| 784 |
throws IOException |
| 785 |
{ |
| 786 |
if (matchToken(Token.RP)) { |
| 787 |
fnNode.setRp(ts.tokenBeg - fnNode.getPosition()); |
| 788 |
return; |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
Map<String, Node> destructuring = null; |
| 793 |
Set<String> paramNames = new HashSet<String>(); |
| 794 |
do { |
| 795 |
int tt = peekToken(); |
| 796 |
if (tt == Token.LB || tt == Token.LC) { |
| 797 |
AstNode expr = destructuringPrimaryExpr(); |
| 798 |
markDestructuring(expr); |
| 799 |
fnNode.addParam(expr); |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
if (destructuring == null) { |
| 804 |
destructuring = new HashMap<String, Node>(); |
| 805 |
} |
| 806 |
String pname = currentScriptOrFn.getNextTempName(); |
| 807 |
defineSymbol(Token.LP, pname, false); |
| 808 |
destructuring.put(pname, expr); |
| 809 |
} else { |
| 810 |
if (mustMatchToken(Token.NAME, "msg.no.parm")) { |
| 811 |
fnNode.addParam(createNameNode()); |
| 812 |
String paramName = ts.getString(); |
| 813 |
defineSymbol(Token.LP, paramName); |
| 814 |
if (this.inUseStrictDirective) { |
| 815 |
if ("eval".equals(paramName) || |
| 816 |
"arguments".equals(paramName)) |
| 817 |
{ |
| 818 |
reportError("msg.bad.id.strict", paramName); |
| 819 |
} |
| 820 |
if (paramNames.contains(paramName)) |
| 821 |
addError("msg.dup.param.strict", paramName); |
| 822 |
paramNames.add(paramName); |
| 823 |
} |
| 824 |
} else { |
| 825 |
fnNode.addParam(makeErrorNode()); |
| 826 |
} |
| 827 |
} |
| 828 |
} while (matchToken(Token.COMMA)); |
| 829 |
|
| 830 |
if (destructuring != null) { |
| 831 |
Node destructuringNode = new Node(Token.COMMA); |
| 832 |
|
| 833 |
for (Map.Entry<String, Node> param: destructuring.entrySet()) { |
| 834 |
Node assign = createDestructuringAssignment(Token.VAR, |
| 835 |
param.getValue(), createName(param.getKey())); |
| 836 |
destructuringNode.addChildToBack(assign); |
| 837 |
|
| 838 |
} |
| 839 |
fnNode.putProp(Node.DESTRUCTURING_PARAMS, destructuringNode); |
| 840 |
} |
| 841 |
|
| 842 |
if (mustMatchToken(Token.RP, "msg.no.paren.after.parms")) { |
| 843 |
fnNode.setRp(ts.tokenBeg - fnNode.getPosition()); |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
private FunctionNode function(int type) |
| 848 |
throws IOException |
| 849 |
{ |
| 850 |
int syntheticType = type; |
| 851 |
int baseLineno = ts.lineno; |
| 852 |
int functionSourceStart = ts.tokenBeg; |
| 853 |
Name name = null; |
| 854 |
AstNode memberExprNode = null; |
| 855 |
|
| 856 |
if (matchToken(Token.NAME)) { |
| 857 |
name = createNameNode(true, Token.NAME); |
| 858 |
if (inUseStrictDirective) { |
| 859 |
String id = name.getIdentifier(); |
| 860 |
if ("eval".equals(id)|| "arguments".equals(id)) { |
| 861 |
reportError("msg.bad.id.strict", id); |
| 862 |
} |
| 863 |
} |
| 864 |
if (!matchToken(Token.LP)) { |
| 865 |
if (compilerEnv.isAllowMemberExprAsFunctionName()) { |
| 866 |
AstNode memberExprHead = name; |
| 867 |
name = null; |
| 868 |
memberExprNode = memberExprTail(false, memberExprHead); |
| 869 |
} |
| 870 |
mustMatchToken(Token.LP, "msg.no.paren.parms"); |
| 871 |
} |
| 872 |
} else if (matchToken(Token.LP)) { |
| 873 |
|
| 874 |
} else { |
| 875 |
if (compilerEnv.isAllowMemberExprAsFunctionName()) { |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
memberExprNode = memberExpr(false); |
| 880 |
} |
| 881 |
mustMatchToken(Token.LP, "msg.no.paren.parms"); |
| 882 |
} |
| 883 |
int lpPos = currentToken == Token.LP ? ts.tokenBeg : -1; |
| 884 |
|
| 885 |
if (memberExprNode != null) { |
| 886 |
syntheticType = FunctionNode.FUNCTION_EXPRESSION; |
| 887 |
} |
| 888 |
|
| 889 |
if (syntheticType != FunctionNode.FUNCTION_EXPRESSION |
| 890 |
&& name != null && name.length() > 0) { |
| 891 |
|
| 892 |
defineSymbol(Token.FUNCTION, name.getIdentifier()); |
| 893 |
} |
| 894 |
|
| 895 |
FunctionNode fnNode = new FunctionNode(functionSourceStart, name); |
| 896 |
fnNode.setFunctionType(type); |
| 897 |
if (lpPos != -1) |
| 898 |
fnNode.setLp(lpPos - functionSourceStart); |
| 899 |
|
| 900 |
fnNode.setJsDocNode(getAndResetJsDoc()); |
| 901 |
|
| 902 |
PerFunctionVariables savedVars = new PerFunctionVariables(fnNode); |
| 903 |
try { |
| 904 |
parseFunctionParams(fnNode); |
| 905 |
fnNode.setBody(parseFunctionBody(type, fnNode)); |
| 906 |
fnNode.setEncodedSourceBounds(functionSourceStart, ts.tokenEnd); |
| 907 |
fnNode.setLength(ts.tokenEnd - functionSourceStart); |
| 908 |
|
| 909 |
if (compilerEnv.isStrictMode() |
| 910 |
&& !fnNode.getBody().hasConsistentReturnUsage()) { |
| 911 |
String msg = (name != null && name.length() > 0) |
| 912 |
? "msg.no.return.value" |
| 913 |
: "msg.anon.no.return.value"; |
| 914 |
addStrictWarning(msg, name == null ? "" : name.getIdentifier()); |
| 915 |
} |
| 916 |
} finally { |
| 917 |
savedVars.restore(); |
| 918 |
} |
| 919 |
|
| 920 |
if (memberExprNode != null) { |
| 921 |
|
| 922 |
Kit.codeBug(); |
| 923 |
fnNode.setMemberExprNode(memberExprNode); |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
} |
| 934 |
|
| 935 |
fnNode.setSourceName(sourceURI); |
| 936 |
fnNode.setBaseLineno(baseLineno); |
| 937 |
fnNode.setEndLineno(ts.lineno); |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
if (compilerEnv.isIdeMode()) { |
| 944 |
fnNode.setParentScope(currentScope); |
| 945 |
} |
| 946 |
return fnNode; |
| 947 |
} |
| 948 |
|
| 949 |
private AstNode arrowFunction(AstNode params) throws IOException { |
| 950 |
int baseLineno = ts.lineno; |
| 951 |
int functionSourceStart = params != null ? params.getPosition() : -1; |
| 952 |
|
| 953 |
FunctionNode fnNode = new FunctionNode(functionSourceStart); |
| 954 |
fnNode.setFunctionType(FunctionNode.ARROW_FUNCTION); |
| 955 |
fnNode.setJsDocNode(getAndResetJsDoc()); |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
Map<String, Node> destructuring = new HashMap<String, Node>(); |
| 960 |
Set<String> paramNames = new HashSet<String>(); |
| 961 |
|
| 962 |
PerFunctionVariables savedVars = new PerFunctionVariables(fnNode); |
| 963 |
try { |
| 964 |
if (params instanceof ParenthesizedExpression) { |
| 965 |
fnNode.setParens(0, params.getLength()); |
| 966 |
AstNode p = ((ParenthesizedExpression)params).getExpression(); |
| 967 |
if (!(p instanceof EmptyExpression)) { |
| 968 |
arrowFunctionParams(fnNode, p, destructuring, paramNames); |
| 969 |
} |
| 970 |
} else { |
| 971 |
arrowFunctionParams(fnNode, params, destructuring, paramNames); |
| 972 |
} |
| 973 |
|
| 974 |
if (!destructuring.isEmpty()) { |
| 975 |
Node destructuringNode = new Node(Token.COMMA); |
| 976 |
|
| 977 |
for (Map.Entry<String, Node> param: destructuring.entrySet()) { |
| 978 |
Node assign = createDestructuringAssignment(Token.VAR, |
| 979 |
param.getValue(), createName(param.getKey())); |
| 980 |
destructuringNode.addChildToBack(assign); |
| 981 |
|
| 982 |
} |
| 983 |
fnNode.putProp(Node.DESTRUCTURING_PARAMS, destructuringNode); |
| 984 |
} |
| 985 |
|
| 986 |
fnNode.setBody(parseFunctionBody(FunctionNode.ARROW_FUNCTION, fnNode)); |
| 987 |
fnNode.setEncodedSourceBounds(functionSourceStart, ts.tokenEnd); |
| 988 |
fnNode.setLength(ts.tokenEnd - functionSourceStart); |
| 989 |
} finally { |
| 990 |
savedVars.restore(); |
| 991 |
} |
| 992 |
|
| 993 |
if (fnNode.isGenerator()) { |
| 994 |
reportError("msg.arrowfunction.generator"); |
| 995 |
return makeErrorNode(); |
| 996 |
} |
| 997 |
|
| 998 |
fnNode.setSourceName(sourceURI); |
| 999 |
fnNode.setBaseLineno(baseLineno); |
| 1000 |
fnNode.setEndLineno(ts.lineno); |
| 1001 |
|
| 1002 |
return fnNode; |
| 1003 |
} |
| 1004 |
|
| 1005 |
private void arrowFunctionParams(FunctionNode fnNode, AstNode params, Map<String, Node> destructuring, Set<String> paramNames) { |
| 1006 |
if (params instanceof ArrayLiteral || params instanceof ObjectLiteral) { |
| 1007 |
markDestructuring(params); |
| 1008 |
fnNode.addParam(params); |
| 1009 |
String pname = currentScriptOrFn.getNextTempName(); |
| 1010 |
defineSymbol(Token.LP, pname, false); |
| 1011 |
destructuring.put(pname, params); |
| 1012 |
} else if (params instanceof InfixExpression && params.getType() == Token.COMMA) { |
| 1013 |
arrowFunctionParams(fnNode, ((InfixExpression)params).getLeft(), destructuring, paramNames); |
| 1014 |
arrowFunctionParams(fnNode, ((InfixExpression)params).getRight(), destructuring, paramNames); |
| 1015 |
} else if (params instanceof Name) { |
| 1016 |
fnNode.addParam(params); |
| 1017 |
String paramName = ((Name)params).getIdentifier(); |
| 1018 |
defineSymbol(Token.LP, paramName); |
| 1019 |
|
| 1020 |
if (this.inUseStrictDirective) { |
| 1021 |
if ("eval".equals(paramName) || |
| 1022 |
"arguments".equals(paramName)) |
| 1023 |
{ |
| 1024 |
reportError("msg.bad.id.strict", paramName); |
| 1025 |
} |
| 1026 |
if (paramNames.contains(paramName)) |
| 1027 |
addError("msg.dup.param.strict", paramName); |
| 1028 |
paramNames.add(paramName); |
| 1029 |
} |
| 1030 |
} else { |
| 1031 |
reportError("msg.no.parm", params.getPosition(), params.getLength()); |
| 1032 |
fnNode.addParam(makeErrorNode()); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
private AstNode statements(AstNode parent) throws IOException { |
| 1045 |
if (currentToken != Token.LC |
| 1046 |
&& !compilerEnv.isIdeMode()) codeBug(); |
| 1047 |
int pos = ts.tokenBeg; |
| 1048 |
AstNode block = parent != null ? parent : new Block(pos); |
| 1049 |
block.setLineno(ts.lineno); |
| 1050 |
|
| 1051 |
int tt; |
| 1052 |
while ((tt = peekToken()) > Token.EOF && tt != Token.RC) { |
| 1053 |
block.addChild(statement()); |
| 1054 |
} |
| 1055 |
block.setLength(ts.tokenBeg - pos); |
| 1056 |
return block; |
| 1057 |
} |
| 1058 |
|
| 1059 |
private AstNode statements() throws IOException { |
| 1060 |
return statements(null); |
| 1061 |
} |
| 1062 |
|
| 1063 |
private static class ConditionData { |
| 1064 |
AstNode condition; |
| 1065 |
int lp = -1; |
| 1066 |
int rp = -1; |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
private ConditionData condition() |
| 1071 |
throws IOException |
| 1072 |
{ |
| 1073 |
ConditionData data = new ConditionData(); |
| 1074 |
|
| 1075 |
if (mustMatchToken(Token.LP, "msg.no.paren.cond")) |
| 1076 |
data.lp = ts.tokenBeg; |
| 1077 |
|
| 1078 |
data.condition = expr(); |
| 1079 |
|
| 1080 |
if (mustMatchToken(Token.RP, "msg.no.paren.after.cond")) |
| 1081 |
data.rp = ts.tokenBeg; |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
if (data.condition instanceof Assignment) { |
| 1086 |
addStrictWarning("msg.equal.as.assign", "", |
| 1087 |
data.condition.getPosition(), |
| 1088 |
data.condition.getLength()); |
| 1089 |
} |
| 1090 |
return data; |
| 1091 |
} |
| 1092 |
|
| 1093 |
private AstNode statement() |
| 1094 |
throws IOException |
| 1095 |
{ |
| 1096 |
int pos = ts.tokenBeg; |
| 1097 |
try { |
| 1098 |
AstNode pn = statementHelper(); |
| 1099 |
if (pn != null) { |
| 1100 |
if (compilerEnv.isStrictMode() && !pn.hasSideEffects()) { |
| 1101 |
int beg = pn.getPosition(); |
| 1102 |
beg = Math.max(beg, lineBeginningFor(beg)); |
| 1103 |
addStrictWarning(pn instanceof EmptyStatement |
| 1104 |
? "msg.extra.trailing.semi" |
| 1105 |
: "msg.no.side.effects", |
| 1106 |
"", beg, nodeEnd(pn) - beg); |
| 1107 |
} |
| 1108 |
return pn; |
| 1109 |
} |
| 1110 |
} catch (ParserException e) { |
| 1111 |
|
| 1112 |
} |
| 1113 |
|
| 1114 |
|
| 1115 |
guessingStatementEnd: for (;;) { |
| 1116 |
int tt = peekTokenOrEOL(); |
| 1117 |
consumeToken(); |
| 1118 |
switch (tt) { |
| 1119 |
case Token.ERROR: |
| 1120 |
case Token.EOF: |
| 1121 |
case Token.EOL: |
| 1122 |
case Token.SEMI: |
| 1123 |
break guessingStatementEnd; |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
return new EmptyStatement(pos, ts.tokenBeg - pos); |
| 1130 |
} |
| 1131 |
|
| 1132 |
private AstNode statementHelper() |
| 1133 |
throws IOException |
| 1134 |
{ |
| 1135 |
|
| 1136 |
if (currentLabel != null && currentLabel.getStatement() != null) |
| 1137 |
currentLabel = null; |
| 1138 |
|
| 1139 |
AstNode pn = null; |
| 1140 |
int tt = peekToken(), pos = ts.tokenBeg; |
| 1141 |
|
| 1142 |
switch (tt) { |
| 1143 |
case Token.IF: |
| 1144 |
return ifStatement(); |
| 1145 |
|
| 1146 |
case Token.SWITCH: |
| 1147 |
return switchStatement(); |
| 1148 |
|
| 1149 |
case Token.WHILE: |
| 1150 |
return whileLoop(); |
| 1151 |
|
| 1152 |
case Token.DO: |
| 1153 |
return doLoop(); |
| 1154 |
|
| 1155 |
case Token.FOR: |
| 1156 |
return forLoop(); |
| 1157 |
|
| 1158 |
case Token.TRY: |
| 1159 |
return tryStatement(); |
| 1160 |
|
| 1161 |
case Token.THROW: |
| 1162 |
pn = throwStatement(); |
| 1163 |
break; |
| 1164 |
|
| 1165 |
case Token.BREAK: |
| 1166 |
pn = breakStatement(); |
| 1167 |
break; |
| 1168 |
|
| 1169 |
case Token.CONTINUE: |
| 1170 |
pn = continueStatement(); |
| 1171 |
break; |
| 1172 |
|
| 1173 |
case Token.WITH: |
| 1174 |
if (this.inUseStrictDirective) { |
| 1175 |
reportError("msg.no.with.strict"); |
| 1176 |
} |
| 1177 |
return withStatement(); |
| 1178 |
|
| 1179 |
case Token.CONST: |
| 1180 |
case Token.VAR: |
| 1181 |
consumeToken(); |
| 1182 |
int lineno = ts.lineno; |
| 1183 |
pn = variables(currentToken, ts.tokenBeg, true); |
| 1184 |
pn.setLineno(lineno); |
| 1185 |
break; |
| 1186 |
|
| 1187 |
case Token.LET: |
| 1188 |
pn = letStatement(); |
| 1189 |
if (pn instanceof VariableDeclaration |
| 1190 |
&& peekToken() == Token.SEMI) |
| 1191 |
break; |
| 1192 |
return pn; |
| 1193 |
|
| 1194 |
case Token.RETURN: |
| 1195 |
case Token.YIELD: |
| 1196 |
pn = returnOrYield(tt, false); |
| 1197 |
break; |
| 1198 |
|
| 1199 |
case Token.DEBUGGER: |
| 1200 |
consumeToken(); |
| 1201 |
pn = new KeywordLiteral(ts.tokenBeg, |
| 1202 |
ts.tokenEnd - ts.tokenBeg, tt); |
| 1203 |
pn.setLineno(ts.lineno); |
| 1204 |
break; |
| 1205 |
|
| 1206 |
case Token.LC: |
| 1207 |
return block(); |
| 1208 |
|
| 1209 |
case Token.ERROR: |
| 1210 |
consumeToken(); |
| 1211 |
return makeErrorNode(); |
| 1212 |
|
| 1213 |
case Token.SEMI: |
| 1214 |
consumeToken(); |
| 1215 |
pos = ts.tokenBeg; |
| 1216 |
pn = new EmptyStatement(pos, ts.tokenEnd - pos); |
| 1217 |
pn.setLineno(ts.lineno); |
| 1218 |
return pn; |
| 1219 |
|
| 1220 |
case Token.FUNCTION: |
| 1221 |
consumeToken(); |
| 1222 |
return function(FunctionNode.FUNCTION_EXPRESSION_STATEMENT); |
| 1223 |
|
| 1224 |
case Token.DEFAULT : |
| 1225 |
pn = defaultXmlNamespace(); |
| 1226 |
break; |
| 1227 |
|
| 1228 |
case Token.NAME: |
| 1229 |
pn = nameOrLabel(); |
| 1230 |
if (pn instanceof ExpressionStatement) |
| 1231 |
break; |
| 1232 |
return pn; |
| 1233 |
|
| 1234 |
default: |
| 1235 |
lineno = ts.lineno; |
| 1236 |
pn = new ExpressionStatement(expr(), !insideFunction()); |
| 1237 |
pn.setLineno(lineno); |
| 1238 |
break; |
| 1239 |
} |
| 1240 |
|
| 1241 |
autoInsertSemicolon(pn); |
| 1242 |
return pn; |
| 1243 |
} |
| 1244 |
|
| 1245 |
private void autoInsertSemicolon(AstNode pn) throws IOException { |
| 1246 |
int ttFlagged = peekFlaggedToken(); |
| 1247 |
int pos = pn.getPosition(); |
| 1248 |
switch (ttFlagged & CLEAR_TI_MASK) { |
| 1249 |
case Token.SEMI: |
| 1250 |
|
| 1251 |
consumeToken(); |
| 1252 |
|
| 1253 |
pn.setLength(ts.tokenEnd - pos); |
| 1254 |
break; |
| 1255 |
case Token.ERROR: |
| 1256 |
case Token.EOF: |
| 1257 |
case Token.RC: |
| 1258 |
|
| 1259 |
warnMissingSemi(pos, nodeEnd(pn)); |
| 1260 |
break; |
| 1261 |
default: |
| 1262 |
if ((ttFlagged & TI_AFTER_EOL) == 0) { |
| 1263 |
|
| 1264 |
reportError("msg.no.semi.stmt"); |
| 1265 |
} else { |
| 1266 |
warnMissingSemi(pos, nodeEnd(pn)); |
| 1267 |
} |
| 1268 |
break; |
| 1269 |
} |
| 1270 |
} |
| 1271 |
|
| 1272 |
private IfStatement ifStatement() |
| 1273 |
throws IOException |
| 1274 |
{ |
| 1275 |
if (currentToken != Token.IF) codeBug(); |
| 1276 |
consumeToken(); |
| 1277 |
int pos = ts.tokenBeg, lineno = ts.lineno, elsePos = -1; |
| 1278 |
ConditionData data = condition(); |
| 1279 |
AstNode ifTrue = statement(), ifFalse = null; |
| 1280 |
if (matchToken(Token.ELSE)) { |
| 1281 |
elsePos = ts.tokenBeg - pos; |
| 1282 |
ifFalse = statement(); |
| 1283 |
} |
| 1284 |
int end = getNodeEnd(ifFalse != null ? ifFalse : ifTrue); |
| 1285 |
IfStatement pn = new IfStatement(pos, end - pos); |
| 1286 |
pn.setCondition(data.condition); |
| 1287 |
pn.setParens(data.lp - pos, data.rp - pos); |
| 1288 |
pn.setThenPart(ifTrue); |
| 1289 |
pn.setElsePart(ifFalse); |
| 1290 |
pn.setElsePosition(elsePos); |
| 1291 |
pn.setLineno(lineno); |
| 1292 |
return pn; |
| 1293 |
} |
| 1294 |
|
| 1295 |
private SwitchStatement switchStatement() |
| 1296 |
throws IOException |
| 1297 |
{ |
| 1298 |
if (currentToken != Token.SWITCH) codeBug(); |
| 1299 |
consumeToken(); |
| 1300 |
int pos = ts.tokenBeg; |
| 1301 |
|
| 1302 |
SwitchStatement pn = new SwitchStatement(pos); |
| 1303 |
if (mustMatchToken(Token.LP, "msg.no.paren.switch")) |
| 1304 |
pn.setLp(ts.tokenBeg - pos); |
| 1305 |
pn.setLineno(ts.lineno); |
| 1306 |
|
| 1307 |
AstNode discriminant = expr(); |
| 1308 |
pn.setExpression(discriminant); |
| 1309 |
enterSwitch(pn); |
| 1310 |
|
| 1311 |
try { |
| 1312 |
if (mustMatchToken(Token.RP, "msg.no.paren.after.switch")) |
| 1313 |
pn.setRp(ts.tokenBeg - pos); |
| 1314 |
|
| 1315 |
mustMatchToken(Token.LC, "msg.no.brace.switch"); |
| 1316 |
|
| 1317 |
boolean hasDefault = false; |
| 1318 |
int tt; |
| 1319 |
switchLoop: for (;;) { |
| 1320 |
tt = nextToken(); |
| 1321 |
int casePos = ts.tokenBeg; |
| 1322 |
int caseLineno = ts.lineno; |
| 1323 |
AstNode caseExpression = null; |
| 1324 |
switch (tt) { |
| 1325 |
case Token.RC: |
| 1326 |
pn.setLength(ts.tokenEnd - pos); |
| 1327 |
break switchLoop; |
| 1328 |
|
| 1329 |
case Token.CASE: |
| 1330 |
caseExpression = expr(); |
| 1331 |
mustMatchToken(Token.COLON, "msg.no.colon.case"); |
| 1332 |
break; |
| 1333 |
|
| 1334 |
case Token.DEFAULT: |
| 1335 |
if (hasDefault) { |
| 1336 |
reportError("msg.double.switch.default"); |
| 1337 |
} |
| 1338 |
hasDefault = true; |
| 1339 |
caseExpression = null; |
| 1340 |
mustMatchToken(Token.COLON, "msg.no.colon.case"); |
| 1341 |
break; |
| 1342 |
|
| 1343 |
default: |
| 1344 |
reportError("msg.bad.switch"); |
| 1345 |
break switchLoop; |
| 1346 |
} |
| 1347 |
|
| 1348 |
SwitchCase caseNode = new SwitchCase(casePos); |
| 1349 |
caseNode.setExpression(caseExpression); |
| 1350 |
caseNode.setLength(ts.tokenEnd - pos); |
| 1351 |
caseNode.setLineno(caseLineno); |
| 1352 |
|
| 1353 |
while ((tt = peekToken()) != Token.RC |
| 1354 |
&& tt != Token.CASE |
| 1355 |
&& tt != Token.DEFAULT |
| 1356 |
&& tt != Token.EOF) |
| 1357 |
{ |
| 1358 |
caseNode.addStatement(statement()); |
| 1359 |
} |
| 1360 |
pn.addCase(caseNode); |
| 1361 |
} |
| 1362 |
} finally { |
| 1363 |
exitSwitch(); |
| 1364 |
} |
| 1365 |
return pn; |
| 1366 |
} |
| 1367 |
|
| 1368 |
private WhileLoop whileLoop() |
| 1369 |
throws IOException |
| 1370 |
{ |
| 1371 |
if (currentToken != Token.WHILE) codeBug(); |
| 1372 |
consumeToken(); |
| 1373 |
int pos = ts.tokenBeg; |
| 1374 |
WhileLoop pn = new WhileLoop(pos); |
| 1375 |
pn.setLineno(ts.lineno); |
| 1376 |
enterLoop(pn); |
| 1377 |
try { |
| 1378 |
ConditionData data = condition(); |
| 1379 |
pn.setCondition(data.condition); |
| 1380 |
pn.setParens(data.lp - pos, data.rp - pos); |
| 1381 |
AstNode body = statement(); |
| 1382 |
pn.setLength(getNodeEnd(body) - pos); |
| 1383 |
pn.setBody(body); |
| 1384 |
} finally { |
| 1385 |
exitLoop(); |
| 1386 |
} |
| 1387 |
return pn; |
| 1388 |
} |
| 1389 |
|
| 1390 |
private DoLoop doLoop() |
| 1391 |
throws IOException |
| 1392 |
{ |
| 1393 |
if (currentToken != Token.DO) codeBug(); |
| 1394 |
consumeToken(); |
| 1395 |
int pos = ts.tokenBeg, end; |
| 1396 |
DoLoop pn = new DoLoop(pos); |
| 1397 |
pn.setLineno(ts.lineno); |
| 1398 |
enterLoop(pn); |
| 1399 |
try { |
| 1400 |
AstNode body = statement(); |
| 1401 |
mustMatchToken(Token.WHILE, "msg.no.while.do"); |
| 1402 |
pn.setWhilePosition(ts.tokenBeg - pos); |
| 1403 |
ConditionData data = condition(); |
| 1404 |
pn.setCondition(data.condition); |
| 1405 |
pn.setParens(data.lp - pos, data.rp - pos); |
| 1406 |
end = getNodeEnd(body); |
| 1407 |
pn.setBody(body); |
| 1408 |
} finally { |
| 1409 |
exitLoop(); |
| 1410 |
} |
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
if (matchToken(Token.SEMI)) { |
| 1415 |
end = ts.tokenEnd; |
| 1416 |
} |
| 1417 |
pn.setLength(end - pos); |
| 1418 |
return pn; |
| 1419 |
} |
| 1420 |
|
| 1421 |
private Loop forLoop() |
| 1422 |
throws IOException |
| 1423 |
{ |
| 1424 |
if (currentToken != Token.FOR) codeBug(); |
| 1425 |
consumeToken(); |
| 1426 |
int forPos = ts.tokenBeg, lineno = ts.lineno; |
| 1427 |
boolean isForEach = false, isForIn = false, isForOf = false; |
| 1428 |
int eachPos = -1, inPos = -1, lp = -1, rp = -1; |
| 1429 |
AstNode init = null; |
| 1430 |
AstNode cond = null; |
| 1431 |
AstNode incr = null; |
| 1432 |
Loop pn = null; |
| 1433 |
|
| 1434 |
Scope tempScope = new Scope(); |
| 1435 |
pushScope(tempScope); |
| 1436 |
try { |
| 1437 |
|
| 1438 |
if (matchToken(Token.NAME)) { |
| 1439 |
if ("each".equals(ts.getString())) { |
| 1440 |
isForEach = true; |
| 1441 |
eachPos = ts.tokenBeg - forPos; |
| 1442 |
} else { |
| 1443 |
reportError("msg.no.paren.for"); |
| 1444 |
} |
| 1445 |
} |
| 1446 |
|
| 1447 |
if (mustMatchToken(Token.LP, "msg.no.paren.for")) |
| 1448 |
lp = ts.tokenBeg - forPos; |
| 1449 |
int tt = peekToken(); |
| 1450 |
|
| 1451 |
init = forLoopInit(tt); |
| 1452 |
if (matchToken(Token.IN)) { |
| 1453 |
isForIn = true; |
| 1454 |
inPos = ts.tokenBeg - forPos; |
| 1455 |
cond = expr(); |
| 1456 |
} else if (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6 && |
| 1457 |
matchToken(Token.NAME) && "of".equals(ts.getString())) { |
| 1458 |
isForOf = true; |
| 1459 |
inPos = ts.tokenBeg - forPos; |
| 1460 |
cond = expr(); |
| 1461 |
} else { |
| 1462 |
mustMatchToken(Token.SEMI, "msg.no.semi.for"); |
| 1463 |
if (peekToken() == Token.SEMI) { |
| 1464 |
|
| 1465 |
cond = new EmptyExpression(ts.tokenBeg, 1); |
| 1466 |
cond.setLineno(ts.lineno); |
| 1467 |
} else { |
| 1468 |
cond = expr(); |
| 1469 |
} |
| 1470 |
|
| 1471 |
mustMatchToken(Token.SEMI, "msg.no.semi.for.cond"); |
| 1472 |
int tmpPos = ts.tokenEnd; |
| 1473 |
if (peekToken() == Token.RP) { |
| 1474 |
incr = new EmptyExpression(tmpPos, 1); |
| 1475 |
incr.setLineno(ts.lineno); |
| 1476 |
} else { |
| 1477 |
incr = expr(); |
| 1478 |
} |
| 1479 |
} |
| 1480 |
|
| 1481 |
if (mustMatchToken(Token.RP, "msg.no.paren.for.ctrl")) |
| 1482 |
rp = ts.tokenBeg - forPos; |
| 1483 |
|
| 1484 |
if (isForIn || isForOf) { |
| 1485 |
ForInLoop fis = new ForInLoop(forPos); |
| 1486 |
if (init instanceof VariableDeclaration) { |
| 1487 |
|
| 1488 |
if (((VariableDeclaration)init).getVariables().size() > 1) { |
| 1489 |
reportError("msg.mult.index"); |
| 1490 |
} |
| 1491 |
} |
| 1492 |
if (isForOf && isForEach) { |
| 1493 |
reportError("msg.invalid.for.each"); |
| 1494 |
} |
| 1495 |
fis.setIterator(init); |
| 1496 |
fis.setIteratedObject(cond); |
| 1497 |
fis.setInPosition(inPos); |
| 1498 |
fis.setIsForEach(isForEach); |
| 1499 |
fis.setEachPosition(eachPos); |
| 1500 |
fis.setIsForOf(isForOf); |
| 1501 |
pn = fis; |
| 1502 |
} else { |
| 1503 |
ForLoop fl = new ForLoop(forPos); |
| 1504 |
fl.setInitializer(init); |
| 1505 |
fl.setCondition(cond); |
| 1506 |
fl.setIncrement(incr); |
| 1507 |
pn = fl; |
| 1508 |
} |
| 1509 |
|
| 1510 |
|
| 1511 |
currentScope.replaceWith(pn); |
| 1512 |
popScope(); |
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
enterLoop(pn); |
| 1518 |
try { |
| 1519 |
AstNode body = statement(); |
| 1520 |
pn.setLength(getNodeEnd(body) - forPos); |
| 1521 |
pn.setBody(body); |
| 1522 |
} finally { |
| 1523 |
exitLoop(); |
| 1524 |
} |
| 1525 |
|
| 1526 |
} finally { |
| 1527 |
if (currentScope == tempScope) { |
| 1528 |
popScope(); |
| 1529 |
} |
| 1530 |
} |
| 1531 |
pn.setParens(lp, rp); |
| 1532 |
pn.setLineno(lineno); |
| 1533 |
return pn; |
| 1534 |
} |
| 1535 |
|
| 1536 |
private AstNode forLoopInit(int tt) throws IOException { |
| 1537 |
try { |
| 1538 |
inForInit = true; |
| 1539 |
AstNode init = null; |
| 1540 |
if (tt == Token.SEMI) { |
| 1541 |
init = new EmptyExpression(ts.tokenBeg, 1); |
| 1542 |
init.setLineno(ts.lineno); |
| 1543 |
} else if (tt == Token.VAR || tt == Token.LET) { |
| 1544 |
consumeToken(); |
| 1545 |
init = variables(tt, ts.tokenBeg, false); |
| 1546 |
} else { |
| 1547 |
init = expr(); |
| 1548 |
markDestructuring(init); |
| 1549 |
} |
| 1550 |
return init; |
| 1551 |
} finally { |
| 1552 |
inForInit = false; |
| 1553 |
} |
| 1554 |
} |
| 1555 |
|
| 1556 |
private TryStatement tryStatement() |
| 1557 |
throws IOException |
| 1558 |
{ |
| 1559 |
if (currentToken != Token.TRY) codeBug(); |
| 1560 |
consumeToken(); |
| 1561 |
|
| 1562 |
|
| 1563 |
Comment jsdocNode = getAndResetJsDoc(); |
| 1564 |
|
| 1565 |
int tryPos = ts.tokenBeg, lineno = ts.lineno, finallyPos = -1; |
| 1566 |
if (peekToken() != Token.LC) { |
| 1567 |
reportError("msg.no.brace.try"); |
| 1568 |
} |
| 1569 |
AstNode tryBlock = statement(); |
| 1570 |
int tryEnd = getNodeEnd(tryBlock); |
| 1571 |
|
| 1572 |
List<CatchClause> clauses = null; |
| 1573 |
|
| 1574 |
boolean sawDefaultCatch = false; |
| 1575 |
int peek = peekToken(); |
| 1576 |
if (peek == Token.CATCH) { |
| 1577 |
while (matchToken(Token.CATCH)) { |
| 1578 |
int catchLineNum = ts.lineno; |
| 1579 |
if (sawDefaultCatch) { |
| 1580 |
reportError("msg.catch.unreachable"); |
| 1581 |
} |
| 1582 |
int catchPos = ts.tokenBeg, lp = -1, rp = -1, guardPos = -1; |
| 1583 |
if (mustMatchToken(Token.LP, "msg.no.paren.catch")) |
| 1584 |
lp = ts.tokenBeg; |
| 1585 |
|
| 1586 |
mustMatchToken(Token.NAME, "msg.bad.catchcond"); |
| 1587 |
Name varName = createNameNode(); |
| 1588 |
String varNameString = varName.getIdentifier(); |
| 1589 |
if (inUseStrictDirective) { |
| 1590 |
if ("eval".equals(varNameString) || |
| 1591 |
"arguments".equals(varNameString)) |
| 1592 |
{ |
| 1593 |
reportError("msg.bad.id.strict", varNameString); |
| 1594 |
} |
| 1595 |
} |
| 1596 |
|
| 1597 |
AstNode catchCond = null; |
| 1598 |
if (matchToken(Token.IF)) { |
| 1599 |
guardPos = ts.tokenBeg; |
| 1600 |
catchCond = expr(); |
| 1601 |
} else { |
| 1602 |
sawDefaultCatch = true; |
| 1603 |
} |
| 1604 |
|
| 1605 |
if (mustMatchToken(Token.RP, "msg.bad.catchcond")) |
| 1606 |
rp = ts.tokenBeg; |
| 1607 |
mustMatchToken(Token.LC, "msg.no.brace.catchblock"); |
| 1608 |
|
| 1609 |
Block catchBlock = (Block)statements(); |
| 1610 |
tryEnd = getNodeEnd(catchBlock); |
| 1611 |
CatchClause catchNode = new CatchClause(catchPos); |
| 1612 |
catchNode.setVarName(varName); |
| 1613 |
catchNode.setCatchCondition(catchCond); |
| 1614 |
catchNode.setBody(catchBlock); |
| 1615 |
if (guardPos != -1) { |
| 1616 |
catchNode.setIfPosition(guardPos - catchPos); |
| 1617 |
} |
| 1618 |
catchNode.setParens(lp, rp); |
| 1619 |
catchNode.setLineno(catchLineNum); |
| 1620 |
|
| 1621 |
if (mustMatchToken(Token.RC, "msg.no.brace.after.body")) |
| 1622 |
tryEnd = ts.tokenEnd; |
| 1623 |
catchNode.setLength(tryEnd - catchPos); |
| 1624 |
if (clauses == null) |
| 1625 |
clauses = new ArrayList<CatchClause>(); |
| 1626 |
clauses.add(catchNode); |
| 1627 |
} |
| 1628 |
} else if (peek != Token.FINALLY) { |
| 1629 |
mustMatchToken(Token.FINALLY, "msg.try.no.catchfinally"); |
| 1630 |
} |
| 1631 |
|
| 1632 |
AstNode finallyBlock = null; |
| 1633 |
if (matchToken(Token.FINALLY)) { |
| 1634 |
finallyPos = ts.tokenBeg; |
| 1635 |
finallyBlock = statement(); |
| 1636 |
tryEnd = getNodeEnd(finallyBlock); |
| 1637 |
} |
| 1638 |
|
| 1639 |
TryStatement pn = new TryStatement(tryPos, tryEnd - tryPos); |
| 1640 |
pn.setTryBlock(tryBlock); |
| 1641 |
pn.setCatchClauses(clauses); |
| 1642 |
pn.setFinallyBlock(finallyBlock); |
| 1643 |
if (finallyPos != -1) { |
| 1644 |
pn.setFinallyPosition(finallyPos - tryPos); |
| 1645 |
} |
| 1646 |
pn.setLineno(lineno); |
| 1647 |
|
| 1648 |
if (jsdocNode != null) { |
| 1649 |
pn.setJsDocNode(jsdocNode); |
| 1650 |
} |
| 1651 |
|
| 1652 |
return pn; |
| 1653 |
} |
| 1654 |
|
| 1655 |
private ThrowStatement throwStatement() |
| 1656 |
throws IOException |
| 1657 |
{ |
| 1658 |
if (currentToken != Token.THROW) codeBug(); |
| 1659 |
consumeToken(); |
| 1660 |
int pos = ts.tokenBeg, lineno = ts.lineno; |
| 1661 |
if (peekTokenOrEOL() == Token.EOL) { |
| 1662 |
|
| 1663 |
|
| 1664 |
reportError("msg.bad.throw.eol"); |
| 1665 |
} |
| 1666 |
AstNode expr = expr(); |
| 1667 |
ThrowStatement pn = new ThrowStatement(pos, getNodeEnd(expr), expr); |
| 1668 |
pn.setLineno(lineno); |
| 1669 |
return pn; |
| 1670 |
} |
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
private LabeledStatement matchJumpLabelName() |
| 1679 |
throws IOException |
| 1680 |
{ |
| 1681 |
LabeledStatement label = null; |
| 1682 |
|
| 1683 |
if (peekTokenOrEOL() == Token.NAME) { |
| 1684 |
consumeToken(); |
| 1685 |
if (labelSet != null) { |
| 1686 |
label = labelSet.get(ts.getString()); |
| 1687 |
} |
| 1688 |
if (label == null) { |
| 1689 |
reportError("msg.undef.label"); |
| 1690 |
} |
| 1691 |
} |
| 1692 |
|
| 1693 |
return label; |
| 1694 |
} |
| 1695 |
|
| 1696 |
private BreakStatement breakStatement() |
| 1697 |
throws IOException |
| 1698 |
{ |
| 1699 |
if (currentToken != Token.BREAK) codeBug(); |
| 1700 |
consumeToken(); |
| 1701 |
int lineno = ts.lineno, pos = ts.tokenBeg, end = ts.tokenEnd; |
| 1702 |
Name breakLabel = null; |
| 1703 |
if (peekTokenOrEOL() == Token.NAME) { |
| 1704 |
breakLabel = createNameNode(); |
| 1705 |
end = getNodeEnd(breakLabel); |
| 1706 |
} |
| 1707 |
|
| 1708 |
|
| 1709 |
LabeledStatement labels = matchJumpLabelName(); |
| 1710 |
|
| 1711 |
Jump breakTarget = labels == null ? null : labels.getFirstLabel(); |
| 1712 |
|
| 1713 |
if (breakTarget == null && breakLabel == null) { |
| 1714 |
if (loopAndSwitchSet == null || loopAndSwitchSet.size() == 0) { |
| 1715 |
if (breakLabel == null) { |
| 1716 |
reportError("msg.bad.break", pos, end - pos); |
| 1717 |
} |
| 1718 |
} else { |
| 1719 |
breakTarget = loopAndSwitchSet.get(loopAndSwitchSet.size() - 1); |
| 1720 |
} |
| 1721 |
} |
| 1722 |
|
| 1723 |
BreakStatement pn = new BreakStatement(pos, end - pos); |
| 1724 |
pn.setBreakLabel(breakLabel); |
| 1725 |
|
| 1726 |
if (breakTarget != null) |
| 1727 |
pn.setBreakTarget(breakTarget); |
| 1728 |
pn.setLineno(lineno); |
| 1729 |
return pn; |
| 1730 |
} |
| 1731 |
|
| 1732 |
private ContinueStatement continueStatement() |
| 1733 |
throws IOException |
| 1734 |
{ |
| 1735 |
if (currentToken != Token.CONTINUE) codeBug(); |
| 1736 |
consumeToken(); |
| 1737 |
int lineno = ts.lineno, pos = ts.tokenBeg, end = ts.tokenEnd; |
| 1738 |
Name label = null; |
| 1739 |
if (peekTokenOrEOL() == Token.NAME) { |
| 1740 |
label = createNameNode(); |
| 1741 |
end = getNodeEnd(label); |
| 1742 |
} |
| 1743 |
|
| 1744 |
|
| 1745 |
LabeledStatement labels = matchJumpLabelName(); |
| 1746 |
Loop target = null; |
| 1747 |
if (labels == null && label == null) { |
| 1748 |
if (loopSet == null || loopSet.size() == 0) { |
| 1749 |
reportError("msg.continue.outside"); |
| 1750 |
} else { |
| 1751 |
target = loopSet.get(loopSet.size() - 1); |
| 1752 |
} |
| 1753 |
} else { |
| 1754 |
if (labels == null || !(labels.getStatement() instanceof Loop)) { |
| 1755 |
reportError("msg.continue.nonloop", pos, end - pos); |
| 1756 |
} |
| 1757 |
target = labels == null ? null : (Loop)labels.getStatement(); |
| 1758 |
} |
| 1759 |
|
| 1760 |
ContinueStatement pn = new ContinueStatement(pos, end - pos); |
| 1761 |
if (target != null) |
| 1762 |
pn.setTarget(target); |
| 1763 |
pn.setLabel(label); |
| 1764 |
pn.setLineno(lineno); |
| 1765 |
return pn; |
| 1766 |
} |
| 1767 |
|
| 1768 |
private WithStatement withStatement() |
| 1769 |
throws IOException |
| 1770 |
{ |
| 1771 |
if (currentToken != Token.WITH) codeBug(); |
| 1772 |
consumeToken(); |
| 1773 |
|
| 1774 |
Comment withComment = getAndResetJsDoc(); |
| 1775 |
|
| 1776 |
int lineno = ts.lineno, pos = ts.tokenBeg, lp = -1, rp = -1; |
| 1777 |
if (mustMatchToken(Token.LP, "msg.no.paren.with")) |
| 1778 |
lp = ts.tokenBeg; |
| 1779 |
|
| 1780 |
AstNode obj = expr(); |
| 1781 |
|
| 1782 |
if (mustMatchToken(Token.RP, "msg.no.paren.after.with")) |
| 1783 |
rp = ts.tokenBeg; |
| 1784 |
|
| 1785 |
AstNode body = statement(); |
| 1786 |
|
| 1787 |
WithStatement pn = new WithStatement(pos, getNodeEnd(body) - pos); |
| 1788 |
pn.setJsDocNode(withComment); |
| 1789 |
pn.setExpression(obj); |
| 1790 |
pn.setStatement(body); |
| 1791 |
pn.setParens(lp, rp); |
| 1792 |
pn.setLineno(lineno); |
| 1793 |
return pn; |
| 1794 |
} |
| 1795 |
|
| 1796 |
private AstNode letStatement() |
| 1797 |
throws IOException |
| 1798 |
{ |
| 1799 |
if (currentToken != Token.LET) codeBug(); |
| 1800 |
consumeToken(); |
| 1801 |
int lineno = ts.lineno, pos = ts.tokenBeg; |
| 1802 |
AstNode pn; |
| 1803 |
if (peekToken() == Token.LP) { |
| 1804 |
pn = let(true, pos); |
| 1805 |
} else { |
| 1806 |
pn = variables(Token.LET, pos, true); |
| 1807 |
} |
| 1808 |
pn.setLineno(lineno); |
| 1809 |
return pn; |
| 1810 |
} |
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
private static final boolean nowAllSet(int before, int after, int mask) { |
| 1821 |
return ((before & mask) != mask) && ((after & mask) == mask); |
| 1822 |
} |
| 1823 |
|
| 1824 |
private AstNode returnOrYield(int tt, boolean exprContext) |
| 1825 |
throws IOException |
| 1826 |
{ |
| 1827 |
if (!insideFunction()) { |
| 1828 |
reportError(tt == Token.RETURN ? "msg.bad.return" |
| 1829 |
: "msg.bad.yield"); |
| 1830 |
} |
| 1831 |
consumeToken(); |
| 1832 |
int lineno = ts.lineno, pos = ts.tokenBeg, end = ts.tokenEnd; |
| 1833 |
|
| 1834 |
AstNode e = null; |
| 1835 |
|
| 1836 |
switch (peekTokenOrEOL()) { |
| 1837 |
case Token.SEMI: case Token.RC: case Token.RB: case Token.RP: |
| 1838 |
case Token.EOF: case Token.EOL: case Token.ERROR: case Token.YIELD: |
| 1839 |
break; |
| 1840 |
default: |
| 1841 |
e = expr(); |
| 1842 |
end = getNodeEnd(e); |
| 1843 |
} |
| 1844 |
|
| 1845 |
int before = endFlags; |
| 1846 |
AstNode ret; |
| 1847 |
|
| 1848 |
if (tt == Token.RETURN) { |
| 1849 |
endFlags |= e == null ? Node.END_RETURNS : Node.END_RETURNS_VALUE; |
| 1850 |
ret = new ReturnStatement(pos, end - pos, e); |
| 1851 |
|
| 1852 |
|
| 1853 |
if (nowAllSet(before, endFlags, |
| 1854 |
Node.END_RETURNS|Node.END_RETURNS_VALUE)) |
| 1855 |
addStrictWarning("msg.return.inconsistent", "", pos, end - pos); |
| 1856 |
} else { |
| 1857 |
if (!insideFunction()) |
| 1858 |
reportError("msg.bad.yield"); |
| 1859 |
endFlags |= Node.END_YIELDS; |
| 1860 |
ret = new Yield(pos, end - pos, e); |
| 1861 |
setRequiresActivation(); |
| 1862 |
setIsGenerator(); |
| 1863 |
if (!exprContext) { |
| 1864 |
ret = new ExpressionStatement(ret); |
| 1865 |
} |
| 1866 |
} |
| 1867 |
|
| 1868 |
|
| 1869 |
if (insideFunction() |
| 1870 |
&& nowAllSet(before, endFlags, |
| 1871 |
Node.END_YIELDS|Node.END_RETURNS_VALUE)) { |
| 1872 |
Name name = ((FunctionNode)currentScriptOrFn).getFunctionName(); |
| 1873 |
if (name == null || name.length() == 0) |
| 1874 |
addError("msg.anon.generator.returns", ""); |
| 1875 |
else |
| 1876 |
addError("msg.generator.returns", name.getIdentifier()); |
| 1877 |
} |
| 1878 |
|
| 1879 |
ret.setLineno(lineno); |
| 1880 |
return ret; |
| 1881 |
} |
| 1882 |
|
| 1883 |
private AstNode block() |
| 1884 |
throws IOException |
| 1885 |
{ |
| 1886 |
if (currentToken != Token.LC) codeBug(); |
| 1887 |
consumeToken(); |
| 1888 |
int pos = ts.tokenBeg; |
| 1889 |
Scope block = new Scope(pos); |
| 1890 |
block.setLineno(ts.lineno); |
| 1891 |
pushScope(block); |
| 1892 |
try { |
| 1893 |
statements(block); |
| 1894 |
mustMatchToken(Token.RC, "msg.no.brace.block"); |
| 1895 |
block.setLength(ts.tokenEnd - pos); |
| 1896 |
return block; |
| 1897 |
} finally { |
| 1898 |
popScope(); |
| 1899 |
} |
| 1900 |
} |
| 1901 |
|
| 1902 |
private AstNode defaultXmlNamespace() |
| 1903 |
throws IOException |
| 1904 |
{ |
| 1905 |
if (currentToken != Token.DEFAULT) codeBug(); |
| 1906 |
consumeToken(); |
| 1907 |
mustHaveXML(); |
| 1908 |
setRequiresActivation(); |
| 1909 |
int lineno = ts.lineno, pos = ts.tokenBeg; |
| 1910 |
|
| 1911 |
if (!(matchToken(Token.NAME) && "xml".equals(ts.getString()))) { |
| 1912 |
reportError("msg.bad.namespace"); |
| 1913 |
} |
| 1914 |
if (!(matchToken(Token.NAME) && "namespace".equals(ts.getString()))) { |
| 1915 |
reportError("msg.bad.namespace"); |
| 1916 |
} |
| 1917 |
if (!matchToken(Token.ASSIGN)) { |
| 1918 |
reportError("msg.bad.namespace"); |
| 1919 |
} |
| 1920 |
|
| 1921 |
AstNode e = expr(); |
| 1922 |
UnaryExpression dxmln = new UnaryExpression(pos, getNodeEnd(e) - pos); |
| 1923 |
dxmln.setOperator(Token.DEFAULTNAMESPACE); |
| 1924 |
dxmln.setOperand(e); |
| 1925 |
dxmln.setLineno(lineno); |
| 1926 |
|
| 1927 |
ExpressionStatement es = new ExpressionStatement(dxmln, true); |
| 1928 |
return es; |
| 1929 |
} |
| 1930 |
|
| 1931 |
private void recordLabel(Label label, LabeledStatement bundle) |
| 1932 |
throws IOException |
| 1933 |
{ |
| 1934 |
|
| 1935 |
if (peekToken() != Token.COLON) codeBug(); |
| 1936 |
consumeToken(); |
| 1937 |
String name = label.getName(); |
| 1938 |
if (labelSet == null) { |
| 1939 |
labelSet = new HashMap<String,LabeledStatement>(); |
| 1940 |
} else { |
| 1941 |
LabeledStatement ls = labelSet.get(name); |
| 1942 |
if (ls != null) { |
| 1943 |
if (compilerEnv.isIdeMode()) { |
| 1944 |
Label dup = ls.getLabelByName(name); |
| 1945 |
reportError("msg.dup.label", |
| 1946 |
dup.getAbsolutePosition(), dup.getLength()); |
| 1947 |
} |
| 1948 |
reportError("msg.dup.label", |
| 1949 |
label.getPosition(), label.getLength()); |
| 1950 |
} |
| 1951 |
} |
| 1952 |
bundle.addLabel(label); |
| 1953 |
labelSet.put(name, bundle); |
| 1954 |
} |
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
private AstNode nameOrLabel() |
| 1963 |
throws IOException |
| 1964 |
{ |
| 1965 |
if (currentToken != Token.NAME) throw codeBug(); |
| 1966 |
int pos = ts.tokenBeg; |
| 1967 |
|
| 1968 |
|
| 1969 |
currentFlaggedToken |= TI_CHECK_LABEL; |
| 1970 |
AstNode expr = expr(); |
| 1971 |
|
| 1972 |
if (expr.getType() != Token.LABEL) { |
| 1973 |
AstNode n = new ExpressionStatement(expr, !insideFunction()); |
| 1974 |
n.lineno = expr.lineno; |
| 1975 |
return n; |
| 1976 |
} |
| 1977 |
|
| 1978 |
LabeledStatement bundle = new LabeledStatement(pos); |
| 1979 |
recordLabel((Label)expr, bundle); |
| 1980 |
bundle.setLineno(ts.lineno); |
| 1981 |
|
| 1982 |
AstNode stmt = null; |
| 1983 |
while (peekToken() == Token.NAME) { |
| 1984 |
currentFlaggedToken |= TI_CHECK_LABEL; |
| 1985 |
expr = expr(); |
| 1986 |
if (expr.getType() != Token.LABEL) { |
| 1987 |
stmt = new ExpressionStatement(expr, !insideFunction()); |
| 1988 |
autoInsertSemicolon(stmt); |
| 1989 |
break; |
| 1990 |
} |
| 1991 |
recordLabel((Label)expr, bundle); |
| 1992 |
} |
| 1993 |
|
| 1994 |
|
| 1995 |
try { |
| 1996 |
currentLabel = bundle; |
| 1997 |
if (stmt == null) { |
| 1998 |
stmt = statementHelper(); |
| 1999 |
} |
| 2000 |
} finally { |
| 2001 |
currentLabel = null; |
| 2002 |
|
| 2003 |
for (Label lb : bundle.getLabels()) { |
| 2004 |
labelSet.remove(lb.getName()); |
| 2005 |
} |
| 2006 |
} |
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
bundle.setLength(stmt.getParent() == null |
| 2011 |
? getNodeEnd(stmt) - pos |
| 2012 |
: getNodeEnd(stmt)); |
| 2013 |
bundle.setStatement(stmt); |
| 2014 |
return bundle; |
| 2015 |
} |
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
private VariableDeclaration variables(int declType, int pos, boolean isStatement) |
| 2028 |
throws IOException |
| 2029 |
{ |
| 2030 |
int end; |
| 2031 |
VariableDeclaration pn = new VariableDeclaration(pos); |
| 2032 |
pn.setType(declType); |
| 2033 |
pn.setLineno(ts.lineno); |
| 2034 |
Comment varjsdocNode = getAndResetJsDoc(); |
| 2035 |
if (varjsdocNode != null) { |
| 2036 |
pn.setJsDocNode(varjsdocNode); |
| 2037 |
} |
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
for (;;) { |
| 2042 |
AstNode destructuring = null; |
| 2043 |
Name name = null; |
| 2044 |
int tt = peekToken(), kidPos = ts.tokenBeg; |
| 2045 |
end = ts.tokenEnd; |
| 2046 |
|
| 2047 |
if (tt == Token.LB || tt == Token.LC) { |
| 2048 |
|
| 2049 |
destructuring = destructuringPrimaryExpr(); |
| 2050 |
end = getNodeEnd(destructuring); |
| 2051 |
if (!(destructuring instanceof DestructuringForm)) |
| 2052 |
reportError("msg.bad.assign.left", kidPos, end - kidPos); |
| 2053 |
markDestructuring(destructuring); |
| 2054 |
} else { |
| 2055 |
|
| 2056 |
mustMatchToken(Token.NAME, "msg.bad.var"); |
| 2057 |
name = createNameNode(); |
| 2058 |
name.setLineno(ts.getLineno()); |
| 2059 |
if (inUseStrictDirective) { |
| 2060 |
String id = ts.getString(); |
| 2061 |
if ("eval".equals(id) || "arguments".equals(ts.getString())) |
| 2062 |
{ |
| 2063 |
reportError("msg.bad.id.strict", id); |
| 2064 |
} |
| 2065 |
} |
| 2066 |
defineSymbol(declType, ts.getString(), inForInit); |
| 2067 |
} |
| 2068 |
|
| 2069 |
int lineno = ts.lineno; |
| 2070 |
|
| 2071 |
Comment jsdocNode = getAndResetJsDoc(); |
| 2072 |
|
| 2073 |
AstNode init = null; |
| 2074 |
if (matchToken(Token.ASSIGN)) { |
| 2075 |
init = assignExpr(); |
| 2076 |
end = getNodeEnd(init); |
| 2077 |
} |
| 2078 |
|
| 2079 |
VariableInitializer vi = new VariableInitializer(kidPos, end - kidPos); |
| 2080 |
if (destructuring != null) { |
| 2081 |
if (init == null && !inForInit) { |
| 2082 |
reportError("msg.destruct.assign.no.init"); |
| 2083 |
} |
| 2084 |
vi.setTarget(destructuring); |
| 2085 |
} else { |
| 2086 |
vi.setTarget(name); |
| 2087 |
} |
| 2088 |
vi.setInitializer(init); |
| 2089 |
vi.setType(declType); |
| 2090 |
vi.setJsDocNode(jsdocNode); |
| 2091 |
vi.setLineno(lineno); |
| 2092 |
pn.addVariable(vi); |
| 2093 |
|
| 2094 |
if (!matchToken(Token.COMMA)) |
| 2095 |
break; |
| 2096 |
} |
| 2097 |
pn.setLength(end - pos); |
| 2098 |
pn.setIsStatement(isStatement); |
| 2099 |
return pn; |
| 2100 |
} |
| 2101 |
|
| 2102 |
|
| 2103 |
private AstNode let(boolean isStatement, int pos) |
| 2104 |
throws IOException |
| 2105 |
{ |
| 2106 |
LetNode pn = new LetNode(pos); |
| 2107 |
pn.setLineno(ts.lineno); |
| 2108 |
if (mustMatchToken(Token.LP, "msg.no.paren.after.let")) |
| 2109 |
pn.setLp(ts.tokenBeg - pos); |
| 2110 |
pushScope(pn); |
| 2111 |
try { |
| 2112 |
VariableDeclaration vars = variables(Token.LET, ts.tokenBeg, isStatement); |
| 2113 |
pn.setVariables(vars); |
| 2114 |
if (mustMatchToken(Token.RP, "msg.no.paren.let")) { |
| 2115 |
pn.setRp(ts.tokenBeg - pos); |
| 2116 |
} |
| 2117 |
if (isStatement && peekToken() == Token.LC) { |
| 2118 |
|
| 2119 |
consumeToken(); |
| 2120 |
int beg = ts.tokenBeg; |
| 2121 |
AstNode stmt = statements(); |
| 2122 |
mustMatchToken(Token.RC, "msg.no.curly.let"); |
| 2123 |
stmt.setLength(ts.tokenEnd - beg); |
| 2124 |
pn.setLength(ts.tokenEnd - pos); |
| 2125 |
pn.setBody(stmt); |
| 2126 |
pn.setType(Token.LET); |
| 2127 |
} else { |
| 2128 |
|
| 2129 |
AstNode expr = expr(); |
| 2130 |
pn.setLength(getNodeEnd(expr) - pos); |
| 2131 |
pn.setBody(expr); |
| 2132 |
if (isStatement) { |
| 2133 |
|
| 2134 |
ExpressionStatement es = |
| 2135 |
new ExpressionStatement(pn, !insideFunction()); |
| 2136 |
es.setLineno(pn.getLineno()); |
| 2137 |
return es; |
| 2138 |
} |
| 2139 |
} |
| 2140 |
} finally { |
| 2141 |
popScope(); |
| 2142 |
} |
| 2143 |
return pn; |
| 2144 |
} |
| 2145 |
|
| 2146 |
void defineSymbol(int declType, String name) { |
| 2147 |
defineSymbol(declType, name, false); |
| 2148 |
} |
| 2149 |
|
| 2150 |
void defineSymbol(int declType, String name, boolean ignoreNotInBlock) { |
| 2151 |
if (name == null) { |
| 2152 |
if (compilerEnv.isIdeMode()) { |
| 2153 |
return; |
| 2154 |
} else { |
| 2155 |
codeBug(); |
| 2156 |
} |
| 2157 |
} |
| 2158 |
Scope definingScope = currentScope.getDefiningScope(name); |
| 2159 |
Symbol symbol = definingScope != null |
| 2160 |
? definingScope.getSymbol(name) |
| 2161 |
: null; |
| 2162 |
int symDeclType = symbol != null ? symbol.getDeclType() : -1; |
| 2163 |
if (symbol != null |
| 2164 |
&& (symDeclType == Token.CONST |
| 2165 |
|| declType == Token.CONST |
| 2166 |
|| (definingScope == currentScope && symDeclType == Token.LET))) |
| 2167 |
{ |
| 2168 |
addError(symDeclType == Token.CONST ? "msg.const.redecl" : |
| 2169 |
symDeclType == Token.LET ? "msg.let.redecl" : |
| 2170 |
symDeclType == Token.VAR ? "msg.var.redecl" : |
| 2171 |
symDeclType == Token.FUNCTION ? "msg.fn.redecl" : |
| 2172 |
"msg.parm.redecl", name); |
| 2173 |
return; |
| 2174 |
} |
| 2175 |
switch (declType) { |
| 2176 |
case Token.LET: |
| 2177 |
if (!ignoreNotInBlock && |
| 2178 |
((currentScope.getType() == Token.IF) || |
| 2179 |
currentScope instanceof Loop)) { |
| 2180 |
addError("msg.let.decl.not.in.block"); |
| 2181 |
return; |
| 2182 |
} |
| 2183 |
currentScope.putSymbol(new Symbol(declType, name)); |
| 2184 |
return; |
| 2185 |
|
| 2186 |
case Token.VAR: |
| 2187 |
case Token.CONST: |
| 2188 |
case Token.FUNCTION: |
| 2189 |
if (symbol != null) { |
| 2190 |
if (symDeclType == Token.VAR) |
| 2191 |
addStrictWarning("msg.var.redecl", name); |
| 2192 |
else if (symDeclType == Token.LP) { |
| 2193 |
addStrictWarning("msg.var.hides.arg", name); |
| 2194 |
} |
| 2195 |
} else { |
| 2196 |
currentScriptOrFn.putSymbol(new Symbol(declType, name)); |
| 2197 |
} |
| 2198 |
return; |
| 2199 |
|
| 2200 |
case Token.LP: |
| 2201 |
if (symbol != null) { |
| 2202 |
|
| 2203 |
|
| 2204 |
addWarning("msg.dup.parms", name); |
| 2205 |
} |
| 2206 |
currentScriptOrFn.putSymbol(new Symbol(declType, name)); |
| 2207 |
return; |
| 2208 |
|
| 2209 |
default: |
| 2210 |
throw codeBug(); |
| 2211 |
} |
| 2212 |
} |
| 2213 |
|
| 2214 |
private AstNode expr() |
| 2215 |
throws IOException |
| 2216 |
{ |
| 2217 |
AstNode pn = assignExpr(); |
| 2218 |
int pos = pn.getPosition(); |
| 2219 |
while (matchToken(Token.COMMA)) { |
| 2220 |
int opPos = ts.tokenBeg; |
| 2221 |
if (compilerEnv.isStrictMode() && !pn.hasSideEffects()) |
| 2222 |
addStrictWarning("msg.no.side.effects", "", |
| 2223 |
pos, nodeEnd(pn) - pos); |
| 2224 |
if (peekToken() == Token.YIELD) |
| 2225 |
reportError("msg.yield.parenthesized"); |
| 2226 |
pn = new InfixExpression(Token.COMMA, pn, assignExpr(), opPos); |
| 2227 |
} |
| 2228 |
return pn; |
| 2229 |
} |
| 2230 |
|
| 2231 |
private AstNode assignExpr() |
| 2232 |
throws IOException |
| 2233 |
{ |
| 2234 |
int tt = peekToken(); |
| 2235 |
if (tt == Token.YIELD) { |
| 2236 |
return returnOrYield(tt, true); |
| 2237 |
} |
| 2238 |
AstNode pn = condExpr(); |
| 2239 |
boolean hasEOL = false; |
| 2240 |
tt = peekTokenOrEOL(); |
| 2241 |
if (tt == Token.EOL) { |
| 2242 |
hasEOL = true; |
| 2243 |
tt = peekToken(); |
| 2244 |
} |
| 2245 |
if (Token.FIRST_ASSIGN <= tt && tt <= Token.LAST_ASSIGN) { |
| 2246 |
consumeToken(); |
| 2247 |
|
| 2248 |
|
| 2249 |
Comment jsdocNode = getAndResetJsDoc(); |
| 2250 |
|
| 2251 |
markDestructuring(pn); |
| 2252 |
int opPos = ts.tokenBeg; |
| 2253 |
|
| 2254 |
pn = new Assignment(tt, pn, assignExpr(), opPos); |
| 2255 |
|
| 2256 |
if (jsdocNode != null) { |
| 2257 |
pn.setJsDocNode(jsdocNode); |
| 2258 |
} |
| 2259 |
} else if (tt == Token.SEMI) { |
| 2260 |
|
| 2261 |
|
| 2262 |
if (currentJsDocComment != null) { |
| 2263 |
pn.setJsDocNode(getAndResetJsDoc()); |
| 2264 |
} |
| 2265 |
} else if (!hasEOL && tt == Token.ARROW) { |
| 2266 |
consumeToken(); |
| 2267 |
pn = arrowFunction(pn); |
| 2268 |
} |
| 2269 |
return pn; |
| 2270 |
} |
| 2271 |
|
| 2272 |
private AstNode condExpr() |
| 2273 |
throws IOException |
| 2274 |
{ |
| 2275 |
AstNode pn = orExpr(); |
| 2276 |
if (matchToken(Token.HOOK)) { |
| 2277 |
int line = ts.lineno; |
| 2278 |
int qmarkPos = ts.tokenBeg, colonPos = -1; |
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
boolean wasInForInit = inForInit; |
| 2285 |
inForInit = false; |
| 2286 |
AstNode ifTrue; |
| 2287 |
try { |
| 2288 |
ifTrue = assignExpr(); |
| 2289 |
} finally { |
| 2290 |
inForInit = wasInForInit; |
| 2291 |
} |
| 2292 |
if (mustMatchToken(Token.COLON, "msg.no.colon.cond")) |
| 2293 |
colonPos = ts.tokenBeg; |
| 2294 |
AstNode ifFalse = assignExpr(); |
| 2295 |
int beg = pn.getPosition(), len = getNodeEnd(ifFalse) - beg; |
| 2296 |
ConditionalExpression ce = new ConditionalExpression(beg, len); |
| 2297 |
ce.setLineno(line); |
| 2298 |
ce.setTestExpression(pn); |
| 2299 |
ce.setTrueExpression(ifTrue); |
| 2300 |
ce.setFalseExpression(ifFalse); |
| 2301 |
ce.setQuestionMarkPosition(qmarkPos - beg); |
| 2302 |
ce.setColonPosition(colonPos - beg); |
| 2303 |
pn = ce; |
| 2304 |
} |
| 2305 |
return pn; |
| 2306 |
} |
| 2307 |
|
| 2308 |
private AstNode orExpr() |
| 2309 |
throws IOException |
| 2310 |
{ |
| 2311 |
AstNode pn = andExpr(); |
| 2312 |
if (matchToken(Token.OR)) { |
| 2313 |
int opPos = ts.tokenBeg; |
| 2314 |
pn = new InfixExpression(Token.OR, pn, orExpr(), opPos); |
| 2315 |
} |
| 2316 |
return pn; |
| 2317 |
} |
| 2318 |
|
| 2319 |
private AstNode andExpr() |
| 2320 |
throws IOException |
| 2321 |
{ |
| 2322 |
AstNode pn = bitOrExpr(); |
| 2323 |
if (matchToken(Token.AND)) { |
| 2324 |
int opPos = ts.tokenBeg; |
| 2325 |
pn = new InfixExpression(Token.AND, pn, andExpr(), opPos); |
| 2326 |
} |
| 2327 |
return pn; |
| 2328 |
} |
| 2329 |
|
| 2330 |
private AstNode bitOrExpr() |
| 2331 |
throws IOException |
| 2332 |
{ |
| 2333 |
AstNode pn = bitXorExpr(); |
| 2334 |
while (matchToken(Token.BITOR)) { |
| 2335 |
int opPos = ts.tokenBeg; |
| 2336 |
pn = new InfixExpression(Token.BITOR, pn, bitXorExpr(), opPos); |
| 2337 |
} |
| 2338 |
return pn; |
| 2339 |
} |
| 2340 |
|
| 2341 |
private AstNode bitXorExpr() |
| 2342 |
throws IOException |
| 2343 |
{ |
| 2344 |
AstNode pn = bitAndExpr(); |
| 2345 |
while (matchToken(Token.BITXOR)) { |
| 2346 |
int opPos = ts.tokenBeg; |
| 2347 |
pn = new InfixExpression(Token.BITXOR, pn, bitAndExpr(), opPos); |
| 2348 |
} |
| 2349 |
return pn; |
| 2350 |
} |
| 2351 |
|
| 2352 |
private AstNode bitAndExpr() |
| 2353 |
throws IOException |
| 2354 |
{ |
| 2355 |
AstNode pn = eqExpr(); |
| 2356 |
while (matchToken(Token.BITAND)) { |
| 2357 |
int opPos = ts.tokenBeg; |
| 2358 |
pn = new InfixExpression(Token.BITAND, pn, eqExpr(), opPos); |
| 2359 |
} |
| 2360 |
return pn; |
| 2361 |
} |
| 2362 |
|
| 2363 |
private AstNode eqExpr() |
| 2364 |
throws IOException |
| 2365 |
{ |
| 2366 |
AstNode pn = relExpr(); |
| 2367 |
for (;;) { |
| 2368 |
int tt = peekToken(), opPos = ts.tokenBeg; |
| 2369 |
switch (tt) { |
| 2370 |
case Token.EQ: |
| 2371 |
case Token.NE: |
| 2372 |
case Token.SHEQ: |
| 2373 |
case Token.SHNE: |
| 2374 |
consumeToken(); |
| 2375 |
int parseToken = tt; |
| 2376 |
if (compilerEnv.getLanguageVersion() == Context.VERSION_1_2) { |
| 2377 |
|
| 2378 |
if (tt == Token.EQ) |
| 2379 |
parseToken = Token.SHEQ; |
| 2380 |
else if (tt == Token.NE) |
| 2381 |
parseToken = Token.SHNE; |
| 2382 |
} |
| 2383 |
pn = new InfixExpression(parseToken, pn, relExpr(), opPos); |
| 2384 |
continue; |
| 2385 |
} |
| 2386 |
break; |
| 2387 |
} |
| 2388 |
return pn; |
| 2389 |
} |
| 2390 |
|
| 2391 |
private AstNode relExpr() |
| 2392 |
throws IOException |
| 2393 |
{ |
| 2394 |
AstNode pn = shiftExpr(); |
| 2395 |
for (;;) { |
| 2396 |
int tt = peekToken(), opPos = ts.tokenBeg; |
| 2397 |
switch (tt) { |
| 2398 |
case Token.IN: |
| 2399 |
if (inForInit) |
| 2400 |
break; |
| 2401 |
|
| 2402 |
case Token.INSTANCEOF: |
| 2403 |
case Token.LE: |
| 2404 |
case Token.LT: |
| 2405 |
case Token.GE: |
| 2406 |
case Token.GT: |
| 2407 |
consumeToken(); |
| 2408 |
pn = new InfixExpression(tt, pn, shiftExpr(), opPos); |
| 2409 |
continue; |
| 2410 |
} |
| 2411 |
break; |
| 2412 |
} |
| 2413 |
return pn; |
| 2414 |
} |
| 2415 |
|
| 2416 |
private AstNode shiftExpr() |
| 2417 |
throws IOException |
| 2418 |
{ |
| 2419 |
AstNode pn = addExpr(); |
| 2420 |
for (;;) { |
| 2421 |
int tt = peekToken(), opPos = ts.tokenBeg; |
| 2422 |
switch (tt) { |
| 2423 |
case Token.LSH: |
| 2424 |
case Token.URSH: |
| 2425 |
case Token.RSH: |
| 2426 |
consumeToken(); |
| 2427 |
pn = new InfixExpression(tt, pn, addExpr(), opPos); |
| 2428 |
continue; |
| 2429 |
} |
| 2430 |
break; |
| 2431 |
} |
| 2432 |
return pn; |
| 2433 |
} |
| 2434 |
|
| 2435 |
private AstNode addExpr() |
| 2436 |
throws IOException |
| 2437 |
{ |
| 2438 |
AstNode pn = mulExpr(); |
| 2439 |
for (;;) { |
| 2440 |
int tt = peekToken(), opPos = ts.tokenBeg; |
| 2441 |
if (tt == Token.ADD || tt == Token.SUB) { |
| 2442 |
consumeToken(); |
| 2443 |
pn = new InfixExpression(tt, pn, mulExpr(), opPos); |
| 2444 |
continue; |
| 2445 |
} |
| 2446 |
break; |
| 2447 |
} |
| 2448 |
return pn; |
| 2449 |
} |
| 2450 |
|
| 2451 |
private AstNode mulExpr() |
| 2452 |
throws IOException |
| 2453 |
{ |
| 2454 |
AstNode pn = unaryExpr(); |
| 2455 |
for (;;) { |
| 2456 |
int tt = peekToken(), opPos = ts.tokenBeg; |
| 2457 |
switch (tt) { |
| 2458 |
case Token.MUL: |
| 2459 |
case Token.DIV: |
| 2460 |
case Token.MOD: |
| 2461 |
consumeToken(); |
| 2462 |
pn = new InfixExpression(tt, pn, unaryExpr(), opPos); |
| 2463 |
continue; |
| 2464 |
} |
| 2465 |
break; |
| 2466 |
} |
| 2467 |
return pn; |
| 2468 |
} |
| 2469 |
|
| 2470 |
private AstNode unaryExpr() |
| 2471 |
throws IOException |
| 2472 |
{ |
| 2473 |
AstNode node; |
| 2474 |
int tt = peekToken(); |
| 2475 |
int line = ts.lineno; |
| 2476 |
|
| 2477 |
switch(tt) { |
| 2478 |
case Token.VOID: |
| 2479 |
case Token.NOT: |
| 2480 |
case Token.BITNOT: |
| 2481 |
case Token.TYPEOF: |
| 2482 |
consumeToken(); |
| 2483 |
node = new UnaryExpression(tt, ts.tokenBeg, unaryExpr()); |
| 2484 |
node.setLineno(line); |
| 2485 |
return node; |
| 2486 |
|
| 2487 |
case Token.ADD: |
| 2488 |
consumeToken(); |
| 2489 |
|
| 2490 |
node = new UnaryExpression(Token.POS, ts.tokenBeg, unaryExpr()); |
| 2491 |
node.setLineno(line); |
| 2492 |
return node; |
| 2493 |
|
| 2494 |
case Token.SUB: |
| 2495 |
consumeToken(); |
| 2496 |
|
| 2497 |
node = new UnaryExpression(Token.NEG, ts.tokenBeg, unaryExpr()); |
| 2498 |
node.setLineno(line); |
| 2499 |
return node; |
| 2500 |
|
| 2501 |
case Token.INC: |
| 2502 |
case Token.DEC: |
| 2503 |
consumeToken(); |
| 2504 |
UnaryExpression expr = new UnaryExpression(tt, ts.tokenBeg, |
| 2505 |
memberExpr(true)); |
| 2506 |
expr.setLineno(line); |
| 2507 |
checkBadIncDec(expr); |
| 2508 |
return expr; |
| 2509 |
|
| 2510 |
case Token.DELPROP: |
| 2511 |
consumeToken(); |
| 2512 |
node = new UnaryExpression(tt, ts.tokenBeg, unaryExpr()); |
| 2513 |
node.setLineno(line); |
| 2514 |
return node; |
| 2515 |
|
| 2516 |
case Token.ERROR: |
| 2517 |
consumeToken(); |
| 2518 |
return makeErrorNode(); |
| 2519 |
|
| 2520 |
case Token.LT: |
| 2521 |
|
| 2522 |
if (compilerEnv.isXmlAvailable()) { |
| 2523 |
consumeToken(); |
| 2524 |
return memberExprTail(true, xmlInitializer()); |
| 2525 |
} |
| 2526 |
|
| 2527 |
|
| 2528 |
default: |
| 2529 |
AstNode pn = memberExpr(true); |
| 2530 |
|
| 2531 |
tt = peekTokenOrEOL(); |
| 2532 |
if (!(tt == Token.INC || tt == Token.DEC)) { |
| 2533 |
return pn; |
| 2534 |
} |
| 2535 |
consumeToken(); |
| 2536 |
UnaryExpression uexpr = |
| 2537 |
new UnaryExpression(tt, ts.tokenBeg, pn, true); |
| 2538 |
uexpr.setLineno(line); |
| 2539 |
checkBadIncDec(uexpr); |
| 2540 |
return uexpr; |
| 2541 |
} |
| 2542 |
} |
| 2543 |
|
| 2544 |
private AstNode xmlInitializer() |
| 2545 |
throws IOException |
| 2546 |
{ |
| 2547 |
if (currentToken != Token.LT) codeBug(); |
| 2548 |
int pos = ts.tokenBeg, tt = ts.getFirstXMLToken(); |
| 2549 |
if (tt != Token.XML && tt != Token.XMLEND) { |
| 2550 |
reportError("msg.syntax"); |
| 2551 |
return makeErrorNode(); |
| 2552 |
} |
| 2553 |
|
| 2554 |
XmlLiteral pn = new XmlLiteral(pos); |
| 2555 |
pn.setLineno(ts.lineno); |
| 2556 |
|
| 2557 |
for (;;tt = ts.getNextXMLToken()) { |
| 2558 |
switch (tt) { |
| 2559 |
case Token.XML: |
| 2560 |
pn.addFragment(new XmlString(ts.tokenBeg, ts.getString())); |
| 2561 |
mustMatchToken(Token.LC, "msg.syntax"); |
| 2562 |
int beg = ts.tokenBeg; |
| 2563 |
AstNode expr = (peekToken() == Token.RC) |
| 2564 |
? new EmptyExpression(beg, ts.tokenEnd - beg) |
| 2565 |
: expr(); |
| 2566 |
mustMatchToken(Token.RC, "msg.syntax"); |
| 2567 |
XmlExpression xexpr = new XmlExpression(beg, expr); |
| 2568 |
xexpr.setIsXmlAttribute(ts.isXMLAttribute()); |
| 2569 |
xexpr.setLength(ts.tokenEnd - beg); |
| 2570 |
pn.addFragment(xexpr); |
| 2571 |
break; |
| 2572 |
|
| 2573 |
case Token.XMLEND: |
| 2574 |
pn.addFragment(new XmlString(ts.tokenBeg, ts.getString())); |
| 2575 |
return pn; |
| 2576 |
|
| 2577 |
default: |
| 2578 |
reportError("msg.syntax"); |
| 2579 |
return makeErrorNode(); |
| 2580 |
} |
| 2581 |
} |
| 2582 |
} |
| 2583 |
|
| 2584 |
private List<AstNode> argumentList() |
| 2585 |
throws IOException |
| 2586 |
{ |
| 2587 |
if (matchToken(Token.RP)) |
| 2588 |
return null; |
| 2589 |
|
| 2590 |
List<AstNode> result = new ArrayList<AstNode>(); |
| 2591 |
boolean wasInForInit = inForInit; |
| 2592 |
inForInit = false; |
| 2593 |
try { |
| 2594 |
do { |
| 2595 |
if (peekToken() == Token.YIELD) { |
| 2596 |
reportError("msg.yield.parenthesized"); |
| 2597 |
} |
| 2598 |
AstNode en = assignExpr(); |
| 2599 |
if (peekToken() == Token.FOR) { |
| 2600 |
try { |
| 2601 |
result.add(generatorExpression(en, 0, true)); |
| 2602 |
} |
| 2603 |
catch(IOException ex) { |
| 2604 |
|
| 2605 |
} |
| 2606 |
} |
| 2607 |
else { |
| 2608 |
result.add(en); |
| 2609 |
} |
| 2610 |
} while (matchToken(Token.COMMA)); |
| 2611 |
} finally { |
| 2612 |
inForInit = wasInForInit; |
| 2613 |
} |
| 2614 |
|
| 2615 |
mustMatchToken(Token.RP, "msg.no.paren.arg"); |
| 2616 |
return result; |
| 2617 |
} |
| 2618 |
|
| 2619 |
|
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
|
| 2624 |
private AstNode memberExpr(boolean allowCallSyntax) |
| 2625 |
throws IOException |
| 2626 |
{ |
| 2627 |
int tt = peekToken(), lineno = ts.lineno; |
| 2628 |
AstNode pn; |
| 2629 |
|
| 2630 |
if (tt != Token.NEW) { |
| 2631 |
pn = primaryExpr(); |
| 2632 |
} else { |
| 2633 |
consumeToken(); |
| 2634 |
int pos = ts.tokenBeg; |
| 2635 |
NewExpression nx = new NewExpression(pos); |
| 2636 |
|
| 2637 |
AstNode target = memberExpr(false); |
| 2638 |
int end = getNodeEnd(target); |
| 2639 |
nx.setTarget(target); |
| 2640 |
|
| 2641 |
int lp = -1; |
| 2642 |
if (matchToken(Token.LP)) { |
| 2643 |
lp = ts.tokenBeg; |
| 2644 |
List<AstNode> args = argumentList(); |
| 2645 |
if (args != null && args.size() > ARGC_LIMIT) |
| 2646 |
reportError("msg.too.many.constructor.args"); |
| 2647 |
int rp = ts.tokenBeg; |
| 2648 |
end = ts.tokenEnd; |
| 2649 |
if (args != null) |
| 2650 |
nx.setArguments(args); |
| 2651 |
nx.setParens(lp - pos, rp - pos); |
| 2652 |
} |
| 2653 |
|
| 2654 |
|
| 2655 |
|
| 2656 |
|
| 2657 |
|
| 2658 |
if (matchToken(Token.LC)) { |
| 2659 |
ObjectLiteral initializer = objectLiteral(); |
| 2660 |
end = getNodeEnd(initializer); |
| 2661 |
nx.setInitializer(initializer); |
| 2662 |
} |
| 2663 |
nx.setLength(end - pos); |
| 2664 |
pn = nx; |
| 2665 |
} |
| 2666 |
pn.setLineno(lineno); |
| 2667 |
AstNode tail = memberExprTail(allowCallSyntax, pn); |
| 2668 |
return tail; |
| 2669 |
} |
| 2670 |
|
| 2671 |
|
| 2672 |
|
| 2673 |
|
| 2674 |
|
| 2675 |
|
| 2676 |
|
| 2677 |
|
| 2678 |
private AstNode memberExprTail(boolean allowCallSyntax, AstNode pn) |
| 2679 |
throws IOException |
| 2680 |
{ |
| 2681 |
|
| 2682 |
if (pn == null) codeBug(); |
| 2683 |
int pos = pn.getPosition(); |
| 2684 |
int lineno; |
| 2685 |
tailLoop: |
| 2686 |
for (;;) { |
| 2687 |
int tt = peekToken(); |
| 2688 |
switch (tt) { |
| 2689 |
case Token.DOT: |
| 2690 |
case Token.DOTDOT: |
| 2691 |
lineno = ts.lineno; |
| 2692 |
pn = propertyAccess(tt, pn); |
| 2693 |
pn.setLineno(lineno); |
| 2694 |
break; |
| 2695 |
|
| 2696 |
case Token.DOTQUERY: |
| 2697 |
consumeToken(); |
| 2698 |
int opPos = ts.tokenBeg, rp = -1; |
| 2699 |
lineno = ts.lineno; |
| 2700 |
mustHaveXML(); |
| 2701 |
setRequiresActivation(); |
| 2702 |
AstNode filter = expr(); |
| 2703 |
int end = getNodeEnd(filter); |
| 2704 |
if (mustMatchToken(Token.RP, "msg.no.paren")) { |
| 2705 |
rp = ts.tokenBeg; |
| 2706 |
end = ts.tokenEnd; |
| 2707 |
} |
| 2708 |
XmlDotQuery q = new XmlDotQuery(pos, end - pos); |
| 2709 |
q.setLeft(pn); |
| 2710 |
q.setRight(filter); |
| 2711 |
q.setOperatorPosition(opPos); |
| 2712 |
q.setRp(rp - pos); |
| 2713 |
q.setLineno(lineno); |
| 2714 |
pn = q; |
| 2715 |
break; |
| 2716 |
|
| 2717 |
case Token.LB: |
| 2718 |
consumeToken(); |
| 2719 |
int lb = ts.tokenBeg, rb = -1; |
| 2720 |
lineno = ts.lineno; |
| 2721 |
AstNode expr = expr(); |
| 2722 |
end = getNodeEnd(expr); |
| 2723 |
if (mustMatchToken(Token.RB, "msg.no.bracket.index")) { |
| 2724 |
rb = ts.tokenBeg; |
| 2725 |
end = ts.tokenEnd; |
| 2726 |
} |
| 2727 |
ElementGet g = new ElementGet(pos, end - pos); |
| 2728 |
g.setTarget(pn); |
| 2729 |
g.setElement(expr); |
| 2730 |
g.setParens(lb, rb); |
| 2731 |
g.setLineno(lineno); |
| 2732 |
pn = g; |
| 2733 |
break; |
| 2734 |
|
| 2735 |
case Token.LP: |
| 2736 |
if (!allowCallSyntax) { |
| 2737 |
break tailLoop; |
| 2738 |
} |
| 2739 |
lineno = ts.lineno; |
| 2740 |
consumeToken(); |
| 2741 |
checkCallRequiresActivation(pn); |
| 2742 |
FunctionCall f = new FunctionCall(pos); |
| 2743 |
f.setTarget(pn); |
| 2744 |
|
| 2745 |
|
| 2746 |
f.setLineno(lineno); |
| 2747 |
f.setLp(ts.tokenBeg - pos); |
| 2748 |
List<AstNode> args = argumentList(); |
| 2749 |
if (args != null && args.size() > ARGC_LIMIT) |
| 2750 |
reportError("msg.too.many.function.args"); |
| 2751 |
f.setArguments(args); |
| 2752 |
f.setRp(ts.tokenBeg - pos); |
| 2753 |
f.setLength(ts.tokenEnd - pos); |
| 2754 |
pn = f; |
| 2755 |
break; |
| 2756 |
|
| 2757 |
default: |
| 2758 |
break tailLoop; |
| 2759 |
} |
| 2760 |
} |
| 2761 |
return pn; |
| 2762 |
} |
| 2763 |
|
| 2764 |
|
| 2765 |
|
| 2766 |
|
| 2767 |
|
| 2768 |
|
| 2769 |
private AstNode propertyAccess(int tt, AstNode pn) |
| 2770 |
throws IOException |
| 2771 |
{ |
| 2772 |
if (pn == null) codeBug(); |
| 2773 |
int memberTypeFlags = 0, lineno = ts.lineno, dotPos = ts.tokenBeg; |
| 2774 |
consumeToken(); |
| 2775 |
|
| 2776 |
if (tt == Token.DOTDOT) { |
| 2777 |
mustHaveXML(); |
| 2778 |
memberTypeFlags = Node.DESCENDANTS_FLAG; |
| 2779 |
} |
| 2780 |
|
| 2781 |
if (!compilerEnv.isXmlAvailable()) { |
| 2782 |
int maybeName = nextToken(); |
| 2783 |
if (maybeName != Token.NAME |
| 2784 |
&& !(compilerEnv.isReservedKeywordAsIdentifier() |
| 2785 |
&& TokenStream.isKeyword(ts.getString(), compilerEnv.getLanguageVersion(), inUseStrictDirective))) { |
| 2786 |
reportError("msg.no.name.after.dot"); |
| 2787 |
} |
| 2788 |
|
| 2789 |
Name name = createNameNode(true, Token.GETPROP); |
| 2790 |
PropertyGet pg = new PropertyGet(pn, name, dotPos); |
| 2791 |
pg.setLineno(lineno); |
| 2792 |
return pg; |
| 2793 |
} |
| 2794 |
|
| 2795 |
AstNode ref = null; |
| 2796 |
|
| 2797 |
int token = nextToken(); |
| 2798 |
switch (token) { |
| 2799 |
case Token.THROW: |
| 2800 |
|
| 2801 |
saveNameTokenData(ts.tokenBeg, "throw", ts.lineno); |
| 2802 |
ref = propertyName(-1, "throw", memberTypeFlags); |
| 2803 |
break; |
| 2804 |
|
| 2805 |
case Token.NAME: |
| 2806 |
|
| 2807 |
ref = propertyName(-1, ts.getString(), memberTypeFlags); |
| 2808 |
break; |
| 2809 |
|
| 2810 |
case Token.MUL: |
| 2811 |
|
| 2812 |
saveNameTokenData(ts.tokenBeg, "*", ts.lineno); |
| 2813 |
ref = propertyName(-1, "*", memberTypeFlags); |
| 2814 |
break; |
| 2815 |
|
| 2816 |
case Token.XMLATTR: |
| 2817 |
|
| 2818 |
|
| 2819 |
ref = attributeAccess(); |
| 2820 |
break; |
| 2821 |
|
| 2822 |
case Token.RESERVED: { |
| 2823 |
String name = ts.getString(); |
| 2824 |
saveNameTokenData(ts.tokenBeg, name, ts.lineno); |
| 2825 |
ref = propertyName(-1, name, memberTypeFlags); |
| 2826 |
break; |
| 2827 |
} |
| 2828 |
|
| 2829 |
default: |
| 2830 |
if (compilerEnv.isReservedKeywordAsIdentifier()) { |
| 2831 |
|
| 2832 |
String name = Token.keywordToName(token); |
| 2833 |
if (name != null) { |
| 2834 |
saveNameTokenData(ts.tokenBeg, name, ts.lineno); |
| 2835 |
ref = propertyName(-1, name, memberTypeFlags); |
| 2836 |
break; |
| 2837 |
} |
| 2838 |
} |
| 2839 |
reportError("msg.no.name.after.dot"); |
| 2840 |
return makeErrorNode(); |
| 2841 |
} |
| 2842 |
|
| 2843 |
boolean xml = ref instanceof XmlRef; |
| 2844 |
InfixExpression result = xml ? new XmlMemberGet() : new PropertyGet(); |
| 2845 |
if (xml && tt == Token.DOT) |
| 2846 |
result.setType(Token.DOT); |
| 2847 |
int pos = pn.getPosition(); |
| 2848 |
result.setPosition(pos); |
| 2849 |
result.setLength(getNodeEnd(ref) - pos); |
| 2850 |
result.setOperatorPosition(dotPos - pos); |
| 2851 |
result.setLineno(pn.getLineno()); |
| 2852 |
result.setLeft(pn); |
| 2853 |
result.setRight(ref); |
| 2854 |
return result; |
| 2855 |
} |
| 2856 |
|
| 2857 |
|
| 2858 |
|
| 2859 |
|
| 2860 |
|
| 2861 |
|
| 2862 |
|
| 2863 |
|
| 2864 |
private AstNode attributeAccess() |
| 2865 |
throws IOException |
| 2866 |
{ |
| 2867 |
int tt = nextToken(), atPos = ts.tokenBeg; |
| 2868 |
|
| 2869 |
switch (tt) { |
| 2870 |
|
| 2871 |
case Token.NAME: |
| 2872 |
return propertyName(atPos, ts.getString(), 0); |
| 2873 |
|
| 2874 |
|
| 2875 |
case Token.MUL: |
| 2876 |
saveNameTokenData(ts.tokenBeg, "*", ts.lineno); |
| 2877 |
return propertyName(atPos, "*", 0); |
| 2878 |
|
| 2879 |
|
| 2880 |
case Token.LB: |
| 2881 |
return xmlElemRef(atPos, null, -1); |
| 2882 |
|
| 2883 |
default: |
| 2884 |
reportError("msg.no.name.after.xmlAttr"); |
| 2885 |
return makeErrorNode(); |
| 2886 |
} |
| 2887 |
} |
| 2888 |
|
| 2889 |
|
| 2890 |
|
| 2891 |
|
| 2892 |
|
| 2893 |
|
| 2894 |
|
| 2895 |
|
| 2896 |
|
| 2897 |
|
| 2898 |
|
| 2899 |
|
| 2900 |
|
| 2901 |
|
| 2902 |
|
| 2903 |
|
| 2904 |
private AstNode propertyName(int atPos, String s, int memberTypeFlags) |
| 2905 |
throws IOException |
| 2906 |
{ |
| 2907 |
int pos = atPos != -1 ? atPos : ts.tokenBeg, lineno = ts.lineno; |
| 2908 |
int colonPos = -1; |
| 2909 |
Name name = createNameNode(true, currentToken); |
| 2910 |
Name ns = null; |
| 2911 |
|
| 2912 |
if (matchToken(Token.COLONCOLON)) { |
| 2913 |
ns = name; |
| 2914 |
colonPos = ts.tokenBeg; |
| 2915 |
|
| 2916 |
switch (nextToken()) { |
| 2917 |
|
| 2918 |
case Token.NAME: |
| 2919 |
name = createNameNode(); |
| 2920 |
break; |
| 2921 |
|
| 2922 |
|
| 2923 |
case Token.MUL: |
| 2924 |
saveNameTokenData(ts.tokenBeg, "*", ts.lineno); |
| 2925 |
name = createNameNode(false, -1); |
| 2926 |
break; |
| 2927 |
|
| 2928 |
|
| 2929 |
case Token.LB: |
| 2930 |
return xmlElemRef(atPos, ns, colonPos); |
| 2931 |
|
| 2932 |
default: |
| 2933 |
reportError("msg.no.name.after.coloncolon"); |
| 2934 |
return makeErrorNode(); |
| 2935 |
} |
| 2936 |
} |
| 2937 |
|
| 2938 |
if (ns == null && memberTypeFlags == 0 && atPos == -1) { |
| 2939 |
return name; |
| 2940 |
} |
| 2941 |
|
| 2942 |
XmlPropRef ref = new XmlPropRef(pos, getNodeEnd(name) - pos); |
| 2943 |
ref.setAtPos(atPos); |
| 2944 |
ref.setNamespace(ns); |
| 2945 |
ref.setColonPos(colonPos); |
| 2946 |
ref.setPropName(name); |
| 2947 |
ref.setLineno(lineno); |
| 2948 |
return ref; |
| 2949 |
} |
| 2950 |
|
| 2951 |
|
| 2952 |
|
| 2953 |
|
| 2954 |
|
| 2955 |
private XmlElemRef xmlElemRef(int atPos, Name namespace, int colonPos) |
| 2956 |
throws IOException |
| 2957 |
{ |
| 2958 |
int lb = ts.tokenBeg, rb = -1, pos = atPos != -1 ? atPos : lb; |
| 2959 |
AstNode expr = expr(); |
| 2960 |
int end = getNodeEnd(expr); |
| 2961 |
if (mustMatchToken(Token.RB, "msg.no.bracket.index")) { |
| 2962 |
rb = ts.tokenBeg; |
| 2963 |
end = ts.tokenEnd; |
| 2964 |
} |
| 2965 |
XmlElemRef ref = new XmlElemRef(pos, end - pos); |
| 2966 |
ref.setNamespace(namespace); |
| 2967 |
ref.setColonPos(colonPos); |
| 2968 |
ref.setAtPos(atPos); |
| 2969 |
ref.setExpression(expr); |
| 2970 |
ref.setBrackets(lb, rb); |
| 2971 |
return ref; |
| 2972 |
} |
| 2973 |
|
| 2974 |
private AstNode destructuringPrimaryExpr() |
| 2975 |
throws IOException, ParserException |
| 2976 |
{ |
| 2977 |
try { |
| 2978 |
inDestructuringAssignment = true; |
| 2979 |
return primaryExpr(); |
| 2980 |
} finally { |
| 2981 |
inDestructuringAssignment = false; |
| 2982 |
} |
| 2983 |
} |
| 2984 |
|
| 2985 |
private AstNode primaryExpr() |
| 2986 |
throws IOException |
| 2987 |
{ |
| 2988 |
int ttFlagged = peekFlaggedToken(); |
| 2989 |
int tt = ttFlagged & CLEAR_TI_MASK; |
| 2990 |
|
| 2991 |
switch(tt) { |
| 2992 |
case Token.FUNCTION: |
| 2993 |
consumeToken(); |
| 2994 |
return function(FunctionNode.FUNCTION_EXPRESSION); |
| 2995 |
|
| 2996 |
case Token.LB: |
| 2997 |
consumeToken(); |
| 2998 |
return arrayLiteral(); |
| 2999 |
|
| 3000 |
case Token.LC: |
| 3001 |
consumeToken(); |
| 3002 |
return objectLiteral(); |
| 3003 |
|
| 3004 |
case Token.LET: |
| 3005 |
consumeToken(); |
| 3006 |
return let(false, ts.tokenBeg); |
| 3007 |
|
| 3008 |
case Token.LP: |
| 3009 |
consumeToken(); |
| 3010 |
return parenExpr(); |
| 3011 |
|
| 3012 |
case Token.XMLATTR: |
| 3013 |
consumeToken(); |
| 3014 |
mustHaveXML(); |
| 3015 |
return attributeAccess(); |
| 3016 |
|
| 3017 |
case Token.NAME: |
| 3018 |
consumeToken(); |
| 3019 |
return name(ttFlagged, tt); |
| 3020 |
|
| 3021 |
case Token.NUMBER: { |
| 3022 |
consumeToken(); |
| 3023 |
String s = ts.getString(); |
| 3024 |
if (this.inUseStrictDirective && ts.isNumberOldOctal()) { |
| 3025 |
reportError("msg.no.old.octal.strict"); |
| 3026 |
} |
| 3027 |
if (ts.isNumberBinary()) { |
| 3028 |
s = "0b"+s; |
| 3029 |
} |
| 3030 |
if (ts.isNumberOldOctal()) { |
| 3031 |
s = "0"+s; |
| 3032 |
} |
| 3033 |
if (ts.isNumberOctal()) { |
| 3034 |
s = "0o"+s; |
| 3035 |
} |
| 3036 |
if (ts.isNumberHex()) { |
| 3037 |
s = "0x"+s; |
| 3038 |
} |
| 3039 |
return new NumberLiteral(ts.tokenBeg, |
| 3040 |
s, |
| 3041 |
ts.getNumber()); |
| 3042 |
} |
| 3043 |
|
| 3044 |
case Token.STRING: |
| 3045 |
consumeToken(); |
| 3046 |
return createStringLiteral(); |
| 3047 |
|
| 3048 |
case Token.DIV: |
| 3049 |
case Token.ASSIGN_DIV: |
| 3050 |
consumeToken(); |
| 3051 |
|
| 3052 |
ts.readRegExp(tt); |
| 3053 |
int pos = ts.tokenBeg, end = ts.tokenEnd; |
| 3054 |
RegExpLiteral re = new RegExpLiteral(pos, end - pos); |
| 3055 |
re.setValue(ts.getString()); |
| 3056 |
re.setFlags(ts.readAndClearRegExpFlags()); |
| 3057 |
return re; |
| 3058 |
|
| 3059 |
case Token.NULL: |
| 3060 |
case Token.THIS: |
| 3061 |
case Token.FALSE: |
| 3062 |
case Token.TRUE: |
| 3063 |
consumeToken(); |
| 3064 |
pos = ts.tokenBeg; end = ts.tokenEnd; |
| 3065 |
return new KeywordLiteral(pos, end - pos, tt); |
| 3066 |
|
| 3067 |
case Token.RP: |
| 3068 |
return new EmptyExpression(); |
| 3069 |
|
| 3070 |
case Token.RESERVED: |
| 3071 |
consumeToken(); |
| 3072 |
reportError("msg.reserved.id"); |
| 3073 |
break; |
| 3074 |
|
| 3075 |
case Token.ERROR: |
| 3076 |
consumeToken(); |
| 3077 |
|
| 3078 |
break; |
| 3079 |
|
| 3080 |
case Token.EOF: |
| 3081 |
consumeToken(); |
| 3082 |
reportError("msg.unexpected.eof"); |
| 3083 |
break; |
| 3084 |
|
| 3085 |
default: |
| 3086 |
consumeToken(); |
| 3087 |
reportError("msg.syntax"); |
| 3088 |
break; |
| 3089 |
} |
| 3090 |
|
| 3091 |
consumeToken(); |
| 3092 |
return makeErrorNode(); |
| 3093 |
} |
| 3094 |
|
| 3095 |
private AstNode parenExpr() throws IOException { |
| 3096 |
boolean wasInForInit = inForInit; |
| 3097 |
inForInit = false; |
| 3098 |
try { |
| 3099 |
Comment jsdocNode = getAndResetJsDoc(); |
| 3100 |
int lineno = ts.lineno; |
| 3101 |
int begin = ts.tokenBeg; |
| 3102 |
AstNode e = expr(); |
| 3103 |
if (peekToken() == Token.FOR) { |
| 3104 |
return generatorExpression(e, begin); |
| 3105 |
} |
| 3106 |
ParenthesizedExpression pn = new ParenthesizedExpression(e); |
| 3107 |
if (jsdocNode == null) { |
| 3108 |
jsdocNode = getAndResetJsDoc(); |
| 3109 |
} |
| 3110 |
if (jsdocNode != null) { |
| 3111 |
pn.setJsDocNode(jsdocNode); |
| 3112 |
} |
| 3113 |
mustMatchToken(Token.RP, "msg.no.paren"); |
| 3114 |
if (e.getType() == Token.EMPTY && peekToken() != Token.ARROW) { |
| 3115 |
reportError("msg.syntax"); |
| 3116 |
return makeErrorNode(); |
| 3117 |
} |
| 3118 |
pn.setLength(ts.tokenEnd - pn.getPosition()); |
| 3119 |
pn.setLineno(lineno); |
| 3120 |
return pn; |
| 3121 |
} finally { |
| 3122 |
inForInit = wasInForInit; |
| 3123 |
} |
| 3124 |
} |
| 3125 |
|
| 3126 |
private AstNode name(int ttFlagged, int tt) throws IOException { |
| 3127 |
String nameString = ts.getString(); |
| 3128 |
int namePos = ts.tokenBeg, nameLineno = ts.lineno; |
| 3129 |
if (0 != (ttFlagged & TI_CHECK_LABEL) && peekToken() == Token.COLON) { |
| 3130 |
|
| 3131 |
|
| 3132 |
Label label = new Label(namePos, ts.tokenEnd - namePos); |
| 3133 |
label.setName(nameString); |
| 3134 |
label.setLineno(ts.lineno); |
| 3135 |
return label; |
| 3136 |
} |
| 3137 |
|
| 3138 |
|
| 3139 |
|
| 3140 |
saveNameTokenData(namePos, nameString, nameLineno); |
| 3141 |
|
| 3142 |
if (compilerEnv.isXmlAvailable()) { |
| 3143 |
return propertyName(-1, nameString, 0); |
| 3144 |
} else { |
| 3145 |
return createNameNode(true, Token.NAME); |
| 3146 |
} |
| 3147 |
} |
| 3148 |
|
| 3149 |
|
| 3150 |
|
| 3151 |
|
| 3152 |
private AstNode arrayLiteral() |
| 3153 |
throws IOException |
| 3154 |
{ |
| 3155 |
if (currentToken != Token.LB) codeBug(); |
| 3156 |
int pos = ts.tokenBeg, end = ts.tokenEnd; |
| 3157 |
List<AstNode> elements = new ArrayList<AstNode>(); |
| 3158 |
ArrayLiteral pn = new ArrayLiteral(pos); |
| 3159 |
boolean after_lb_or_comma = true; |
| 3160 |
int afterComma = -1; |
| 3161 |
int skipCount = 0; |
| 3162 |
for (;;) { |
| 3163 |
int tt = peekToken(); |
| 3164 |
if (tt == Token.COMMA) { |
| 3165 |
consumeToken(); |
| 3166 |
afterComma = ts.tokenEnd; |
| 3167 |
if (!after_lb_or_comma) { |
| 3168 |
after_lb_or_comma = true; |
| 3169 |
} else { |
| 3170 |
elements.add(new EmptyExpression(ts.tokenBeg, 1)); |
| 3171 |
skipCount++; |
| 3172 |
} |
| 3173 |
} else if (tt == Token.RB) { |
| 3174 |
consumeToken(); |
| 3175 |
|
| 3176 |
|
| 3177 |
|
| 3178 |
|
| 3179 |
|
| 3180 |
end = ts.tokenEnd; |
| 3181 |
pn.setDestructuringLength(elements.size() + |
| 3182 |
(after_lb_or_comma ? 1 : 0)); |
| 3183 |
pn.setSkipCount(skipCount); |
| 3184 |
if (afterComma != -1) |
| 3185 |
warnTrailingComma(pos, elements, afterComma); |
| 3186 |
break; |
| 3187 |
} else if (tt == Token.FOR && !after_lb_or_comma |
| 3188 |
&& elements.size() == 1) { |
| 3189 |
return arrayComprehension(elements.get(0), pos); |
| 3190 |
} else if (tt == Token.EOF) { |
| 3191 |
reportError("msg.no.bracket.arg"); |
| 3192 |
break; |
| 3193 |
} else { |
| 3194 |
if (!after_lb_or_comma) { |
| 3195 |
reportError("msg.no.bracket.arg"); |
| 3196 |
} |
| 3197 |
elements.add(assignExpr()); |
| 3198 |
after_lb_or_comma = false; |
| 3199 |
afterComma = -1; |
| 3200 |
} |
| 3201 |
} |
| 3202 |
for (AstNode e : elements) { |
| 3203 |
pn.addElement(e); |
| 3204 |
} |
| 3205 |
pn.setLength(end - pos); |
| 3206 |
return pn; |
| 3207 |
} |
| 3208 |
|
| 3209 |
|
| 3210 |
|
| 3211 |
|
| 3212 |
|
| 3213 |
|
| 3214 |
|
| 3215 |
private AstNode arrayComprehension(AstNode result, int pos) |
| 3216 |
throws IOException |
| 3217 |
{ |
| 3218 |
List<ArrayComprehensionLoop> loops = |
| 3219 |
new ArrayList<ArrayComprehensionLoop>(); |
| 3220 |
while (peekToken() == Token.FOR) { |
| 3221 |
loops.add(arrayComprehensionLoop()); |
| 3222 |
} |
| 3223 |
int ifPos = -1; |
| 3224 |
ConditionData data = null; |
| 3225 |
if (peekToken() == Token.IF) { |
| 3226 |
consumeToken(); |
| 3227 |
ifPos = ts.tokenBeg - pos; |
| 3228 |
data = condition(); |
| 3229 |
} |
| 3230 |
mustMatchToken(Token.RB, "msg.no.bracket.arg"); |
| 3231 |
ArrayComprehension pn = new ArrayComprehension(pos, ts.tokenEnd - pos); |
| 3232 |
pn.setResult(result); |
| 3233 |
pn.setLoops(loops); |
| 3234 |
if (data != null) { |
| 3235 |
pn.setIfPosition(ifPos); |
| 3236 |
pn.setFilter(data.condition); |
| 3237 |
pn.setFilterLp(data.lp - pos); |
| 3238 |
pn.setFilterRp(data.rp - pos); |
| 3239 |
} |
| 3240 |
return pn; |
| 3241 |
} |
| 3242 |
|
| 3243 |
private ArrayComprehensionLoop arrayComprehensionLoop() |
| 3244 |
throws IOException |
| 3245 |
{ |
| 3246 |
if (nextToken() != Token.FOR) codeBug(); |
| 3247 |
int pos = ts.tokenBeg; |
| 3248 |
int eachPos = -1, lp = -1, rp = -1, inPos = -1; |
| 3249 |
boolean isForIn = false, isForOf = false; |
| 3250 |
ArrayComprehensionLoop pn = new ArrayComprehensionLoop(pos); |
| 3251 |
|
| 3252 |
pushScope(pn); |
| 3253 |
try { |
| 3254 |
if (matchToken(Token.NAME)) { |
| 3255 |
if (ts.getString().equals("each")) { |
| 3256 |
eachPos = ts.tokenBeg - pos; |
| 3257 |
} else { |
| 3258 |
reportError("msg.no.paren.for"); |
| 3259 |
} |
| 3260 |
} |
| 3261 |
if (mustMatchToken(Token.LP, "msg.no.paren.for")) { |
| 3262 |
lp = ts.tokenBeg - pos; |
| 3263 |
} |
| 3264 |
|
| 3265 |
AstNode iter = null; |
| 3266 |
switch (peekToken()) { |
| 3267 |
case Token.LB: |
| 3268 |
case Token.LC: |
| 3269 |
|
| 3270 |
iter = destructuringPrimaryExpr(); |
| 3271 |
markDestructuring(iter); |
| 3272 |
break; |
| 3273 |
case Token.NAME: |
| 3274 |
consumeToken(); |
| 3275 |
iter = createNameNode(); |
| 3276 |
break; |
| 3277 |
default: |
| 3278 |
reportError("msg.bad.var"); |
| 3279 |
} |
| 3280 |
|
| 3281 |
|
| 3282 |
|
| 3283 |
if (iter.getType() == Token.NAME) { |
| 3284 |
defineSymbol(Token.LET, ts.getString(), true); |
| 3285 |
} |
| 3286 |
|
| 3287 |
switch (nextToken()) { |
| 3288 |
case Token.IN: |
| 3289 |
inPos = ts.tokenBeg - pos; |
| 3290 |
isForIn = true; |
| 3291 |
break; |
| 3292 |
case Token.NAME: |
| 3293 |
if ("of".equals(ts.getString())) { |
| 3294 |
if (eachPos != -1) { |
| 3295 |
reportError("msg.invalid.for.each"); |
| 3296 |
} |
| 3297 |
inPos = ts.tokenBeg - pos; |
| 3298 |
isForOf = true; |
| 3299 |
break; |
| 3300 |
} |
| 3301 |
default: |
| 3302 |
reportError("msg.in.after.for.name"); |
| 3303 |
} |
| 3304 |
AstNode obj = expr(); |
| 3305 |
if (mustMatchToken(Token.RP, "msg.no.paren.for.ctrl")) |
| 3306 |
rp = ts.tokenBeg - pos; |
| 3307 |
|
| 3308 |
pn.setLength(ts.tokenEnd - pos); |
| 3309 |
pn.setIterator(iter); |
| 3310 |
pn.setIteratedObject(obj); |
| 3311 |
pn.setInPosition(inPos); |
| 3312 |
pn.setEachPosition(eachPos); |
| 3313 |
pn.setIsForEach(eachPos != -1); |
| 3314 |
pn.setParens(lp, rp); |
| 3315 |
pn.setIsForOf(isForOf); |
| 3316 |
return pn; |
| 3317 |
} finally { |
| 3318 |
popScope(); |
| 3319 |
} |
| 3320 |
} |
| 3321 |
|
| 3322 |
private AstNode generatorExpression(AstNode result, int pos) |
| 3323 |
throws IOException |
| 3324 |
{ |
| 3325 |
return generatorExpression(result, pos, false); |
| 3326 |
} |
| 3327 |
|
| 3328 |
private AstNode generatorExpression(AstNode result, int pos, boolean inFunctionParams) |
| 3329 |
throws IOException |
| 3330 |
{ |
| 3331 |
|
| 3332 |
List<GeneratorExpressionLoop> loops = |
| 3333 |
new ArrayList<GeneratorExpressionLoop>(); |
| 3334 |
while (peekToken() == Token.FOR) { |
| 3335 |
loops.add(generatorExpressionLoop()); |
| 3336 |
} |
| 3337 |
int ifPos = -1; |
| 3338 |
ConditionData data = null; |
| 3339 |
if (peekToken() == Token.IF) { |
| 3340 |
consumeToken(); |
| 3341 |
ifPos = ts.tokenBeg - pos; |
| 3342 |
data = condition(); |
| 3343 |
} |
| 3344 |
if(!inFunctionParams) { |
| 3345 |
mustMatchToken(Token.RP, "msg.no.paren.let"); |
| 3346 |
} |
| 3347 |
GeneratorExpression pn = new GeneratorExpression(pos, ts.tokenEnd - pos); |
| 3348 |
pn.setResult(result); |
| 3349 |
pn.setLoops(loops); |
| 3350 |
if (data != null) { |
| 3351 |
pn.setIfPosition(ifPos); |
| 3352 |
pn.setFilter(data.condition); |
| 3353 |
pn.setFilterLp(data.lp - pos); |
| 3354 |
pn.setFilterRp(data.rp - pos); |
| 3355 |
} |
| 3356 |
return pn; |
| 3357 |
} |
| 3358 |
|
| 3359 |
private GeneratorExpressionLoop generatorExpressionLoop() |
| 3360 |
throws IOException |
| 3361 |
{ |
| 3362 |
if (nextToken() != Token.FOR) codeBug(); |
| 3363 |
int pos = ts.tokenBeg; |
| 3364 |
int lp = -1, rp = -1, inPos = -1; |
| 3365 |
GeneratorExpressionLoop pn = new GeneratorExpressionLoop(pos); |
| 3366 |
|
| 3367 |
pushScope(pn); |
| 3368 |
try { |
| 3369 |
if (mustMatchToken(Token.LP, "msg.no.paren.for")) { |
| 3370 |
lp = ts.tokenBeg - pos; |
| 3371 |
} |
| 3372 |
|
| 3373 |
AstNode iter = null; |
| 3374 |
switch (peekToken()) { |
| 3375 |
case Token.LB: |
| 3376 |
case Token.LC: |
| 3377 |
|
| 3378 |
iter = destructuringPrimaryExpr(); |
| 3379 |
markDestructuring(iter); |
| 3380 |
break; |
| 3381 |
case Token.NAME: |
| 3382 |
consumeToken(); |
| 3383 |
iter = createNameNode(); |
| 3384 |
break; |
| 3385 |
default: |
| 3386 |
reportError("msg.bad.var"); |
| 3387 |
} |
| 3388 |
|
| 3389 |
|
| 3390 |
|
| 3391 |
if (iter.getType() == Token.NAME) { |
| 3392 |
defineSymbol(Token.LET, ts.getString(), true); |
| 3393 |
} |
| 3394 |
|
| 3395 |
if (mustMatchToken(Token.IN, "msg.in.after.for.name")) |
| 3396 |
inPos = ts.tokenBeg - pos; |
| 3397 |
AstNode obj = expr(); |
| 3398 |
if (mustMatchToken(Token.RP, "msg.no.paren.for.ctrl")) |
| 3399 |
rp = ts.tokenBeg - pos; |
| 3400 |
|
| 3401 |
pn.setLength(ts.tokenEnd - pos); |
| 3402 |
pn.setIterator(iter); |
| 3403 |
pn.setIteratedObject(obj); |
| 3404 |
pn.setInPosition(inPos); |
| 3405 |
pn.setParens(lp, rp); |
| 3406 |
return pn; |
| 3407 |
} finally { |
| 3408 |
popScope(); |
| 3409 |
} |
| 3410 |
} |
| 3411 |
|
| 3412 |
private static final int PROP_ENTRY = 1; |
| 3413 |
private static final int GET_ENTRY = 2; |
| 3414 |
private static final int SET_ENTRY = 4; |
| 3415 |
private static final int METHOD_ENTRY = 8; |
| 3416 |
|
| 3417 |
private ObjectLiteral objectLiteral() |
| 3418 |
throws IOException |
| 3419 |
{ |
| 3420 |
int pos = ts.tokenBeg, lineno = ts.lineno; |
| 3421 |
int afterComma = -1; |
| 3422 |
List<ObjectProperty> elems = new ArrayList<ObjectProperty>(); |
| 3423 |
Set<String> getterNames = null; |
| 3424 |
Set<String> setterNames = null; |
| 3425 |
if (this.inUseStrictDirective) { |
| 3426 |
getterNames = new HashSet<String>(); |
| 3427 |
setterNames = new HashSet<String>(); |
| 3428 |
} |
| 3429 |
Comment objJsdocNode = getAndResetJsDoc(); |
| 3430 |
|
| 3431 |
commaLoop: |
| 3432 |
for (;;) { |
| 3433 |
String propertyName = null; |
| 3434 |
int entryKind = PROP_ENTRY; |
| 3435 |
int tt = peekToken(); |
| 3436 |
Comment jsdocNode = getAndResetJsDoc(); |
| 3437 |
|
| 3438 |
if (tt == Token.RC) { |
| 3439 |
if (afterComma != -1) |
| 3440 |
warnTrailingComma(pos, elems, afterComma); |
| 3441 |
break commaLoop; |
| 3442 |
} else { |
| 3443 |
AstNode pname = objliteralProperty(); |
| 3444 |
if (pname == null) { |
| 3445 |
propertyName = null; |
| 3446 |
reportError("msg.bad.prop"); |
| 3447 |
} else { |
| 3448 |
propertyName = ts.getString(); |
| 3449 |
int ppos = ts.tokenBeg; |
| 3450 |
consumeToken(); |
| 3451 |
|
| 3452 |
|
| 3453 |
|
| 3454 |
|
| 3455 |
|
| 3456 |
|
| 3457 |
|
| 3458 |
|
| 3459 |
|
| 3460 |
int peeked = peekToken(); |
| 3461 |
if (peeked != Token.COMMA |
| 3462 |
&& peeked != Token.COLON |
| 3463 |
&& peeked != Token.RC) |
| 3464 |
{ |
| 3465 |
if (peeked == Token.LP) { |
| 3466 |
entryKind = METHOD_ENTRY; |
| 3467 |
} else if (pname.getType() == Token.NAME) { |
| 3468 |
if ("get".equals(propertyName)) { |
| 3469 |
entryKind = GET_ENTRY; |
| 3470 |
} else if ("set".equals(propertyName)) { |
| 3471 |
entryKind = SET_ENTRY; |
| 3472 |
} |
| 3473 |
} |
| 3474 |
if (entryKind == GET_ENTRY || entryKind == SET_ENTRY) { |
| 3475 |
pname = objliteralProperty(); |
| 3476 |
if (pname == null) { |
| 3477 |
reportError("msg.bad.prop"); |
| 3478 |
} |
| 3479 |
consumeToken(); |
| 3480 |
} |
| 3481 |
if (pname == null) { |
| 3482 |
propertyName = null; |
| 3483 |
} else { |
| 3484 |
propertyName = ts.getString(); |
| 3485 |
ObjectProperty objectProp = methodDefinition( |
| 3486 |
ppos, pname, entryKind); |
| 3487 |
pname.setJsDocNode(jsdocNode); |
| 3488 |
elems.add(objectProp); |
| 3489 |
} |
| 3490 |
} else { |
| 3491 |
pname.setJsDocNode(jsdocNode); |
| 3492 |
elems.add(plainProperty(pname, tt)); |
| 3493 |
} |
| 3494 |
} |
| 3495 |
} |
| 3496 |
|
| 3497 |
if (this.inUseStrictDirective && propertyName != null) { |
| 3498 |
switch (entryKind) { |
| 3499 |
case PROP_ENTRY: |
| 3500 |
case METHOD_ENTRY: |
| 3501 |
if (getterNames.contains(propertyName) |
| 3502 |
|| setterNames.contains(propertyName)) { |
| 3503 |
addError("msg.dup.obj.lit.prop.strict", propertyName); |
| 3504 |
} |
| 3505 |
getterNames.add(propertyName); |
| 3506 |
setterNames.add(propertyName); |
| 3507 |
break; |
| 3508 |
case GET_ENTRY: |
| 3509 |
if (getterNames.contains(propertyName)) { |
| 3510 |
addError("msg.dup.obj.lit.prop.strict", propertyName); |
| 3511 |
} |
| 3512 |
getterNames.add(propertyName); |
| 3513 |
break; |
| 3514 |
case SET_ENTRY: |
| 3515 |
if (setterNames.contains(propertyName)) { |
| 3516 |
addError("msg.dup.obj.lit.prop.strict", propertyName); |
| 3517 |
} |
| 3518 |
setterNames.add(propertyName); |
| 3519 |
break; |
| 3520 |
} |
| 3521 |
} |
| 3522 |
|
| 3523 |
|
| 3524 |
getAndResetJsDoc(); |
| 3525 |
|
| 3526 |
if (matchToken(Token.COMMA)) { |
| 3527 |
afterComma = ts.tokenEnd; |
| 3528 |
} else { |
| 3529 |
break commaLoop; |
| 3530 |
} |
| 3531 |
} |
| 3532 |
|
| 3533 |
mustMatchToken(Token.RC, "msg.no.brace.prop"); |
| 3534 |
ObjectLiteral pn = new ObjectLiteral(pos, ts.tokenEnd - pos); |
| 3535 |
if (objJsdocNode != null) { |
| 3536 |
pn.setJsDocNode(objJsdocNode); |
| 3537 |
} |
| 3538 |
pn.setElements(elems); |
| 3539 |
pn.setLineno(lineno); |
| 3540 |
return pn; |
| 3541 |
} |
| 3542 |
|
| 3543 |
private AstNode objliteralProperty() throws IOException { |
| 3544 |
AstNode pname; |
| 3545 |
int tt = peekToken(); |
| 3546 |
switch(tt) { |
| 3547 |
case Token.NAME: |
| 3548 |
pname = createNameNode(); |
| 3549 |
break; |
| 3550 |
|
| 3551 |
case Token.STRING: |
| 3552 |
pname = createStringLiteral(); |
| 3553 |
break; |
| 3554 |
|
| 3555 |
case Token.NUMBER: |
| 3556 |
pname = new NumberLiteral( |
| 3557 |
ts.tokenBeg, ts.getString(), ts.getNumber()); |
| 3558 |
break; |
| 3559 |
|
| 3560 |
default: |
| 3561 |
if (compilerEnv.isReservedKeywordAsIdentifier() |
| 3562 |
&& TokenStream.isKeyword(ts.getString(), compilerEnv.getLanguageVersion(), inUseStrictDirective)) { |
| 3563 |
|
| 3564 |
pname = createNameNode(); |
| 3565 |
break; |
| 3566 |
} |
| 3567 |
return null; |
| 3568 |
} |
| 3569 |
|
| 3570 |
return pname; |
| 3571 |
} |
| 3572 |
|
| 3573 |
private ObjectProperty plainProperty(AstNode property, int ptt) |
| 3574 |
throws IOException |
| 3575 |
{ |
| 3576 |
|
| 3577 |
|
| 3578 |
int tt = peekToken(); |
| 3579 |
if ((tt == Token.COMMA || tt == Token.RC) && ptt == Token.NAME |
| 3580 |
&& compilerEnv.getLanguageVersion() >= Context.VERSION_1_8) { |
| 3581 |
if (!inDestructuringAssignment) { |
| 3582 |
reportError("msg.bad.object.init"); |
| 3583 |
} |
| 3584 |
AstNode nn = new Name(property.getPosition(), property.getString()); |
| 3585 |
ObjectProperty pn = new ObjectProperty(); |
| 3586 |
pn.putProp(Node.DESTRUCTURING_SHORTHAND, Boolean.TRUE); |
| 3587 |
pn.setLeftAndRight(property, nn); |
| 3588 |
return pn; |
| 3589 |
} |
| 3590 |
mustMatchToken(Token.COLON, "msg.no.colon.prop"); |
| 3591 |
ObjectProperty pn = new ObjectProperty(); |
| 3592 |
pn.setOperatorPosition(ts.tokenBeg); |
| 3593 |
pn.setLeftAndRight(property, assignExpr()); |
| 3594 |
return pn; |
| 3595 |
} |
| 3596 |
|
| 3597 |
private ObjectProperty methodDefinition(int pos, AstNode propName, int entryKind) |
| 3598 |
throws IOException |
| 3599 |
{ |
| 3600 |
FunctionNode fn = function(FunctionNode.FUNCTION_EXPRESSION); |
| 3601 |
|
| 3602 |
Name name = fn.getFunctionName(); |
| 3603 |
if (name != null && name.length() != 0) { |
| 3604 |
reportError("msg.bad.prop"); |
| 3605 |
} |
| 3606 |
ObjectProperty pn = new ObjectProperty(pos); |
| 3607 |
switch (entryKind) { |
| 3608 |
case GET_ENTRY: |
| 3609 |
pn.setIsGetterMethod(); |
| 3610 |
fn.setFunctionIsGetterMethod(); |
| 3611 |
break; |
| 3612 |
case SET_ENTRY: |
| 3613 |
pn.setIsSetterMethod(); |
| 3614 |
fn.setFunctionIsSetterMethod(); |
| 3615 |
break; |
| 3616 |
case METHOD_ENTRY: |
| 3617 |
pn.setIsNormalMethod(); |
| 3618 |
fn.setFunctionIsNormalMethod(); |
| 3619 |
break; |
| 3620 |
} |
| 3621 |
int end = getNodeEnd(fn); |
| 3622 |
pn.setLeft(propName); |
| 3623 |
pn.setRight(fn); |
| 3624 |
pn.setLength(end - pos); |
| 3625 |
return pn; |
| 3626 |
} |
| 3627 |
|
| 3628 |
private Name createNameNode() { |
| 3629 |
return createNameNode(false, Token.NAME); |
| 3630 |
} |
| 3631 |
|
| 3632 |
|
| 3633 |
|
| 3634 |
|
| 3635 |
|
| 3636 |
|
| 3637 |
|
| 3638 |
|
| 3639 |
private Name createNameNode(boolean checkActivation, int token) { |
| 3640 |
int beg = ts.tokenBeg; |
| 3641 |
String s = ts.getString(); |
| 3642 |
int lineno = ts.lineno; |
| 3643 |
if (!"".equals(prevNameTokenString)) { |
| 3644 |
beg = prevNameTokenStart; |
| 3645 |
s = prevNameTokenString; |
| 3646 |
lineno = prevNameTokenLineno; |
| 3647 |
prevNameTokenStart = 0; |
| 3648 |
prevNameTokenString = ""; |
| 3649 |
prevNameTokenLineno = 0; |
| 3650 |
} |
| 3651 |
if (s == null) { |
| 3652 |
if (compilerEnv.isIdeMode()) { |
| 3653 |
s = ""; |
| 3654 |
} else { |
| 3655 |
codeBug(); |
| 3656 |
} |
| 3657 |
} |
| 3658 |
Name name = new Name(beg, s); |
| 3659 |
name.setLineno(lineno); |
| 3660 |
if (checkActivation) { |
| 3661 |
checkActivationName(s, token); |
| 3662 |
} |
| 3663 |
return name; |
| 3664 |
} |
| 3665 |
|
| 3666 |
private StringLiteral createStringLiteral() { |
| 3667 |
int pos = ts.tokenBeg, end = ts.tokenEnd; |
| 3668 |
StringLiteral s = new StringLiteral(pos, end - pos); |
| 3669 |
s.setLineno(ts.lineno); |
| 3670 |
s.setValue(ts.getString()); |
| 3671 |
s.setQuoteCharacter(ts.getQuoteChar()); |
| 3672 |
return s; |
| 3673 |
} |
| 3674 |
|
| 3675 |
protected void checkActivationName(String name, int token) { |
| 3676 |
if (!insideFunction()) { |
| 3677 |
return; |
| 3678 |
} |
| 3679 |
boolean activation = false; |
| 3680 |
if ("arguments".equals(name) && |
| 3681 |
|
| 3682 |
((FunctionNode)currentScriptOrFn).getFunctionType() != FunctionNode.ARROW_FUNCTION) { |
| 3683 |
activation = true; |
| 3684 |
} else if (compilerEnv.getActivationNames() != null |
| 3685 |
&& compilerEnv.getActivationNames().contains(name)) { |
| 3686 |
activation = true; |
| 3687 |
} else if ("length".equals(name)) { |
| 3688 |
if (token == Token.GETPROP |
| 3689 |
&& compilerEnv.getLanguageVersion() == Context.VERSION_1_2) |
| 3690 |
{ |
| 3691 |
|
| 3692 |
activation = true; |
| 3693 |
} |
| 3694 |
} |
| 3695 |
if (activation) { |
| 3696 |
setRequiresActivation(); |
| 3697 |
} |
| 3698 |
} |
| 3699 |
|
| 3700 |
protected void setRequiresActivation() { |
| 3701 |
if (insideFunction()) { |
| 3702 |
((FunctionNode)currentScriptOrFn).setRequiresActivation(); |
| 3703 |
} |
| 3704 |
} |
| 3705 |
|
| 3706 |
private void checkCallRequiresActivation(AstNode pn) { |
| 3707 |
if ((pn.getType() == Token.NAME |
| 3708 |
&& "eval".equals(((Name)pn).getIdentifier())) |
| 3709 |
|| (pn.getType() == Token.GETPROP && |
| 3710 |
"eval".equals(((PropertyGet)pn).getProperty().getIdentifier()))) |
| 3711 |
setRequiresActivation(); |
| 3712 |
} |
| 3713 |
|
| 3714 |
protected void setIsGenerator() { |
| 3715 |
if (insideFunction()) { |
| 3716 |
((FunctionNode)currentScriptOrFn).setIsGenerator(); |
| 3717 |
} |
| 3718 |
} |
| 3719 |
|
| 3720 |
private void checkBadIncDec(UnaryExpression expr) { |
| 3721 |
AstNode op = removeParens(expr.getOperand()); |
| 3722 |
int tt = op.getType(); |
| 3723 |
if (!(tt == Token.NAME |
| 3724 |
|| tt == Token.GETPROP |
| 3725 |
|| tt == Token.GETELEM |
| 3726 |
|| tt == Token.GET_REF |
| 3727 |
|| tt == Token.CALL)) |
| 3728 |
reportError(expr.getType() == Token.INC |
| 3729 |
? "msg.bad.incr" |
| 3730 |
: "msg.bad.decr"); |
| 3731 |
} |
| 3732 |
|
| 3733 |
private ErrorNode makeErrorNode() { |
| 3734 |
ErrorNode pn = new ErrorNode(ts.tokenBeg, ts.tokenEnd - ts.tokenBeg); |
| 3735 |
pn.setLineno(ts.lineno); |
| 3736 |
return pn; |
| 3737 |
} |
| 3738 |
|
| 3739 |
|
| 3740 |
private int nodeEnd(AstNode node) { |
| 3741 |
return node.getPosition() + node.getLength(); |
| 3742 |
} |
| 3743 |
|
| 3744 |
private void saveNameTokenData(int pos, String name, int lineno) { |
| 3745 |
prevNameTokenStart = pos; |
| 3746 |
prevNameTokenString = name; |
| 3747 |
prevNameTokenLineno = lineno; |
| 3748 |
} |
| 3749 |
|
| 3750 |
|
| 3751 |
|
| 3752 |
|
| 3753 |
|
| 3754 |
|
| 3755 |
|
| 3756 |
|
| 3757 |
|
| 3758 |
|
| 3759 |
|
| 3760 |
|
| 3761 |
|
| 3762 |
|
| 3763 |
private int lineBeginningFor(int pos) { |
| 3764 |
if (sourceChars == null) { |
| 3765 |
return -1; |
| 3766 |
} |
| 3767 |
if (pos <= 0) { |
| 3768 |
return 0; |
| 3769 |
} |
| 3770 |
char[] buf = sourceChars; |
| 3771 |
if (pos >= buf.length) { |
| 3772 |
pos = buf.length - 1; |
| 3773 |
} |
| 3774 |
while (--pos >= 0) { |
| 3775 |
char c = buf[pos]; |
| 3776 |
if (ScriptRuntime.isJSLineTerminator(c)) { |
| 3777 |
return pos + 1; |
| 3778 |
} |
| 3779 |
} |
| 3780 |
return 0; |
| 3781 |
} |
| 3782 |
|
| 3783 |
private void warnMissingSemi(int pos, int end) { |
| 3784 |
|
| 3785 |
|
| 3786 |
|
| 3787 |
if (compilerEnv.isStrictMode()) { |
| 3788 |
int[] linep = new int[2]; |
| 3789 |
String line = ts.getLine(end, linep); |
| 3790 |
|
| 3791 |
|
| 3792 |
|
| 3793 |
int beg = compilerEnv.isIdeMode() |
| 3794 |
? Math.max(pos, end - linep[1]) |
| 3795 |
: pos; |
| 3796 |
if (line != null) { |
| 3797 |
addStrictWarning("msg.missing.semi", "", beg, end - beg, |
| 3798 |
linep[0], line, linep[1]); |
| 3799 |
} else { |
| 3800 |
|
| 3801 |
addStrictWarning("msg.missing.semi", "", beg, end - beg); |
| 3802 |
} |
| 3803 |
} |
| 3804 |
} |
| 3805 |
|
| 3806 |
private void warnTrailingComma(int pos, List<?> elems, int commaPos) { |
| 3807 |
if (compilerEnv.getWarnTrailingComma()) { |
| 3808 |
|
| 3809 |
if (!elems.isEmpty()) { |
| 3810 |
pos = ((AstNode)elems.get(0)).getPosition(); |
| 3811 |
} |
| 3812 |
pos = Math.max(pos, lineBeginningFor(commaPos)); |
| 3813 |
addWarning("msg.extra.trailing.comma", pos, commaPos - pos); |
| 3814 |
} |
| 3815 |
} |
| 3816 |
|
| 3817 |
|
| 3818 |
private String readFully(Reader reader) throws IOException { |
| 3819 |
BufferedReader in = new BufferedReader(reader); |
| 3820 |
try { |
| 3821 |
char[] cbuf = new char[1024]; |
| 3822 |
StringBuilder sb = new StringBuilder(1024); |
| 3823 |
int bytes_read; |
| 3824 |
while ((bytes_read = in.read(cbuf, 0, 1024)) != -1) { |
| 3825 |
sb.append(cbuf, 0, bytes_read); |
| 3826 |
} |
| 3827 |
return sb.toString(); |
| 3828 |
} finally { |
| 3829 |
in.close(); |
| 3830 |
} |
| 3831 |
} |
| 3832 |
|
| 3833 |
|
| 3834 |
protected class PerFunctionVariables |
| 3835 |
{ |
| 3836 |
private ScriptNode savedCurrentScriptOrFn; |
| 3837 |
private Scope savedCurrentScope; |
| 3838 |
private int savedEndFlags; |
| 3839 |
private boolean savedInForInit; |
| 3840 |
private Map<String,LabeledStatement> savedLabelSet; |
| 3841 |
private List<Loop> savedLoopSet; |
| 3842 |
private List<Jump> savedLoopAndSwitchSet; |
| 3843 |
|
| 3844 |
PerFunctionVariables(FunctionNode fnNode) { |
| 3845 |
savedCurrentScriptOrFn = Parser.this.currentScriptOrFn; |
| 3846 |
Parser.this.currentScriptOrFn = fnNode; |
| 3847 |
|
| 3848 |
savedCurrentScope = Parser.this.currentScope; |
| 3849 |
Parser.this.currentScope = fnNode; |
| 3850 |
|
| 3851 |
savedLabelSet = Parser.this.labelSet; |
| 3852 |
Parser.this.labelSet = null; |
| 3853 |
|
| 3854 |
savedLoopSet = Parser.this.loopSet; |
| 3855 |
Parser.this.loopSet = null; |
| 3856 |
|
| 3857 |
savedLoopAndSwitchSet = Parser.this.loopAndSwitchSet; |
| 3858 |
Parser.this.loopAndSwitchSet = null; |
| 3859 |
|
| 3860 |
savedEndFlags = Parser.this.endFlags; |
| 3861 |
Parser.this.endFlags = 0; |
| 3862 |
|
| 3863 |
savedInForInit = Parser.this.inForInit; |
| 3864 |
Parser.this.inForInit = false; |
| 3865 |
} |
| 3866 |
|
| 3867 |
void restore() { |
| 3868 |
Parser.this.currentScriptOrFn = savedCurrentScriptOrFn; |
| 3869 |
Parser.this.currentScope = savedCurrentScope; |
| 3870 |
Parser.this.labelSet = savedLabelSet; |
| 3871 |
Parser.this.loopSet = savedLoopSet; |
| 3872 |
Parser.this.loopAndSwitchSet = savedLoopAndSwitchSet; |
| 3873 |
Parser.this.endFlags = savedEndFlags; |
| 3874 |
Parser.this.inForInit = savedInForInit; |
| 3875 |
} |
| 3876 |
} |
| 3877 |
|
| 3878 |
|
| 3879 |
|
| 3880 |
|
| 3881 |
|
| 3882 |
|
| 3883 |
|
| 3884 |
|
| 3885 |
|
| 3886 |
|
| 3887 |
|
| 3888 |
|
| 3889 |
|
| 3890 |
Node createDestructuringAssignment(int type, Node left, Node right) |
| 3891 |
{ |
| 3892 |
String tempName = currentScriptOrFn.getNextTempName(); |
| 3893 |
Node result = destructuringAssignmentHelper(type, left, right, |
| 3894 |
tempName); |
| 3895 |
Node comma = result.getLastChild(); |
| 3896 |
comma.addChildToBack(createName(tempName)); |
| 3897 |
return result; |
| 3898 |
} |
| 3899 |
|
| 3900 |
Node destructuringAssignmentHelper(int variableType, Node left, |
| 3901 |
Node right, String tempName) |
| 3902 |
{ |
| 3903 |
Scope result = createScopeNode(Token.LETEXPR, left.getLineno()); |
| 3904 |
result.addChildToFront(new Node(Token.LET, |
| 3905 |
createName(Token.NAME, tempName, right))); |
| 3906 |
try { |
| 3907 |
pushScope(result); |
| 3908 |
defineSymbol(Token.LET, tempName, true); |
| 3909 |
} finally { |
| 3910 |
popScope(); |
| 3911 |
} |
| 3912 |
Node comma = new Node(Token.COMMA); |
| 3913 |
result.addChildToBack(comma); |
| 3914 |
List<String> destructuringNames = new ArrayList<String>(); |
| 3915 |
boolean empty = true; |
| 3916 |
switch (left.getType()) { |
| 3917 |
case Token.ARRAYLIT: |
| 3918 |
empty = destructuringArray((ArrayLiteral)left, |
| 3919 |
variableType, tempName, comma, |
| 3920 |
destructuringNames); |
| 3921 |
break; |
| 3922 |
case Token.OBJECTLIT: |
| 3923 |
empty = destructuringObject((ObjectLiteral)left, |
| 3924 |
variableType, tempName, comma, |
| 3925 |
destructuringNames); |
| 3926 |
break; |
| 3927 |
case Token.GETPROP: |
| 3928 |
case Token.GETELEM: |
| 3929 |
switch (variableType) { |
| 3930 |
case Token.CONST: |
| 3931 |
case Token.LET: |
| 3932 |
case Token.VAR: |
| 3933 |
reportError("msg.bad.assign.left"); |
| 3934 |
} |
| 3935 |
comma.addChildToBack(simpleAssignment(left, createName(tempName))); |
| 3936 |
break; |
| 3937 |
default: |
| 3938 |
reportError("msg.bad.assign.left"); |
| 3939 |
} |
| 3940 |
if (empty) { |
| 3941 |
|
| 3942 |
comma.addChildToBack(createNumber(0)); |
| 3943 |
} |
| 3944 |
result.putProp(Node.DESTRUCTURING_NAMES, destructuringNames); |
| 3945 |
return result; |
| 3946 |
} |
| 3947 |
|
| 3948 |
boolean destructuringArray(ArrayLiteral array, |
| 3949 |
int variableType, |
| 3950 |
String tempName, |
| 3951 |
Node parent, |
| 3952 |
List<String> destructuringNames) |
| 3953 |
{ |
| 3954 |
boolean empty = true; |
| 3955 |
int setOp = variableType == Token.CONST |
| 3956 |
? Token.SETCONST : Token.SETNAME; |
| 3957 |
int index = 0; |
| 3958 |
for (AstNode n : array.getElements()) { |
| 3959 |
if (n.getType() == Token.EMPTY) { |
| 3960 |
index++; |
| 3961 |
continue; |
| 3962 |
} |
| 3963 |
Node rightElem = new Node(Token.GETELEM, |
| 3964 |
createName(tempName), |
| 3965 |
createNumber(index)); |
| 3966 |
if (n.getType() == Token.NAME) { |
| 3967 |
String name = n.getString(); |
| 3968 |
parent.addChildToBack(new Node(setOp, |
| 3969 |
createName(Token.BINDNAME, |
| 3970 |
name, null), |
| 3971 |
rightElem)); |
| 3972 |
if (variableType != -1) { |
| 3973 |
defineSymbol(variableType, name, true); |
| 3974 |
destructuringNames.add(name); |
| 3975 |
} |
| 3976 |
} else { |
| 3977 |
parent.addChildToBack |
| 3978 |
(destructuringAssignmentHelper |
| 3979 |
(variableType, n, |
| 3980 |
rightElem, |
| 3981 |
currentScriptOrFn.getNextTempName())); |
| 3982 |
} |
| 3983 |
index++; |
| 3984 |
empty = false; |
| 3985 |
} |
| 3986 |
return empty; |
| 3987 |
} |
| 3988 |
|
| 3989 |
boolean destructuringObject(ObjectLiteral node, |
| 3990 |
int variableType, |
| 3991 |
String tempName, |
| 3992 |
Node parent, |
| 3993 |
List<String> destructuringNames) |
| 3994 |
{ |
| 3995 |
boolean empty = true; |
| 3996 |
int setOp = variableType == Token.CONST |
| 3997 |
? Token.SETCONST : Token.SETNAME; |
| 3998 |
|
| 3999 |
for (ObjectProperty prop : node.getElements()) { |
| 4000 |
int lineno = 0; |
| 4001 |
|
| 4002 |
|
| 4003 |
|
| 4004 |
if (ts != null) { |
| 4005 |
lineno = ts.lineno; |
| 4006 |
} |
| 4007 |
AstNode id = prop.getLeft(); |
| 4008 |
Node rightElem = null; |
| 4009 |
if (id instanceof Name) { |
| 4010 |
Node s = Node.newString(((Name)id).getIdentifier()); |
| 4011 |
rightElem = new Node(Token.GETPROP, createName(tempName), s); |
| 4012 |
} else if (id instanceof StringLiteral) { |
| 4013 |
Node s = Node.newString(((StringLiteral)id).getValue()); |
| 4014 |
rightElem = new Node(Token.GETPROP, createName(tempName), s); |
| 4015 |
} else if (id instanceof NumberLiteral) { |
| 4016 |
Node s = createNumber((int)((NumberLiteral)id).getNumber()); |
| 4017 |
rightElem = new Node(Token.GETELEM, createName(tempName), s); |
| 4018 |
} else { |
| 4019 |
throw codeBug(); |
| 4020 |
} |
| 4021 |
rightElem.setLineno(lineno); |
| 4022 |
AstNode value = prop.getRight(); |
| 4023 |
if (value.getType() == Token.NAME) { |
| 4024 |
String name = ((Name)value).getIdentifier(); |
| 4025 |
parent.addChildToBack(new Node(setOp, |
| 4026 |
createName(Token.BINDNAME, |
| 4027 |
name, null), |
| 4028 |
rightElem)); |
| 4029 |
if (variableType != -1) { |
| 4030 |
defineSymbol(variableType, name, true); |
| 4031 |
destructuringNames.add(name); |
| 4032 |
} |
| 4033 |
} else { |
| 4034 |
parent.addChildToBack |
| 4035 |
(destructuringAssignmentHelper |
| 4036 |
(variableType, value, rightElem, |
| 4037 |
currentScriptOrFn.getNextTempName())); |
| 4038 |
} |
| 4039 |
empty = false; |
| 4040 |
} |
| 4041 |
return empty; |
| 4042 |
} |
| 4043 |
|
| 4044 |
protected Node createName(String name) { |
| 4045 |
checkActivationName(name, Token.NAME); |
| 4046 |
return Node.newString(Token.NAME, name); |
| 4047 |
} |
| 4048 |
|
| 4049 |
protected Node createName(int type, String name, Node child) { |
| 4050 |
Node result = createName(name); |
| 4051 |
result.setType(type); |
| 4052 |
if (child != null) |
| 4053 |
result.addChildToBack(child); |
| 4054 |
return result; |
| 4055 |
} |
| 4056 |
|
| 4057 |
protected Node createNumber(double number) { |
| 4058 |
return Node.newNumber(number); |
| 4059 |
} |
| 4060 |
|
| 4061 |
|
| 4062 |
|
| 4063 |
|
| 4064 |
|
| 4065 |
|
| 4066 |
|
| 4067 |
|
| 4068 |
|
| 4069 |
protected Scope createScopeNode(int token, int lineno) { |
| 4070 |
Scope scope =new Scope(); |
| 4071 |
scope.setType(token); |
| 4072 |
scope.setLineno(lineno); |
| 4073 |
return scope; |
| 4074 |
} |
| 4075 |
|
| 4076 |
|
| 4077 |
|
| 4078 |
|
| 4079 |
|
| 4080 |
|
| 4081 |
|
| 4082 |
|
| 4083 |
|
| 4084 |
|
| 4085 |
|
| 4086 |
|
| 4087 |
|
| 4088 |
|
| 4089 |
|
| 4090 |
|
| 4091 |
|
| 4092 |
|
| 4093 |
|
| 4094 |
|
| 4095 |
|
| 4096 |
|
| 4097 |
|
| 4098 |
protected Node simpleAssignment(Node left, Node right) { |
| 4099 |
int nodeType = left.getType(); |
| 4100 |
switch (nodeType) { |
| 4101 |
case Token.NAME: |
| 4102 |
String name = ((Name) left).getIdentifier(); |
| 4103 |
if (inUseStrictDirective && |
| 4104 |
("eval".equals(name) || "arguments".equals(name))) |
| 4105 |
{ |
| 4106 |
reportError("msg.bad.id.strict", name); |
| 4107 |
} |
| 4108 |
left.setType(Token.BINDNAME); |
| 4109 |
return new Node(Token.SETNAME, left, right); |
| 4110 |
|
| 4111 |
case Token.GETPROP: |
| 4112 |
case Token.GETELEM: { |
| 4113 |
Node obj, id; |
| 4114 |
|
| 4115 |
|
| 4116 |
|
| 4117 |
|
| 4118 |
if (left instanceof PropertyGet) { |
| 4119 |
obj = ((PropertyGet)left).getTarget(); |
| 4120 |
id = ((PropertyGet)left).getProperty(); |
| 4121 |
} else if (left instanceof ElementGet) { |
| 4122 |
obj = ((ElementGet)left).getTarget(); |
| 4123 |
id = ((ElementGet)left).getElement(); |
| 4124 |
} else { |
| 4125 |
|
| 4126 |
obj = left.getFirstChild(); |
| 4127 |
id = left.getLastChild(); |
| 4128 |
} |
| 4129 |
int type; |
| 4130 |
if (nodeType == Token.GETPROP) { |
| 4131 |
type = Token.SETPROP; |
| 4132 |
|
| 4133 |
|
| 4134 |
|
| 4135 |
|
| 4136 |
|
| 4137 |
id.setType(Token.STRING); |
| 4138 |
} else { |
| 4139 |
type = Token.SETELEM; |
| 4140 |
} |
| 4141 |
return new Node(type, obj, id, right); |
| 4142 |
} |
| 4143 |
case Token.GET_REF: { |
| 4144 |
Node ref = left.getFirstChild(); |
| 4145 |
checkMutableReference(ref); |
| 4146 |
return new Node(Token.SET_REF, ref, right); |
| 4147 |
} |
| 4148 |
} |
| 4149 |
|
| 4150 |
throw codeBug(); |
| 4151 |
} |
| 4152 |
|
| 4153 |
protected void checkMutableReference(Node n) { |
| 4154 |
int memberTypeFlags = n.getIntProp(Node.MEMBER_TYPE_PROP, 0); |
| 4155 |
if ((memberTypeFlags & Node.DESCENDANTS_FLAG) != 0) { |
| 4156 |
reportError("msg.bad.assign.left"); |
| 4157 |
} |
| 4158 |
} |
| 4159 |
|
| 4160 |
|
| 4161 |
protected AstNode removeParens(AstNode node) { |
| 4162 |
while (node instanceof ParenthesizedExpression) { |
| 4163 |
node = ((ParenthesizedExpression)node).getExpression(); |
| 4164 |
} |
| 4165 |
return node; |
| 4166 |
} |
| 4167 |
|
| 4168 |
void markDestructuring(AstNode node) { |
| 4169 |
if (node instanceof DestructuringForm) { |
| 4170 |
((DestructuringForm)node).setIsDestructuring(true); |
| 4171 |
} else if (node instanceof ParenthesizedExpression) { |
| 4172 |
markDestructuring(((ParenthesizedExpression)node).getExpression()); |
| 4173 |
} |
| 4174 |
} |
| 4175 |
|
| 4176 |
|
| 4177 |
private RuntimeException codeBug() |
| 4178 |
throws RuntimeException |
| 4179 |
{ |
| 4180 |
throw Kit.codeBug("ts.cursor=" + ts.cursor |
| 4181 |
+ ", ts.tokenBeg=" + ts.tokenBeg |
| 4182 |
+ ", currentToken=" + currentToken); |
| 4183 |
} |
| 4184 |
|
| 4185 |
public void setDefaultUseStrictDirective(boolean useStrict) { |
| 4186 |
defaultUseStrictDirective = useStrict; |
| 4187 |
} |
| 4188 |
|
| 4189 |
public boolean inUseStrictDirective() { |
| 4190 |
return inUseStrictDirective; |
| 4191 |
} |
| 4192 |
} |