| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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 } |
| OLD | NEW |