| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 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 | 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/fasta/deprecated_problems.dart'; | 5 import '../../scanner/token.dart' show BeginToken, Token; |
| 6 import 'package:front_end/src/scanner/token.dart' show BeginToken, Token; | 6 |
| 7 import '../problems.dart' show internalProblem; |
| 8 |
| 9 import '../fasta_codes.dart' show messageInternalProblemPreviousTokenNotFound; |
| 7 | 10 |
| 8 /// Provides the capability of inserting tokens into a token stream by rewriting | 11 /// Provides the capability of inserting tokens into a token stream by rewriting |
| 9 /// the previous token to point to the inserted token. | 12 /// the previous token to point to the inserted token. |
| 10 /// | 13 /// |
| 11 /// This class has been designed to take advantage of "previousToken" pointers | 14 /// This class has been designed to take advantage of "previousToken" pointers |
| 12 /// when they are present, but not to depend on them. When they are not | 15 /// when they are present, but not to depend on them. When they are not |
| 13 /// present, it uses heuristics to try to find the find the previous token as | 16 /// present, it uses heuristics to try to find the find the previous token as |
| 14 /// quickly as possible by walking through tokens starting at the start of the | 17 /// quickly as possible by walking through tokens starting at the start of the |
| 15 /// file. | 18 /// file. |
| 16 class TokenStreamRewriter { | 19 class TokenStreamRewriter { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 // if it makes sense to do so. | 70 // if it makes sense to do so. |
| 68 if (_lastPreviousToken != null && | 71 if (_lastPreviousToken != null && |
| 69 target.charOffset >= _lastPreviousToken.charOffset) { | 72 target.charOffset >= _lastPreviousToken.charOffset) { |
| 70 Token previous = _scanForPreviousToken(target, _lastPreviousToken); | 73 Token previous = _scanForPreviousToken(target, _lastPreviousToken); |
| 71 if (previous != null) return previous; | 74 if (previous != null) return previous; |
| 72 } | 75 } |
| 73 | 76 |
| 74 // Otherwise scan forward from the start of the token stream. | 77 // Otherwise scan forward from the start of the token stream. |
| 75 Token previous = _scanForPreviousToken(target, _head); | 78 Token previous = _scanForPreviousToken(target, _head); |
| 76 if (previous == null) { | 79 if (previous == null) { |
| 77 deprecated_internalProblem('Could not find previous token'); | 80 internalProblem( |
| 81 messageInternalProblemPreviousTokenNotFound, target.charOffset, null); |
| 78 } | 82 } |
| 79 return previous; | 83 return previous; |
| 80 } | 84 } |
| 81 | 85 |
| 82 /// Searches for the token that immediately precedes [target], using [pos] as | 86 /// Searches for the token that immediately precedes [target], using [pos] as |
| 83 /// a starting point. | 87 /// a starting point. |
| 84 /// | 88 /// |
| 85 /// Uses heuristics to skip matching `{}`, `[]`, `()`, and `<>` if possible. | 89 /// Uses heuristics to skip matching `{}`, `[]`, `()`, and `<>` if possible. |
| 86 /// | 90 /// |
| 87 /// If no such token is found, returns `null`. | 91 /// If no such token is found, returns `null`. |
| 88 Token _scanForPreviousToken(Token target, Token pos) { | 92 Token _scanForPreviousToken(Token target, Token pos) { |
| 89 while (!identical(pos.next, target)) { | 93 while (!identical(pos.next, target)) { |
| 90 Token nextPos; | 94 Token nextPos; |
| 91 if (pos is BeginToken && | 95 if (pos is BeginToken && |
| 92 pos.endGroup != null && | 96 pos.endGroup != null && |
| 93 pos.endGroup.charOffset < target.charOffset) { | 97 pos.endGroup.charOffset < target.charOffset) { |
| 94 nextPos = pos.endGroup; | 98 nextPos = pos.endGroup; |
| 95 } else { | 99 } else { |
| 96 nextPos = pos.next; | 100 nextPos = pos.next; |
| 97 if (nextPos == null || nextPos.charOffset > target.charOffset) { | 101 if (nextPos == null || nextPos.charOffset > target.charOffset) { |
| 98 return null; | 102 return null; |
| 99 } | 103 } |
| 100 } | 104 } |
| 101 pos = nextPos; | 105 pos = nextPos; |
| 102 } | 106 } |
| 103 return pos; | 107 return pos; |
| 104 } | 108 } |
| 105 } | 109 } |
| OLD | NEW |