OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library analyzer.src.dart.scanner.reader; |
| 6 |
| 7 /** |
| 8 * A [CharacterReader] that reads a range of characters from another character |
| 9 * reader. |
| 10 */ |
| 11 class CharacterRangeReader extends CharacterReader { |
| 12 /** |
| 13 * The reader from which the characters are actually being read. |
| 14 */ |
| 15 final CharacterReader baseReader; |
| 16 |
| 17 /** |
| 18 * The last character to be read. |
| 19 */ |
| 20 final int endIndex; |
| 21 |
| 22 /** |
| 23 * Initialize a newly created reader to read the characters from the given |
| 24 * [baseReader] between the [startIndex] inclusive to [endIndex] exclusive. |
| 25 */ |
| 26 CharacterRangeReader(this.baseReader, int startIndex, this.endIndex) { |
| 27 baseReader.offset = startIndex - 1; |
| 28 } |
| 29 |
| 30 @override |
| 31 int get offset => baseReader.offset; |
| 32 |
| 33 @override |
| 34 void set offset(int offset) { |
| 35 baseReader.offset = offset; |
| 36 } |
| 37 |
| 38 @override |
| 39 int advance() { |
| 40 if (baseReader.offset + 1 >= endIndex) { |
| 41 return -1; |
| 42 } |
| 43 return baseReader.advance(); |
| 44 } |
| 45 |
| 46 @override |
| 47 String getString(int start, int endDelta) => |
| 48 baseReader.getString(start, endDelta); |
| 49 |
| 50 @override |
| 51 int peek() { |
| 52 if (baseReader.offset + 1 >= endIndex) { |
| 53 return -1; |
| 54 } |
| 55 return baseReader.peek(); |
| 56 } |
| 57 } |
| 58 |
| 59 /** |
| 60 * An object used by the scanner to read the characters to be scanned. |
| 61 */ |
| 62 abstract class CharacterReader { |
| 63 /** |
| 64 * The current offset relative to the beginning of the source. Return the |
| 65 * initial offset if the scanner has not yet scanned the source code, and one |
| 66 * (1) past the end of the source code if the entire source code has been |
| 67 * scanned. |
| 68 */ |
| 69 int get offset; |
| 70 |
| 71 /** |
| 72 * Set the current offset relative to the beginning of the source to the given |
| 73 * [offset]. The new offset must be between the initial offset and one (1) |
| 74 * past the end of the source code. |
| 75 */ |
| 76 void set offset(int offset); |
| 77 |
| 78 /** |
| 79 * Advance the current position and return the character at the new current |
| 80 * position. |
| 81 */ |
| 82 int advance(); |
| 83 |
| 84 /** |
| 85 * Return the substring of the source code between the [start] offset and the |
| 86 * modified current position. The current position is modified by adding the |
| 87 * [endDelta], which is the number of characters after the current location to |
| 88 * be included in the string, or the number of characters before the current |
| 89 * location to be excluded if the offset is negative. |
| 90 */ |
| 91 String getString(int start, int endDelta); |
| 92 |
| 93 /** |
| 94 * Return the character at the current position without changing the current |
| 95 * position. |
| 96 */ |
| 97 int peek(); |
| 98 } |
| 99 |
| 100 /** |
| 101 * A [CharacterReader] that reads characters from a character sequence. |
| 102 */ |
| 103 class CharSequenceReader implements CharacterReader { |
| 104 /** |
| 105 * The sequence from which characters will be read. |
| 106 */ |
| 107 final String _sequence; |
| 108 |
| 109 /** |
| 110 * The number of characters in the string. |
| 111 */ |
| 112 int _stringLength = 0; |
| 113 |
| 114 /** |
| 115 * The index, relative to the string, of the last character that was read. |
| 116 */ |
| 117 int _charOffset = 0; |
| 118 |
| 119 /** |
| 120 * Initialize a newly created reader to read the characters in the given |
| 121 * [_sequence]. |
| 122 */ |
| 123 CharSequenceReader(this._sequence) { |
| 124 this._stringLength = _sequence.length; |
| 125 this._charOffset = -1; |
| 126 } |
| 127 |
| 128 @override |
| 129 int get offset => _charOffset; |
| 130 |
| 131 @override |
| 132 void set offset(int offset) { |
| 133 _charOffset = offset; |
| 134 } |
| 135 |
| 136 @override |
| 137 int advance() { |
| 138 if (_charOffset + 1 >= _stringLength) { |
| 139 return -1; |
| 140 } |
| 141 return _sequence.codeUnitAt(++_charOffset); |
| 142 } |
| 143 |
| 144 @override |
| 145 String getString(int start, int endDelta) => |
| 146 _sequence.substring(start, _charOffset + 1 + endDelta).toString(); |
| 147 |
| 148 @override |
| 149 int peek() { |
| 150 if (_charOffset + 1 >= _stringLength) { |
| 151 return -1; |
| 152 } |
| 153 return _sequence.codeUnitAt(_charOffset + 1); |
| 154 } |
| 155 } |
| 156 |
| 157 /** |
| 158 * A [CharacterReader] that reads characters from a character sequence, but adds |
| 159 * a delta when reporting the current character offset so that the character |
| 160 * sequence can be a subsequence from a larger sequence. |
| 161 */ |
| 162 class SubSequenceReader extends CharSequenceReader { |
| 163 /** |
| 164 * The offset from the beginning of the file to the beginning of the source |
| 165 * being scanned. |
| 166 */ |
| 167 final int _offsetDelta; |
| 168 |
| 169 /** |
| 170 * Initialize a newly created reader to read the characters in the given |
| 171 * [sequence]. The [_offsetDelta] is the offset from the beginning of the file |
| 172 * to the beginning of the source being scanned |
| 173 */ |
| 174 SubSequenceReader(String sequence, this._offsetDelta) : super(sequence); |
| 175 |
| 176 @override |
| 177 int get offset => _offsetDelta + super.offset; |
| 178 |
| 179 @override |
| 180 void set offset(int offset) { |
| 181 super.offset = offset - _offsetDelta; |
| 182 } |
| 183 |
| 184 @override |
| 185 String getString(int start, int endDelta) => |
| 186 super.getString(start - _offsetDelta, endDelta); |
| 187 } |
OLD | NEW |