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/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 /// |
(...skipping 15 matching lines...) Expand all Loading... |
26 /// Creates a [TokenStreamRewriter] which is prepared to rewrite the token | 26 /// Creates a [TokenStreamRewriter] which is prepared to rewrite the token |
27 /// stream whose first token is [firstToken]. | 27 /// stream whose first token is [firstToken]. |
28 TokenStreamRewriter(Token firstToken) | 28 TokenStreamRewriter(Token firstToken) |
29 : _head = firstToken.previous ?? (new Token.eof(-1)..next = firstToken); | 29 : _head = firstToken.previous ?? (new Token.eof(-1)..next = firstToken); |
30 | 30 |
31 /// Gets the first token in the stream (which may not be the same token that | 31 /// Gets the first token in the stream (which may not be the same token that |
32 /// was passed to the constructor, if something was inserted before it). | 32 /// was passed to the constructor, if something was inserted before it). |
33 Token get firstToken => _head.next; | 33 Token get firstToken => _head.next; |
34 | 34 |
35 /// Inserts [newToken] into the token stream just before [insertionPoint], and | 35 /// Inserts [newToken] into the token stream just before [insertionPoint], and |
36 /// fixes up all "next" and "previous" pointers. | 36 /// fixes up all "next" and "previous" pointers. Returns [newToken]. |
37 /// | 37 /// |
38 /// Caller is required to ensure that [insertionPoint] is actually present in | 38 /// Caller is required to ensure that [insertionPoint] is actually present in |
39 /// the token stream. | 39 /// the token stream. |
40 void insertTokenBefore(Token newToken, Token insertionPoint) { | 40 Token insertTokenBefore(Token newToken, Token insertionPoint) { |
41 Token previous = _findPreviousToken(insertionPoint); | 41 Token previous = _findPreviousToken(insertionPoint); |
42 _lastPreviousToken = previous; | 42 _lastPreviousToken = previous; |
43 newToken.next = insertionPoint; | 43 newToken.next = insertionPoint; |
44 previous.next = newToken; | 44 previous.next = newToken; |
45 { | 45 { |
46 // Note: even though previousToken is deprecated, we need to hook it up in | 46 // Note: even though previousToken is deprecated, we need to hook it up in |
47 // case any uses of it remain. Once previousToken is removed it should be | 47 // case any uses of it remain. Once previousToken is removed it should be |
48 // safe to remove this block of code. | 48 // safe to remove this block of code. |
49 insertionPoint.previous = newToken; | 49 insertionPoint.previous = newToken; |
50 newToken.previous = previous; | 50 newToken.previous = previous; |
51 } | 51 } |
| 52 return newToken; |
52 } | 53 } |
53 | 54 |
54 /// Finds the token that immediately precedes [target]. | 55 /// Finds the token that immediately precedes [target]. |
55 Token _findPreviousToken(Token target) { | 56 Token _findPreviousToken(Token target) { |
56 // First see if the target has a previous token pointer. If it does, then | 57 // 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 | 58 // 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 | 59 // 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 | 60 // 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. | 61 // remove the "if" test below, and always use the fallback code. |
61 if (target.previous != null) { | 62 if (target.previous != null) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 nextPos = pos.next; | 96 nextPos = pos.next; |
96 if (nextPos == null || nextPos.charOffset > target.charOffset) { | 97 if (nextPos == null || nextPos.charOffset > target.charOffset) { |
97 return null; | 98 return null; |
98 } | 99 } |
99 } | 100 } |
100 pos = nextPos; | 101 pos = nextPos; |
101 } | 102 } |
102 return pos; | 103 return pos; |
103 } | 104 } |
104 } | 105 } |
OLD | NEW |