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 757 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
768 | 768 |
769 void test_minus_minus() { | 769 void test_minus_minus() { |
770 _assertToken(TokenType.MINUS_MINUS, "--"); | 770 _assertToken(TokenType.MINUS_MINUS, "--"); |
771 } | 771 } |
772 | 772 |
773 void test_mismatched_closer() { | 773 void test_mismatched_closer() { |
774 // When openers and closers are mismatched, analyzer favors considering the | 774 // When openers and closers are mismatched, analyzer favors considering the |
775 // closer to be mismatched, which means that `(])` parses as a pair of | 775 // closer to be mismatched, which means that `(])` parses as a pair of |
776 // matched parentheses with an unmatched closing bracket between them. | 776 // matched parentheses with an unmatched closing bracket between them. |
777 var openParen = _scan('(])') as BeginToken; | 777 var openParen = _scan('(])') as BeginToken; |
778 var closeBracket = openParen.next; | 778 if (usingFasta) { |
779 var closeParen = closeBracket.next; | 779 // When openers and closers are mismatched, |
780 expect(closeParen.next.type, TokenType.EOF); | 780 // fasta favors considering the opener to be mismatched, |
781 expect(openParen.endToken, same(closeParen)); | 781 // and inserts synthetic closers as needed. |
| 782 // `(])` is parsed as `()])` where the first `)` is synthetic |
| 783 // and the trailing `])` are unmatched. |
| 784 var closeParen = openParen.next; |
| 785 expect(closeParen.isSynthetic, isTrue); |
| 786 var closeBracket = closeParen.next; |
| 787 expect(closeBracket.isSynthetic, isFalse); |
| 788 var closeParen2 = closeBracket.next; |
| 789 expect(closeParen2.isSynthetic, isFalse); |
| 790 expect(closeParen2.next.type, TokenType.EOF); |
| 791 expect(openParen.endToken, same(closeParen)); |
| 792 } else { |
| 793 var closeBracket = openParen.next; |
| 794 var closeParen = closeBracket.next; |
| 795 expect(closeParen.next.type, TokenType.EOF); |
| 796 expect(openParen.endToken, same(closeParen)); |
| 797 } |
782 } | 798 } |
783 | 799 |
784 void test_mismatched_opener() { | 800 void test_mismatched_opener() { |
785 // When openers and closers are mismatched, analyzer favors considering the | 801 // When openers and closers are mismatched, analyzer favors considering the |
786 // closer to be mismatched, which means that `([)` parses as three unmatched | 802 // closer to be mismatched, which means that `([)` parses as three unmatched |
787 // tokens. | 803 // tokens. |
788 var openParen = _scan('([)') as BeginToken; | 804 var openParen = _scan('([)') as BeginToken; |
789 var openBracket = openParen.next as BeginToken; | 805 var openBracket = openParen.next as BeginToken; |
790 var closeParen = openBracket.next; | 806 if (usingFasta) { |
791 expect(closeParen.next.type, TokenType.EOF); | 807 // When openers and closers are mismatched, |
792 expect(openParen.endToken, isNull); | 808 // fasta favors considering the opener to be mismatched |
793 expect(openBracket.endToken, isNull); | 809 // and inserts synthetic closers as needed. |
| 810 // `([)` is scanned as `([])` where `]` is synthetic. |
| 811 var closeBracket = openBracket.next; |
| 812 expect(closeBracket.isSynthetic, isTrue); |
| 813 var closeParen = closeBracket.next; |
| 814 expect(closeParen.isSynthetic, isFalse); |
| 815 expect(closeParen.next.type, TokenType.EOF); |
| 816 expect(openBracket.endToken, closeBracket); |
| 817 expect(openParen.endToken, closeParen); |
| 818 } else { |
| 819 var closeParen = openBracket.next; |
| 820 expect(closeParen.next.type, TokenType.EOF); |
| 821 expect(openParen.endToken, isNull); |
| 822 expect(openBracket.endToken, isNull); |
| 823 } |
794 } | 824 } |
795 | 825 |
796 void test_mismatched_opener_in_interpolation() { | 826 void test_mismatched_opener_in_interpolation() { |
797 // In an interpolation expression, analyzer considers a closing `}` to | 827 // In an interpolation expression, analyzer considers a closing `}` to |
798 // always match the preceding unmatched `{`, even if there are intervening | 828 // always match the preceding unmatched `{`, even if there are intervening |
799 // unmatched tokens, which means that `"${({(}}"` parses as though the open | 829 // unmatched tokens, which means that `"${({(}}"` parses as though the open |
800 // parens are unmatched but everything else is matched. | 830 // parens are unmatched but everything else is matched. |
801 var stringStart = _scan(r'"${({(}}"'); | 831 var stringStart = _scan(r'"${({(}}"'); |
802 var interpolationStart = stringStart.next as BeginToken; | 832 var interpolationStart = stringStart.next as BeginToken; |
803 var openParen1 = interpolationStart.next as BeginToken; | 833 var openParen1 = interpolationStart.next as BeginToken; |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1176 void test_string_simple_interpolation_identifier() { | 1206 void test_string_simple_interpolation_identifier() { |
1177 _assertTokens("'Hello \$name!'", [ | 1207 _assertTokens("'Hello \$name!'", [ |
1178 new StringToken(TokenType.STRING, "'Hello ", 0), | 1208 new StringToken(TokenType.STRING, "'Hello ", 0), |
1179 new StringToken(TokenType.STRING_INTERPOLATION_IDENTIFIER, "\$", 7), | 1209 new StringToken(TokenType.STRING_INTERPOLATION_IDENTIFIER, "\$", 7), |
1180 new StringToken(TokenType.IDENTIFIER, "name", 8), | 1210 new StringToken(TokenType.IDENTIFIER, "name", 8), |
1181 new StringToken(TokenType.STRING, "!'", 12) | 1211 new StringToken(TokenType.STRING, "!'", 12) |
1182 ]); | 1212 ]); |
1183 } | 1213 } |
1184 | 1214 |
1185 void test_string_simple_interpolation_missingIdentifier() { | 1215 void test_string_simple_interpolation_missingIdentifier() { |
1186 _assertTokens("'\$x\$'", [ | 1216 var expectedTokens = <Token>[ |
1187 new StringToken(TokenType.STRING, "'", 0), | 1217 new StringToken(TokenType.STRING, "'", 0), |
1188 new StringToken(TokenType.STRING_INTERPOLATION_IDENTIFIER, "\$", 1), | 1218 new StringToken(TokenType.STRING_INTERPOLATION_IDENTIFIER, "\$", 1), |
1189 new StringToken(TokenType.IDENTIFIER, "x", 2), | 1219 new StringToken(TokenType.IDENTIFIER, "x", 2), |
1190 new StringToken(TokenType.STRING, "", 3), | 1220 new StringToken(TokenType.STRING, "", 3), |
1191 new StringToken(TokenType.STRING_INTERPOLATION_IDENTIFIER, "\$", 3), | 1221 new StringToken(TokenType.STRING_INTERPOLATION_IDENTIFIER, "\$", 3), |
1192 new StringToken(TokenType.STRING, "'", 4) | 1222 ]; |
1193 ]); | 1223 if (usingFasta) { |
| 1224 // Fasta scanner inserts a synthetic identifier |
| 1225 expectedTokens.addAll([ |
| 1226 new SyntheticStringToken(TokenType.IDENTIFIER, "", 4, 0), |
| 1227 new StringToken(TokenType.STRING, "'", 4), |
| 1228 ]); |
| 1229 } else { |
| 1230 expectedTokens.addAll([ |
| 1231 new StringToken(TokenType.STRING, "'", 4), |
| 1232 ]); |
| 1233 } |
| 1234 _assertTokens("'\$x\$'", expectedTokens); |
1194 } | 1235 } |
1195 | 1236 |
1196 void test_string_simple_interpolation_nonIdentifier() { | 1237 void test_string_simple_interpolation_nonIdentifier() { |
1197 _assertTokens("'\$1'", [ | 1238 var expectedTokens = <Token>[ |
1198 new StringToken(TokenType.STRING, "'", 0), | 1239 new StringToken(TokenType.STRING, "'", 0), |
1199 new StringToken(TokenType.STRING_INTERPOLATION_IDENTIFIER, "\$", 1), | 1240 new StringToken(TokenType.STRING_INTERPOLATION_IDENTIFIER, "\$", 1), |
1200 new StringToken(TokenType.STRING, "1'", 2) | 1241 ]; |
| 1242 if (usingFasta) { |
| 1243 expectedTokens.addAll([ |
| 1244 new SyntheticStringToken(TokenType.IDENTIFIER, "", 2), |
| 1245 ]); |
| 1246 } |
| 1247 expectedTokens.addAll([ |
| 1248 new StringToken(TokenType.STRING, "1'", 2), |
1201 ]); | 1249 ]); |
| 1250 _assertTokens("'\$1'", expectedTokens); |
1202 } | 1251 } |
1203 | 1252 |
1204 void test_string_simple_single() { | 1253 void test_string_simple_single() { |
1205 _assertToken(TokenType.STRING, "'string'"); | 1254 _assertToken(TokenType.STRING, "'string'"); |
1206 } | 1255 } |
1207 | 1256 |
1208 void test_string_simple_unterminated_eof() { | 1257 void test_string_simple_unterminated_eof() { |
1209 String source = "'string"; | 1258 String source = "'string"; |
1210 List<Token> expectedTokens = []; | 1259 List<Token> expectedTokens = []; |
1211 if (usingFasta) { | 1260 if (usingFasta) { |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1613 _TestScanner(CharacterReader reader, [this.listener]) : super.create(reader); | 1662 _TestScanner(CharacterReader reader, [this.listener]) : super.create(reader); |
1614 | 1663 |
1615 @override | 1664 @override |
1616 void reportError( | 1665 void reportError( |
1617 ScannerErrorCode errorCode, int offset, List<Object> arguments) { | 1666 ScannerErrorCode errorCode, int offset, List<Object> arguments) { |
1618 if (listener != null) { | 1667 if (listener != null) { |
1619 listener.errors.add(new TestError(offset, errorCode, arguments)); | 1668 listener.errors.add(new TestError(offset, errorCode, arguments)); |
1620 } | 1669 } |
1621 } | 1670 } |
1622 } | 1671 } |
OLD | NEW |