| 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 'dart:collection'; | 9 import 'dart:collection'; |
| 10 | 10 |
| 11 import 'common.dart'; | 11 import 'common.dart'; |
| 12 import 'tokens/token.dart' show Token; | 12 import 'package:dart_scanner/dart_scanner.dart' show Token; |
| 13 import 'tree/dartstring.dart' show DartString; | 13 import 'tree/dartstring.dart' show DartString; |
| 14 import 'tree/nodes.dart' show StringQuoting; | 14 import 'tree/nodes.dart' show StringQuoting; |
| 15 import 'util/characters.dart'; | 15 import 'package:dart_scanner/src/characters.dart'; |
| 16 | 16 |
| 17 class StringValidator { | 17 class StringValidator { |
| 18 final DiagnosticReporter reporter; | 18 final DiagnosticReporter reporter; |
| 19 | 19 |
| 20 StringValidator(this.reporter); | 20 StringValidator(this.reporter); |
| 21 | 21 |
| 22 DartString validateInterpolationPart(Token token, StringQuoting quoting, | 22 DartString validateInterpolationPart(Token token, StringQuoting quoting, |
| 23 {bool isFirst: false, bool isLast: false}) { | 23 {bool isFirst: false, bool isLast: false}) { |
| 24 String source = token.value; | 24 String source = token.value; |
| 25 int leftQuote = 0; | 25 int leftQuote = 0; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 */ | 93 */ |
| 94 String copyWithoutQuotes(String string, int initial, int terminal) { | 94 String copyWithoutQuotes(String string, int initial, int terminal) { |
| 95 assert(0 <= initial); | 95 assert(0 <= initial); |
| 96 assert(0 <= terminal); | 96 assert(0 <= terminal); |
| 97 assert(initial + terminal <= string.length); | 97 assert(initial + terminal <= string.length); |
| 98 return string.substring(initial, string.length - terminal); | 98 return string.substring(initial, string.length - terminal); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void stringParseError(String message, Token token, int offset) { | 101 void stringParseError(String message, Token token, int offset) { |
| 102 reporter.reportErrorMessage( | 102 reporter.reportErrorMessage( |
| 103 token, MessageKind.GENERIC, {'text': "$message @ $offset"}); | 103 reporter.spanFromToken(token), MessageKind.GENERIC, |
| 104 {'text': "$message @ $offset"}); |
| 104 } | 105 } |
| 105 | 106 |
| 106 /** | 107 /** |
| 107 * Validates the escape sequences and special characters of a string literal. | 108 * Validates the escape sequences and special characters of a string literal. |
| 108 * Returns a DartString if valid, and null if not. | 109 * Returns a DartString if valid, and null if not. |
| 109 */ | 110 */ |
| 110 DartString validateString( | 111 DartString validateString( |
| 111 Token token, int startOffset, String string, StringQuoting quoting) { | 112 Token token, int startOffset, String string, StringQuoting quoting) { |
| 112 // We need to check for invalid x and u escapes, for line | 113 // We need to check for invalid x and u escapes, for line |
| 113 // terminators in non-multiline strings, and for invalid Unicode | 114 // terminators in non-multiline strings, and for invalid Unicode |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 } | 206 } |
| 206 } | 207 } |
| 207 // String literal successfully validated. | 208 // String literal successfully validated. |
| 208 if (quoting.raw || !containsEscape) { | 209 if (quoting.raw || !containsEscape) { |
| 209 // A string without escapes could just as well have been raw. | 210 // A string without escapes could just as well have been raw. |
| 210 return new DartString.rawString(string, length); | 211 return new DartString.rawString(string, length); |
| 211 } | 212 } |
| 212 return new DartString.escapedString(string, length); | 213 return new DartString.escapedString(string, length); |
| 213 } | 214 } |
| 214 } | 215 } |
| OLD | NEW |