Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Unified Diff: pkg/front_end/lib/src/scanner/reader.dart

Issue 2508483002: Move scanner tests into the front_end package. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/front_end/lib/src/scanner/reader.dart
diff --git a/pkg/front_end/lib/src/scanner/reader.dart b/pkg/front_end/lib/src/scanner/reader.dart
index ee9d6c0a63cce8406b9fed36e561f3d68394bb06..b0791396e4f703e914258edce7ecb6e6d8b3bbbe 100644
--- a/pkg/front_end/lib/src/scanner/reader.dart
+++ b/pkg/front_end/lib/src/scanner/reader.dart
@@ -42,3 +42,92 @@ abstract class CharacterReader {
*/
int peek();
}
+
+/**
+ * A [CharacterReader] that reads characters from a character sequence.
+ */
+class CharSequenceReader implements CharacterReader {
+ /**
+ * The sequence from which characters will be read.
+ */
+ final String _sequence;
+
+ /**
+ * The number of characters in the string.
+ */
+ int _stringLength;
+
+ /**
+ * The index, relative to the string, of the next character to be read.
+ */
+ int _charOffset;
+
+ /**
+ * Initialize a newly created reader to read the characters in the given
+ * [_sequence].
+ */
+ CharSequenceReader(this._sequence) {
+ this._stringLength = _sequence.length;
+ this._charOffset = 0;
+ }
+
+ @override
+ int get offset => _charOffset - 1;
+
+ @override
+ void set offset(int offset) {
+ _charOffset = offset + 1;
+ }
+
+ @override
+ int advance() {
+ if (_charOffset >= _stringLength) {
+ return -1;
+ }
+ return _sequence.codeUnitAt(_charOffset++);
+ }
+
+ @override
+ String getString(int start, int endDelta) =>
+ _sequence.substring(start, _charOffset + endDelta);
+
+ @override
+ int peek() {
+ if (_charOffset >= _stringLength) {
+ return -1;
+ }
+ return _sequence.codeUnitAt(_charOffset);
+ }
+}
+
+/**
+ * A [CharacterReader] that reads characters from a character sequence, but adds
+ * a delta when reporting the current character offset so that the character
+ * sequence can be a subsequence from a larger sequence.
+ */
+class SubSequenceReader extends CharSequenceReader {
+ /**
+ * The offset from the beginning of the file to the beginning of the source
+ * being scanned.
+ */
+ final int _offsetDelta;
+
+ /**
+ * Initialize a newly created reader to read the characters in the given
+ * [sequence]. The [_offsetDelta] is the offset from the beginning of the file
+ * to the beginning of the source being scanned
+ */
+ SubSequenceReader(String sequence, this._offsetDelta) : super(sequence);
+
+ @override
+ int get offset => _offsetDelta + super.offset;
+
+ @override
+ void set offset(int offset) {
+ super.offset = offset - _offsetDelta;
+ }
+
+ @override
+ String getString(int start, int endDelta) =>
+ super.getString(start - _offsetDelta, endDelta);
+}

Powered by Google App Engine
This is Rietveld 408576698