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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/string_validator.dart

Issue 336413002: Allow whitespace and \ before the first newline of multiline string. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Forgot to save. Created 6 years, 6 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
OLDNEW
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 // Check the validity of string literals. 5 // Check the validity of string literals.
6 6
7 library stringvalidator; 7 library stringvalidator;
8 8
9 import "dart:collection"; 9 import "dart:collection";
10 10
(...skipping 18 matching lines...) Expand all
29 String content = copyWithoutQuotes(source, leftQuote, rightQuote); 29 String content = copyWithoutQuotes(source, leftQuote, rightQuote);
30 return validateString(token, 30 return validateString(token,
31 token.charOffset + leftQuote, 31 token.charOffset + leftQuote,
32 content, 32 content,
33 quoting); 33 quoting);
34 } 34 }
35 35
36 static StringQuoting quotingFromString(String sourceString) { 36 static StringQuoting quotingFromString(String sourceString) {
37 Iterator<int> source = sourceString.codeUnits.iterator; 37 Iterator<int> source = sourceString.codeUnits.iterator;
38 bool raw = false; 38 bool raw = false;
39 int quoteLength = 1; 39 int leftQuoteLength = 1;
40 source.moveNext(); 40 source.moveNext();
41 int quoteChar = source.current; 41 int quoteChar = source.current;
42 if (quoteChar == $r) { 42 if (quoteChar == $r) {
43 raw = true; 43 raw = true;
44 source.moveNext(); 44 source.moveNext();
45 quoteChar = source.current; 45 quoteChar = source.current;
46 } 46 }
47 assert(quoteChar == $SQ || quoteChar == $DQ); 47 assert(quoteChar == $SQ || quoteChar == $DQ);
48 // String has at least one quote. Check it if has three. 48 // String has at least one quote. Check it if has three.
49 // If it only have two, the string must be an empty string literal, 49 // If it only has two, the string must be an empty string literal,
50 // and end after the second quote. 50 // and end after the second quote.
51 bool multiline = false; 51 bool multiline = false;
52 if (source.moveNext() && source.current == quoteChar && source.moveNext()) { 52 if (source.moveNext() && source.current == quoteChar && source.moveNext()) {
53 int code = source.current; 53 int code = source.current;
54 assert(code == quoteChar); // If not, there is a bug in the parser. 54 assert(code == quoteChar); // If not, there is a bug in the parser.
55 quoteLength = 3; 55 leftQuoteLength = 3;
56 // Check if a multiline string starts with a newline (CR, LF or CR+LF). 56
57 if (source.moveNext()) { 57 // Check if a multiline string starts with optional whitespace followed by
58 // a newline (CR, LF or CR+LF).
59 // We also accept if the these characters are escaped by a backslash.
60 int newLineLength = 1;
61 while (true) {
62 // Due to string-interpolations we are not guaranteed to see the
63 // trailing quoting characters. The invocations to `moveNext()` may
64 // therefore return false and the `current`-getter return `null`. The
65 // code does not need to handle this specially (as it will not find the
66 // newline characters).
67 source.moveNext();
58 code = source.current; 68 code = source.current;
69 if (code == $BACKSLASH) {
70 newLineLength++;
71 source.moveNext();
72 code = source.current;
73 }
74 if (code == $TAB || code == $SPACE) {
75 newLineLength++;
76 continue;
77 }
59 if (code == $CR) { 78 if (code == $CR) {
60 quoteLength += 1;
61 if (source.moveNext() && source.current == $LF) { 79 if (source.moveNext() && source.current == $LF) {
62 quoteLength += 1; 80 newLineLength++;
63 } 81 }
82 leftQuoteLength += newLineLength;
64 } else if (code == $LF) { 83 } else if (code == $LF) {
65 quoteLength += 1; 84 leftQuoteLength += newLineLength;
66 } 85 }
86 break;
67 } 87 }
68 } 88 }
69 return StringQuoting.getQuoting(quoteChar, raw, quoteLength); 89 return StringQuoting.getQuoting(quoteChar, raw, leftQuoteLength);
70 } 90 }
71 91
72 /** 92 /**
73 * Return the string [string] witout its [initial] first and [terminal] last 93 * Return the string [string] witout its [initial] first and [terminal] last
74 * characters. This is intended to be used to remove quotes from string 94 * characters. This is intended to be used to remove quotes from string
75 * literals (including an initial 'r' for raw strings). 95 * literals (including an initial 'r' for raw strings).
76 */ 96 */
77 String copyWithoutQuotes(String string, int initial, int terminal) { 97 String copyWithoutQuotes(String string, int initial, int terminal) {
78 assert(0 <= initial); 98 assert(0 <= initial);
79 assert(0 <= terminal); 99 assert(0 <= terminal);
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 return null; 226 return null;
207 } 227 }
208 // String literal successfully validated. 228 // String literal successfully validated.
209 if (quoting.raw || !containsEscape) { 229 if (quoting.raw || !containsEscape) {
210 // A string without escapes could just as well have been raw. 230 // A string without escapes could just as well have been raw.
211 return new DartString.rawString(string, length); 231 return new DartString.rawString(string, length);
212 } 232 }
213 return new DartString.escapedString(string, length); 233 return new DartString.escapedString(string, length);
214 } 234 }
215 } 235 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698