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

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: Use more intuitive test. 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 quoteLength = 1;
Johnni Winther 2014/06/18 08:43:44 Rename to [leftQuoteLength]. Also in StringQuoting
floitsch 2014/06/18 12:14:22 Done.
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 quoteLength = 3;
56 // Check if a multiline string starts with a newline (CR, LF or CR+LF). 56 // Due to string-interpolations we are not guaranteed to see the trailing
57 if (source.moveNext()) { 57 // quoting characters. The invocations to `moveNext()` may therefore
58 // return false and the `current`-getter return `null`. The code does
59 // not need to handle this specially (as it will not find the newline
60 // characters).
61
62 // Check if a multiline string starts with optional whitespace followed by
63 // a newline (CR, LF or CR+LF).
64 // We also accept if the these characters are escaped by a backslash.
65 int newLineLength = 1;
66 bool foundWhitespace;
67 do {
68 foundWhitespace = false;
Lasse Reichstein Nielsen 2014/06/18 09:05:32 This "break variable" is more confusing than a "wh
floitsch 2014/06/18 12:14:22 changed to while(true). Initially switched away fr
69 source.moveNext();
Lasse Reichstein Nielsen 2014/06/18 09:05:32 Why is the return value of moveNext not checked? I
floitsch 2014/06/18 12:14:22 Moved comment from line 56 to here.
58 code = source.current; 70 code = source.current;
59 if (code == $CR) { 71 if (code == $BACKSLASH) {
60 quoteLength += 1; 72 newLineLength++;
73 source.moveNext();
74 code = source.current;
75 }
76 if (code == $TAB || code == $SPACE) {
77 newLineLength++;
78 foundWhitespace = true;
79 } else if (code == $CR) {
Lasse Reichstein Nielsen 2014/06/18 09:05:32 The spec says "If the first line of a multiline ..
61 if (source.moveNext() && source.current == $LF) { 80 if (source.moveNext() && source.current == $LF) {
Lasse Reichstein Nielsen 2014/06/18 09:05:32 Does the spec require \\\r\\\n to work as a newlin
62 quoteLength += 1; 81 newLineLength++;
63 } 82 }
83 quoteLength += newLineLength;
64 } else if (code == $LF) { 84 } else if (code == $LF) {
65 quoteLength += 1; 85 quoteLength += newLineLength;
66 } 86 }
67 } 87 } while (foundWhitespace);
68 } 88 }
69 return StringQuoting.getQuoting(quoteChar, raw, quoteLength); 89 return StringQuoting.getQuoting(quoteChar, raw, quoteLength);
Johnni Winther 2014/06/18 08:43:44 StringQuoting.getQuoting expects [quoteLength] to
floitsch 2014/06/18 12:14:23 It shouldn't anymore. I changed it to be accept an
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