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

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: 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 | « no previous file | frog/leg/scanner/string_scanner.dart » ('j') | frog/leg/ssa/nodes.dart » ('J')
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
632 int tokenizeSingleLineString(int next, int q1, int start) { 638 int tokenizeSingleLineString(int next, int q1, int start) {
633 while (next !== $EOF) { 639 loop: while (next !== $EOF) {
634 if (next === q1) { 640 if (next === q1) {
635 appendByteStringToken(STRING_INFO, utf8String(start, 0)); 641 appendByteStringToken(STRING_INFO, utf8String(start, 0));
636 return advance(); 642 return advance();
637 } else if (next === $BACKSLASH) { 643 } else if (next === $BACKSLASH) {
638 next = advance(); 644 next = advance();
639 if (next === $EOF) { 645 if (next === $EOF || next === $LF || next === $CR) {
640 throw new MalformedInputException(charOffset); 646 throw new MalformedInputException(charOffset);
641 } 647 }
648 switch (next) {
649 case $u:
650 next = advance();
651 bool isBracketed = false;
652 int maxLength = 4;
653 if (next === $OPEN_CURLY_BRACKET) {
654 isBracketed = true;
655 maxLength = 7;
656 next = advance();
657 }
658 int startOffset = charOffset;
659 int unicodePoint = 0;
660 int length = 0;
661 while (length < maxLength && isHexDigit(next)) {
662 int digit = next <= $9 ? next - 0x30 : (next | 0x20) - ($a - 10);
663 unicodePoint = unicodePoint * 16 + digit;
664 length += 1;
665 next = advance();
666 }
667 if (isBracketed) {
668 if (length === 0) {
669 throw new MalformedInputException(startOffset);
670 }
671 if (next !== $CLOSE_CURLY_BRACKET) {
672 throw new MalformedInputException(charOffset);
673 }
674 next = advance();
675 } else if (length != 4) {
676 throw new MalformedInputException(startOffset + length);
677 }
678 if ((0xD800 <= unicodePoint && unicodePoint <= 0xDFFF) ||
679 unicodePoint > 0x10ffff) {
680 // Don't allow invalid code points in strings.
681 throw new MalformedInputException(startOffset);
682 }
683 // TODO(lrn): This should be just "continue", but the VM is bugged.
684 continue loop;
685 case $x:
686 for (int i = 0; i < 2; i++) {
687 next = advance();
688 if (!isHexDigit(next)) {
689 throw new MalformedInputException(charOffset);
690 }
691 }
692 break;
693 default:
694 // All other escapes are trivially valid.
695 }
642 } else if (next === $$) { 696 } else if (next === $$) {
643 beginToken(); 697 beginToken();
644 next = advance(); 698 next = advance();
645 if (next === $OPEN_CURLY_BRACKET) { 699 if (next === $OPEN_CURLY_BRACKET) {
646 next = tokenizeInterpolatedExpression(next, start); 700 next = tokenizeInterpolatedExpression(next, start);
647 } else { 701 } else {
648 next = tokenizeInterpolatedIdentifier(next, start); 702 next = tokenizeInterpolatedIdentifier(next, start);
649 } 703 }
650 start = byteOffset; 704 start = byteOffset;
651 continue; 705 continue;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 } 763 }
710 return next; 764 return next;
711 } 765 }
712 } 766 }
713 767
714 class MalformedInputException { 768 class MalformedInputException {
715 final message; 769 final message;
716 MalformedInputException(this.message); 770 MalformedInputException(this.message);
717 toString() => message.toString(); 771 toString() => message.toString();
718 } 772 }
OLDNEW
« no previous file with comments | « no previous file | frog/leg/scanner/string_scanner.dart » ('j') | frog/leg/ssa/nodes.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698