Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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. |
| (...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 557 } | 557 } |
| 558 } | 558 } |
| 559 | 559 |
| 560 int tokenizeIdentifier(int next, int start) { | 560 int tokenizeIdentifier(int next, int start) { |
| 561 bool isAscii = true; | 561 bool isAscii = true; |
| 562 while (true) { | 562 while (true) { |
| 563 if (($a <= next && next <= $z) || | 563 if (($a <= next && next <= $z) || |
| 564 ($A <= next && next <= $Z) || | 564 ($A <= next && next <= $Z) || |
| 565 ($0 <= next && next <= $9) || | 565 ($0 <= next && next <= $9) || |
| 566 next === $_ || | 566 next === $_ || |
| 567 next === $$) { | 567 next === $$) { |
|
ngeoffray
2011/12/20 11:54:45
Is a boolean check really that bad here, compared
ahe
2011/12/20 13:19:24
I don't know. I'll try to clean this up in a follo
| |
| 568 next = advance(); | 568 next = advance(); |
| 569 } else if (next < 128) { | 569 } else if (next < 128) { |
| 570 if (isAscii) { | 570 if (isAscii) { |
| 571 appendByteStringToken(IDENTIFIER_TOKEN, asciiString(start, 0)); | 571 appendByteStringToken(IDENTIFIER_TOKEN, asciiString(start, 0)); |
| 572 } else { | 572 } else { |
| 573 appendByteStringToken(IDENTIFIER_TOKEN, utf8String(start, -1)); | 573 appendByteStringToken(IDENTIFIER_TOKEN, utf8String(start, -1)); |
| 574 } | 574 } |
| 575 return next; | 575 return next; |
| 576 } else { | 576 } else { |
| 577 int nonAsciiStart = byteOffset; | 577 int nonAsciiStart = byteOffset; |
| 578 do { | 578 do { |
| 579 next = nextByte(); | 579 next = nextByte(); |
| 580 } while (next > 127); | 580 } while (next > 127); |
| 581 String string = utf8String(nonAsciiStart, -1).toString(); | 581 String string = utf8String(nonAsciiStart, -1).toString(); |
| 582 isAscii = false; | 582 isAscii = false; |
| 583 int byteLength = nonAsciiStart - byteOffset; | 583 int byteLength = nonAsciiStart - byteOffset; |
| 584 addToCharOffset(string.length - byteLength); | 584 addToCharOffset(string.length - byteLength); |
| 585 } | 585 } |
| 586 } | 586 } |
| 587 } | 587 } |
| 588 | 588 |
| 589 int tokenizeIdentifierOrKeywordNoDollar(int next) { | |
|
ngeoffray
2011/12/20 11:54:45
The other method is called tokenizeKeywordOrIdenti
ahe
2011/12/20 13:19:24
Next CL.
| |
| 590 KeywordState state = KeywordState.KEYWORD_STATE; | |
| 591 int start = byteOffset; | |
| 592 while (state !== null && $a <= next && next <= $z) { | |
| 593 state = state.next(next); | |
| 594 next = advance(); | |
| 595 } | |
| 596 if (state === null || state.keyword === null) { | |
| 597 return tokenizeIdentifierNoDollar(next, start); | |
| 598 } | |
| 599 if (($A <= next && next <= $Z) || | |
| 600 ($0 <= next && next <= $9) || | |
| 601 next === $_ || | |
| 602 next === $$) { | |
| 603 return tokenizeIdentifierNoDollar(next, start); | |
| 604 } else if (next < 128) { | |
| 605 appendKeywordToken(state.keyword); | |
| 606 return next; | |
| 607 } else { | |
| 608 return tokenizeIdentifierNoDollar(next, start); | |
| 609 } | |
| 610 } | |
| 611 | |
| 612 int tokenizeIdentifierNoDollar(int next, int start) { | |
| 613 bool isAscii = true; | |
| 614 while (true) { | |
| 615 if (($a <= next && next <= $z) || | |
| 616 ($A <= next && next <= $Z) || | |
| 617 ($0 <= next && next <= $9) || | |
| 618 next === $_) { | |
| 619 next = advance(); | |
| 620 } else if (next < 128) { | |
| 621 if (isAscii) { | |
| 622 appendByteStringToken(IDENTIFIER_TOKEN, asciiString(start, 0)); | |
| 623 } else { | |
| 624 appendByteStringToken(IDENTIFIER_TOKEN, utf8String(start, -1)); | |
| 625 } | |
| 626 return next; | |
| 627 } else { | |
| 628 int nonAsciiStart = byteOffset; | |
| 629 do { | |
| 630 next = nextByte(); | |
| 631 } while (next > 127); | |
| 632 String string = utf8String(nonAsciiStart, -1).toString(); | |
| 633 isAscii = false; | |
| 634 int byteLength = nonAsciiStart - byteOffset; | |
| 635 addToCharOffset(string.length - byteLength); | |
| 636 } | |
| 637 } | |
| 638 } | |
| 639 | |
| 589 int tokenizeRawString(int next) { | 640 int tokenizeRawString(int next) { |
| 590 int start = byteOffset; | 641 int start = byteOffset; |
| 591 next = advance(); | 642 next = advance(); |
| 592 if (next === $DQ || next === $SQ) { | 643 if (next === $DQ || next === $SQ) { |
| 593 return tokenizeString(next, start, true); | 644 return tokenizeString(next, start, true); |
| 594 } else { | 645 } else { |
| 595 throw new MalformedInputException(charOffset); | 646 throw new MalformedInputException(charOffset); |
| 596 } | 647 } |
| 597 } | 648 } |
| 598 | 649 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 624 return advance(); | 675 return advance(); |
| 625 } else if (next === $BACKSLASH) { | 676 } else if (next === $BACKSLASH) { |
| 626 next = advance(); | 677 next = advance(); |
| 627 if (next === $EOF) { | 678 if (next === $EOF) { |
| 628 throw new MalformedInputException(charOffset); | 679 throw new MalformedInputException(charOffset); |
| 629 } | 680 } |
| 630 } else if (next === $$) { | 681 } else if (next === $$) { |
| 631 beginToken(); | 682 beginToken(); |
| 632 next = advance(); | 683 next = advance(); |
| 633 if (next === $OPEN_CURLY_BRACKET) { | 684 if (next === $OPEN_CURLY_BRACKET) { |
| 634 next = tokenizeInterpolatedExpression(next, q1, start); | 685 next = tokenizeInterpolatedExpression(next, start); |
| 686 } else { | |
| 687 next = tokenizeInterpolatedIdentifier(next, start); | |
| 635 } | 688 } |
| 689 start = byteOffset; | |
| 636 continue; | 690 continue; |
| 637 } else if (next === $LF || next === $CR) { | 691 } else if (next === $LF || next === $CR) { |
| 638 throw new MalformedInputException(charOffset); | 692 throw new MalformedInputException(charOffset); |
| 639 } | 693 } |
| 640 next = advance(); | 694 next = advance(); |
| 641 } | 695 } |
| 642 throw new MalformedInputException(charOffset); | 696 throw new MalformedInputException(charOffset); |
| 643 } | 697 } |
| 644 | 698 |
| 645 int tokenizeInterpolatedExpression(int next, int q, int start) { | 699 int tokenizeInterpolatedExpression(int next, int start) { |
| 646 appendByteStringToken(STRING_TOKEN, utf8String(start, 0)); | 700 appendByteStringToken(STRING_TOKEN, utf8String(start, -2)); |
| 647 appendBeginGroup(STRING_INTERPOLATION_TOKEN, "\${"); | 701 appendBeginGroup(STRING_INTERPOLATION_TOKEN, "\${"); |
| 648 next = advance(); | 702 next = advance(); |
| 649 while (next !== $EOF && next !== $STX) { | 703 while (next !== $EOF && next !== $STX) { |
| 650 next = bigSwitch(next); | 704 next = bigSwitch(next); |
| 651 } | 705 } |
| 652 if (next === $EOF) return next; | 706 if (next === $EOF) return next; |
| 653 return advance(); | 707 return advance(); |
| 654 } | 708 } |
| 655 | 709 |
| 710 int tokenizeInterpolatedIdentifier(int next, int start) { | |
| 711 appendByteStringToken(STRING_TOKEN, utf8String(start, -2)); | |
| 712 appendBeginGroup(STRING_INTERPOLATION_TOKEN, "\${"); | |
| 713 next = tokenizeIdentifierOrKeywordNoDollar(next); | |
| 714 appendEndGroup(CLOSE_CURLY_BRACKET_TOKEN, "}", OPEN_CURLY_BRACKET_TOKEN); | |
| 715 return next; | |
| 716 } | |
| 717 | |
| 656 int tokenizeSingleLineRawString(int next, int q1, int start) { | 718 int tokenizeSingleLineRawString(int next, int q1, int start) { |
| 657 next = advance(); | 719 next = advance(); |
| 658 while (next != $EOF) { | 720 while (next != $EOF) { |
| 659 if (next === q1) { | 721 if (next === q1) { |
| 660 appendByteStringToken(STRING_TOKEN, utf8String(start, 0)); | 722 appendByteStringToken(STRING_TOKEN, utf8String(start, 0)); |
| 661 return advance(); | 723 return advance(); |
| 662 } else if (next === $LF || next === $CR) { | 724 } else if (next === $LF || next === $CR) { |
| 663 throw new MalformedInputException(charOffset); | 725 throw new MalformedInputException(charOffset); |
| 664 } | 726 } |
| 665 next = advance(); | 727 next = advance(); |
| 666 } | 728 } |
| 667 throw new MalformedInputException(charOffset); | 729 throw new MalformedInputException(charOffset); |
| 668 } | 730 } |
| 669 | 731 |
| 670 int tokenizeMultiLineString(int q, int start, bool raw) { | 732 int tokenizeMultiLineString(int q, int start, bool raw) { |
| 671 // TODO(ahe): Handle escapes. | 733 // TODO(ahe): Handle escapes. |
| 734 // TODO(ahe): Handle string interpolation. | |
| 672 int next = advance(); | 735 int next = advance(); |
| 673 while (next != $EOF) { | 736 while (next != $EOF) { |
| 674 if (next === q) { | 737 if (next === q) { |
| 675 next = advance(); | 738 next = advance(); |
| 676 if (next === q) { | 739 if (next === q) { |
| 677 next = advance(); | 740 next = advance(); |
| 678 if (next === q) { | 741 if (next === q) { |
| 679 appendByteStringToken(STRING_TOKEN, utf8String(start, 0)); | 742 appendByteStringToken(STRING_TOKEN, utf8String(start, 0)); |
| 680 return advance(); | 743 return advance(); |
| 681 } | 744 } |
| 682 } | 745 } |
| 683 } | 746 } |
| 684 next = advance(); | 747 next = advance(); |
| 685 } | 748 } |
| 686 return next; | 749 return next; |
| 687 } | 750 } |
| 688 } | 751 } |
| 689 | 752 |
| 690 class MalformedInputException { | 753 class MalformedInputException { |
| 691 final message; | 754 final message; |
| 692 MalformedInputException(this.message); | 755 MalformedInputException(this.message); |
| 693 toString() => message.toString(); | 756 toString() => message.toString(); |
| 694 } | 757 } |
| OLD | NEW |