| 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/errors.dart'; |
| 6 import 'package:front_end/src/fasta/scanner/precedence.dart'; | 6 import 'package:front_end/src/fasta/scanner/precedence.dart'; |
| 7 import 'package:front_end/src/fasta/scanner/token.dart'; | 7 import 'package:front_end/src/fasta/scanner/token.dart'; |
| 8 | 8 |
| 9 /// Provides the capability of inserting tokens into a token stream by rewriting | 9 /// Provides the capability of inserting tokens into a token stream by rewriting |
| 10 /// the previous token to point to the inserted token. | 10 /// the previous token to point to the inserted token. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 /// Inserts [newToken] into the token stream just before [insertionPoint], and | 37 /// Inserts [newToken] into the token stream just before [insertionPoint], and |
| 38 /// fixes up all "next" and "previous" pointers. | 38 /// fixes up all "next" and "previous" pointers. |
| 39 /// | 39 /// |
| 40 /// Caller is required to ensure that [insertionPoint] is actually present in | 40 /// Caller is required to ensure that [insertionPoint] is actually present in |
| 41 /// the token stream. | 41 /// the token stream. |
| 42 void insertTokenBefore(Token newToken, Token insertionPoint) { | 42 void insertTokenBefore(Token newToken, Token insertionPoint) { |
| 43 Token previous = _findPreviousToken(insertionPoint); | 43 Token previous = _findPreviousToken(insertionPoint); |
| 44 _lastPreviousToken = previous; | 44 _lastPreviousToken = previous; |
| 45 newToken.next = insertionPoint; | 45 newToken.next = insertionPoint; |
| 46 previous.next = newToken; | 46 previous.next = newToken; |
| 47 // Note: even though previousToken is deprecated, we need to hook it up in | 47 { |
| 48 // case any uses of it remain. Once previousToken is removed it should be | 48 // Note: even though previousToken is deprecated, we need to hook it up in |
| 49 // safe to remove the code below. | 49 // case any uses of it remain. Once previousToken is removed it should be |
| 50 insertionPoint.previousToken = newToken; | 50 // safe to remove this block of code. |
| 51 newToken.previousToken = previous; | 51 insertionPoint.previousToken = newToken; |
| 52 newToken.previousToken = previous; |
| 53 } |
| 52 } | 54 } |
| 53 | 55 |
| 54 /// Finds the token that immediately precedes [target]. | 56 /// Finds the token that immediately precedes [target]. |
| 55 Token _findPreviousToken(Token target) { | 57 Token _findPreviousToken(Token target) { |
| 56 // First see if the target has a previous token pointer. If it does, then | 58 // First see if the target has a previous token pointer. If it does, then |
| 57 // we can find the previous token with no extra effort. Note: it's ok that | 59 // we can find the previous token with no extra effort. Note: it's ok that |
| 58 // we're accessing the deprecated member previousToken here, because we have | 60 // we're accessing the deprecated member previousToken here, because we have |
| 59 // a fallback if it is not available. Once previousToken is removed, we can | 61 // a fallback if it is not available. Once previousToken is removed, we can |
| 60 // remove the "if" test below, and always use the fallback code. | 62 // remove the "if" test below, and always use the fallback code. |
| 61 if (target.previousToken != null) { | 63 if (target.previousToken != null) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 74 Token previous = _scanForPreviousToken(target, _head); | 76 Token previous = _scanForPreviousToken(target, _head); |
| 75 if (previous == null) { | 77 if (previous == null) { |
| 76 internalError('Could not find previous token'); | 78 internalError('Could not find previous token'); |
| 77 } | 79 } |
| 78 return previous; | 80 return previous; |
| 79 } | 81 } |
| 80 | 82 |
| 81 /// Searches for the token that immediately precedes [target], using [pos] as | 83 /// Searches for the token that immediately precedes [target], using [pos] as |
| 82 /// a starting point. | 84 /// a starting point. |
| 83 /// | 85 /// |
| 84 /// Uses heuristics to skip matching {}, [], (), and <> if possible. | 86 /// Uses heuristics to skip matching `{}`, `[]`, `()`, and `<>` if possible. |
| 85 /// | 87 /// |
| 86 /// If no such token is found, returns `null`. | 88 /// If no such token is found, returns `null`. |
| 87 Token _scanForPreviousToken(Token target, Token pos) { | 89 Token _scanForPreviousToken(Token target, Token pos) { |
| 88 while (!identical(pos.next, target)) { | 90 while (!identical(pos.next, target)) { |
| 89 Token nextPos; | 91 Token nextPos; |
| 90 if (pos is BeginGroupToken && | 92 if (pos is BeginGroupToken && |
| 91 pos.endGroup != null && | 93 pos.endGroup != null && |
| 92 pos.endGroup.charOffset < target.charOffset) { | 94 pos.endGroup.charOffset < target.charOffset) { |
| 93 nextPos = pos.endGroup; | 95 nextPos = pos.endGroup; |
| 94 } else { | 96 } else { |
| 95 nextPos = pos.next; | 97 nextPos = pos.next; |
| 96 if (nextPos == null || nextPos.charOffset > target.charOffset) { | 98 if (nextPos == null || nextPos.charOffset > target.charOffset) { |
| 97 return null; | 99 return null; |
| 98 } | 100 } |
| 99 } | 101 } |
| 100 pos = nextPos; | 102 pos = nextPos; |
| 101 } | 103 } |
| 102 return pos; | 104 return pos; |
| 103 } | 105 } |
| 104 } | 106 } |
| OLD | NEW |