| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 library fasta.quote; | 5 library fasta.quote; |
| 6 | 6 |
| 7 import 'errors.dart' show inputError, internalError; | 7 import 'deprecated_problems.dart' |
| 8 show deprecated_inputError, deprecated_internalProblem; |
| 8 | 9 |
| 9 import 'scanner/characters.dart' | 10 import 'scanner/characters.dart' |
| 10 show | 11 show |
| 11 $BACKSLASH, | 12 $BACKSLASH, |
| 12 $BS, | 13 $BS, |
| 13 $CLOSE_CURLY_BRACKET, | 14 $CLOSE_CURLY_BRACKET, |
| 14 $CR, | 15 $CR, |
| 15 $FF, | 16 $FF, |
| 16 $LF, | 17 $LF, |
| 17 $OPEN_CURLY_BRACKET, | 18 $OPEN_CURLY_BRACKET, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 42 | 43 |
| 43 Quote analyzeQuote(String first) { | 44 Quote analyzeQuote(String first) { |
| 44 if (first.startsWith('"""')) return Quote.MultiLineDouble; | 45 if (first.startsWith('"""')) return Quote.MultiLineDouble; |
| 45 if (first.startsWith('r"""')) return Quote.RawMultiLineDouble; | 46 if (first.startsWith('r"""')) return Quote.RawMultiLineDouble; |
| 46 if (first.startsWith("'''")) return Quote.MultiLineSingle; | 47 if (first.startsWith("'''")) return Quote.MultiLineSingle; |
| 47 if (first.startsWith("r'''")) return Quote.RawMultiLineSingle; | 48 if (first.startsWith("r'''")) return Quote.RawMultiLineSingle; |
| 48 if (first.startsWith('"')) return Quote.Double; | 49 if (first.startsWith('"')) return Quote.Double; |
| 49 if (first.startsWith('r"')) return Quote.RawDouble; | 50 if (first.startsWith('r"')) return Quote.RawDouble; |
| 50 if (first.startsWith("'")) return Quote.Single; | 51 if (first.startsWith("'")) return Quote.Single; |
| 51 if (first.startsWith("r'")) return Quote.RawSingle; | 52 if (first.startsWith("r'")) return Quote.RawSingle; |
| 52 return internalError("Unexpected string literal: $first"); | 53 return deprecated_internalProblem("Unexpected string literal: $first"); |
| 53 } | 54 } |
| 54 | 55 |
| 55 // Note: based on [StringValidator.quotingFromString] | 56 // Note: based on [StringValidator.quotingFromString] |
| 56 // (pkg/compiler/lib/src/string_validator.dart). | 57 // (pkg/compiler/lib/src/string_validator.dart). |
| 57 int lengthOfOptionalWhitespacePrefix(String first, int start) { | 58 int lengthOfOptionalWhitespacePrefix(String first, int start) { |
| 58 List<int> codeUnits = first.codeUnits; | 59 List<int> codeUnits = first.codeUnits; |
| 59 for (int i = start; i < codeUnits.length; i++) { | 60 for (int i = start; i < codeUnits.length; i++) { |
| 60 int code = codeUnits[i]; | 61 int code = codeUnits[i]; |
| 61 if (code == $BACKSLASH) { | 62 if (code == $BACKSLASH) { |
| 62 i++; | 63 i++; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 92 return lengthOfOptionalWhitespacePrefix(first, 3); | 93 return lengthOfOptionalWhitespacePrefix(first, 3); |
| 93 | 94 |
| 94 case Quote.RawSingle: | 95 case Quote.RawSingle: |
| 95 case Quote.RawDouble: | 96 case Quote.RawDouble: |
| 96 return 2; | 97 return 2; |
| 97 | 98 |
| 98 case Quote.RawMultiLineSingle: | 99 case Quote.RawMultiLineSingle: |
| 99 case Quote.RawMultiLineDouble: | 100 case Quote.RawMultiLineDouble: |
| 100 return lengthOfOptionalWhitespacePrefix(first, 4); | 101 return lengthOfOptionalWhitespacePrefix(first, 4); |
| 101 } | 102 } |
| 102 return internalError("Unhandled string quote: $quote"); | 103 return deprecated_internalProblem("Unhandled string quote: $quote"); |
| 103 } | 104 } |
| 104 | 105 |
| 105 int lastQuoteLength(Quote quote) { | 106 int lastQuoteLength(Quote quote) { |
| 106 switch (quote) { | 107 switch (quote) { |
| 107 case Quote.Single: | 108 case Quote.Single: |
| 108 case Quote.Double: | 109 case Quote.Double: |
| 109 case Quote.RawSingle: | 110 case Quote.RawSingle: |
| 110 case Quote.RawDouble: | 111 case Quote.RawDouble: |
| 111 return 1; | 112 return 1; |
| 112 | 113 |
| 113 case Quote.MultiLineSingle: | 114 case Quote.MultiLineSingle: |
| 114 case Quote.MultiLineDouble: | 115 case Quote.MultiLineDouble: |
| 115 case Quote.RawMultiLineSingle: | 116 case Quote.RawMultiLineSingle: |
| 116 case Quote.RawMultiLineDouble: | 117 case Quote.RawMultiLineDouble: |
| 117 return 3; | 118 return 3; |
| 118 } | 119 } |
| 119 return internalError("Unhandled string quote: $quote"); | 120 return deprecated_internalProblem("Unhandled string quote: $quote"); |
| 120 } | 121 } |
| 121 | 122 |
| 122 String unescapeFirstStringPart(String first, Quote quote) { | 123 String unescapeFirstStringPart(String first, Quote quote) { |
| 123 return unescape(first.substring(firstQuoteLength(first, quote)), quote); | 124 return unescape(first.substring(firstQuoteLength(first, quote)), quote); |
| 124 } | 125 } |
| 125 | 126 |
| 126 String unescapeLastStringPart(String last, Quote quote) { | 127 String unescapeLastStringPart(String last, Quote quote) { |
| 127 return unescape( | 128 return unescape( |
| 128 last.substring(0, last.length - lastQuoteLength(quote)), quote); | 129 last.substring(0, last.length - lastQuoteLength(quote)), quote); |
| 129 } | 130 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 153 case Quote.RawSingle: | 154 case Quote.RawSingle: |
| 154 case Quote.RawDouble: | 155 case Quote.RawDouble: |
| 155 return string; | 156 return string; |
| 156 | 157 |
| 157 case Quote.RawMultiLineSingle: | 158 case Quote.RawMultiLineSingle: |
| 158 case Quote.RawMultiLineDouble: | 159 case Quote.RawMultiLineDouble: |
| 159 return !string.contains("\r") | 160 return !string.contains("\r") |
| 160 ? string | 161 ? string |
| 161 : unescapeCodeUnits(string.codeUnits, true); | 162 : unescapeCodeUnits(string.codeUnits, true); |
| 162 } | 163 } |
| 163 return internalError("Internal error: Unexpected quote: $quote."); | 164 return deprecated_internalProblem( |
| 165 "Internal error: Unexpected quote: $quote."); |
| 164 } | 166 } |
| 165 | 167 |
| 166 const String incompleteSequence = "Incomplete escape sequence."; | 168 const String incompleteSequence = "Incomplete escape sequence."; |
| 167 | 169 |
| 168 const String invalidCharacter = "Invalid character in escape sequence."; | 170 const String invalidCharacter = "Invalid character in escape sequence."; |
| 169 | 171 |
| 170 const String invalidCodePoint = "Invalid code point."; | 172 const String invalidCodePoint = "Invalid code point."; |
| 171 | 173 |
| 172 // Note: based on | 174 // Note: based on |
| 173 // [StringValidator.validateString](pkg/compiler/lib/src/string_validator.dart). | 175 // [StringValidator.validateString](pkg/compiler/lib/src/string_validator.dart). |
| 174 String unescapeCodeUnits(List<int> codeUnits, bool isRaw) { | 176 String unescapeCodeUnits(List<int> codeUnits, bool isRaw) { |
| 175 // Can't use Uint8List or Uint16List here, the code units may be larger. | 177 // Can't use Uint8List or Uint16List here, the code units may be larger. |
| 176 List<int> result = new List<int>(codeUnits.length); | 178 List<int> result = new List<int>(codeUnits.length); |
| 177 int resultOffset = 0; | 179 int resultOffset = 0; |
| 178 error(int offset, String message) { | 180 error(int offset, String message) { |
| 179 inputError(null, null, message); | 181 deprecated_inputError(null, null, message); |
| 180 } | 182 } |
| 181 | 183 |
| 182 for (int i = 0; i < codeUnits.length; i++) { | 184 for (int i = 0; i < codeUnits.length; i++) { |
| 183 int code = codeUnits[i]; | 185 int code = codeUnits[i]; |
| 184 if (code == $CR) { | 186 if (code == $CR) { |
| 185 if (i + 1 < codeUnits.length && codeUnits[i + 1] == $LF) { | 187 if (i + 1 < codeUnits.length && codeUnits[i + 1] == $LF) { |
| 186 i++; | 188 i++; |
| 187 } | 189 } |
| 188 code = $LF; | 190 code = $LF; |
| 189 } else if (!isRaw && code == $BACKSLASH) { | 191 } else if (!isRaw && code == $BACKSLASH) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 } | 247 } |
| 246 } else { | 248 } else { |
| 247 // Nothing, escaped character is passed through; | 249 // Nothing, escaped character is passed through; |
| 248 } | 250 } |
| 249 if (code > 0x10FFFF) return error(i, invalidCodePoint); | 251 if (code > 0x10FFFF) return error(i, invalidCodePoint); |
| 250 } | 252 } |
| 251 result[resultOffset++] = code; | 253 result[resultOffset++] = code; |
| 252 } | 254 } |
| 253 return new String.fromCharCodes(result, 0, resultOffset); | 255 return new String.fromCharCodes(result, 0, resultOffset); |
| 254 } | 256 } |
| OLD | NEW |