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

Side by Side Diff: packages/string_scanner/lib/src/line_scanner.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 months 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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 library string_scanner.line_scanner;
6
7 import 'package:charcode/ascii.dart'; 5 import 'package:charcode/ascii.dart';
8 6
9 import 'string_scanner.dart'; 7 import 'string_scanner.dart';
10 8
11 // Note that much of this code is duplicated in eager_span_scanner.dart. 9 // Note that much of this code is duplicated in eager_span_scanner.dart.
12 10
13 /// A regular expression matching newlines across platforms. 11 /// A regular expression matching newlines across platforms.
14 final _newlineRegExp = new RegExp(r"\r\n?|\n"); 12 final _newlineRegExp = new RegExp(r"\r\n?|\n");
15 13
16 /// A subclass of [StringScanner] that tracks line and column information. 14 /// A subclass of [StringScanner] that tracks line and column information.
17 class LineScanner extends StringScanner { 15 class LineScanner extends StringScanner {
18 /// The scanner's current (zero-based) line number. 16 /// The scanner's current (zero-based) line number.
19 int get line => _line; 17 int get line => _line;
20 int _line = 0; 18 int _line = 0;
21 19
22 /// The scanner's current (zero-based) column number. 20 /// The scanner's current (zero-based) column number.
23 int get column => _column; 21 int get column => _column;
24 int _column = 0; 22 int _column = 0;
25 23
26 /// The scanner's state, including line and column information. 24 /// The scanner's state, including line and column information.
27 /// 25 ///
28 /// This can be used to efficiently save and restore the state of the scanner 26 /// This can be used to efficiently save and restore the state of the scanner
29 /// when backtracking. A given [LineScannerState] is only valid for the 27 /// when backtracking. A given [LineScannerState] is only valid for the
30 /// [LineScanner] that created it. 28 /// [LineScanner] that created it.
29 ///
30 /// This does not include the scanner's match information.
31 LineScannerState get state => 31 LineScannerState get state =>
32 new LineScannerState._(this, position, line, column); 32 new LineScannerState._(this, position, line, column);
33 33
34 /// Whether the current position is between a CR character and an LF 34 /// Whether the current position is between a CR character and an LF
35 /// charactet. 35 /// charactet.
36 bool get _betweenCRLF => peekChar(-1) == $cr && peekChar() == $lf; 36 bool get _betweenCRLF => peekChar(-1) == $cr && peekChar() == $lf;
37 37
38 set state(LineScannerState state) { 38 set state(LineScannerState state) {
39 if (!identical(state._scanner, this)) { 39 if (!identical(state._scanner, this)) {
40 throw new ArgumentError("The given LineScannerState was not returned by " 40 throw new ArgumentError("The given LineScannerState was not returned by "
(...skipping 27 matching lines...) Expand all
68 } else { 68 } else {
69 _column = newPosition - 69 _column = newPosition -
70 string.lastIndexOf(_newlineRegExp, newPosition) - 1; 70 string.lastIndexOf(_newlineRegExp, newPosition) - 1;
71 } 71 }
72 } 72 }
73 } 73 }
74 74
75 LineScanner(String string, {sourceUrl, int position}) 75 LineScanner(String string, {sourceUrl, int position})
76 : super(string, sourceUrl: sourceUrl, position: position); 76 : super(string, sourceUrl: sourceUrl, position: position);
77 77
78 bool scanChar(int character) {
79 if (!super.scanChar(character)) return false;
80 _adjustLineAndColumn(character);
81 return true;
82 }
83
78 int readChar() { 84 int readChar() {
79 var char = super.readChar(); 85 var character = super.readChar();
80 if (char == $lf || (char == $cr && peekChar() != $lf)) { 86 _adjustLineAndColumn(character);
87 return character;
88 }
89
90 /// Adjusts [_line] and [_column] after having consumed [character].
91 void _adjustLineAndColumn(int character) {
92 if (character == $lf || (character == $cr && peekChar() != $lf)) {
81 _line += 1; 93 _line += 1;
82 _column = 0; 94 _column = 0;
83 } else { 95 } else {
84 _column += 1; 96 _column += 1;
85 } 97 }
86 return char;
87 } 98 }
88 99
89 bool scan(Pattern pattern) { 100 bool scan(Pattern pattern) {
90 if (!super.scan(pattern)) return false; 101 if (!super.scan(pattern)) return false;
91 102
92 var newlines = _newlinesIn(lastMatch[0]); 103 var newlines = _newlinesIn(lastMatch[0]);
93 _line += newlines.length; 104 _line += newlines.length;
94 if (newlines.isEmpty) { 105 if (newlines.isEmpty) {
95 _column += lastMatch[0].length; 106 _column += lastMatch[0].length;
96 } else { 107 } else {
(...skipping 21 matching lines...) Expand all
118 final int position; 129 final int position;
119 130
120 /// The zero-based line number of the scanner in this state. 131 /// The zero-based line number of the scanner in this state.
121 final int line; 132 final int line;
122 133
123 /// The zero-based column number of the scanner in this state. 134 /// The zero-based column number of the scanner in this state.
124 final int column; 135 final int column;
125 136
126 LineScannerState._(this._scanner, this.position, this.line, this.column); 137 LineScannerState._(this._scanner, this.position, this.line, this.column);
127 } 138 }
OLDNEW
« no previous file with comments | « packages/string_scanner/lib/src/exception.dart ('k') | packages/string_scanner/lib/src/relative_span_scanner.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698