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

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

Issue 9301037: Made multiline string literals ignore an initial newline. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix test that didn't expect initial newlines to disappear. Created 8 years, 10 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/token.dart ('k') | frog/leg/tree/nodes.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) 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("leg.dart"); 9 #import("leg.dart");
10 #import("scanner/scannerlib.dart"); 10 #import("scanner/scannerlib.dart");
(...skipping 29 matching lines...) Expand all
40 SourceString content = source.copyWithoutQuotes(leftQuote, rightQuote); 40 SourceString content = source.copyWithoutQuotes(leftQuote, rightQuote);
41 return validateString(token, 41 return validateString(token,
42 token.charOffset + leftQuote, 42 token.charOffset + leftQuote,
43 content, 43 content,
44 quoting); 44 quoting);
45 } 45 }
46 46
47 static StringQuoting quotingFromString(SourceString sourceString) { 47 static StringQuoting quotingFromString(SourceString sourceString) {
48 Iterator<int> source = sourceString.iterator(); 48 Iterator<int> source = sourceString.iterator();
49 bool raw = false; 49 bool raw = false;
50 int quoteLength = 1;
50 int quoteChar = source.next(); 51 int quoteChar = source.next();
51 if (quoteChar == $AT) { 52 if (quoteChar == $AT) {
52 raw = true; 53 raw = true;
53 quoteChar = source.next(); 54 quoteChar = source.next();
54 } 55 }
55 assert(quoteChar === $SQ || quoteChar === $DQ); 56 assert(quoteChar === $SQ || quoteChar === $DQ);
56 // String has at least one quote. Check it if has three. 57 // String has at least one quote. Check it if has three.
57 // If it only have two, the string must be an empty string literal, 58 // If it only have two, the string must be an empty string literal,
58 // and end after the second quote. 59 // and end after the second quote.
59 bool multiline = false; 60 bool multiline = false;
60 if (source.hasNext() && source.next() == quoteChar && source.hasNext()) { 61 if (source.hasNext() && source.next() === quoteChar && source.hasNext()) {
61 assert(source.next() == quoteChar); 62 int code = source.next();
62 multiline = true; 63 assert(code === quoteChar); // If not, there is a bug in the parser.
64 quoteLength = 3;
65 // Check if a multiline string starts with a newline (CR, LF or CR+LF).
66 if (source.hasNext()) {
67 code = source.next();
68 if (code === $CR) {
69 quoteLength += 1;
70 if (source.hasNext() && source.next() === $LF) {
71 quoteLength += 1;
72 }
73 } else if (code === $LF) {
74 quoteLength += 1;
75 }
76 }
63 } 77 }
64 return StringQuoting.get(quoteChar, raw, multiline); 78 return StringQuoting.get(quoteChar, raw, quoteLength);
65 } 79 }
66 80
67 void stringParseError(String message, Token token, int offset) { 81 void stringParseError(String message, Token token, int offset) {
68 listener.cancel("$message @ $offset", token : token); 82 listener.cancel("$message @ $offset", token : token);
69 } 83 }
70 84
71 /** 85 /**
72 * Validates the escape sequences and special characters of a string literal. 86 * Validates the escape sequences and special characters of a string literal.
73 * Returns a DartString if valid, and null if not. 87 * Returns a DartString if valid, and null if not.
74 */ 88 */
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 } 178 }
165 } 179 }
166 // String literal successfully validated. 180 // String literal successfully validated.
167 if (quoting.raw || !containsEscape) { 181 if (quoting.raw || !containsEscape) {
168 // A string without escapes could just as well have been raw. 182 // A string without escapes could just as well have been raw.
169 return new DartString.rawString(string, length); 183 return new DartString.rawString(string, length);
170 } 184 }
171 return new DartString.escapedString(string, length); 185 return new DartString.escapedString(string, length);
172 } 186 }
173 } 187 }
OLDNEW
« no previous file with comments | « frog/leg/scanner/token.dart ('k') | frog/leg/tree/nodes.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698