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

Side by Side Diff: pkg/front_end/lib/src/fasta/scanner/token.dart

Issue 2794653002: fasta tokens implement analyzer token API (Closed)
Patch Set: merge Created 3 years, 8 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 library fasta.scanner.token; 5 library fasta.scanner.token;
6 6
7 import '../../scanner/token.dart' as analyzer; 7 import '../../scanner/token.dart' as analyzer;
8 8
9 import 'keyword.dart' show Keyword; 9 import 'keyword.dart' show Keyword;
10 10
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 SymbolToken(this.info, int charOffset) : super(charOffset); 257 SymbolToken(this.info, int charOffset) : super(charOffset);
258 258
259 factory SymbolToken.eof(int charOffset) { 259 factory SymbolToken.eof(int charOffset) {
260 var eof = new SyntheticSymbolToken(EOF_INFO, charOffset); 260 var eof = new SyntheticSymbolToken(EOF_INFO, charOffset);
261 // EOF points to itself so there's always infinite look-ahead. 261 // EOF points to itself so there's always infinite look-ahead.
262 eof.previousToken = eof; 262 eof.previousToken = eof;
263 eof.next = eof; 263 eof.next = eof;
264 return eof; 264 return eof;
265 } 265 }
266 266
267 @override
267 String get lexeme => info.value; 268 String get lexeme => info.value;
268 269
270 @override
269 String get stringValue => info.value; 271 String get stringValue => info.value;
270 272
273 @override
271 bool isIdentifier() => false; 274 bool isIdentifier() => false;
272 275
273 String toString() => "SymbolToken(${info == EOF_INFO ? '-eof-' : lexeme})"; 276 @override
277 String toString() => "SymbolToken(${isEof ? '-eof-' : lexeme})";
274 278
279 @override
275 bool get isEof => info == EOF_INFO; 280 bool get isEof => info == EOF_INFO;
276 281
277 @override 282 @override
278 Token copyWithoutComments() => new SymbolToken(info, charOffset); 283 Token copyWithoutComments() => isEof
284 ? new SymbolToken.eof(charOffset)
285 : new SymbolToken(info, charOffset);
279 } 286 }
280 287
281 /** 288 /**
282 * A [SyntheticSymbolToken] represents the symbol in its precedence info 289 * A [SyntheticSymbolToken] represents the symbol in its precedence info
283 * which does not exist in the original source. 290 * which does not exist in the original source.
284 * For example, if the scanner finds '(' missing a ')' 291 * For example, if the scanner finds '(' missing a ')'
285 * then it will insert an synthetic ')'. 292 * then it will insert an synthetic ')'.
286 */ 293 */
287 class SyntheticSymbolToken extends SymbolToken { 294 class SyntheticSymbolToken extends SymbolToken {
288 SyntheticSymbolToken(PrecedenceInfo info, int charOffset) 295 SyntheticSymbolToken(PrecedenceInfo info, int charOffset)
289 : super(info, charOffset); 296 : super(info, charOffset);
290 297
291 @override 298 @override
292 int get charCount => 0; 299 int get charCount => 0;
293 300
294 @override 301 @override
295 bool get isSynthetic => true; 302 bool get isSynthetic => true;
303
304 @override
305 Token copyWithoutComments() => isEof
306 ? new SymbolToken.eof(charOffset)
307 : new SyntheticSymbolToken(info, charOffset);
296 } 308 }
297 309
298 /** 310 /**
299 * A [BeginGroupToken] represents a symbol that may be the beginning of 311 * A [BeginGroupToken] represents a symbol that may be the beginning of
300 * a pair of brackets, i.e., ( { [ < or ${ 312 * a pair of brackets, i.e., ( { [ < or ${
301 * The [endGroup] token points to the matching closing bracked in case 313 * The [endGroup] token points to the matching closing bracked in case
302 * it can be identified during scanning. 314 * it can be identified during scanning.
303 */ 315 */
304 class BeginGroupToken extends SymbolToken implements analyzer.BeginToken { 316 class BeginGroupToken extends SymbolToken
317 implements analyzer.BeginTokenWithComment {
305 Token endGroup; 318 Token endGroup;
306 319
307 BeginGroupToken(PrecedenceInfo info, int charOffset) 320 BeginGroupToken(PrecedenceInfo info, int charOffset)
308 : super(info, charOffset); 321 : super(info, charOffset);
309 322
310 @override 323 @override
311 analyzer.Token get endToken => endGroup; 324 analyzer.Token get endToken => endGroup;
312 325
313 @override 326 @override
314 void set endToken(analyzer.Token token) { 327 void set endToken(analyzer.Token token) {
315 endGroup = token; 328 endGroup = token;
316 } 329 }
330
331 @override
332 Token copyWithoutComments() => new BeginGroupToken(info, charOffset);
317 } 333 }
318 334
319 /** 335 /**
320 * A keyword token. 336 * A keyword token.
321 */ 337 */
322 class KeywordToken extends Token { 338 class KeywordToken extends Token implements analyzer.KeywordTokenWithComment {
323 final Keyword keyword; 339 final Keyword keyword;
324 340
325 KeywordToken(this.keyword, int charOffset) : super(charOffset); 341 KeywordToken(this.keyword, int charOffset) : super(charOffset);
326 342
327 PrecedenceInfo get info => keyword.info; 343 PrecedenceInfo get info => keyword.info;
328 344
329 String get lexeme => keyword.syntax; 345 String get lexeme => keyword.syntax;
330 346
331 String get stringValue => keyword.syntax; 347 String get stringValue => keyword.syntax;
332 348
333 bool isIdentifier() => keyword.isPseudo || keyword.isBuiltIn; 349 bool isIdentifier() => keyword.isPseudo || keyword.isBuiltIn;
334 350
335 bool get isPseudo => keyword.isPseudo; 351 bool get isPseudo => keyword.isPseudo;
336 352
337 bool get isBuiltInIdentifier => keyword.isBuiltIn; 353 bool get isBuiltInIdentifier => keyword.isBuiltIn;
338 354
339 String toString() => "KeywordToken($lexeme)"; 355 String toString() => "KeywordToken($lexeme)";
340 356
341 @override 357 @override
342 Token copyWithoutComments() => new KeywordToken(keyword, charOffset); 358 Token copyWithoutComments() => new KeywordToken(keyword, charOffset);
343 359
344 @override 360 @override
345 // Analyzer considers pseudo-keywords to have a different value 361 // Analyzer considers pseudo-keywords to have a different value
346 Object value() => isPseudo ? lexeme : keyword; 362 Keyword value() => isPseudo ? lexeme : keyword;
347 363
348 @override 364 @override
349 // Analyzer considers pseudo-keywords to be identifiers 365 // Analyzer considers pseudo-keywords to be identifiers
350 analyzer.TokenType get type => isPseudo ? IDENTIFIER_INFO : KEYWORD_INFO; 366 analyzer.TokenType get type => isPseudo ? IDENTIFIER_INFO : KEYWORD_INFO;
351 } 367 }
352 368
353 /** 369 /**
370 * A synthetic keyword token.
371 */
372 class SyntheticKeywordToken extends KeywordToken
373 implements analyzer.SyntheticKeywordToken {
374 /**
375 * Initialize a newly created token to represent the given [keyword] at the
376 * given [offset].
377 */
378 SyntheticKeywordToken(Keyword keyword, int offset) : super(keyword, offset);
379
380 @override
381 bool get isSynthetic => true;
382
383 @override
384 int get length => 0;
385
386 @override
387 Token copyWithoutComments() => new SyntheticKeywordToken(keyword, offset);
388 }
389
390 /**
354 * A String-valued token. Represents identifiers, string literals, 391 * A String-valued token. Represents identifiers, string literals,
355 * number literals, comments, and error tokens, using the corresponding 392 * number literals, comments, and error tokens, using the corresponding
356 * precedence info. 393 * precedence info.
357 */ 394 */
358 class StringToken extends Token implements analyzer.StringToken { 395 class StringToken extends Token implements analyzer.StringTokenWithComment {
359 /** 396 /**
360 * The length threshold above which substring tokens are computed lazily. 397 * The length threshold above which substring tokens are computed lazily.
361 * 398 *
362 * For string tokens that are substrings of the program source, the actual 399 * For string tokens that are substrings of the program source, the actual
363 * substring extraction is performed lazily. This is beneficial because 400 * substring extraction is performed lazily. This is beneficial because
364 * not all scanned code is actually used. For unused parts, the substrings 401 * not all scanned code is actually used. For unused parts, the substrings
365 * are never computed and allocated. 402 * are never computed and allocated.
366 */ 403 */
367 static const int LAZY_THRESHOLD = 4; 404 static const int LAZY_THRESHOLD = 4;
368 405
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 } 492 }
456 493
457 @override 494 @override
458 Token copyWithoutComments() => 495 Token copyWithoutComments() =>
459 new StringToken._(info, valueOrLazySubstring, charOffset); 496 new StringToken._(info, valueOrLazySubstring, charOffset);
460 497
461 @override 498 @override
462 String value() => lexeme; 499 String value() => lexeme;
463 } 500 }
464 501
502 /**
503 * A String-valued token that does not exist in the original source.
504 */
505 class SyntheticStringToken extends StringToken
506 implements analyzer.SyntheticStringToken {
507 SyntheticStringToken(PrecedenceInfo info, String value, int offset)
508 : super._(info, value, offset);
509
510 @override
511 bool get isSynthetic => true;
512
513 @override
514 int get length => 0;
515
516 @override
517 Token copyWithoutComments() =>
518 new SyntheticStringToken(info, valueOrLazySubstring, offset);
519 }
520
465 class CommentToken extends StringToken implements analyzer.CommentToken { 521 class CommentToken extends StringToken implements analyzer.CommentToken {
466 @override 522 @override
467 analyzer.TokenWithComment parent; 523 analyzer.TokenWithComment parent;
468 524
469 /** 525 /**
470 * Creates a lazy comment token. If [canonicalize] is true, the string 526 * Creates a lazy comment token. If [canonicalize] is true, the string
471 * is canonicalized before the token is created. 527 * is canonicalized before the token is created.
472 */ 528 */
473 CommentToken.fromSubstring( 529 CommentToken.fromSubstring(
474 PrecedenceInfo info, String data, int start, int end, int charOffset, 530 PrecedenceInfo info, String data, int start, int end, int charOffset,
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
637 identical(value, "<=") || 693 identical(value, "<=") ||
638 identical(value, "<") || 694 identical(value, "<") ||
639 identical(value, "&") || 695 identical(value, "&") ||
640 identical(value, "^") || 696 identical(value, "^") ||
641 identical(value, "|"); 697 identical(value, "|");
642 } 698 }
643 699
644 bool isTernaryOperator(String value) => identical(value, "[]="); 700 bool isTernaryOperator(String value) => identical(value, "[]=");
645 701
646 bool isMinusOperator(String value) => identical(value, "-"); 702 bool isMinusOperator(String value) => identical(value, "-");
OLDNEW
« no previous file with comments | « pkg/front_end/lib/src/fasta/scanner/precedence.dart ('k') | pkg/front_end/lib/src/scanner/scanner.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698