|
4 | 4 | import java.util.List; |
5 | 5 | import java.util.regex.Matcher; |
6 | 6 |
|
| 7 | +import com.semmle.jcorn.TokenType.Properties; |
7 | 8 | import com.semmle.jcorn.flow.FlowParser; |
8 | 9 | import com.semmle.js.ast.ArrayExpression; |
9 | 10 | import com.semmle.js.ast.AssignmentExpression; |
10 | 11 | import com.semmle.js.ast.BlockStatement; |
11 | 12 | import com.semmle.js.ast.CallExpression; |
12 | 13 | import com.semmle.js.ast.CatchClause; |
| 14 | +import com.semmle.js.ast.ClassExpression; |
13 | 15 | import com.semmle.js.ast.ComprehensionBlock; |
14 | 16 | import com.semmle.js.ast.ComprehensionExpression; |
| 17 | +import com.semmle.js.ast.Decorator; |
15 | 18 | import com.semmle.js.ast.Expression; |
16 | 19 | import com.semmle.js.ast.ExpressionStatement; |
17 | 20 | import com.semmle.js.ast.ForInStatement; |
|
28 | 31 | import com.semmle.js.ast.Position; |
29 | 32 | import com.semmle.js.ast.SourceLocation; |
30 | 33 | import com.semmle.js.ast.Statement; |
| 34 | +import com.semmle.js.ast.Token; |
31 | 35 | import com.semmle.js.ast.TryStatement; |
32 | 36 | import com.semmle.js.ast.VariableDeclaration; |
| 37 | +import com.semmle.js.ast.XMLAnyName; |
| 38 | +import com.semmle.js.ast.XMLAttributeSelector; |
| 39 | +import com.semmle.js.ast.XMLDotDotExpression; |
| 40 | +import com.semmle.js.ast.XMLFilterExpression; |
| 41 | +import com.semmle.js.ast.XMLQualifiedIdentifier; |
33 | 42 | import com.semmle.util.data.Pair; |
34 | 43 |
|
35 | 44 | /** |
@@ -155,6 +164,20 @@ protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) { |
155 | 164 | List<Expression> args = this.parseExprList(TokenType.parenR, false, false, null); |
156 | 165 | CallExpression node = new CallExpression(new SourceLocation(startLoc), name, new ArrayList<>(), args, false, false); |
157 | 166 | return this.finishNode(node); |
| 167 | + } else if (options.e4x() && this.type == at) { |
| 168 | + // this could be either a decorator or an attribute selector; we first |
| 169 | + // try parsing it as a decorator, and then convert it to an attribute selector |
| 170 | + // if the next token turns out not to be `class` |
| 171 | + List<Decorator> decorators = parseDecorators(); |
| 172 | + Expression attr = null; |
| 173 | + if (decorators.size() > 1 || |
| 174 | + this.type == TokenType._class || |
| 175 | + ((attr = decoratorToAttributeSelector(decorators.get(0))) == null)) { |
| 176 | + ClassExpression ce = (ClassExpression) this.parseClass(startLoc, false); |
| 177 | + ce.addDecorators(decorators); |
| 178 | + return ce; |
| 179 | + } |
| 180 | + return attr; |
158 | 181 | } else { |
159 | 182 | return super.parseExprAtom(refDestructuringErrors); |
160 | 183 | } |
@@ -320,4 +343,170 @@ protected Expression parseNew() { |
320 | 343 | } |
321 | 344 | return res; |
322 | 345 | } |
| 346 | + |
| 347 | + /* |
| 348 | + * E4X |
| 349 | + * |
| 350 | + * PrimaryExpression : |
| 351 | + * PropertyIdentifier |
| 352 | + * XMLInitialiser |
| 353 | + * XMLListInitialiser |
| 354 | + * |
| 355 | + * PropertyIdentifier : |
| 356 | + * AttributeIdentifier |
| 357 | + * QualifiedIdentifier |
| 358 | + * WildcardIdent |
| 359 | + * |
| 360 | + * AttributeIdentifier : |
| 361 | + * @ PropertySelector |
| 362 | + * @ QualifiedIdentifier |
| 363 | + * @ [ Expression ] |
| 364 | + * |
| 365 | + * PropertySelector : |
| 366 | + * Identifier |
| 367 | + * WildcardIdentifier |
| 368 | + * |
| 369 | + * QualifiedIdentifier : |
| 370 | + * PropertySelector :: PropertySelector |
| 371 | + * PropertySelector :: [ Expression ] |
| 372 | + * |
| 373 | + * WildcardIdentifier : |
| 374 | + * * |
| 375 | + * |
| 376 | + * MemberExpression : |
| 377 | + * MemberExpression . PropertyIdentifier |
| 378 | + * MemberExpression .. Identifier |
| 379 | + * MemberExpression .. PropertyIdentifier |
| 380 | + * MemberExpression . ( Expression ) |
| 381 | + * |
| 382 | + * DefaultXMLNamespaceStatement : |
| 383 | + * default xml namespace = Expression |
| 384 | + */ |
| 385 | + |
| 386 | + protected TokenType doubleDot = new TokenType(new Properties(":").beforeExpr()); |
| 387 | + |
| 388 | + @Override |
| 389 | + protected Token getTokenFromCode(int code) { |
| 390 | + if (options.e4x() && code == '.' && charAt(this.pos+1) == '.' && charAt(this.pos+2) != '.') { |
| 391 | + this.pos += 2; |
| 392 | + return this.finishToken(doubleDot); |
| 393 | + } |
| 394 | + return super.getTokenFromCode(code); |
| 395 | + } |
| 396 | + |
| 397 | + // add parsing of E4X property, attribute and descendant accesses, as well as filter expressions |
| 398 | + @Override |
| 399 | + protected Pair<Expression, Boolean> parseSubscript(Expression base, Position startLoc, boolean noCalls) { |
| 400 | + if (options.e4x() && this.eat(TokenType.dot)) { |
| 401 | + SourceLocation start = new SourceLocation(startLoc); |
| 402 | + if (this.eat(TokenType.parenL)) { |
| 403 | + Expression filter = parseExpression(false, null); |
| 404 | + this.expect(TokenType.parenR); |
| 405 | + return Pair.make(this.finishNode(new XMLFilterExpression(start, base, filter)), true); |
| 406 | + } |
| 407 | + |
| 408 | + Expression property = this.parsePropertyIdentifierOrIdentifier(); |
| 409 | + MemberExpression node = new MemberExpression(start, base, property, false, false, isOnOptionalChain(false, base)); |
| 410 | + return Pair.make(this.finishNode(node), true); |
| 411 | + } else if (this.eat(doubleDot)) { |
| 412 | + SourceLocation start = new SourceLocation(startLoc); |
| 413 | + Expression property = this.parsePropertyIdentifierOrIdentifier(); |
| 414 | + return Pair.make(this.finishNode(new XMLDotDotExpression(start, base, property)), true); |
| 415 | + } |
| 416 | + return super.parseSubscript(base, startLoc, noCalls); |
| 417 | + } |
| 418 | + |
| 419 | + /** |
| 420 | + * Parse a an attribute identifier, a wildcard identifier, a qualified identifier, |
| 421 | + * or a plain identifier. |
| 422 | + */ |
| 423 | + protected Expression parsePropertyIdentifierOrIdentifier() { |
| 424 | + Position start = this.startLoc; |
| 425 | + if (this.eat(at)) { |
| 426 | + // attribute identifier |
| 427 | + return parseAttributeIdentifier(new SourceLocation(start)); |
| 428 | + } else { |
| 429 | + return parsePossiblyQualifiedIdentifier(); |
| 430 | + } |
| 431 | + } |
| 432 | + |
| 433 | + /** |
| 434 | + * Parse a wildcard identifier, a qualified identifier, or a plain identifier. |
| 435 | + */ |
| 436 | + protected Expression parsePossiblyQualifiedIdentifier() { |
| 437 | + SourceLocation start = new SourceLocation(startLoc); |
| 438 | + Expression res = parsePropertySelector(start); |
| 439 | + |
| 440 | + if (!this.eat(doubleColon)) |
| 441 | + return res; |
| 442 | + |
| 443 | + if (this.eat(TokenType.bracketL)) { |
| 444 | + Expression e = parseExpression(false, null); |
| 445 | + this.expect(TokenType.bracketR); |
| 446 | + return this.finishNode(new XMLQualifiedIdentifier(start, res, e, true)); |
| 447 | + } else { |
| 448 | + Expression e = parsePropertySelector(new SourceLocation(startLoc)); |
| 449 | + return this.finishNode(new XMLQualifiedIdentifier(start, res, e, false)); |
| 450 | + } |
| 451 | + } |
| 452 | + |
| 453 | + /** |
| 454 | + * Parse a property selector, that is, either a wildcard identifier or a plain identifier. |
| 455 | + */ |
| 456 | + protected Expression parsePropertySelector(SourceLocation start) { |
| 457 | + Expression res; |
| 458 | + if (this.eat(TokenType.star)) { |
| 459 | + // wildcard identifier |
| 460 | + res = this.finishNode(new XMLAnyName(start)); |
| 461 | + } else { |
| 462 | + res = this.parseIdent(true); |
| 463 | + } |
| 464 | + return res; |
| 465 | + } |
| 466 | + |
| 467 | + /** |
| 468 | + * Parse an attribute identifier, either computed ({@code [ Expr ]}) or a possibly |
| 469 | + * qualified identifier. |
| 470 | + */ |
| 471 | + protected Expression parseAttributeIdentifier(SourceLocation start) { |
| 472 | + if (this.eat(TokenType.bracketL)) { |
| 473 | + Expression idx = parseExpression(false, null); |
| 474 | + this.expect(TokenType.bracketR); |
| 475 | + return this.finishNode(new XMLAttributeSelector(start, idx, true)); |
| 476 | + } else { |
| 477 | + return this.finishNode(new XMLAttributeSelector(start, parsePossiblyQualifiedIdentifier(), false)); |
| 478 | + } |
| 479 | + } |
| 480 | + |
| 481 | + @Override |
| 482 | + protected Expression parseDecoratorBody() { |
| 483 | + SourceLocation start = new SourceLocation(startLoc); |
| 484 | + if (options.e4x() && this.eat(TokenType.bracketL)) { |
| 485 | + // this must be an attribute selector, so only allow a single expression |
| 486 | + // followed by a right bracket, which will later be converted by |
| 487 | + // `decoratorToAttributeSelector` below |
| 488 | + List<Expression> elements = new ArrayList<>(); |
| 489 | + elements.add(parseExpression(false, null)); |
| 490 | + this.expect(TokenType.bracketR); |
| 491 | + return this.finishNode(new ArrayExpression(start, elements)); |
| 492 | + } |
| 493 | + |
| 494 | + return super.parseDecoratorBody(); |
| 495 | + } |
| 496 | + |
| 497 | + /** |
| 498 | + * Convert a decorator that resulted from mis-parsing an attribute selector into |
| 499 | + * an attribute selector. |
| 500 | + */ |
| 501 | + protected XMLAttributeSelector decoratorToAttributeSelector(Decorator d) { |
| 502 | + Expression e = d.getExpression(); |
| 503 | + if (e instanceof ArrayExpression) { |
| 504 | + ArrayExpression ae = (ArrayExpression) e; |
| 505 | + if (ae.getElements().size() == 1) |
| 506 | + return new XMLAttributeSelector(d.getLoc(), ae.getElements().get(0), true); |
| 507 | + } else if (e instanceof Identifier) { |
| 508 | + return new XMLAttributeSelector(d.getLoc(), e, false); |
| 509 | + } |
| 510 | + return null; |
| 511 | + } |
323 | 512 | } |
0 commit comments