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

Side by Side Diff: frog/leg/scanner/scanner.dart

Issue 9190038: Handle escapes in string literals. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added hadnling of scanner exceptions. Created 8 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « frog/leg/scanner/characters.dart ('k') | frog/leg/scanner/scanner_implementation.dart » ('j') | 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) 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 611 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 return next; 622 return next;
623 } 623 }
624 } 624 }
625 if (raw) { 625 if (raw) {
626 return tokenizeSingleLineRawString(next, q, start); 626 return tokenizeSingleLineRawString(next, q, start);
627 } else { 627 } else {
628 return tokenizeSingleLineString(next, q, start); 628 return tokenizeSingleLineString(next, q, start);
629 } 629 }
630 } 630 }
631 631
632 static bool isHexDigit(int character) {
633 if ($0 <= character && character <= $9) return true;
634 character |= 0x20;
635 return ($a <= character && character <= $f);
636 }
637
638 int tokenizeStringEscape(int next) {
639 if (next === $u) {
640 next = advance();
641 bool isBracketed = false;
642 int maxLength = 4;
643 if (next === $OPEN_CURLY_BRACKET) {
644 isBracketed = true;
645 maxLength = 7;
646 next = advance();
647 }
648 int startOffset = charOffset;
649 int unicodePoint = 0;
650 int length = 0;
651 while (length < maxLength && isHexDigit(next)) {
652 int digit = next <= $9 ? next - $0 : (next | 0x20) - ($a - 10);
653 unicodePoint = unicodePoint * 16 + digit;
654 length += 1;
655 next = advance();
656 }
657 if (isBracketed) {
658 if (length === 0) {
659 throw new MalformedInputException(startOffset);
660 }
661 if (next !== $CLOSE_CURLY_BRACKET) {
662 throw new MalformedInputException(charOffset);
663 }
664 next = advance();
665 } else if (length != 4) {
666 throw new MalformedInputException(startOffset + length);
667 }
668 if ((0xD800 <= unicodePoint && unicodePoint <= 0xDFFF) ||
669 unicodePoint > 0x10ffff) {
670 // Don't allow invalid code points in strings.
671 throw new MalformedInputException(startOffset);
672 }
673 return next;
674 } else if (next === $x) {
675 for (int i = 0; i < 2; i++) {
676 next = advance();
677 if (!isHexDigit(next)) {
678 throw new MalformedInputException(charOffset);
679 }
680 }
681 }
682 // All other characters following a backslash are always valid escapes
683 // in Dart.
684 return advance();
685 }
686
687
632 int tokenizeSingleLineString(int next, int q1, int start) { 688 int tokenizeSingleLineString(int next, int q1, int start) {
633 while (next !== $EOF) { 689 while (next !== $EOF) {
634 if (next === q1) { 690 if (next === q1) {
635 appendByteStringToken(STRING_INFO, utf8String(start, 0)); 691 appendByteStringToken(STRING_INFO, utf8String(start, 0));
636 return advance(); 692 return advance();
637 } else if (next === $BACKSLASH) { 693 } else if (next === $BACKSLASH) {
638 next = advance(); 694 next = advance();
639 if (next === $EOF) { 695 if (next === $EOF || next === $LF || next === $CR) {
ahe 2012/01/17 13:21:39 Shouldn't this be in tokenizeStringEscape?
Lasse Reichstein Nielsen 2012/01/18 08:22:34 This method has been reverted. This shouldn't hav
640 throw new MalformedInputException(charOffset); 696 throw new MalformedInputException(charOffset);
641 } 697 }
698 next = tokenizeStringEscape(next);
699 continue;
642 } else if (next === $$) { 700 } else if (next === $$) {
643 beginToken(); 701 beginToken();
644 next = advance(); 702 next = advance();
645 if (next === $OPEN_CURLY_BRACKET) { 703 if (next === $OPEN_CURLY_BRACKET) {
646 next = tokenizeInterpolatedExpression(next, start); 704 next = tokenizeInterpolatedExpression(next, start);
647 } else { 705 } else {
648 next = tokenizeInterpolatedIdentifier(next, start); 706 next = tokenizeInterpolatedIdentifier(next, start);
649 } 707 }
650 start = byteOffset; 708 start = byteOffset;
651 continue; 709 continue;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 } 767 }
710 return next; 768 return next;
711 } 769 }
712 } 770 }
713 771
714 class MalformedInputException { 772 class MalformedInputException {
715 final message; 773 final message;
716 MalformedInputException(this.message); 774 MalformedInputException(this.message);
717 toString() => message.toString(); 775 toString() => message.toString();
718 } 776 }
OLDNEW
« no previous file with comments | « frog/leg/scanner/characters.dart ('k') | frog/leg/scanner/scanner_implementation.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698