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