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

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

Issue 9271037: Inserted string validation as separate task in compiler. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments. Created 8 years, 10 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
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("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 // A two-byte hex escape can't generate an invalid value.
109 continue;
110 } else if (code === $u) {
111 int escapeStart = index - 1;
112 index++;
113 code = iter.next();
114 int value = 0;
115 if (code == $OPEN_CURLY_BRACKET) {
116 // expect 1-6 hex digits.
117 int count = 0;
118 index++;
119 code = iter.next();
120 do {
121 if (!isHexDigit(code)) {
122 stringParseError("Invalid character in escape sequence",
123 token, index);
124 return null;
125 }
126 count++;
127 value = value * 16 + hexDigitValue(code);
128 index++;
129 code = iter.next();
130 } while (code != $CLOSE_CURLY_BRACKET);
131 if (count > 6) {
132 stringParseError("Invalid character in escape sequence",
133 token, index - (count - 6));
134 return null;
135 }
136 } else {
137 // Expect four hex digits, including the one just read.
138 for (int i = 0; i < 4; i++) {
139 if (i > 0) {
140 index++;
141 code = iter.next();
142 }
143 if (!isHexDigit(code)) {
144 stringParseError("Invalid character in escape sequence",
145 token, index);
146 return null;
147 }
148 value = value * 16 + hexDigitValue(code);
149 }
150 }
151 code = value;
152 }
153 }
154 // This handles both unescaped characters and the value of unicode
155 // escapes.
156 if (!isUnicodeScalarValue(code)) {
157 stringParseError(
158 "Invalid Unicode scalar value U+${code.toRadixString(16)}",
159 token, index);
160 return null;
161 }
162 }
163 // String literal successfully validated.
164 return new QuotedString(string, quoting, length);
165 }
166 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698