Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(627)

Side by Side Diff: pkg/front_end/lib/src/scanner/token.dart

Issue 3010533002: flatten token hierarchy (Closed)
Patch Set: Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/front_end/lib/src/scanner/scanner.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 26 matching lines...) Expand all
37 class BeginToken extends SimpleToken { 37 class BeginToken extends SimpleToken {
38 /** 38 /**
39 * The token that corresponds to this token. 39 * The token that corresponds to this token.
40 */ 40 */
41 Token endToken; 41 Token endToken;
42 42
43 /** 43 /**
44 * Initialize a newly created token to have the given [type] at the given 44 * Initialize a newly created token to have the given [type] at the given
45 * [offset]. 45 * [offset].
46 */ 46 */
47 BeginToken(TokenType type, int offset) : super(type, offset) { 47 BeginToken(TokenType type, int offset, [CommentToken precedingComment])
48 : super(type, offset, precedingComment) {
48 assert(type == TokenType.LT || 49 assert(type == TokenType.LT ||
49 type == TokenType.OPEN_CURLY_BRACKET || 50 type == TokenType.OPEN_CURLY_BRACKET ||
50 type == TokenType.OPEN_PAREN || 51 type == TokenType.OPEN_PAREN ||
51 type == TokenType.OPEN_SQUARE_BRACKET || 52 type == TokenType.OPEN_SQUARE_BRACKET ||
52 type == TokenType.STRING_INTERPOLATION_EXPRESSION); 53 type == TokenType.STRING_INTERPOLATION_EXPRESSION);
53 } 54 }
54 55
55 @override 56 @override
56 Token copy() => new BeginToken(type, offset); 57 Token copy() => new BeginToken(type, offset, copyComments(precedingComments));
57 58
58 /** 59 /**
59 * The token that corresponds to this token. 60 * The token that corresponds to this token.
60 */ 61 */
61 Token get endGroup => endToken; 62 Token get endGroup => endToken;
62 63
63 /** 64 /**
64 * Set the token that corresponds to this token. 65 * Set the token that corresponds to this token.
65 */ 66 */
66 set endGroup(Token token) { 67 set endGroup(Token token) {
67 endToken = token; 68 endToken = token;
68 } 69 }
69 } 70 }
70 71
71 /** 72 /**
72 * A begin token that is preceded by comments.
73 */
74 class BeginTokenWithComment extends BeginToken implements TokenWithComment {
75 /**
76 * The first comment in the list of comments that precede this token.
77 */
78 @override
79 CommentToken _precedingComment;
80
81 /**
82 * Initialize a newly created token to have the given [type] at the given
83 * [offset] and to be preceded by the comments reachable from the given
84 * [_precedingComment].
85 */
86 BeginTokenWithComment(TokenType type, int offset, this._precedingComment)
87 : super(type, offset) {
88 _setCommentParent(_precedingComment);
89 }
90
91 @override
92 CommentToken get precedingComments => _precedingComment;
93
94 @override
95 void set precedingComments(CommentToken comment) {
96 _precedingComment = comment;
97 _setCommentParent(_precedingComment);
98 }
99
100 @override
101 Token copy() =>
102 new BeginTokenWithComment(type, offset, copyComments(precedingComments));
103 }
104
105 /**
106 * A token representing a comment. 73 * A token representing a comment.
107 */ 74 */
108 class CommentToken extends StringToken { 75 class CommentToken extends StringToken {
109 /** 76 /**
110 * The token that contains this comment. 77 * The token that contains this comment.
111 */ 78 */
112 TokenWithComment parent; 79 SimpleToken parent;
113 80
114 /** 81 /**
115 * Initialize a newly created token to represent a token of the given [type] 82 * Initialize a newly created token to represent a token of the given [type]
116 * with the given [value] at the given [offset]. 83 * with the given [value] at the given [offset].
117 */ 84 */
118 CommentToken(TokenType type, String value, int offset) 85 CommentToken(TokenType type, String value, int offset)
119 : super(type, value, offset); 86 : super(type, value, offset);
120 87
121 @override 88 @override
122 CommentToken copy() => new CommentToken(type, _value, offset); 89 CommentToken copy() => new CommentToken(type, _value, offset);
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 * A token representing a keyword in the language. 411 * A token representing a keyword in the language.
445 */ 412 */
446 class KeywordToken extends SimpleToken { 413 class KeywordToken extends SimpleToken {
447 @override 414 @override
448 final Keyword keyword; 415 final Keyword keyword;
449 416
450 /** 417 /**
451 * Initialize a newly created token to represent the given [keyword] at the 418 * Initialize a newly created token to represent the given [keyword] at the
452 * given [offset]. 419 * given [offset].
453 */ 420 */
454 KeywordToken(this.keyword, int offset) : super(keyword, offset); 421 KeywordToken(this.keyword, int offset, [CommentToken precedingComment])
422 : super(keyword, offset, precedingComment);
455 423
456 @override 424 @override
457 Token copy() => new KeywordToken(keyword, offset); 425 Token copy() =>
426 new KeywordToken(keyword, offset, copyComments(precedingComments));
458 427
459 @override 428 @override
460 bool get isIdentifier => keyword.isPseudo || keyword.isBuiltIn; 429 bool get isIdentifier => keyword.isPseudo || keyword.isBuiltIn;
461 430
462 @override 431 @override
463 Object value() => keyword; 432 Object value() => keyword;
464 } 433 }
465 434
466 /** 435 /**
467 * A keyword token that is preceded by comments.
468 */
469 class KeywordTokenWithComment extends KeywordToken implements TokenWithComment {
470 /**
471 * The first comment in the list of comments that precede this token.
472 */
473 @override
474 CommentToken _precedingComment;
475
476 /**
477 * Initialize a newly created token to to represent the given [keyword] at the
478 * given [offset] and to be preceded by the comments reachable from the given
479 * [_precedingComment].
480 */
481 KeywordTokenWithComment(Keyword keyword, int offset, this._precedingComment)
482 : super(keyword, offset) {
483 _setCommentParent(_precedingComment);
484 }
485
486 @override
487 CommentToken get precedingComments => _precedingComment;
488
489 void set precedingComments(CommentToken comment) {
490 _precedingComment = comment;
491 _setCommentParent(_precedingComment);
492 }
493
494 @override
495 Token copy() => new KeywordTokenWithComment(
496 keyword, offset, copyComments(precedingComments));
497 }
498
499 /**
500 * A token that was scanned from the input. Each token knows which tokens 436 * A token that was scanned from the input. Each token knows which tokens
501 * precede and follow it, acting as a link in a doubly linked list of tokens. 437 * precede and follow it, acting as a link in a doubly linked list of tokens.
502 */ 438 */
503 class SimpleToken implements Token { 439 class SimpleToken implements Token {
504 /** 440 /**
505 * The type of the token. 441 * The type of the token.
506 */ 442 */
507 @override 443 @override
508 final TokenType type; 444 final TokenType type;
509 445
510 /** 446 /**
511 * The offset from the beginning of the file to the first character in the 447 * The offset from the beginning of the file to the first character in the
512 * token. 448 * token.
513 */ 449 */
514 @override 450 @override
515 int offset = 0; 451 int offset = 0;
516 452
517 /** 453 /**
518 * The previous token in the token stream. 454 * The previous token in the token stream.
519 */ 455 */
520 @override 456 @override
521 Token previous; 457 Token previous;
522 458
523 @override 459 @override
524 Token next; 460 Token next;
525 461
526 /** 462 /**
463 * The first comment in the list of comments that precede this token.
464 */
465 CommentToken _precedingComment;
466
467 /**
527 * Initialize a newly created token to have the given [type] and [offset]. 468 * Initialize a newly created token to have the given [type] and [offset].
528 */ 469 */
529 SimpleToken(this.type, this.offset); 470 SimpleToken(this.type, this.offset, [this._precedingComment]) {
471 _setCommentParent(_precedingComment);
472 }
530 473
531 @override 474 @override
532 int get charCount => length; 475 int get charCount => length;
533 476
534 @override 477 @override
535 int get charOffset => offset; 478 int get charOffset => offset;
536 479
537 @override 480 @override
538 int get charEnd => end; 481 int get charEnd => end;
539 482
(...skipping 21 matching lines...) Expand all
561 @override 504 @override
562 int get kind => type.kind; 505 int get kind => type.kind;
563 506
564 @override 507 @override
565 int get length => lexeme.length; 508 int get length => lexeme.length;
566 509
567 @override 510 @override
568 String get lexeme => type.lexeme; 511 String get lexeme => type.lexeme;
569 512
570 @override 513 @override
571 CommentToken get precedingComments => null; 514 CommentToken get precedingComments => _precedingComment;
515
516 void set precedingComments(CommentToken comment) {
517 _precedingComment = comment;
518 _setCommentParent(_precedingComment);
519 }
572 520
573 @override 521 @override
574 String get stringValue => type.stringValue; 522 String get stringValue => type.stringValue;
575 523
576 @override 524 @override
577 Token copy() => new Token(type, offset); 525 Token copy() =>
526 new SimpleToken(type, offset, copyComments(precedingComments));
578 527
579 @override 528 @override
580 Token copyComments(Token token) { 529 Token copyComments(Token token) {
581 if (token == null) { 530 if (token == null) {
582 return null; 531 return null;
583 } 532 }
584 Token head = token.copy(); 533 Token head = token.copy();
585 Token tail = head; 534 Token tail = head;
586 token = token.next; 535 token = token.next;
587 while (token != null) { 536 while (token != null) {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 class StringToken extends SimpleToken { 587 class StringToken extends SimpleToken {
639 /** 588 /**
640 * The lexeme represented by this token. 589 * The lexeme represented by this token.
641 */ 590 */
642 String _value; 591 String _value;
643 592
644 /** 593 /**
645 * Initialize a newly created token to represent a token of the given [type] 594 * Initialize a newly created token to represent a token of the given [type]
646 * with the given [value] at the given [offset]. 595 * with the given [value] at the given [offset].
647 */ 596 */
648 StringToken(TokenType type, String value, int offset) : super(type, offset) { 597 StringToken(TokenType type, String value, int offset,
598 [CommentToken precedingComment])
599 : super(type, offset, precedingComment) {
649 this._value = StringUtilities.intern(value); 600 this._value = StringUtilities.intern(value);
650 } 601 }
651 602
652 @override 603 @override
653 bool get isIdentifier => identical(kind, IDENTIFIER_TOKEN); 604 bool get isIdentifier => identical(kind, IDENTIFIER_TOKEN);
654 605
655 @override 606 @override
656 String get lexeme => _value; 607 String get lexeme => _value;
657 608
658 @override 609 @override
659 Token copy() => new StringToken(type, _value, offset); 610 Token copy() =>
611 new StringToken(type, _value, offset, copyComments(precedingComments));
660 612
661 @override 613 @override
662 String value() => _value; 614 String value() => _value;
663 } 615 }
664 616
665 /** 617 /**
666 * A string token that is preceded by comments.
667 */
668 class StringTokenWithComment extends StringToken implements TokenWithComment {
669 /**
670 * The first comment in the list of comments that precede this token.
671 */
672 CommentToken _precedingComment;
673
674 /**
675 * Initialize a newly created token to have the given [type] at the given
676 * [offset] and to be preceded by the comments reachable from the given
677 * [comment].
678 */
679 StringTokenWithComment(
680 TokenType type, String value, int offset, this._precedingComment)
681 : super(type, value, offset) {
682 _setCommentParent(_precedingComment);
683 }
684
685 @override
686 CommentToken get precedingComments => _precedingComment;
687
688 void set precedingComments(CommentToken comment) {
689 _precedingComment = comment;
690 _setCommentParent(_precedingComment);
691 }
692
693 @override
694 Token copy() => new StringTokenWithComment(
695 type, lexeme, offset, copyComments(precedingComments));
696 }
697
698 /**
699 * A synthetic keyword token. 618 * A synthetic keyword token.
700 */ 619 */
701 class SyntheticKeywordToken extends KeywordToken { 620 class SyntheticKeywordToken extends KeywordToken {
702 /** 621 /**
703 * Initialize a newly created token to represent the given [keyword] at the 622 * Initialize a newly created token to represent the given [keyword] at the
704 * given [offset]. 623 * given [offset].
705 */ 624 */
706 SyntheticKeywordToken(Keyword keyword, int offset) : super(keyword, offset); 625 SyntheticKeywordToken(Keyword keyword, int offset) : super(keyword, offset);
707 626
708 @override 627 @override
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
752 /** 671 /**
753 * A token that was scanned from the input. Each token knows which tokens 672 * A token that was scanned from the input. Each token knows which tokens
754 * precede and follow it, acting as a link in a doubly linked list of tokens. 673 * precede and follow it, acting as a link in a doubly linked list of tokens.
755 * 674 *
756 * Clients may not extend, implement or mix-in this class. 675 * Clients may not extend, implement or mix-in this class.
757 */ 676 */
758 abstract class Token implements SyntacticEntity { 677 abstract class Token implements SyntacticEntity {
759 /** 678 /**
760 * Initialize a newly created token to have the given [type] and [offset]. 679 * Initialize a newly created token to have the given [type] and [offset].
761 */ 680 */
762 factory Token(TokenType type, int offset) = SimpleToken; 681 factory Token(TokenType type, int offset, [CommentToken preceedingComment]) =
682 SimpleToken;
763 683
764 /** 684 /**
765 * Initialize a newly created end-of-file token to have the given [offset]. 685 * Initialize a newly created end-of-file token to have the given [offset].
766 */ 686 */
767 factory Token.eof(int offset, [CommentToken precedingComments]) { 687 factory Token.eof(int offset, [CommentToken precedingComments]) {
768 Token eof = precedingComments == null 688 Token eof = new SimpleToken(TokenType.EOF, offset, precedingComments);
769 ? new SimpleToken(TokenType.EOF, offset)
770 : new TokenWithComment(TokenType.EOF, offset, precedingComments);
771 // EOF points to itself so there's always infinite look-ahead. 689 // EOF points to itself so there's always infinite look-ahead.
772 eof.previous = eof; 690 eof.previous = eof;
773 eof.next = eof; 691 eof.next = eof;
774 return eof; 692 return eof;
775 } 693 }
776 694
777 /** 695 /**
778 * The number of characters parsed by this token. 696 * The number of characters parsed by this token.
779 */ 697 */
780 int get charCount; 698 int get charCount;
(...skipping 876 matching lines...) Expand 10 before | Expand all | Expand 10 after
1657 1575
1658 @override 1576 @override
1659 String toString() => name; 1577 String toString() => name;
1660 1578
1661 /** 1579 /**
1662 * Use [lexeme] instead of this method 1580 * Use [lexeme] instead of this method
1663 */ 1581 */
1664 @deprecated 1582 @deprecated
1665 String get value => lexeme; 1583 String get value => lexeme;
1666 } 1584 }
1667
1668 /**
1669 * A normal token that is preceded by comments.
1670 */
1671 class TokenWithComment extends SimpleToken {
1672 /**
1673 * The first comment in the list of comments that precede this token.
1674 */
1675 CommentToken _precedingComment;
1676
1677 /**
1678 * Initialize a newly created token to have the given [type] at the given
1679 * [offset] and to be preceded by the comments reachable from the given
1680 * [comment].
1681 */
1682 TokenWithComment(TokenType type, int offset, this._precedingComment)
1683 : super(type, offset) {
1684 _setCommentParent(_precedingComment);
1685 }
1686
1687 @override
1688 CommentToken get precedingComments => _precedingComment;
1689
1690 void set precedingComments(CommentToken comment) {
1691 _precedingComment = comment;
1692 _setCommentParent(_precedingComment);
1693 }
1694
1695 @override
1696 Token copy() =>
1697 new TokenWithComment(type, offset, copyComments(precedingComments));
1698 }
OLDNEW
« no previous file with comments | « pkg/front_end/lib/src/scanner/scanner.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698