Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(681)

Side by Side Diff: frog/leg/string_validator.dart

Issue 9873021: Move frog/leg to lib/compiler/implementation. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « frog/leg/ssa/value_set.dart ('k') | frog/leg/tools/find_file_to_parse.sh » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Check the validity of string literals.
6
7 #library("stringvalidator");
8
9 #import("leg.dart");
10 #import("scanner/scannerlib.dart");
11 #import("tree/tree.dart");
12 #import("elements/elements.dart");
13 #import("util/characters.dart");
14
15 class StringValidator {
16 final DiagnosticListener listener;
17
18 StringValidator(this.listener);
19
20 DartString validateQuotedString(Token token) {
21 SourceString source = token.value;
22 StringQuoting quoting = quotingFromString(source);
23 int leftQuote = quoting.leftQuoteLength;
24 int rightQuote = quoting.rightQuoteLength;
25 SourceString content = source.copyWithoutQuotes(leftQuote, rightQuote);
26 return validateString(token,
27 token.charOffset + leftQuote,
28 content,
29 quoting);
30 }
31
32 DartString validateInterpolationPart(Token token, StringQuoting quoting,
33 [bool isFirst = false,
34 bool isLast = false]) {
35 SourceString source = token.value;
36 int leftQuote = 0;
37 int rightQuote = 0;
38 if (isFirst) leftQuote = quoting.leftQuoteLength;
39 if (isLast) rightQuote = quoting.rightQuoteLength;
40 SourceString content = source.copyWithoutQuotes(leftQuote, rightQuote);
41 return validateString(token,
42 token.charOffset + leftQuote,
43 content,
44 quoting);
45 }
46
47 static StringQuoting quotingFromString(SourceString sourceString) {
48 Iterator<int> source = sourceString.iterator();
49 bool raw = false;
50 int quoteLength = 1;
51 int quoteChar = source.next();
52 if (quoteChar == $AT) {
53 raw = true;
54 quoteChar = source.next();
55 }
56 assert(quoteChar === $SQ || quoteChar === $DQ);
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,
59 // and end after the second quote.
60 bool multiline = false;
61 if (source.hasNext() && source.next() === quoteChar && source.hasNext()) {
62 int code = source.next();
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 }
77 }
78 return StringQuoting.getQuoting(quoteChar, raw, quoteLength);
79 }
80
81 void stringParseError(String message, Token token, int offset) {
82 listener.cancel("$message @ $offset", token : token);
83 }
84
85 /**
86 * Validates the escape sequences and special characters of a string literal.
87 * Returns a DartString if valid, and null if not.
88 */
89 DartString validateString(Token token,
90 int startOffset,
91 SourceString string,
92 StringQuoting quoting) {
93 // We only need to check for invalid x and u escapes, for line
94 // terminators in non-multiline strings, and for invalid Unicode
95 // scalar values (either directly or as u-escape values).
96 int length = 0;
97 int index = startOffset;
98 bool containsEscape = false;
99 for(Iterator<int> iter = string.iterator(); iter.hasNext(); length++) {
100 index++;
101 int code = iter.next();
102 if (code === $BACKSLASH) {
103 if (quoting.raw) continue;
104 containsEscape = true;
105 if (!iter.hasNext()) {
106 stringParseError("Incomplete escape sequence",token, index);
107 return null;
108 }
109 index++;
110 code = iter.next();
111 if (code === $x) {
112 for (int i = 0; i < 2; i++) {
113 if (!iter.hasNext()) {
114 stringParseError("Incomplete escape sequence", token, index);
115 return null;
116 }
117 index++;
118 code = iter.next();
119 if (!isHexDigit(code)) {
120 stringParseError("Invalid character in escape sequence",
121 token, index);
122 return null;
123 }
124 }
125 // A two-byte hex escape can't generate an invalid value.
126 continue;
127 } else if (code === $u) {
128 int escapeStart = index - 1;
129 index++;
130 code = iter.next();
131 int value = 0;
132 if (code == $OPEN_CURLY_BRACKET) {
133 // expect 1-6 hex digits.
134 int count = 0;
135 index++;
136 code = iter.next();
137 do {
138 if (!isHexDigit(code)) {
139 stringParseError("Invalid character in escape sequence",
140 token, index);
141 return null;
142 }
143 count++;
144 value = value * 16 + hexDigitValue(code);
145 index++;
146 code = iter.next();
147 } while (code != $CLOSE_CURLY_BRACKET);
148 if (count > 6) {
149 stringParseError("Invalid character in escape sequence",
150 token, index - (count - 6));
151 return null;
152 }
153 } else {
154 // Expect four hex digits, including the one just read.
155 for (int i = 0; i < 4; i++) {
156 if (i > 0) {
157 index++;
158 code = iter.next();
159 }
160 if (!isHexDigit(code)) {
161 stringParseError("Invalid character in escape sequence",
162 token, index);
163 return null;
164 }
165 value = value * 16 + hexDigitValue(code);
166 }
167 }
168 code = value;
169 }
170 }
171 // This handles both unescaped characters and the value of unicode
172 // escapes.
173 if (!isUnicodeScalarValue(code)) {
174 stringParseError(
175 "Invalid Unicode scalar value U+${code.toRadixString(16)}",
176 token, index);
177 return null;
178 }
179 }
180 // String literal successfully validated.
181 if (quoting.raw || !containsEscape) {
182 // A string without escapes could just as well have been raw.
183 return new DartString.rawString(string, length);
184 }
185 return new DartString.escapedString(string, length);
186 }
187 }
OLDNEW
« no previous file with comments | « frog/leg/ssa/value_set.dart ('k') | frog/leg/tools/find_file_to_parse.sh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698