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

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

Issue 2758113002: fasta.PrecedenceInfo implement analyzer.TokenType (Closed)
Patch Set: merge Created 3 years, 9 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
« no previous file with comments | « pkg/front_end/lib/src/scanner/token.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
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.
4
5 import 'package:front_end/src/fasta/scanner/precedence.dart';
6 import 'package:front_end/src/fasta/scanner/string_scanner.dart';
7 import 'package:front_end/src/fasta/scanner/token.dart' as fasta;
8 import 'package:front_end/src/scanner/token.dart';
9 import 'package:test/test.dart';
10 import 'package:test_reflective_loader/test_reflective_loader.dart';
11
12 main() {
13 defineReflectiveSuite(() {
14 defineReflectiveTests(PrecedenceInfoTest);
15 });
16 }
17
18 /// Assert that fasta PrecedenceInfo implements analyzer TokenType.
19 @reflectiveTest
20 class PrecedenceInfoTest {
21 var allTokenTypes = new Set<TokenType>.from(const [
22 TokenType.EOF,
23 TokenType.DOUBLE,
24 TokenType.HEXADECIMAL,
25 TokenType.IDENTIFIER,
26 TokenType.INT,
27 TokenType.KEYWORD,
28 TokenType.MULTI_LINE_COMMENT,
29 TokenType.SCRIPT_TAG,
30 TokenType.SINGLE_LINE_COMMENT,
31 TokenType.STRING,
32 TokenType.AMPERSAND,
33 TokenType.AMPERSAND_AMPERSAND,
34 TokenType.AMPERSAND_EQ,
35 TokenType.AT,
36 TokenType.BANG,
37 TokenType.BANG_EQ,
38 TokenType.BAR,
39 TokenType.BAR_BAR,
40 TokenType.BAR_EQ,
41 TokenType.COLON,
42 TokenType.COMMA,
43 TokenType.CARET,
44 TokenType.CARET_EQ,
45 TokenType.CLOSE_CURLY_BRACKET,
46 TokenType.CLOSE_PAREN,
47 TokenType.CLOSE_SQUARE_BRACKET,
48 TokenType.EQ,
49 TokenType.EQ_EQ,
50 TokenType.FUNCTION,
51 TokenType.GT,
52 TokenType.GT_EQ,
53 TokenType.GT_GT,
54 TokenType.GT_GT_EQ,
55 TokenType.HASH,
56 TokenType.INDEX,
57 TokenType.INDEX_EQ,
58 TokenType.LT,
59 TokenType.LT_EQ,
60 TokenType.LT_LT,
61 TokenType.LT_LT_EQ,
62 TokenType.MINUS,
63 TokenType.MINUS_EQ,
64 TokenType.MINUS_MINUS,
65 TokenType.OPEN_CURLY_BRACKET,
66 TokenType.OPEN_PAREN,
67 TokenType.OPEN_SQUARE_BRACKET,
68 TokenType.PERCENT,
69 TokenType.PERCENT_EQ,
70 TokenType.PERIOD,
71 TokenType.PERIOD_PERIOD,
72 TokenType.PLUS,
73 TokenType.PLUS_EQ,
74 TokenType.PLUS_PLUS,
75 TokenType.QUESTION,
76 TokenType.QUESTION_PERIOD,
77 TokenType.QUESTION_QUESTION,
78 TokenType.QUESTION_QUESTION_EQ,
79 TokenType.SEMICOLON,
80 TokenType.SLASH,
81 TokenType.SLASH_EQ,
82 TokenType.STAR,
83 TokenType.STAR_EQ,
84 TokenType.STRING_INTERPOLATION_EXPRESSION,
85 TokenType.STRING_INTERPOLATION_IDENTIFIER,
86 TokenType.TILDE,
87 TokenType.TILDE_SLASH,
88 TokenType.TILDE_SLASH_EQ,
89 TokenType.BACKPING,
90 TokenType.BACKSLASH,
91 TokenType.PERIOD_PERIOD_PERIOD,
92 TokenType.GENERIC_METHOD_TYPE_LIST,
93 TokenType.GENERIC_METHOD_TYPE_ASSIGN,
94
95 // These are not yet part of the language and not supported by fasta
96 //TokenType.AMPERSAND_AMPERSAND_EQ,
97 //TokenType.BAR_BAR_EQ,
98 ]);
99
100 void assertInfo(check(String source, fasta.Token token),
101 {bool includeLazyAssignmentOperators: true}) {
102 void assertLexeme(String source) {
103 if (source == null || source.isEmpty) return;
104 var scanner = new StringScanner(source, includeComments: true);
105 var token = scanner.tokenize();
106 check(source, token);
107 }
108
109 for (PrecedenceInfo info in PrecedenceInfo.all) {
110 assertLexeme(info.value);
111 }
112 for (TokenType tt in allTokenTypes) {
113 assertLexeme(tt.lexeme);
114 }
115 if (includeLazyAssignmentOperators) {
116 assertLexeme('&&=');
117 assertLexeme('||=');
118 }
119 }
120
121 void test_isOperator() {
122 var operatorLexemes = new Set<String>.from(const [
123 '&',
124 '&&',
125 '&&=',
126 '&=',
127 '!',
128 '!=',
129 '|',
130 '||',
131 '||=',
132 '|=',
133 '^',
134 '^=',
135 '=',
136 '==',
137 '>',
138 '>=',
139 '>>',
140 '>>=',
141 '[]',
142 '[]=',
143 '<',
144 '<=',
145 '<<',
146 '<<=',
147 '-',
148 '-=',
149 '--',
150 '%',
151 '%=',
152 '..',
153 '+',
154 '+=',
155 '++',
156 '?',
157 '?.',
158 '??',
159 '??=',
160 '/',
161 '/=',
162 '*',
163 '*=',
164 '~',
165 '~/',
166 '~/=',
167 ]);
168
169 assertInfo((String source, fasta.Token token) {
170 expect(token.isOperator, operatorLexemes.contains(source),
171 reason: source);
172 expect(token.type.isOperator, operatorLexemes.contains(source),
173 reason: source);
174 });
175 }
176
177 void test_isAdditiveOperator() {
178 var additiveLexemes = [
179 '-',
180 '+',
181 ];
182 assertInfo((String source, fasta.Token token) {
183 expect(token.type.isAdditiveOperator, additiveLexemes.contains(source),
184 reason: source);
185 });
186 }
187
188 void test_isAssignmentOperator() {
189 const assignmentLexemes = const [
190 '&=',
191 '|=',
192 '^=',
193 '=',
194 '>>=',
195 '<<=',
196 '-=',
197 '%=',
198 '+=',
199 '??=',
200 '/=',
201 '*=',
202 '~/=',
203 ];
204 assertInfo((String source, fasta.Token token) {
205 expect(
206 token.type.isAssignmentOperator, assignmentLexemes.contains(source),
207 reason: source);
208 });
209 }
210
211 void test_isAssociativeOperator() {
212 const associativeLexemes = const [
213 '&',
214 '&&',
215 '|',
216 '||',
217 '^',
218 '+',
219 '*',
220 ];
221 assertInfo((String source, fasta.Token token) {
222 expect(
223 token.type.isAssociativeOperator, associativeLexemes.contains(source),
224 reason: source);
225 }, includeLazyAssignmentOperators: false);
226 }
227
228 void test_isEqualityOperator() {
229 const equalityLexemes = const [
230 '!=',
231 '==',
232 ];
233 assertInfo((String source, fasta.Token token) {
234 expect(token.type.isEqualityOperator, equalityLexemes.contains(source),
235 reason: source);
236 });
237 }
238
239 void test_isIncrementOperator() {
240 const incrementLexemes = const [
241 '--',
242 '++',
243 ];
244 assertInfo((String source, fasta.Token token) {
245 expect(token.type.isIncrementOperator, incrementLexemes.contains(source),
246 reason: source);
247 });
248 }
249
250 void test_isMultiplicativeOperator() {
251 const multiplicativeLexemes = const [
252 '%',
253 '/',
254 '*',
255 '~/',
256 ];
257 assertInfo((String source, fasta.Token token) {
258 expect(token.type.isMultiplicativeOperator,
259 multiplicativeLexemes.contains(source),
260 reason: source);
261 });
262 }
263
264 void test_isRelationalOperator() {
265 const relationalLexemes = const [
266 '>',
267 '>=',
268 '<',
269 '<=',
270 ];
271 assertInfo((String source, fasta.Token token) {
272 expect(
273 token.type.isRelationalOperator, relationalLexemes.contains(source),
274 reason: source);
275 });
276 }
277
278 void test_isShiftOperator() {
279 const shiftLexemes = const [
280 '>>',
281 '<<',
282 ];
283 assertInfo((String source, fasta.Token token) {
284 expect(token.type.isShiftOperator, shiftLexemes.contains(source),
285 reason: source);
286 });
287 }
288
289 void test_isUnaryPostfixOperator() {
290 const unaryPostfixLexemes = const [
291 '--',
292 '(',
293 '[',
294 '.',
295 '++',
296 '?.',
297 ];
298 assertInfo((String source, fasta.Token token) {
299 expect(token.type.isUnaryPostfixOperator,
300 unaryPostfixLexemes.contains(source),
301 reason: source);
302 });
303 }
304
305 void test_isUnaryPrefixOperator() {
306 const unaryPrefixLexemes = const [
307 '!',
308 '--',
309 '++',
310 '~',
311 ];
312 assertInfo((String source, fasta.Token token) {
313 expect(
314 token.type.isUnaryPrefixOperator, unaryPrefixLexemes.contains(source),
315 reason: source);
316 });
317 }
318
319 void test_isUserDefinableOperator() {
320 const userDefinableOperatorLexemes = const [
321 '&',
322 '|',
323 '^',
324 '==',
325 '>',
326 '>=',
327 '>>',
328 '[]',
329 '[]=',
330 '<',
331 '<=',
332 '<<',
333 '-',
334 '%',
335 '+',
336 '/',
337 '*',
338 '~',
339 '~/',
340 ];
341 assertInfo((String source, fasta.Token token) {
342 expect(token.type.isUserDefinableOperator,
343 userDefinableOperatorLexemes.contains(source),
344 reason: source);
345 expect(token.isUserDefinableOperator,
346 userDefinableOperatorLexemes.contains(source),
347 reason: source);
348 });
349 }
350
351 void test_name() {
352 void assertName(String source, String name, {int offset: 0}) {
353 if (source == null || source.isEmpty) return;
354 var scanner = new StringScanner(source, includeComments: true);
355 var token = scanner.tokenize();
356 while (token.offset < offset) {
357 token = token.next;
358 }
359 expect(token.type.name, name,
360 reason: 'source: $source\ntoken: ${token.lexeme}');
361 }
362
363 assertName('&', 'AMPERSAND');
364 assertName('&&', 'AMPERSAND_AMPERSAND');
365 assertName('&=', 'AMPERSAND_EQ');
366 assertName('@', 'AT');
367 assertName('!', 'BANG');
368 assertName('!=', 'BANG_EQ');
369 assertName('|', 'BAR');
370 assertName('||', 'BAR_BAR');
371 assertName('|=', 'BAR_EQ');
372 assertName(':', 'COLON');
373 assertName(',', 'COMMA');
374 assertName('^', 'CARET');
375 assertName('^=', 'CARET_EQ');
376 assertName('}', 'CLOSE_CURLY_BRACKET');
377 assertName(')', 'CLOSE_PAREN');
378 assertName(']', 'CLOSE_SQUARE_BRACKET');
379 assertName('=', 'EQ');
380 assertName('==', 'EQ_EQ');
381 assertName('=>', 'FUNCTION');
382 assertName('>', 'GT');
383 assertName('>=', 'GT_EQ');
384 assertName('>>', 'GT_GT');
385 assertName('>>=', 'GT_GT_EQ');
386 assertName('#', 'HASH');
387 assertName('[]', 'INDEX');
388 assertName('[]=', 'INDEX_EQ');
389 assertName('<', 'LT');
390 assertName('<=', 'LT_EQ');
391 assertName('<<', 'LT_LT');
392 assertName('<<=', 'LT_LT_EQ');
393 assertName('-', 'MINUS');
394 assertName('-=', 'MINUS_EQ');
395 assertName('--', 'MINUS_MINUS');
396 assertName('{', 'OPEN_CURLY_BRACKET');
397 assertName('(', 'OPEN_PAREN');
398 assertName('[', 'OPEN_SQUARE_BRACKET');
399 assertName('%', 'PERCENT');
400 assertName('%=', 'PERCENT_EQ');
401 assertName('.', 'PERIOD');
402 assertName('..', 'PERIOD_PERIOD');
403 assertName('+', 'PLUS');
404 assertName('+=', 'PLUS_EQ');
405 assertName('++', 'PLUS_PLUS');
406 assertName('?', 'QUESTION');
407 assertName('?.', 'QUESTION_PERIOD');
408 assertName('??', 'QUESTION_QUESTION');
409 assertName('??=', 'QUESTION_QUESTION_EQ');
410 assertName(';', 'SEMICOLON');
411 assertName('/', 'SLASH');
412 assertName('/=', 'SLASH_EQ');
413 assertName('*', 'STAR');
414 assertName('*=', 'STAR_EQ');
415 assertName('"\${', 'STRING_INTERPOLATION_EXPRESSION', offset: 1);
416 assertName('"\$', 'STRING_INTERPOLATION_IDENTIFIER', offset: 1);
417 assertName('~', 'TILDE');
418 assertName('~/', 'TILDE_SLASH');
419 assertName('~/=', 'TILDE_SLASH_EQ');
420 assertName('`', 'BACKPING');
421 assertName('\\', 'BACKSLASH');
422 assertName('...', 'PERIOD_PERIOD_PERIOD');
423 }
424
425 /// Assert precedence as per the Dart language spec
426 ///
427 /// Prefix "++" and "--" are excluded from the prefix (15) list
428 /// because they are interpreted as being in the postfix (16) list.
429 /// Leading "-" is excluded from the precedence 15 list
430 /// because it is interpreted as a minus token (precedence 13).
431 void test_precedence() {
432 const precedenceTable = const <int, List<String>>{
433 16: const <String>['.', '?.', '++', '--', '[', '('],
434 15: const <String>['!', '~'], // excluded '-', '++', '--'
435 14: const <String>['*', '/', '~/', '%'],
436 13: const <String>['+', '-'],
437 12: const <String>['<<', '>>'],
438 11: const <String>['&'],
439 10: const <String>['^'],
440 9: const <String>['|'],
441 8: const <String>['<', '>', '<=', '>=', 'as', 'is', 'is!'],
442 7: const <String>['==', '!='],
443 6: const <String>['&&'],
444 5: const <String>['||'],
445 4: const <String>['??'],
446 3: const <String>['? :'],
447 2: const <String>['..'],
448 1: const <String>['=', '*=', '/=', '+=', '-=', '&=', '^='],
449 };
450 precedenceTable.forEach((precedence, lexemes) {
451 for (String source in lexemes) {
452 var scanner = new StringScanner(source, includeComments: true);
453 var token = scanner.tokenize();
454 expect(token.type.precedence, precedence, reason: source);
455 }
456 });
457 }
458 }
OLDNEW
« no previous file with comments | « pkg/front_end/lib/src/scanner/token.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698