| OLD | NEW |
| 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 1082 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1093 * [expectedError] the error that should be produced | 1093 * [expectedError] the error that should be produced |
| 1094 * [expectedOffset] the string offset that should be associated with the error | 1094 * [expectedOffset] the string offset that should be associated with the error |
| 1095 * [source] the source to be scanned to produce the error | 1095 * [source] the source to be scanned to produce the error |
| 1096 */ | 1096 */ |
| 1097 void _assertError( | 1097 void _assertError( |
| 1098 ScannerErrorCode expectedError, int expectedOffset, String source, | 1098 ScannerErrorCode expectedError, int expectedOffset, String source, |
| 1099 [List<Object> arguments]) { | 1099 [List<Object> arguments]) { |
| 1100 ErrorListener listener = new ErrorListener(); | 1100 ErrorListener listener = new ErrorListener(); |
| 1101 scanWithListener(source, listener); | 1101 scanWithListener(source, listener); |
| 1102 listener.assertErrors( | 1102 listener.assertErrors( |
| 1103 [new TestError(expectedOffset, 1, expectedError, arguments)]); | 1103 [new TestError(expectedOffset, expectedError, arguments)]); |
| 1104 } | 1104 } |
| 1105 | 1105 |
| 1106 /** | 1106 /** |
| 1107 * Assert that scanning the given [source] produces an error with the given | 1107 * Assert that scanning the given [source] produces an error with the given |
| 1108 * code, and also produces the given tokens. | 1108 * code, and also produces the given tokens. |
| 1109 * | 1109 * |
| 1110 * [expectedError] the error that should be produced | 1110 * [expectedError] the error that should be produced |
| 1111 * [expectedOffset] the string offset that should be associated with the error | 1111 * [expectedOffset] the string offset that should be associated with the error |
| 1112 * [source] the source to be scanned to produce the error | 1112 * [source] the source to be scanned to produce the error |
| 1113 * [expectedTokens] the tokens that are expected to be in the source | 1113 * [expectedTokens] the tokens that are expected to be in the source |
| 1114 */ | 1114 */ |
| 1115 void _assertErrorAndTokens(ScannerErrorCode expectedError, int expectedOffset, | 1115 void _assertErrorAndTokens(ScannerErrorCode expectedError, int expectedOffset, |
| 1116 String source, List<Token> expectedTokens) { | 1116 String source, List<Token> expectedTokens) { |
| 1117 ErrorListener listener = new ErrorListener(); | 1117 ErrorListener listener = new ErrorListener(); |
| 1118 Token token = scanWithListener(source, listener); | 1118 Token token = scanWithListener(source, listener); |
| 1119 listener | 1119 listener.assertErrors([new TestError(expectedOffset, expectedError, null)]); |
| 1120 .assertErrors([new TestError(expectedOffset, 1, expectedError, null)]); | |
| 1121 _checkTokens(token, expectedTokens); | 1120 _checkTokens(token, expectedTokens); |
| 1122 } | 1121 } |
| 1123 | 1122 |
| 1124 /** | 1123 /** |
| 1125 * Assert that when scanned the given [source] contains a single identifier | 1124 * Assert that when scanned the given [source] contains a single identifier |
| 1126 * token with the same lexeme as the original source. | 1125 * token with the same lexeme as the original source. |
| 1127 */ | 1126 */ |
| 1128 void _assertIdentifierToken(String source) { | 1127 void _assertIdentifierToken(String source) { |
| 1129 void check(String s, int expectedOffset) { | 1128 void check(String s, int expectedOffset) { |
| 1130 Token token = _scan(s); | 1129 Token token = _scan(s); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1256 Token token = scanWithListener(source, listener, | 1255 Token token = scanWithListener(source, listener, |
| 1257 genericMethodComments: genericMethodComments, | 1256 genericMethodComments: genericMethodComments, |
| 1258 lazyAssignmentOperators: lazyAssignmentOperators); | 1257 lazyAssignmentOperators: lazyAssignmentOperators); |
| 1259 listener.assertNoErrors(); | 1258 listener.assertNoErrors(); |
| 1260 return token; | 1259 return token; |
| 1261 } | 1260 } |
| 1262 } | 1261 } |
| 1263 | 1262 |
| 1264 class TestError { | 1263 class TestError { |
| 1265 final int offset; | 1264 final int offset; |
| 1266 final int length; | |
| 1267 final ErrorCode errorCode; | 1265 final ErrorCode errorCode; |
| 1268 final List<Object> arguments; | 1266 final List<Object> arguments; |
| 1269 | 1267 |
| 1270 TestError(this.offset, this.length, this.errorCode, this.arguments); | 1268 TestError(this.offset, this.errorCode, this.arguments); |
| 1271 | 1269 |
| 1272 @override | 1270 @override |
| 1273 get hashCode { | 1271 get hashCode { |
| 1274 var h = new JenkinsSmiHash()..add(offset)..add(length)..add(errorCode); | 1272 var h = new JenkinsSmiHash()..add(offset)..add(errorCode); |
| 1275 if (arguments != null) { | 1273 if (arguments != null) { |
| 1276 for (Object argument in arguments) { | 1274 for (Object argument in arguments) { |
| 1277 h.add(argument); | 1275 h.add(argument); |
| 1278 } | 1276 } |
| 1279 } | 1277 } |
| 1280 return h.hashCode; | 1278 return h.hashCode; |
| 1281 } | 1279 } |
| 1282 | 1280 |
| 1283 @override | 1281 @override |
| 1284 operator ==(Object other) { | 1282 operator ==(Object other) { |
| 1285 if (other is TestError && | 1283 if (other is TestError && |
| 1286 offset == other.offset && | 1284 offset == other.offset && |
| 1287 length == other.length && | |
| 1288 errorCode == other.errorCode) { | 1285 errorCode == other.errorCode) { |
| 1289 if (arguments == null) return other.arguments == null; | 1286 if (arguments == null) return other.arguments == null; |
| 1290 if (other.arguments == null) return false; | 1287 if (other.arguments == null) return false; |
| 1291 if (arguments.length != other.arguments.length) return false; | 1288 if (arguments.length != other.arguments.length) return false; |
| 1292 for (int i = 0; i < arguments.length; i++) { | 1289 for (int i = 0; i < arguments.length; i++) { |
| 1293 if (arguments[i] != other.arguments[i]) return false; | 1290 if (arguments[i] != other.arguments[i]) return false; |
| 1294 } | 1291 } |
| 1295 return true; | 1292 return true; |
| 1296 } | 1293 } |
| 1297 return false; | 1294 return false; |
| 1298 } | 1295 } |
| 1299 | 1296 |
| 1300 @override | 1297 @override |
| 1301 toString() { | 1298 toString() { |
| 1302 var end = offset + length; | |
| 1303 var argString = arguments == null ? '' : '(${arguments.join(', ')})'; | 1299 var argString = arguments == null ? '' : '(${arguments.join(', ')})'; |
| 1304 return 'Error($offset..$end, $errorCode$argString)'; | 1300 return 'Error($offset, $errorCode$argString)'; |
| 1305 } | 1301 } |
| 1306 } | 1302 } |
| 1307 | 1303 |
| 1308 @reflectiveTest | 1304 @reflectiveTest |
| 1309 class TokenTypeTest { | 1305 class TokenTypeTest { |
| 1310 void test_isOperator() { | 1306 void test_isOperator() { |
| 1311 expect(TokenType.AMPERSAND.isOperator, isTrue); | 1307 expect(TokenType.AMPERSAND.isOperator, isTrue); |
| 1312 expect(TokenType.AMPERSAND_AMPERSAND.isOperator, isTrue); | 1308 expect(TokenType.AMPERSAND_AMPERSAND.isOperator, isTrue); |
| 1313 expect(TokenType.AMPERSAND_EQ.isOperator, isTrue); | 1309 expect(TokenType.AMPERSAND_EQ.isOperator, isTrue); |
| 1314 expect(TokenType.BANG.isOperator, isTrue); | 1310 expect(TokenType.BANG.isOperator, isTrue); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1375 | 1371 |
| 1376 class _TestScanner extends Scanner { | 1372 class _TestScanner extends Scanner { |
| 1377 final ErrorListener listener; | 1373 final ErrorListener listener; |
| 1378 | 1374 |
| 1379 _TestScanner(CharacterReader reader, [this.listener]) : super(reader); | 1375 _TestScanner(CharacterReader reader, [this.listener]) : super(reader); |
| 1380 | 1376 |
| 1381 @override | 1377 @override |
| 1382 void reportError( | 1378 void reportError( |
| 1383 ScannerErrorCode errorCode, int offset, List<Object> arguments) { | 1379 ScannerErrorCode errorCode, int offset, List<Object> arguments) { |
| 1384 if (listener != null) { | 1380 if (listener != null) { |
| 1385 listener.errors.add(new TestError(offset, 1, errorCode, arguments)); | 1381 listener.errors.add(new TestError(offset, errorCode, arguments)); |
| 1386 } | 1382 } |
| 1387 } | 1383 } |
| 1388 } | 1384 } |
| OLD | NEW |