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

Side by Side Diff: pkg/front_end/lib/src/fasta/scanner/abstract_scanner.dart

Issue 2912693002: improve fasta unterminated string recovery (Closed)
Patch Set: Created 3 years, 6 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
OLDNEW
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 library fasta.scanner.abstract_scanner; 5 library fasta.scanner.abstract_scanner;
6 6
7 import 'dart:collection' show ListMixin; 7 import 'dart:collection' show ListMixin;
8 8
9 import 'dart:typed_data' show Uint16List, Uint32List; 9 import 'dart:typed_data' show Uint16List, Uint32List;
10 10
11 import '../../scanner/token.dart' show BeginToken, Token, TokenType; 11 import '../../scanner/token.dart'
12 show BeginToken, SyntheticStringToken, Token, TokenType;
12 13
13 import '../scanner.dart' 14 import '../scanner.dart'
14 show ErrorToken, Keyword, Scanner, buildUnexpectedCharacterToken; 15 show ErrorToken, Keyword, Scanner, buildUnexpectedCharacterToken;
15 16
16 import 'error_token.dart' show UnterminatedToken; 17 import 'error_token.dart' show UnterminatedToken;
17 18
18 import 'keyword_state.dart' show KeywordState; 19 import 'keyword_state.dart' show KeywordState;
19 20
20 import 'token.dart' show CommentToken, DartDocToken; 21 import 'token.dart' show CommentToken, DartDocToken;
21 22
(...skipping 995 matching lines...) Expand 10 before | Expand all | Expand 10 after
1017 next = tokenizeStringInterpolation(start, asciiOnly); 1018 next = tokenizeStringInterpolation(start, asciiOnly);
1018 start = scanOffset; 1019 start = scanOffset;
1019 asciiOnly = true; 1020 asciiOnly = true;
1020 continue; 1021 continue;
1021 } 1022 }
1022 if (next <= $CR && 1023 if (next <= $CR &&
1023 (identical(next, $LF) || 1024 (identical(next, $LF) ||
1024 identical(next, $CR) || 1025 identical(next, $CR) ||
1025 identical(next, $EOF))) { 1026 identical(next, $EOF))) {
1026 if (!asciiOnly) handleUnicode(start); 1027 if (!asciiOnly) handleUnicode(start);
1027 return unterminatedString(quoteChar); 1028 return unterminatedString(quoteChar, start, asciiOnly);
1028 } 1029 }
1029 if (next > 127) asciiOnly = false; 1030 if (next > 127) asciiOnly = false;
1030 next = advance(); 1031 next = advance();
1031 } 1032 }
1032 if (!asciiOnly) handleUnicode(start); 1033 if (!asciiOnly) handleUnicode(start);
1033 // Advance past the quote character. 1034 // Advance past the quote character.
1034 next = advance(); 1035 next = advance();
1035 appendSubstringToken(TokenType.STRING, start, asciiOnly); 1036 appendSubstringToken(TokenType.STRING, start, asciiOnly);
1036 return next; 1037 return next;
1037 } 1038 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1082 int tokenizeSingleLineRawString(int next, int quoteChar, int start) { 1083 int tokenizeSingleLineRawString(int next, int quoteChar, int start) {
1083 bool asciiOnly = true; 1084 bool asciiOnly = true;
1084 while (next != $EOF) { 1085 while (next != $EOF) {
1085 if (identical(next, quoteChar)) { 1086 if (identical(next, quoteChar)) {
1086 if (!asciiOnly) handleUnicode(start); 1087 if (!asciiOnly) handleUnicode(start);
1087 next = advance(); 1088 next = advance();
1088 appendSubstringToken(TokenType.STRING, start, asciiOnly); 1089 appendSubstringToken(TokenType.STRING, start, asciiOnly);
1089 return next; 1090 return next;
1090 } else if (identical(next, $LF) || identical(next, $CR)) { 1091 } else if (identical(next, $LF) || identical(next, $CR)) {
1091 if (!asciiOnly) handleUnicode(start); 1092 if (!asciiOnly) handleUnicode(start);
1092 return unterminatedRawString(quoteChar); 1093 return unterminatedRawString(quoteChar, start, asciiOnly);
1093 } else if (next > 127) { 1094 } else if (next > 127) {
1094 asciiOnly = false; 1095 asciiOnly = false;
1095 } 1096 }
1096 next = advance(); 1097 next = advance();
1097 } 1098 }
1098 if (!asciiOnly) handleUnicode(start); 1099 if (!asciiOnly) handleUnicode(start);
1099 return unterminatedRawString(quoteChar); 1100 return unterminatedRawString(quoteChar, start, asciiOnly);
1100 } 1101 }
1101 1102
1102 int tokenizeMultiLineRawString(int quoteChar, int start) { 1103 int tokenizeMultiLineRawString(int quoteChar, int start) {
1103 bool asciiOnlyString = true; 1104 bool asciiOnlyString = true;
1104 bool asciiOnlyLine = true; 1105 bool asciiOnlyLine = true;
1105 int unicodeStart = start; 1106 int unicodeStart = start;
1106 int next = advance(); // Advance past the (last) quote (of three). 1107 int next = advance(); // Advance past the (last) quote (of three).
1107 outer: 1108 outer:
1108 while (!identical(next, $EOF)) { 1109 while (!identical(next, $EOF)) {
1109 while (!identical(next, quoteChar)) { 1110 while (!identical(next, quoteChar)) {
(...skipping 17 matching lines...) Expand all
1127 next = advance(); 1128 next = advance();
1128 if (identical(next, quoteChar)) { 1129 if (identical(next, quoteChar)) {
1129 if (!asciiOnlyLine) handleUnicode(unicodeStart); 1130 if (!asciiOnlyLine) handleUnicode(unicodeStart);
1130 next = advance(); 1131 next = advance();
1131 appendSubstringToken(TokenType.STRING, start, asciiOnlyString); 1132 appendSubstringToken(TokenType.STRING, start, asciiOnlyString);
1132 return next; 1133 return next;
1133 } 1134 }
1134 } 1135 }
1135 } 1136 }
1136 if (!asciiOnlyLine) handleUnicode(unicodeStart); 1137 if (!asciiOnlyLine) handleUnicode(unicodeStart);
1137 return unterminatedRawMultiLineString(quoteChar); 1138 return unterminatedRawMultiLineString(quoteChar, start, asciiOnlyLine);
1138 } 1139 }
1139 1140
1140 int tokenizeMultiLineString(int quoteChar, int start, bool raw) { 1141 int tokenizeMultiLineString(int quoteChar, int start, bool raw) {
1141 if (raw) return tokenizeMultiLineRawString(quoteChar, start); 1142 if (raw) return tokenizeMultiLineRawString(quoteChar, start);
1142 bool asciiOnlyString = true; 1143 bool asciiOnlyString = true;
1143 bool asciiOnlyLine = true; 1144 bool asciiOnlyLine = true;
1144 int unicodeStart = start; 1145 int unicodeStart = start;
1145 int next = advance(); // Advance past the (last) quote (of three). 1146 int next = advance(); // Advance past the (last) quote (of three).
1146 while (!identical(next, $EOF)) { 1147 while (!identical(next, $EOF)) {
1147 if (identical(next, $$)) { 1148 if (identical(next, $$)) {
(...skipping 30 matching lines...) Expand all
1178 unicodeStart = scanOffset; 1179 unicodeStart = scanOffset;
1179 } 1180 }
1180 lineFeedInMultiline(); 1181 lineFeedInMultiline();
1181 } else if (next > 127) { 1182 } else if (next > 127) {
1182 asciiOnlyString = false; 1183 asciiOnlyString = false;
1183 asciiOnlyLine = false; 1184 asciiOnlyLine = false;
1184 } 1185 }
1185 next = advance(); 1186 next = advance();
1186 } 1187 }
1187 if (!asciiOnlyLine) handleUnicode(unicodeStart); 1188 if (!asciiOnlyLine) handleUnicode(unicodeStart);
1188 return unterminatedMultiLineString(quoteChar); 1189 return unterminatedMultiLineString(quoteChar, start, asciiOnlyString);
1189 } 1190 }
1190 1191
1191 int unexpected(int character) { 1192 int unexpected(int character) {
1192 appendErrorToken(buildUnexpectedCharacterToken(character, tokenStart)); 1193 appendErrorToken(buildUnexpectedCharacterToken(character, tokenStart));
1193 return advanceAfterError(true); 1194 return advanceAfterError(true);
1194 } 1195 }
1195 1196
1196 int unterminated(String prefix, {bool shouldAdvance: true}) { 1197 int unterminated(String prefix, {bool shouldAdvance: true}) {
1197 appendErrorToken(new UnterminatedToken(prefix, tokenStart, stringOffset)); 1198 appendErrorToken(new UnterminatedToken(prefix, tokenStart, stringOffset));
1198 return advanceAfterError(shouldAdvance); 1199 return advanceAfterError(shouldAdvance);
1199 } 1200 }
1200 1201
1201 int unterminatedString(int quoteChar) { 1202 void terminateUnterminatedString(
1202 return unterminated(new String.fromCharCodes([quoteChar])); 1203 int start, bool asciiOnlyString, String suffix) {
1204 if (start < scanOffset) {
1205 appendSubstringToken(TokenType.STRING, start, asciiOnlyString);
1206 }
1207 beginToken();
1208 appendToken(
1209 new SyntheticStringToken(TokenType.STRING, suffix, tokenStart, 0));
1203 } 1210 }
1204 1211
1205 int unterminatedRawString(int quoteChar) { 1212 int unterminatedString(int quoteChar, int start, bool asciiOnlyString) {
1206 return unterminated('r${new String.fromCharCodes([quoteChar])}'); 1213 String suffix = new String.fromCharCodes([quoteChar]);
1214 terminateUnterminatedString(start, asciiOnlyString, suffix);
1215 return unterminated(suffix);
1207 } 1216 }
1208 1217
1209 int unterminatedMultiLineString(int quoteChar) { 1218 int unterminatedRawString(int quoteChar, int start, bool asciiOnlyString) {
1210 return unterminated( 1219 String suffix = new String.fromCharCodes([quoteChar]);
1211 new String.fromCharCodes([quoteChar, quoteChar, quoteChar])); 1220 terminateUnterminatedString(start, asciiOnlyString, suffix);
1221 return unterminated('r$suffix');
1212 } 1222 }
1213 1223
1214 int unterminatedRawMultiLineString(int quoteChar) { 1224 int unterminatedMultiLineString(
1215 return unterminated( 1225 int quoteChar, int start, bool asciiOnlyString) {
1216 'r${new String.fromCharCodes([quoteChar, quoteChar, quoteChar])}'); 1226 String suffix = new String.fromCharCodes([quoteChar, quoteChar, quoteChar]);
1227 terminateUnterminatedString(start, asciiOnlyString, suffix);
1228 return unterminated(suffix);
1229 }
1230
1231 int unterminatedRawMultiLineString(
1232 int quoteChar, int start, bool asciiOnlyString) {
1233 String suffix = new String.fromCharCodes([quoteChar, quoteChar, quoteChar]);
1234 terminateUnterminatedString(start, asciiOnlyString, suffix);
1235 return unterminated('r$suffix');
1217 } 1236 }
1218 1237
1219 int advanceAfterError(bool shouldAdvance) { 1238 int advanceAfterError(bool shouldAdvance) {
1220 if (atEndOfFile()) return $EOF; 1239 if (atEndOfFile()) return $EOF;
1221 if (shouldAdvance) { 1240 if (shouldAdvance) {
1222 return advance(); // Ensure progress. 1241 return advance(); // Ensure progress.
1223 } else { 1242 } else {
1224 return -1; 1243 return -1;
1225 } 1244 }
1226 } 1245 }
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1305 switchToUint32(newLength); 1324 switchToUint32(newLength);
1306 } 1325 }
1307 } 1326 }
1308 1327
1309 void switchToUint32(int newLength) { 1328 void switchToUint32(int newLength) {
1310 final newArray = new Uint32List(newLength); 1329 final newArray = new Uint32List(newLength);
1311 newArray.setRange(0, arrayLength, array); 1330 newArray.setRange(0, arrayLength, array);
1312 array = newArray; 1331 array = newArray;
1313 } 1332 }
1314 } 1333 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698