OLD | NEW |
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 import 'package:charcode/ascii.dart'; | 5 import 'package:charcode/ascii.dart'; |
6 | 6 |
7 import 'string_scanner.dart'; | 7 import 'string_scanner.dart'; |
8 | 8 |
9 // 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. |
10 | 10 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 } else { | 66 } else { |
67 _column = newPosition - | 67 _column = newPosition - |
68 string.lastIndexOf(_newlineRegExp, newPosition) - 1; | 68 string.lastIndexOf(_newlineRegExp, newPosition) - 1; |
69 } | 69 } |
70 } | 70 } |
71 } | 71 } |
72 | 72 |
73 LineScanner(String string, {sourceUrl, int position}) | 73 LineScanner(String string, {sourceUrl, int position}) |
74 : super(string, sourceUrl: sourceUrl, position: position); | 74 : super(string, sourceUrl: sourceUrl, position: position); |
75 | 75 |
| 76 bool scanChar(int character) { |
| 77 if (!super.scanChar(character)) return false; |
| 78 _adjustLineAndColumn(character); |
| 79 return true; |
| 80 } |
| 81 |
76 int readChar() { | 82 int readChar() { |
77 var char = super.readChar(); | 83 var character = super.readChar(); |
78 if (char == $lf || (char == $cr && peekChar() != $lf)) { | 84 _adjustLineAndColumn(character); |
| 85 return character; |
| 86 } |
| 87 |
| 88 /// Adjusts [_line] and [_column] after having consumed [character]. |
| 89 void _adjustLineAndColumn(int character) { |
| 90 if (character == $lf || (character == $cr && peekChar() != $lf)) { |
79 _line += 1; | 91 _line += 1; |
80 _column = 0; | 92 _column = 0; |
81 } else { | 93 } else { |
82 _column += 1; | 94 _column += 1; |
83 } | 95 } |
84 return char; | |
85 } | 96 } |
86 | 97 |
87 bool scan(Pattern pattern) { | 98 bool scan(Pattern pattern) { |
88 if (!super.scan(pattern)) return false; | 99 if (!super.scan(pattern)) return false; |
89 | 100 |
90 var newlines = _newlinesIn(lastMatch[0]); | 101 var newlines = _newlinesIn(lastMatch[0]); |
91 _line += newlines.length; | 102 _line += newlines.length; |
92 if (newlines.isEmpty) { | 103 if (newlines.isEmpty) { |
93 _column += lastMatch[0].length; | 104 _column += lastMatch[0].length; |
94 } else { | 105 } else { |
(...skipping 21 matching lines...) Expand all Loading... |
116 final int position; | 127 final int position; |
117 | 128 |
118 /// The zero-based line number of the scanner in this state. | 129 /// The zero-based line number of the scanner in this state. |
119 final int line; | 130 final int line; |
120 | 131 |
121 /// The zero-based column number of the scanner in this state. | 132 /// The zero-based column number of the scanner in this state. |
122 final int column; | 133 final int column; |
123 | 134 |
124 LineScannerState._(this._scanner, this.position, this.line, this.column); | 135 LineScannerState._(this._scanner, this.position, this.line, this.column); |
125 } | 136 } |
OLD | NEW |