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

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

Issue 2690073003: When translating analyzer/fasta token streams, match up begin/end tokens. (Closed)
Patch Set: Created 3 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
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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 158
159 void test_ampersand_ampersand_eq() { 159 void test_ampersand_ampersand_eq() {
160 _assertToken(TokenType.AMPERSAND_AMPERSAND_EQ, "&&=", 160 _assertToken(TokenType.AMPERSAND_AMPERSAND_EQ, "&&=",
161 lazyAssignmentOperators: true); 161 lazyAssignmentOperators: true);
162 } 162 }
163 163
164 void test_ampersand_eq() { 164 void test_ampersand_eq() {
165 _assertToken(TokenType.AMPERSAND_EQ, "&="); 165 _assertToken(TokenType.AMPERSAND_EQ, "&=");
166 } 166 }
167 167
168 void test_angle_brackets_are_ordinary_tokens() {
169 // Analyzer's token streams don't consider "<" to be an opener.
170 var lessThan = _scan('<>');
171 var greaterThan = lessThan.next;
172 expect(greaterThan.next.type, TokenType.EOF);
173 expect(lessThan, isNot(new isInstanceOf<BeginToken>()));
174 expect(greaterThan, isNot(new isInstanceOf<BeginToken>()));
175 }
176
168 void test_async_star() { 177 void test_async_star() {
169 Token token = _scan("async*"); 178 Token token = _scan("async*");
170 expect(token.type, TokenType.IDENTIFIER); 179 expect(token.type, TokenType.IDENTIFIER);
171 expect(token.lexeme, 'async'); 180 expect(token.lexeme, 'async');
172 expect(token.next.type, TokenType.STAR); 181 expect(token.next.type, TokenType.STAR);
173 expect(token.next.next.type, TokenType.EOF); 182 expect(token.next.next.type, TokenType.EOF);
174 } 183 }
175 184
176 void test_at() { 185 void test_at() {
177 _assertToken(TokenType.AT, "@"); 186 _assertToken(TokenType.AT, "@");
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after
690 } 699 }
691 700
692 void test_lt_lt() { 701 void test_lt_lt() {
693 _assertToken(TokenType.LT_LT, "<<"); 702 _assertToken(TokenType.LT_LT, "<<");
694 } 703 }
695 704
696 void test_lt_lt_eq() { 705 void test_lt_lt_eq() {
697 _assertToken(TokenType.LT_LT_EQ, "<<="); 706 _assertToken(TokenType.LT_LT_EQ, "<<=");
698 } 707 }
699 708
709 void test_matching_braces() {
710 var openBrace1 = _scan('{1: {2: 3}}') as BeginToken;
711 var one = openBrace1.next;
712 var colon1 = one.next;
713 var openBrace2 = colon1.next as BeginToken;
714 var two = openBrace2.next;
715 var colon2 = two.next;
716 var three = colon2.next;
717 var closeBrace1 = three.next;
718 var closeBrace2 = closeBrace1.next;
719 expect(closeBrace2.next.type, TokenType.EOF);
720 expect(openBrace1.endToken, same(closeBrace2));
721 expect(openBrace2.endToken, same(closeBrace1));
722 }
723
724 void test_matching_brackets() {
725 var openBracket1 = _scan('[1, [2]]') as BeginToken;
726 var one = openBracket1.next;
727 var comma = one.next;
728 var openBracket2 = comma.next as BeginToken;
729 var two = openBracket2.next;
730 var closeBracket1 = two.next;
731 var closeBracket2 = closeBracket1.next;
732 expect(closeBracket2.next.type, TokenType.EOF);
733 expect(openBracket1.endToken, same(closeBracket2));
734 expect(openBracket2.endToken, same(closeBracket1));
735 }
736
737 void test_matching_parens() {
738 var openParen1 = _scan('(f(x))') as BeginToken;
739 var f = openParen1.next;
740 var openParen2 = f.next as BeginToken;
741 var x = openParen2.next;
742 var closeParen1 = x.next;
743 var closeParen2 = closeParen1.next;
744 expect(closeParen2.next.type, TokenType.EOF);
745 expect(openParen1.endToken, same(closeParen2));
746 expect(openParen2.endToken, same(closeParen1));
747 }
748
700 void test_minus() { 749 void test_minus() {
701 _assertToken(TokenType.MINUS, "-"); 750 _assertToken(TokenType.MINUS, "-");
702 } 751 }
703 752
704 void test_minus_eq() { 753 void test_minus_eq() {
705 _assertToken(TokenType.MINUS_EQ, "-="); 754 _assertToken(TokenType.MINUS_EQ, "-=");
706 } 755 }
707 756
708 void test_minus_minus() { 757 void test_minus_minus() {
709 _assertToken(TokenType.MINUS_MINUS, "--"); 758 _assertToken(TokenType.MINUS_MINUS, "--");
710 } 759 }
711 760
761 void test_mismatched_closer() {
762 // When openers and closers are mismatched, analyzer favors considering the
763 // closer to be mismatched, which means that `(])` parses as a pair of
764 // matched parentheses with an unmatched closing bracket between them.
765 var openParen = _scan('(])') as BeginToken;
766 var closeBracket = openParen.next;
767 var closeParen = closeBracket.next;
768 expect(closeParen.next.type, TokenType.EOF);
769 expect(openParen.endToken, same(closeParen));
770 }
771
772 void test_mismatched_opener() {
773 // When openers and closers are mismatched, analyzer favors considering the
774 // closer to be mismatched, which means that `([)` parses as three unmatched
775 // tokens.
776 var openParen = _scan('([)') as BeginToken;
777 var openBracket = openParen.next as BeginToken;
778 var closeParen = openBracket.next;
779 expect(closeParen.next.type, TokenType.EOF);
780 expect(openParen.endToken, isNull);
781 expect(openBracket.endToken, isNull);
782 }
783
784 void test_mismatched_opener_in_interpolation() {
785 // In an interpolation expression, analyzer considers a closing `}` to
786 // always matche the preceding unmatched `{`, even if there are intervening
Siggi Cherem (dart-lang) 2017/02/15 00:23:10 matche => match
787 // unmatched tokens, which means that `"${({(}}"` parses as though the open
788 // parens are unmatched but everything else is matched.
789 var stringStart = _scan(r'"${({(}}"');
790 var interpolationStart = stringStart.next as BeginToken;
791 var openParen1 = interpolationStart.next as BeginToken;
792 var openBrace = openParen1.next as BeginToken;
793 var openParen2 = openBrace.next as BeginToken;
794 var closeBrace = openParen2.next;
795 var interpolationEnd = closeBrace.next;
796 var stringEnd = interpolationEnd.next;
797 expect(stringEnd.next.type, TokenType.EOF);
798 expect(interpolationStart.endToken, same(interpolationEnd));
799 expect(openParen1.endToken, isNull);
800 expect(openBrace.endToken, same(closeBrace));
801 expect(openParen2.endToken, isNull);
802 }
803
712 void test_open_curly_bracket() { 804 void test_open_curly_bracket() {
713 _assertToken(TokenType.OPEN_CURLY_BRACKET, "{"); 805 _assertToken(TokenType.OPEN_CURLY_BRACKET, "{");
714 } 806 }
715 807
716 void test_open_paren() { 808 void test_open_paren() {
717 _assertToken(TokenType.OPEN_PAREN, "("); 809 _assertToken(TokenType.OPEN_PAREN, "(");
718 } 810 }
719 811
720 void test_open_square_bracket() { 812 void test_open_square_bracket() {
721 _assertToken(TokenType.OPEN_SQUARE_BRACKET, "["); 813 _assertToken(TokenType.OPEN_SQUARE_BRACKET, "[");
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
1087 1179
1088 void test_tilde_slash_eq() { 1180 void test_tilde_slash_eq() {
1089 _assertToken(TokenType.TILDE_SLASH_EQ, "~/="); 1181 _assertToken(TokenType.TILDE_SLASH_EQ, "~/=");
1090 } 1182 }
1091 1183
1092 void test_unclosedPairInInterpolation() { 1184 void test_unclosedPairInInterpolation() {
1093 ErrorListener listener = new ErrorListener(); 1185 ErrorListener listener = new ErrorListener();
1094 scanWithListener("'\${(}'", listener); 1186 scanWithListener("'\${(}'", listener);
1095 } 1187 }
1096 1188
1189 void test_unmatched_openers() {
1190 var openBrace = _scan('{[(') as BeginToken;
1191 var openBracket = openBrace.next as BeginToken;
1192 var openParen = openBracket.next as BeginToken;
1193 expect(openParen.next.type, TokenType.EOF);
1194 expect(openBrace.endToken, isNull);
1195 expect(openBracket.endToken, isNull);
1196 expect(openParen.endToken, isNull);
1197 }
1198
1097 void _assertComment(TokenType commentType, String source, 1199 void _assertComment(TokenType commentType, String source,
1098 {bool genericMethodComments: false}) { 1200 {bool genericMethodComments: false}) {
1099 // 1201 //
1100 // Test without a trailing end-of-line marker 1202 // Test without a trailing end-of-line marker
1101 // 1203 //
1102 Token token = _scan(source, genericMethodComments: genericMethodComments); 1204 Token token = _scan(source, genericMethodComments: genericMethodComments);
1103 expect(token, isNotNull); 1205 expect(token, isNotNull);
1104 expect(token.type, TokenType.EOF); 1206 expect(token.type, TokenType.EOF);
1105 Token comment = token.precedingComments; 1207 Token comment = token.precedingComments;
1106 expect(comment, isNotNull); 1208 expect(comment, isNotNull);
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
1411 _TestScanner(CharacterReader reader, [this.listener]) : super(reader); 1513 _TestScanner(CharacterReader reader, [this.listener]) : super(reader);
1412 1514
1413 @override 1515 @override
1414 void reportError( 1516 void reportError(
1415 ScannerErrorCode errorCode, int offset, List<Object> arguments) { 1517 ScannerErrorCode errorCode, int offset, List<Object> arguments) {
1416 if (listener != null) { 1518 if (listener != null) {
1417 listener.errors.add(new TestError(offset, errorCode, arguments)); 1519 listener.errors.add(new TestError(offset, errorCode, arguments));
1418 } 1520 }
1419 } 1521 }
1420 } 1522 }
OLDNEW
« pkg/front_end/test/scanner_fasta_test.dart ('K') | « pkg/front_end/test/scanner_fasta_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698