Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 interface Scanner { | 5 interface Scanner { |
| 6 Token tokenize(); | 6 Token tokenize(); |
| 7 } | 7 } |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Common base class for a Dart scanner. | 10 * Common base class for a Dart scanner. |
| 11 */ | 11 */ |
| 12 class AbstractScanner<T extends SourceString> implements Scanner { | 12 class AbstractScanner<T extends SourceString> implements Scanner { |
| 13 abstract int advance(); | 13 abstract int advance(); |
| 14 abstract int nextByte(); | 14 abstract int nextByte(); |
| 15 abstract int peek(); | 15 abstract int peek(); |
| 16 abstract int select(int choice, PrecedenceInfo yes, PrecedenceInfo no); | 16 abstract int select(int choice, PrecedenceInfo yes, PrecedenceInfo no); |
| 17 /** | |
| 18 * Append a fixed token whose kind and content is determined by [info]. | |
| 19 */ | |
| 17 abstract void appendPrecedenceToken(PrecedenceInfo info); | 20 abstract void appendPrecedenceToken(PrecedenceInfo info); |
| 21 /** | |
| 22 * Append a token whose kind is determined by [info] and content is [value]. | |
| 23 */ | |
| 18 abstract void appendStringToken(PrecedenceInfo info, String value); | 24 abstract void appendStringToken(PrecedenceInfo info, String value); |
| 25 /** | |
| 26 * Append a token whose kind is determined by [info] and content is defined by | |
| 27 * the SourceString [value]. | |
| 28 */ | |
| 19 abstract void appendByteStringToken(PrecedenceInfo info, T value); | 29 abstract void appendByteStringToken(PrecedenceInfo info, T value); |
| 30 /** | |
| 31 * Append a keyword token whose kind is determined by [keyword]. | |
| 32 */ | |
| 20 abstract void appendKeywordToken(Keyword keyword); | 33 abstract void appendKeywordToken(Keyword keyword); |
| 21 abstract void appendWhiteSpace(int next); | 34 abstract void appendWhiteSpace(int next); |
| 22 abstract void appendEofToken(); | 35 abstract void appendEofToken(); |
| 36 /** | |
| 37 * Creates an ascii SourceString whose content begins at the source byte | |
| 38 * offset [start] and ends at [offset] bytes from the current byte offset of | |
| 39 * the scanner. I.e. if the current byte offset is 10, [:asciiString(0,-1):] | |
| 40 * creates an ascii SourceString whose content is found at the [0,9[ byte | |
| 41 * interval of the source text. | |
| 42 */ | |
| 23 abstract T asciiString(int start, int offset); | 43 abstract T asciiString(int start, int offset); |
| 24 abstract T utf8String(int start, int offset); | 44 abstract T utf8String(int start, int offset); |
| 25 abstract Token firstToken(); | 45 abstract Token firstToken(); |
| 26 abstract void beginToken(); | 46 abstract void beginToken(); |
| 27 abstract void addToCharOffset(int offset); | 47 abstract void addToCharOffset(int offset); |
| 28 abstract int get charOffset(); | 48 abstract int get charOffset(); |
| 29 abstract int get byteOffset(); | 49 abstract int get byteOffset(); |
| 30 abstract void appendBeginGroup(PrecedenceInfo info, String value); | 50 abstract void appendBeginGroup(PrecedenceInfo info, String value); |
| 31 abstract int appendEndGroup(PrecedenceInfo info, String value, int openKind); | 51 abstract int appendEndGroup(PrecedenceInfo info, String value, int openKind); |
| 32 abstract void appendGt(PrecedenceInfo info, String value); | 52 abstract void appendGt(PrecedenceInfo info, String value); |
| 33 abstract void appendGtGt(PrecedenceInfo info, String value); | 53 abstract void appendGtGt(PrecedenceInfo info, String value); |
| 34 abstract void appendGtGtGt(PrecedenceInfo info, String value); | 54 abstract void appendGtGtGt(PrecedenceInfo info, String value); |
| 55 abstract void appendComment(); | |
| 35 abstract void discardOpenLt(); | 56 abstract void discardOpenLt(); |
| 36 | 57 |
| 37 // TODO(ahe): Move this class to implementation. | 58 // TODO(ahe): Move this class to implementation. |
| 38 | 59 |
| 39 Token tokenize() { | 60 Token tokenize() { |
| 40 int next = advance(); | 61 int next = advance(); |
| 41 while (next !== $EOF) { | 62 while (next !== $EOF) { |
| 42 next = bigSwitch(next); | 63 next = bigSwitch(next); |
| 43 } | 64 } |
| 44 appendEofToken(); | 65 appendEofToken(); |
| (...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 514 } else { | 535 } else { |
| 515 appendPrecedenceToken(SLASH_INFO); | 536 appendPrecedenceToken(SLASH_INFO); |
| 516 return next; | 537 return next; |
| 517 } | 538 } |
| 518 } | 539 } |
| 519 | 540 |
| 520 int tokenizeSingleLineComment(int next) { | 541 int tokenizeSingleLineComment(int next) { |
| 521 while (true) { | 542 while (true) { |
| 522 next = advance(); | 543 next = advance(); |
| 523 if ($LF === next || $CR === next || $EOF === next) { | 544 if ($LF === next || $CR === next || $EOF === next) { |
| 545 appendComment(); | |
| 524 return next; | 546 return next; |
| 525 } | 547 } |
| 526 } | 548 } |
| 527 } | 549 } |
| 528 | 550 |
| 529 int tokenizeMultiLineComment(int next) { | 551 int tokenizeMultiLineComment(int next) { |
| 530 int nesting = 1; | 552 int nesting = 1; |
| 531 next = advance(); | 553 next = advance(); |
| 532 while (true) { | 554 while (true) { |
| 533 if ($EOF === next) { | 555 if ($EOF === next) { |
| 534 // TODO(ahe): Report error. | 556 // TODO(ahe): Report error. |
| 535 return next; | 557 return next; |
| 536 } else if ($STAR === next) { | 558 } else if ($STAR === next) { |
| 537 next = advance(); | 559 next = advance(); |
| 538 if ($SLASH === next) { | 560 if ($SLASH === next) { |
| 539 --nesting; | 561 --nesting; |
| 562 next = advance(); | |
| 540 if (0 === nesting) { | 563 if (0 === nesting) { |
| 541 return advance(); | 564 appendComment(); |
| 565 return next; | |
| 542 } else { | 566 } else { |
| 543 next = advance(); | 567 next = advance(); |
| 544 } | 568 } |
| 545 } | 569 } |
| 546 } else if ($SLASH === next) { | 570 } else if ($SLASH === next) { |
| 547 next = advance(); | 571 next = advance(); |
| 548 if ($STAR === next) { | 572 if ($STAR === next) { |
| 549 next = advance(); | 573 next = advance(); |
| 550 ++nesting; | 574 ++nesting; |
| 551 } | 575 } |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 657 throw new MalformedInputException("unterminated string literal", | 681 throw new MalformedInputException("unterminated string literal", |
| 658 charOffset); | 682 charOffset); |
| 659 } | 683 } |
| 660 next = advance(); | 684 next = advance(); |
| 661 } | 685 } |
| 662 appendByteStringToken(STRING_INFO, utf8String(start, 0)); | 686 appendByteStringToken(STRING_INFO, utf8String(start, 0)); |
| 663 return advance(); | 687 return advance(); |
| 664 } | 688 } |
| 665 | 689 |
| 666 int tokenizeStringInterpolation(int start) { | 690 int tokenizeStringInterpolation(int start) { |
| 667 beginToken(); | 691 appendByteStringToken(STRING_INFO, utf8String(start, -1)); |
| 692 beginToken(); // $ starts here | |
| 668 int next = advance(); | 693 int next = advance(); |
| 669 if (next === $OPEN_CURLY_BRACKET) { | 694 if (next === $OPEN_CURLY_BRACKET) { |
| 670 return tokenizeInterpolatedExpression(next, start); | 695 return tokenizeInterpolatedExpression(next, start); |
| 671 } else { | 696 } else { |
| 672 return tokenizeInterpolatedIdentifier(next, start); | 697 return tokenizeInterpolatedIdentifier(next, start); |
| 673 } | 698 } |
| 674 } | 699 } |
| 675 | 700 |
| 676 int tokenizeInterpolatedExpression(int next, int start) { | 701 int tokenizeInterpolatedExpression(int next, int start) { |
| 677 appendByteStringToken(STRING_INFO, utf8String(start, -2)); | |
| 678 appendBeginGroup(STRING_INTERPOLATION_INFO, "\${"); | 702 appendBeginGroup(STRING_INTERPOLATION_INFO, "\${"); |
| 703 beginToken(); // expression starts here | |
| 679 next = advance(); | 704 next = advance(); |
| 680 while (next !== $EOF && next !== $STX) { | 705 while (next !== $EOF && next !== $STX) { |
| 681 next = bigSwitch(next); | 706 next = bigSwitch(next); |
| 682 } | 707 } |
| 683 if (next === $EOF) return next; | 708 if (next === $EOF) return next; |
| 684 return advance(); | 709 next = advance(); |
| 710 beginToken(); // string interpolation suffix starts here | |
| 711 return next; | |
| 685 } | 712 } |
| 686 | 713 |
| 687 int tokenizeInterpolatedIdentifier(int next, int start) { | 714 int tokenizeInterpolatedIdentifier(int next, int start) { |
| 688 appendByteStringToken(STRING_INFO, utf8String(start, -2)); | 715 appendBeginGroup(STRING_INTERPOLATION_INFO, "\$"); |
|
Johnni Winther
2012/06/07 08:22:17
There seems to be a problem with using "\$" and no
| |
| 689 appendBeginGroup(STRING_INTERPOLATION_INFO, "\${"); | 716 beginToken(); // identifier starts here |
| 690 next = tokenizeKeywordOrIdentifier(next, false); | 717 next = tokenizeKeywordOrIdentifier(next, false); |
| 691 appendEndGroup(CLOSE_CURLY_BRACKET_INFO, "}", OPEN_CURLY_BRACKET_TOKEN); | 718 beginToken(); // string interpolation suffix starts here |
| 719 appendEndGroup(CLOSE_CURLY_BRACKET_INFO, "", OPEN_CURLY_BRACKET_TOKEN); | |
|
Johnni Winther
2012/06/07 08:22:17
There seems to be a problem with using "" and not
| |
| 692 return next; | 720 return next; |
| 693 } | 721 } |
| 694 | 722 |
| 695 int tokenizeSingleLineRawString(int next, int quoteChar, int start) { | 723 int tokenizeSingleLineRawString(int next, int quoteChar, int start) { |
| 696 next = advance(); | 724 next = advance(); |
| 697 while (next != $EOF) { | 725 while (next != $EOF) { |
| 698 if (next === quoteChar) { | 726 if (next === quoteChar) { |
| 699 appendByteStringToken(STRING_INFO, utf8String(start, 0)); | 727 appendByteStringToken(STRING_INFO, utf8String(start, 0)); |
| 700 return advance(); | 728 return advance(); |
| 701 } else if (next === $LF || next === $CR) { | 729 } else if (next === $LF || next === $CR) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 758 charOffset); | 786 charOffset); |
| 759 } | 787 } |
| 760 } | 788 } |
| 761 | 789 |
| 762 class MalformedInputException { | 790 class MalformedInputException { |
| 763 final String message; | 791 final String message; |
| 764 final position; | 792 final position; |
| 765 MalformedInputException(this.message, this.position); | 793 MalformedInputException(this.message, this.position); |
| 766 toString() => message; | 794 toString() => message; |
| 767 } | 795 } |
| OLD | NEW |