| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Checks a tree structure for whether the string literals are valid. |
| 6 |
| 7 #library("stringvalidator"); |
| 8 |
| 9 #import("scanner/scannerlib.dart"); |
| 10 #import("tree/tree.dart"); |
| 11 #import("elements/elements.dart"); |
| 12 #import("util/characters.dart"); |
| 13 |
| 14 class StringValidator { |
| 15 final Canceler canceler; |
| 16 |
| 17 StringValidator(this.canceler); |
| 18 |
| 19 QuotedString validateQuotedString(Token token) { |
| 20 SourceString source = token.value; |
| 21 StringQuoting quoting = quotingFromString(source); |
| 22 int leftQuote = quoting.leftQuoteLength; |
| 23 int rightQuote = quoting.rightQuoteLength; |
| 24 SourceString content = source.copyWithoutQuotes(leftQuote, rightQuote); |
| 25 return validateString(token, |
| 26 token.charOffset + leftQuote, |
| 27 content, |
| 28 quoting); |
| 29 } |
| 30 |
| 31 QuotedString validateInterpolationPart(Token token, StringQuoting quoting, |
| 32 [bool isFirst = false, |
| 33 bool isLast = false]) { |
| 34 SourceString source = token.value; |
| 35 int leftQuote = 0; |
| 36 int rightQuote = 0; |
| 37 if (isFirst) leftQuote = quoting.leftQuoteLength; |
| 38 if (isLast) rightQuote = quoting.rightQuoteLength; |
| 39 SourceString content = source.copyWithoutQuotes(leftQuote, rightQuote); |
| 40 return validateString(token, |
| 41 token.charOffset + leftQuote, |
| 42 content, |
| 43 quoting); |
| 44 } |
| 45 |
| 46 static StringQuoting quotingFromString(SourceString sourceString) { |
| 47 Iterator<int> source = sourceString.iterator(); |
| 48 bool raw = false; |
| 49 int quoteChar = source.next(); |
| 50 if (quoteChar == $AT) { |
| 51 raw = true; |
| 52 quoteChar = source.next(); |
| 53 } |
| 54 assert(quoteChar === $SQ || quoteChar === $DQ); |
| 55 // String has at least one quote. Check it if has three. |
| 56 // If it only have two, the string must be an empty string literal, |
| 57 // and end after the second quote. |
| 58 bool multiline = false; |
| 59 if (source.hasNext() && source.next() == quoteChar && source.hasNext()) { |
| 60 assert(source.next() == quoteChar); |
| 61 multiline = true; |
| 62 } |
| 63 return StringQuoting.get(quoteChar, raw, multiline); |
| 64 } |
| 65 |
| 66 void stringParseError(String message, Token token, int offset) { |
| 67 canceler.cancel("$message @ $offset", token : token); |
| 68 } |
| 69 |
| 70 /** |
| 71 * Validates the escape sequences and special characters of a string literal. |
| 72 * Returns a QuotedString if valid, and null if not. |
| 73 */ |
| 74 QuotedString validateString(Token token, |
| 75 int startOffset, |
| 76 SourceString string, |
| 77 StringQuoting quoting) { |
| 78 // We only need to check for invalid x and u escapes, for line |
| 79 // terminators in non-multiline strings, and for invalid Unicode |
| 80 // scalar values (either directly or as u-escape values). |
| 81 int length = 0; |
| 82 int index = startOffset; |
| 83 for(Iterator<int> iter = string.iterator(); iter.hasNext(); length++) { |
| 84 index++; |
| 85 int code = iter.next(); |
| 86 if (code === $BACKSLASH) { |
| 87 if (quoting.raw) continue; |
| 88 if (!iter.hasNext()) { |
| 89 stringParseError("Incomplete escape sequence",token, index); |
| 90 return null; |
| 91 } |
| 92 index++; |
| 93 code = iter.next(); |
| 94 if (code === $x) { |
| 95 for (int i = 0; i < 2; i++) { |
| 96 if (!iter.hasNext()) { |
| 97 stringParseError("Incomplete escape sequence", token, index); |
| 98 return null; |
| 99 } |
| 100 index++; |
| 101 code = iter.next(); |
| 102 if (!isHexDigit(code)) { |
| 103 stringParseError("Invalid character in escape sequence", |
| 104 token, index); |
| 105 return null; |
| 106 } |
| 107 } |
| 108 continue; |
| 109 } else if (code === $u) { |
| 110 int escapeStart = index - 1; |
| 111 index++; |
| 112 code = iter.next(); |
| 113 int value = 0; |
| 114 if (code == $OPEN_CURLY_BRACKET) { |
| 115 // expect 1-7 hex digits. |
| 116 int count = 0; |
| 117 index++; |
| 118 code = iter.next(); |
| 119 do { |
| 120 if (!isHexDigit(code)) { |
| 121 stringParseError("Invalid character in escape sequence", |
| 122 token, index); |
| 123 return null; |
| 124 } |
| 125 count++; |
| 126 value = value * 16 + hexDigitValue(code); |
| 127 index++; |
| 128 code = iter.next(); |
| 129 } while (code != $CLOSE_CURLY_BRACKET); |
| 130 if (count > 7) { |
| 131 stringParseError("Invalid character in escape sequence", |
| 132 token, index - (count - 7)); |
| 133 return null; |
| 134 } |
| 135 } else { |
| 136 // Expect four hex digits, including the one just tread. |
| 137 for (int i = 0; i < 4; i++) { |
| 138 if (i > 0) { |
| 139 index++; |
| 140 code = iter.next(); |
| 141 } |
| 142 if (!isHexDigit(code)) { |
| 143 stringParseError("Invalid character in escape sequence", |
| 144 token, index); |
| 145 return null; |
| 146 } |
| 147 value = value * 16 + hexDigitValue(code); |
| 148 } |
| 149 } |
| 150 if (0xd800 <= value && ( value <= 0xdfff || value > 0x10ffff)) { |
| 151 stringParseError( |
| 152 "Invalid unicode scalar value U+${value.toRadixString(16)}", |
| 153 token, index); |
| 154 return null; |
| 155 } |
| 156 continue; |
| 157 } |
| 158 } |
| 159 // This handles borth unescaped characters as well as those |
| 160 // characters after a backslash that doesn't have a special |
| 161 // meaning. |
| 162 if (code >= 0xd800 && (code <= 0xdfff || code > 0x10ffff)) { |
| 163 stringParseError( |
| 164 "Invalid unicode scalar value U+${code.toRadixString(16)}", |
| 165 token, index); |
| 166 return null; |
| 167 } |
| 168 if (!quoting.multiline && (code === $LF || code === $CR)) { |
| 169 stringParseError("Line terminator in single-line string", |
| 170 token, index); |
| 171 return null; |
| 172 } |
| 173 } |
| 174 // String literal successfully validated. |
| 175 return new QuotedString(string, quoting, length); |
| 176 } |
| 177 } |
| OLD | NEW |