| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Defines the tokens that are produced by the scanner, used by the parser, and | 6 * Defines the tokens that are produced by the scanner, used by the parser, and |
| 7 * referenced from the [AST structure](ast.dart). | 7 * referenced from the [AST structure](ast.dart). |
| 8 */ | 8 */ |
| 9 import 'dart:collection'; | 9 import 'dart:collection'; |
| 10 | 10 |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 /** | 377 /** |
| 378 * A table mapping the lexemes of keywords to the corresponding keyword. | 378 * A table mapping the lexemes of keywords to the corresponding keyword. |
| 379 */ | 379 */ |
| 380 static final Map<String, Keyword> keywords = _createKeywordMap(); | 380 static final Map<String, Keyword> keywords = _createKeywordMap(); |
| 381 | 381 |
| 382 /** | 382 /** |
| 383 * A flag indicating whether the keyword is "built-in" identifier. | 383 * A flag indicating whether the keyword is "built-in" identifier. |
| 384 */ | 384 */ |
| 385 final bool isBuiltIn; | 385 final bool isBuiltIn; |
| 386 | 386 |
| 387 /** | 387 @override |
| 388 * A flag indicating whether the keyword can be used as an identifier | |
| 389 * in some situations. | |
| 390 */ | |
| 391 final bool isPseudo; | 388 final bool isPseudo; |
| 392 | 389 |
| 393 /** | 390 /** |
| 394 * Initialize a newly created keyword. | 391 * Initialize a newly created keyword. |
| 395 */ | 392 */ |
| 396 const Keyword(String lexeme, String name, | 393 const Keyword(String lexeme, String name, |
| 397 {this.isBuiltIn: false, | 394 {this.isBuiltIn: false, |
| 398 this.isPseudo: false, | 395 this.isPseudo: false, |
| 399 int precedence: NO_PRECEDENCE}) | 396 int precedence: NO_PRECEDENCE}) |
| 400 : super(lexeme, name, precedence, KEYWORD_TOKEN); | 397 : super(lexeme, name, precedence, KEYWORD_TOKEN); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 /** | 446 /** |
| 450 * Initialize a newly created token to represent the given [keyword] at the | 447 * Initialize a newly created token to represent the given [keyword] at the |
| 451 * given [offset]. | 448 * given [offset]. |
| 452 */ | 449 */ |
| 453 KeywordToken(this.keyword, int offset) : super(keyword, offset); | 450 KeywordToken(this.keyword, int offset) : super(keyword, offset); |
| 454 | 451 |
| 455 @override | 452 @override |
| 456 Token copy() => new KeywordToken(keyword, offset); | 453 Token copy() => new KeywordToken(keyword, offset); |
| 457 | 454 |
| 458 @override | 455 @override |
| 456 bool get isIdentifier => keyword.isPseudo || keyword.isBuiltIn; |
| 457 |
| 458 @override |
| 459 // Changed return type from Keyword to Object because | 459 // Changed return type from Keyword to Object because |
| 460 // fasta considers pseudo-keywords to be keywords rather than identifiers | 460 // fasta considers pseudo-keywords to be keywords rather than identifiers |
| 461 Object value() => keyword; | 461 Object value() => keyword; |
| 462 } | 462 } |
| 463 | 463 |
| 464 /** | 464 /** |
| 465 * A keyword token that is preceded by comments. | 465 * A keyword token that is preceded by comments. |
| 466 */ | 466 */ |
| 467 class KeywordTokenWithComment extends KeywordToken implements TokenWithComment { | 467 class KeywordTokenWithComment extends KeywordToken implements TokenWithComment { |
| 468 /** | 468 /** |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 */ | 521 */ |
| 522 @override | 522 @override |
| 523 int offset = 0; | 523 int offset = 0; |
| 524 | 524 |
| 525 /** | 525 /** |
| 526 * The previous token in the token stream. | 526 * The previous token in the token stream. |
| 527 */ | 527 */ |
| 528 @override | 528 @override |
| 529 Token previous; | 529 Token previous; |
| 530 | 530 |
| 531 /** | 531 @override |
| 532 * The next token in the token stream. | 532 Token next; |
| 533 */ | |
| 534 Token _next; | |
| 535 | 533 |
| 536 /** | 534 /** |
| 537 * Initialize a newly created token to have the given [type] and [offset]. | 535 * Initialize a newly created token to have the given [type] and [offset]. |
| 538 */ | 536 */ |
| 539 SimpleToken(this.type, this.offset); | 537 SimpleToken(this.type, this.offset); |
| 540 | 538 |
| 541 @override | 539 @override |
| 542 int get charCount => length; | 540 int get charCount => length; |
| 543 | 541 |
| 544 @override | 542 @override |
| 545 int get charOffset => offset; | 543 int get charOffset => offset; |
| 546 | 544 |
| 547 @override | 545 @override |
| 548 int get charEnd => end; | 546 int get charEnd => end; |
| 549 | 547 |
| 550 @override | 548 @override |
| 551 int get end => offset + length; | 549 int get end => offset + length; |
| 552 | 550 |
| 553 @override | 551 @override |
| 554 bool get isEof => type == TokenType.EOF; | 552 bool get isEof => type == TokenType.EOF; |
| 555 | 553 |
| 556 @override | 554 @override |
| 555 bool get isIdentifier => false; |
| 556 |
| 557 @override |
| 557 bool get isOperator => type.isOperator; | 558 bool get isOperator => type.isOperator; |
| 558 | 559 |
| 559 @override | 560 @override |
| 560 bool get isSynthetic => length == 0; | 561 bool get isSynthetic => length == 0; |
| 561 | 562 |
| 562 @override | 563 @override |
| 563 bool get isUserDefinableOperator => type.isUserDefinableOperator; | 564 bool get isUserDefinableOperator => type.isUserDefinableOperator; |
| 564 | 565 |
| 565 @override | 566 @override |
| 566 Keyword get keyword => null; | 567 Keyword get keyword => null; |
| 567 | 568 |
| 568 @override | 569 @override |
| 569 int get kind => type.kind; | 570 int get kind => type.kind; |
| 570 | 571 |
| 571 @override | 572 @override |
| 572 int get length => lexeme.length; | 573 int get length => lexeme.length; |
| 573 | 574 |
| 574 @override | 575 @override |
| 575 String get lexeme => type.lexeme; | 576 String get lexeme => type.lexeme; |
| 576 | 577 |
| 577 @override | 578 @override |
| 578 Token get next => _next; | |
| 579 | |
| 580 @override | |
| 581 CommentToken get precedingComments => null; | 579 CommentToken get precedingComments => null; |
| 582 | 580 |
| 583 @override | 581 @override |
| 584 String get stringValue => type.stringValue; | 582 String get stringValue => type.stringValue; |
| 585 | 583 |
| 586 @override | 584 @override |
| 587 void applyDelta(int delta) { | 585 void applyDelta(int delta) { |
| 588 offset += delta; | 586 offset += delta; |
| 589 } | 587 } |
| 590 | 588 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 611 for (TokenType type in types) { | 609 for (TokenType type in types) { |
| 612 if (this.type == type) { | 610 if (this.type == type) { |
| 613 return true; | 611 return true; |
| 614 } | 612 } |
| 615 } | 613 } |
| 616 return false; | 614 return false; |
| 617 } | 615 } |
| 618 | 616 |
| 619 @override | 617 @override |
| 620 Token setNext(Token token) { | 618 Token setNext(Token token) { |
| 621 _next = token; | 619 next = token; |
| 622 token.previous = this; | 620 token.previous = this; |
| 623 return token; | 621 return token; |
| 624 } | 622 } |
| 625 | 623 |
| 626 @override | 624 @override |
| 627 Token setNextWithoutSettingPrevious(Token token) { | 625 Token setNextWithoutSettingPrevious(Token token) { |
| 628 _next = token; | 626 next = token; |
| 629 return token; | 627 return token; |
| 630 } | 628 } |
| 631 | 629 |
| 632 @override | 630 @override |
| 633 String toString() => lexeme; | 631 String toString() => lexeme; |
| 634 | 632 |
| 635 @override | 633 @override |
| 636 Object value() => type.lexeme; | 634 Object value() => type.lexeme; |
| 637 | 635 |
| 638 /** | 636 /** |
| (...skipping 19 matching lines...) Expand all Loading... |
| 658 | 656 |
| 659 /** | 657 /** |
| 660 * Initialize a newly created token to represent a token of the given [type] | 658 * Initialize a newly created token to represent a token of the given [type] |
| 661 * with the given [value] at the given [offset]. | 659 * with the given [value] at the given [offset]. |
| 662 */ | 660 */ |
| 663 StringToken(TokenType type, String value, int offset) : super(type, offset) { | 661 StringToken(TokenType type, String value, int offset) : super(type, offset) { |
| 664 this._value = StringUtilities.intern(value); | 662 this._value = StringUtilities.intern(value); |
| 665 } | 663 } |
| 666 | 664 |
| 667 @override | 665 @override |
| 666 bool get isIdentifier => identical(kind, IDENTIFIER_TOKEN); |
| 667 |
| 668 @override |
| 668 String get lexeme => _value; | 669 String get lexeme => _value; |
| 669 | 670 |
| 670 @override | 671 @override |
| 671 Token copy() => new StringToken(type, _value, offset); | 672 Token copy() => new StringToken(type, _value, offset); |
| 672 | 673 |
| 673 @override | 674 @override |
| 674 String value() => _value; | 675 String value() => _value; |
| 675 } | 676 } |
| 676 | 677 |
| 677 /** | 678 /** |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 | 779 |
| 779 @override | 780 @override |
| 780 int get end; | 781 int get end; |
| 781 | 782 |
| 782 /** | 783 /** |
| 783 * Return `true` if this token represents an end of file. | 784 * Return `true` if this token represents an end of file. |
| 784 */ | 785 */ |
| 785 bool get isEof; | 786 bool get isEof; |
| 786 | 787 |
| 787 /** | 788 /** |
| 789 * True if this token is an identifier. Some keywords allowed as identifiers, |
| 790 * see implementation in [KeywordToken]. |
| 791 */ |
| 792 bool get isIdentifier; |
| 793 |
| 794 /** |
| 788 * Return `true` if this token represents an operator. | 795 * Return `true` if this token represents an operator. |
| 789 */ | 796 */ |
| 790 bool get isOperator; | 797 bool get isOperator; |
| 791 | 798 |
| 792 /** | 799 /** |
| 793 * Return `true` if this token is a synthetic token. A synthetic token is a | 800 * Return `true` if this token is a synthetic token. A synthetic token is a |
| 794 * token that was introduced by the parser in order to recover from an error | 801 * token that was introduced by the parser in order to recover from an error |
| 795 * in the code. | 802 * in the code. |
| 796 */ | 803 */ |
| 797 bool get isSynthetic; | 804 bool get isSynthetic; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 810 /** | 817 /** |
| 811 * The kind enum of this token as determined by its [type]. | 818 * The kind enum of this token as determined by its [type]. |
| 812 */ | 819 */ |
| 813 int get kind; | 820 int get kind; |
| 814 | 821 |
| 815 @override | 822 @override |
| 816 int get length; | 823 int get length; |
| 817 | 824 |
| 818 /** | 825 /** |
| 819 * Return the lexeme that represents this token. | 826 * Return the lexeme that represents this token. |
| 827 * |
| 828 * For [StringToken]s the [lexeme] includes the quotes, explicit escapes, etc. |
| 820 */ | 829 */ |
| 821 String get lexeme; | 830 String get lexeme; |
| 822 | 831 |
| 823 /** | 832 /** |
| 824 * Return the next token in the token stream. | 833 * Return the next token in the token stream. |
| 825 */ | 834 */ |
| 826 Token get next; | 835 Token get next; |
| 827 | 836 |
| 837 /** |
| 838 * Return the next token in the token stream. |
| 839 */ |
| 840 void set next(Token next); |
| 841 |
| 828 @override | 842 @override |
| 829 int get offset; | 843 int get offset; |
| 830 | 844 |
| 831 /** | 845 /** |
| 832 * Set the offset from the beginning of the file to the first character in | 846 * Set the offset from the beginning of the file to the first character in |
| 833 * the token to the given [offset]. | 847 * the token to the given [offset]. |
| 834 */ | 848 */ |
| 835 void set offset(int offset); | 849 void set offset(int offset); |
| 836 | 850 |
| 837 /** | 851 /** |
| (...skipping 728 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1566 */ | 1580 */ |
| 1567 bool get isIncrementOperator => | 1581 bool get isIncrementOperator => |
| 1568 this == TokenType.PLUS_PLUS || this == TokenType.MINUS_MINUS; | 1582 this == TokenType.PLUS_PLUS || this == TokenType.MINUS_MINUS; |
| 1569 | 1583 |
| 1570 /** | 1584 /** |
| 1571 * Return `true` if this type of token is a keyword. | 1585 * Return `true` if this type of token is a keyword. |
| 1572 */ | 1586 */ |
| 1573 bool get isKeyword => kind == KEYWORD_TOKEN; | 1587 bool get isKeyword => kind == KEYWORD_TOKEN; |
| 1574 | 1588 |
| 1575 /** | 1589 /** |
| 1590 * A flag indicating whether the keyword can be used as an identifier |
| 1591 * in some situations. |
| 1592 */ |
| 1593 bool get isPseudo => false; |
| 1594 |
| 1595 /** |
| 1576 * Return `true` if this type of token represents a multiplicative operator. | 1596 * Return `true` if this type of token represents a multiplicative operator. |
| 1577 */ | 1597 */ |
| 1578 bool get isMultiplicativeOperator => precedence == MULTIPLICATIVE_PRECEDENCE; | 1598 bool get isMultiplicativeOperator => precedence == MULTIPLICATIVE_PRECEDENCE; |
| 1579 | 1599 |
| 1580 /** | 1600 /** |
| 1581 * Return `true` if this type of token represents a relational operator. | 1601 * Return `true` if this type of token represents a relational operator. |
| 1582 */ | 1602 */ |
| 1583 bool get isRelationalOperator => | 1603 bool get isRelationalOperator => |
| 1584 this == TokenType.LT || | 1604 this == TokenType.LT || |
| 1585 this == TokenType.LT_EQ || | 1605 this == TokenType.LT_EQ || |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1638 | 1658 |
| 1639 void set precedingComments(CommentToken comment) { | 1659 void set precedingComments(CommentToken comment) { |
| 1640 _precedingComment = comment; | 1660 _precedingComment = comment; |
| 1641 _setCommentParent(_precedingComment); | 1661 _setCommentParent(_precedingComment); |
| 1642 } | 1662 } |
| 1643 | 1663 |
| 1644 @override | 1664 @override |
| 1645 Token copy() => | 1665 Token copy() => |
| 1646 new TokenWithComment(type, offset, copyComments(precedingComments)); | 1666 new TokenWithComment(type, offset, copyComments(precedingComments)); |
| 1647 } | 1667 } |
| OLD | NEW |