OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 /** | 5 /** |
6 * An object used by the scanner to read the characters to be scanned. | 6 * An object used by the scanner to read the characters to be scanned. |
7 */ | 7 */ |
8 abstract class CharacterReader { | 8 abstract class CharacterReader { |
9 /** | 9 /** |
10 * The current offset relative to the beginning of the source. Return the | 10 * The current offset relative to the beginning of the source. Return the |
(...skipping 24 matching lines...) Expand all Loading... |
35 * location to be excluded if the offset is negative. | 35 * location to be excluded if the offset is negative. |
36 */ | 36 */ |
37 String getString(int start, int endDelta); | 37 String getString(int start, int endDelta); |
38 | 38 |
39 /** | 39 /** |
40 * Return the character at the current position without changing the current | 40 * Return the character at the current position without changing the current |
41 * position. | 41 * position. |
42 */ | 42 */ |
43 int peek(); | 43 int peek(); |
44 } | 44 } |
| 45 |
| 46 /** |
| 47 * A [CharacterReader] that reads characters from a character sequence. |
| 48 */ |
| 49 class CharSequenceReader implements CharacterReader { |
| 50 /** |
| 51 * The sequence from which characters will be read. |
| 52 */ |
| 53 final String _sequence; |
| 54 |
| 55 /** |
| 56 * The number of characters in the string. |
| 57 */ |
| 58 int _stringLength; |
| 59 |
| 60 /** |
| 61 * The index, relative to the string, of the next character to be read. |
| 62 */ |
| 63 int _charOffset; |
| 64 |
| 65 /** |
| 66 * Initialize a newly created reader to read the characters in the given |
| 67 * [_sequence]. |
| 68 */ |
| 69 CharSequenceReader(this._sequence) { |
| 70 this._stringLength = _sequence.length; |
| 71 this._charOffset = 0; |
| 72 } |
| 73 |
| 74 @override |
| 75 int get offset => _charOffset - 1; |
| 76 |
| 77 @override |
| 78 void set offset(int offset) { |
| 79 _charOffset = offset + 1; |
| 80 } |
| 81 |
| 82 @override |
| 83 int advance() { |
| 84 if (_charOffset >= _stringLength) { |
| 85 return -1; |
| 86 } |
| 87 return _sequence.codeUnitAt(_charOffset++); |
| 88 } |
| 89 |
| 90 @override |
| 91 String getString(int start, int endDelta) => |
| 92 _sequence.substring(start, _charOffset + endDelta); |
| 93 |
| 94 @override |
| 95 int peek() { |
| 96 if (_charOffset >= _stringLength) { |
| 97 return -1; |
| 98 } |
| 99 return _sequence.codeUnitAt(_charOffset); |
| 100 } |
| 101 } |
| 102 |
| 103 /** |
| 104 * A [CharacterReader] that reads characters from a character sequence, but adds |
| 105 * a delta when reporting the current character offset so that the character |
| 106 * sequence can be a subsequence from a larger sequence. |
| 107 */ |
| 108 class SubSequenceReader extends CharSequenceReader { |
| 109 /** |
| 110 * The offset from the beginning of the file to the beginning of the source |
| 111 * being scanned. |
| 112 */ |
| 113 final int _offsetDelta; |
| 114 |
| 115 /** |
| 116 * Initialize a newly created reader to read the characters in the given |
| 117 * [sequence]. The [_offsetDelta] is the offset from the beginning of the file |
| 118 * to the beginning of the source being scanned |
| 119 */ |
| 120 SubSequenceReader(String sequence, this._offsetDelta) : super(sequence); |
| 121 |
| 122 @override |
| 123 int get offset => _offsetDelta + super.offset; |
| 124 |
| 125 @override |
| 126 void set offset(int offset) { |
| 127 super.offset = offset - _offsetDelta; |
| 128 } |
| 129 |
| 130 @override |
| 131 String getString(int start, int endDelta) => |
| 132 super.getString(start - _offsetDelta, endDelta); |
| 133 } |
OLD | NEW |