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

Side by Side Diff: pkg/front_end/test/scanner_test.dart

Issue 2915093002: improve fasta unterminated string recovery (Closed)
Patch Set: fix dartdoc 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) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 import 'package:front_end/src/base/errors.dart'; 5 import 'package:front_end/src/base/errors.dart';
6 import 'package:front_end/src/base/jenkins_smi_hash.dart'; 6 import 'package:front_end/src/base/jenkins_smi_hash.dart';
7 import 'package:front_end/src/scanner/errors.dart'; 7 import 'package:front_end/src/scanner/errors.dart';
8 import 'package:front_end/src/scanner/reader.dart'; 8 import 'package:front_end/src/scanner/reader.dart';
9 import 'package:front_end/src/scanner/scanner.dart'; 9 import 'package:front_end/src/scanner/scanner.dart';
10 import 'package:front_end/src/scanner/token.dart'; 10 import 'package:front_end/src/scanner/token.dart';
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 // https://code.google.com/p/dart/issues/detail?id=18073 432 // https://code.google.com/p/dart/issues/detail?id=18073
433 List<Token> expectedTokens = [ 433 List<Token> expectedTokens = [
434 new StringToken(TokenType.STRING, "\"foo ", 0), 434 new StringToken(TokenType.STRING, "\"foo ", 0),
435 new StringToken(TokenType.STRING_INTERPOLATION_EXPRESSION, "\${", 5), 435 new StringToken(TokenType.STRING_INTERPOLATION_EXPRESSION, "\${", 5),
436 new StringToken(TokenType.IDENTIFIER, "bar", 7), 436 new StringToken(TokenType.IDENTIFIER, "bar", 7),
437 ]; 437 ];
438 if (usingFasta) { 438 if (usingFasta) {
439 // fasta inserts synthetic closers 439 // fasta inserts synthetic closers
440 expectedTokens.addAll([ 440 expectedTokens.addAll([
441 new SyntheticToken(TokenType.CLOSE_CURLY_BRACKET, 10), 441 new SyntheticToken(TokenType.CLOSE_CURLY_BRACKET, 10),
442 new SyntheticStringToken(TokenType.STRING, "\"", 10, 0),
442 ]); 443 ]);
443 } else { 444 } else {
444 expectedTokens.addAll([ 445 expectedTokens.addAll([
445 new StringToken(TokenType.STRING, "", 10), 446 new StringToken(TokenType.STRING, "", 10),
446 ]); 447 ]);
447 } 448 }
448 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 9, 449 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 9,
449 "\"foo \${bar", expectedTokens); 450 "\"foo \${bar", expectedTokens);
450 } 451 }
451 452
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after
985 986
986 void test_string_multi_single() { 987 void test_string_multi_single() {
987 _assertToken(TokenType.STRING, "'''string'''"); 988 _assertToken(TokenType.STRING, "'''string'''");
988 } 989 }
989 990
990 void test_string_multi_slashEnter() { 991 void test_string_multi_slashEnter() {
991 _assertToken(TokenType.STRING, "'''\\\n'''"); 992 _assertToken(TokenType.STRING, "'''\\\n'''");
992 } 993 }
993 994
994 void test_string_multi_unterminated() { 995 void test_string_multi_unterminated() {
995 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 8, 996 List<Token> expectedTokens = [];
996 "'''string", [new StringToken(TokenType.STRING, "'''string", 0)]); 997 if (usingFasta) {
998 // fasta inserts synthetic closers
ahe 2017/06/02 12:26:43 fasta -> Fasta Add period at end of sentence. Her
999 expectedTokens.addAll([
1000 new SyntheticStringToken(TokenType.STRING, "'''string'''", 0, 9),
1001 ]);
1002 } else {
1003 expectedTokens.addAll([
1004 new StringToken(TokenType.STRING, "'''string", 0),
1005 ]);
1006 }
997 } 1007 }
998 1008
999 void test_string_multi_unterminated_interpolation_block() { 1009 void test_string_multi_unterminated_interpolation_block() {
1000 _assertErrorAndTokens( 1010 List<Token> expectedTokens = [
1001 ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 8, "'''\${name", [
1002 new StringToken(TokenType.STRING, "'''", 0), 1011 new StringToken(TokenType.STRING, "'''", 0),
1003 new StringToken(TokenType.STRING_INTERPOLATION_EXPRESSION, "\${", 3), 1012 new StringToken(TokenType.STRING_INTERPOLATION_EXPRESSION, "\${", 3),
1004 new StringToken(TokenType.IDENTIFIER, "name", 5), 1013 new StringToken(TokenType.IDENTIFIER, "name", 5),
1005 new StringToken(TokenType.STRING, "", 9) 1014 ];
1006 ]); 1015 if (usingFasta) {
1016 // fasta inserts synthetic closers
1017 expectedTokens.addAll([
1018 new SyntheticToken(TokenType.CLOSE_CURLY_BRACKET, 9),
1019 new SyntheticStringToken(TokenType.STRING, "'''", 9, 0),
1020 ]);
1021 } else {
1022 expectedTokens.addAll([
1023 new StringToken(TokenType.STRING, "", 9),
1024 ]);
1025 }
1026 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 8,
1027 "'''\${name", expectedTokens);
1007 } 1028 }
1008 1029
1009 void test_string_multi_unterminated_interpolation_identifier() { 1030 void test_string_multi_unterminated_interpolation_identifier() {
1010 _assertErrorAndTokens( 1031 List<Token> expectedTokens = [
1011 ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 7, "'''\$name", [
1012 new StringToken(TokenType.STRING, "'''", 0), 1032 new StringToken(TokenType.STRING, "'''", 0),
1013 new StringToken(TokenType.STRING_INTERPOLATION_IDENTIFIER, "\$", 3), 1033 new StringToken(TokenType.STRING_INTERPOLATION_IDENTIFIER, "\$", 3),
1014 new StringToken(TokenType.IDENTIFIER, "name", 4), 1034 new StringToken(TokenType.IDENTIFIER, "name", 4),
1015 new StringToken(TokenType.STRING, "", 8) 1035 ];
1016 ]); 1036 if (usingFasta) {
1037 // fasta inserts synthetic closers
1038 expectedTokens.addAll([
1039 new SyntheticStringToken(TokenType.STRING, "'''", 8, 0),
1040 ]);
1041 } else {
1042 expectedTokens.addAll([
1043 new StringToken(TokenType.STRING, "", 8),
1044 ]);
1045 }
1046 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 7,
1047 "'''\$name", expectedTokens);
1017 } 1048 }
1018 1049
1019 void test_string_raw_multi_double() { 1050 void test_string_raw_multi_double() {
1020 _assertToken(TokenType.STRING, "r\"\"\"line1\nline2\"\"\""); 1051 _assertToken(TokenType.STRING, "r\"\"\"line1\nline2\"\"\"");
1021 } 1052 }
1022 1053
1023 void test_string_raw_multi_single() { 1054 void test_string_raw_multi_single() {
1024 _assertToken(TokenType.STRING, "r'''string'''"); 1055 _assertToken(TokenType.STRING, "r'''string'''");
1025 } 1056 }
1026 1057
1027 void test_string_raw_multi_unterminated() { 1058 void test_string_raw_multi_unterminated() {
1028 String source = "r'''string"; 1059 String source = "r'''string";
1060 List<Token> expectedTokens = [];
1061 if (usingFasta) {
1062 // fasta inserts synthetic closers
1063 expectedTokens.addAll([
1064 new SyntheticStringToken(TokenType.STRING, "r'''string'''", 0, 10),
1065 ]);
1066 } else {
1067 expectedTokens.addAll([
1068 new StringToken(TokenType.STRING, "r'''string", 0),
1069 ]);
1070 }
1029 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 9, 1071 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 9,
1030 source, [new StringToken(TokenType.STRING, source, 0)]); 1072 source, expectedTokens);
1031 } 1073 }
1032 1074
1033 void test_string_raw_simple_double() { 1075 void test_string_raw_simple_double() {
1034 _assertToken(TokenType.STRING, "r\"string\""); 1076 _assertToken(TokenType.STRING, "r\"string\"");
1035 } 1077 }
1036 1078
1037 void test_string_raw_simple_single() { 1079 void test_string_raw_simple_single() {
1038 _assertToken(TokenType.STRING, "r'string'"); 1080 _assertToken(TokenType.STRING, "r'string'");
1039 } 1081 }
1040 1082
1041 void test_string_raw_simple_unterminated_eof() { 1083 void test_string_raw_simple_unterminated_eof() {
1042 String source = "r'string"; 1084 String source = "r'string";
1085 List<Token> expectedTokens = [];
1086 if (usingFasta) {
1087 // fasta inserts synthetic closers
1088 expectedTokens.addAll([
1089 new SyntheticStringToken(TokenType.STRING, "r'string'", 0, 8),
1090 ]);
1091 } else {
1092 expectedTokens.addAll([
1093 new StringToken(TokenType.STRING, "r'string", 0),
1094 ]);
1095 }
1043 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 7, 1096 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 7,
1044 source, [new StringToken(TokenType.STRING, source, 0)]); 1097 source, expectedTokens);
1045 } 1098 }
1046 1099
1047 void test_string_raw_simple_unterminated_eol() { 1100 void test_string_raw_simple_unterminated_eol() {
1048 String source = "r'string"; 1101 String source = "r'string\n";
1049 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 8, 1102 List<Token> expectedTokens = [];
1050 "$source\n", [new StringToken(TokenType.STRING, source, 0)]); 1103 if (usingFasta) {
1104 // fasta inserts synthetic closers
1105 expectedTokens.addAll([
1106 new SyntheticStringToken(TokenType.STRING, "r'string'", 0, 8),
1107 ]);
1108 } else {
1109 expectedTokens.addAll([
1110 new StringToken(TokenType.STRING, "r'string", 0),
1111 ]);
1112 }
1113 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL,
1114 usingFasta ? 7 : 8, source, expectedTokens);
1051 } 1115 }
1052 1116
1053 void test_string_simple_double() { 1117 void test_string_simple_double() {
1054 _assertToken(TokenType.STRING, "\"string\""); 1118 _assertToken(TokenType.STRING, "\"string\"");
1055 } 1119 }
1056 1120
1057 void test_string_simple_escapedDollar() { 1121 void test_string_simple_escapedDollar() {
1058 _assertToken(TokenType.STRING, "'a\\\$b'"); 1122 _assertToken(TokenType.STRING, "'a\\\$b'");
1059 } 1123 }
1060 1124
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1136 new StringToken(TokenType.STRING, "1'", 2) 1200 new StringToken(TokenType.STRING, "1'", 2)
1137 ]); 1201 ]);
1138 } 1202 }
1139 1203
1140 void test_string_simple_single() { 1204 void test_string_simple_single() {
1141 _assertToken(TokenType.STRING, "'string'"); 1205 _assertToken(TokenType.STRING, "'string'");
1142 } 1206 }
1143 1207
1144 void test_string_simple_unterminated_eof() { 1208 void test_string_simple_unterminated_eof() {
1145 String source = "'string"; 1209 String source = "'string";
1210 List<Token> expectedTokens = [];
1211 if (usingFasta) {
1212 // fasta inserts synthetic closers
1213 expectedTokens.addAll([
1214 new SyntheticStringToken(TokenType.STRING, "'string'", 0, 7),
1215 ]);
1216 } else {
1217 expectedTokens.addAll([
1218 new StringToken(TokenType.STRING, "'string", 0),
1219 ]);
1220 }
1146 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 6, 1221 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 6,
1147 source, [new StringToken(TokenType.STRING, source, 0)]); 1222 source, expectedTokens);
1148 } 1223 }
1149 1224
1150 void test_string_simple_unterminated_eol() { 1225 void test_string_simple_unterminated_eol() {
1151 String source = "'string"; 1226 String source = "'string\r";
1152 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 7, 1227 List<Token> expectedTokens = [];
1153 "$source\r", [new StringToken(TokenType.STRING, source, 0)]); 1228 if (usingFasta) {
1229 // fasta inserts synthetic closers
1230 expectedTokens.addAll([
1231 new SyntheticStringToken(TokenType.STRING, "'string'", 0, 7),
1232 ]);
1233 } else {
1234 expectedTokens.addAll([
1235 new StringToken(TokenType.STRING, "'string", 0),
1236 ]);
1237 }
1238 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL,
1239 usingFasta ? 6 : 7, source, expectedTokens);
1154 } 1240 }
1155 1241
1156 void test_string_simple_unterminated_interpolation_block() { 1242 void test_string_simple_unterminated_interpolation_block() {
1157 _assertErrorAndTokens( 1243 List<Token> expectedTokens = [
1158 ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 6, "'\${name", [
1159 new StringToken(TokenType.STRING, "'", 0), 1244 new StringToken(TokenType.STRING, "'", 0),
1160 new StringToken(TokenType.STRING_INTERPOLATION_EXPRESSION, "\${", 1), 1245 new StringToken(TokenType.STRING_INTERPOLATION_EXPRESSION, "\${", 1),
1161 new StringToken(TokenType.IDENTIFIER, "name", 3), 1246 new StringToken(TokenType.IDENTIFIER, "name", 3),
1162 new StringToken(TokenType.STRING, "", 7) 1247 ];
1163 ]); 1248 if (usingFasta) {
1249 // fasta inserts synthetic closers
1250 expectedTokens.addAll([
1251 new SyntheticToken(TokenType.CLOSE_CURLY_BRACKET, 7),
1252 new SyntheticStringToken(TokenType.STRING, "'", 7, 0),
1253 ]);
1254 } else {
1255 expectedTokens.addAll([
1256 new StringToken(TokenType.STRING, "", 7),
1257 ]);
1258 }
1259 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 6,
1260 "'\${name", expectedTokens);
1164 } 1261 }
1165 1262
1166 void test_string_simple_unterminated_interpolation_identifier() { 1263 void test_string_simple_unterminated_interpolation_identifier() {
1167 _assertErrorAndTokens( 1264 List<Token> expectedTokens = [
1168 ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 5, "'\$name", [
1169 new StringToken(TokenType.STRING, "'", 0), 1265 new StringToken(TokenType.STRING, "'", 0),
1170 new StringToken(TokenType.STRING_INTERPOLATION_IDENTIFIER, "\$", 1), 1266 new StringToken(TokenType.STRING_INTERPOLATION_IDENTIFIER, "\$", 1),
1171 new StringToken(TokenType.IDENTIFIER, "name", 2), 1267 new StringToken(TokenType.IDENTIFIER, "name", 2),
1172 new StringToken(TokenType.STRING, "", 6) 1268 ];
1173 ]); 1269 if (usingFasta) {
1270 // fasta inserts synthetic closers
1271 expectedTokens.addAll([
1272 new SyntheticStringToken(TokenType.STRING, "'", 6, 0),
1273 ]);
1274 } else {
1275 expectedTokens.addAll([
1276 new StringToken(TokenType.STRING, "", 6),
1277 ]);
1278 }
1279 _assertErrorAndTokens(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 5,
1280 "'\$name", expectedTokens);
1174 } 1281 }
1175 1282
1176 void test_sync_star() { 1283 void test_sync_star() {
1177 Token token = _scan("sync*"); 1284 Token token = _scan("sync*");
1178 expect(token.type.isKeyword, true); 1285 expect(token.type.isKeyword, true);
1179 expect(token.lexeme, 'sync'); 1286 expect(token.lexeme, 'sync');
1180 expect(token.next.type, TokenType.STAR); 1287 expect(token.next.type, TokenType.STAR);
1181 expect(token.next.next.type, TokenType.EOF); 1288 expect(token.next.next.type, TokenType.EOF);
1182 } 1289 }
1183 1290
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
1506 _TestScanner(CharacterReader reader, [this.listener]) : super.create(reader); 1613 _TestScanner(CharacterReader reader, [this.listener]) : super.create(reader);
1507 1614
1508 @override 1615 @override
1509 void reportError( 1616 void reportError(
1510 ScannerErrorCode errorCode, int offset, List<Object> arguments) { 1617 ScannerErrorCode errorCode, int offset, List<Object> arguments) {
1511 if (listener != null) { 1618 if (listener != null) {
1512 listener.errors.add(new TestError(offset, errorCode, arguments)); 1619 listener.errors.add(new TestError(offset, errorCode, arguments));
1513 } 1620 }
1514 } 1621 }
1515 } 1622 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698