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 "dart2jslib.dart"; | 9 import "dart2jslib.dart"; |
10 import "tree/tree.dart"; | 10 import "tree/tree.dart"; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
51 int quoteChar = source.next(); | 51 int quoteChar = source.next(); |
52 if (quoteChar === $r) { | 52 if (quoteChar === $r) { |
53 raw = true; | 53 raw = true; |
54 quoteChar = source.next(); | 54 quoteChar = source.next(); |
55 } | 55 } |
56 assert(identical(quoteChar, $SQ) || identical(quoteChar, $DQ)); | 56 assert(identical(quoteChar, $SQ) || identical(quoteChar, $DQ)); |
57 // String has at least one quote. Check it if has three. | 57 // String has at least one quote. Check it if has three. |
58 // 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, |
59 // and end after the second quote. | 59 // and end after the second quote. |
60 bool multiline = false; | 60 bool multiline = false; |
61 if (source.hasNext() && identical(source.next(), quoteChar) && source.hasNex t()) { | 61 if (source.hasNext && identical(source.next(), quoteChar) && source.hasNext) { |
Mads Ager (google)
2012/10/22 08:12:06
Not your code, but could you reformat to keep with
floitsch
2012/10/24 13:02:47
https://codereview.chromium.org/11233035/
| |
62 int code = source.next(); | 62 int code = source.next(); |
63 assert(identical(code, quoteChar)); // If not, there is a bug in the pars er. | 63 assert(identical(code, quoteChar)); // If not, there is a bug in the pars er. |
64 quoteLength = 3; | 64 quoteLength = 3; |
65 // Check if a multiline string starts with a newline (CR, LF or CR+LF). | 65 // Check if a multiline string starts with a newline (CR, LF or CR+LF). |
66 if (source.hasNext()) { | 66 if (source.hasNext) { |
67 code = source.next(); | 67 code = source.next(); |
68 if (identical(code, $CR)) { | 68 if (identical(code, $CR)) { |
69 quoteLength += 1; | 69 quoteLength += 1; |
70 if (source.hasNext() && identical(source.next(), $LF)) { | 70 if (source.hasNext && identical(source.next(), $LF)) { |
71 quoteLength += 1; | 71 quoteLength += 1; |
72 } | 72 } |
73 } else if (identical(code, $LF)) { | 73 } else if (identical(code, $LF)) { |
74 quoteLength += 1; | 74 quoteLength += 1; |
75 } | 75 } |
76 } | 76 } |
77 } | 77 } |
78 return StringQuoting.getQuoting(quoteChar, raw, quoteLength); | 78 return StringQuoting.getQuoting(quoteChar, raw, quoteLength); |
79 } | 79 } |
80 | 80 |
81 void stringParseError(String message, Token token, int offset) { | 81 void stringParseError(String message, Token token, int offset) { |
82 listener.cancel("$message @ $offset", token : token); | 82 listener.cancel("$message @ $offset", token : token); |
83 } | 83 } |
84 | 84 |
85 /** | 85 /** |
86 * Validates the escape sequences and special characters of a string literal. | 86 * Validates the escape sequences and special characters of a string literal. |
87 * Returns a DartString if valid, and null if not. | 87 * Returns a DartString if valid, and null if not. |
88 */ | 88 */ |
89 DartString validateString(Token token, | 89 DartString validateString(Token token, |
90 int startOffset, | 90 int startOffset, |
91 SourceString string, | 91 SourceString string, |
92 StringQuoting quoting) { | 92 StringQuoting quoting) { |
93 // We only need to check for invalid x and u escapes, for line | 93 // We only need to check for invalid x and u escapes, for line |
94 // terminators in non-multiline strings, and for invalid Unicode | 94 // terminators in non-multiline strings, and for invalid Unicode |
95 // scalar values (either directly or as u-escape values). | 95 // scalar values (either directly or as u-escape values). |
96 int length = 0; | 96 int length = 0; |
97 int index = startOffset; | 97 int index = startOffset; |
98 bool containsEscape = false; | 98 bool containsEscape = false; |
99 for(Iterator<int> iter = string.iterator(); iter.hasNext(); length++) { | 99 for(Iterator<int> iter = string.iterator(); iter.hasNext; length++) { |
100 index++; | 100 index++; |
101 int code = iter.next(); | 101 int code = iter.next(); |
102 if (identical(code, $BACKSLASH)) { | 102 if (identical(code, $BACKSLASH)) { |
103 if (quoting.raw) continue; | 103 if (quoting.raw) continue; |
104 containsEscape = true; | 104 containsEscape = true; |
105 if (!iter.hasNext()) { | 105 if (!iter.hasNext) { |
106 stringParseError("Incomplete escape sequence",token, index); | 106 stringParseError("Incomplete escape sequence",token, index); |
107 return null; | 107 return null; |
108 } | 108 } |
109 index++; | 109 index++; |
110 code = iter.next(); | 110 code = iter.next(); |
111 if (identical(code, $x)) { | 111 if (identical(code, $x)) { |
112 for (int i = 0; i < 2; i++) { | 112 for (int i = 0; i < 2; i++) { |
113 if (!iter.hasNext()) { | 113 if (!iter.hasNext) { |
114 stringParseError("Incomplete escape sequence", token, index); | 114 stringParseError("Incomplete escape sequence", token, index); |
115 return null; | 115 return null; |
116 } | 116 } |
117 index++; | 117 index++; |
118 code = iter.next(); | 118 code = iter.next(); |
119 if (!isHexDigit(code)) { | 119 if (!isHexDigit(code)) { |
120 stringParseError("Invalid character in escape sequence", | 120 stringParseError("Invalid character in escape sequence", |
121 token, index); | 121 token, index); |
122 return null; | 122 return null; |
123 } | 123 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 } | 178 } |
179 } | 179 } |
180 // String literal successfully validated. | 180 // String literal successfully validated. |
181 if (quoting.raw || !containsEscape) { | 181 if (quoting.raw || !containsEscape) { |
182 // A string without escapes could just as well have been raw. | 182 // A string without escapes could just as well have been raw. |
183 return new DartString.rawString(string, length); | 183 return new DartString.rawString(string, length); |
184 } | 184 } |
185 return new DartString.escapedString(string, length); | 185 return new DartString.escapedString(string, length); |
186 } | 186 } |
187 } | 187 } |
OLD | NEW |