OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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; | 5 library string_scanner.eager_span_scanner; |
6 | 6 |
7 import 'package:charcode/ascii.dart'; | 7 import 'package:charcode/ascii.dart'; |
8 | 8 |
9 import 'string_scanner.dart'; | 9 import 'line_scanner.dart'; |
| 10 import 'span_scanner.dart'; |
| 11 |
| 12 // TODO(nweiz): Currently this duplicates code in line_scanner.dart. Once |
| 13 // sdk#23770 is fully complete, we should move the shared code into a mixin. |
10 | 14 |
11 /// A regular expression matching newlines across platforms. | 15 /// A regular expression matching newlines across platforms. |
12 final _newlineRegExp = new RegExp(r"\r\n?|\n"); | 16 final _newlineRegExp = new RegExp(r"\r\n?|\n"); |
13 | 17 |
14 /// A subclass of [StringScanner] that tracks line and column information. | 18 /// A [SpanScanner] that tracks the line and column eagerly, like [LineScanner]. |
15 class LineScanner extends StringScanner { | 19 class EagerSpanScanner extends SpanScanner { |
16 /// The scanner's current (zero-based) line number. | |
17 int get line => _line; | 20 int get line => _line; |
18 int _line = 0; | 21 int _line = 0; |
19 | 22 |
20 /// The scanner's current (zero-based) column number. | |
21 int get column => _column; | 23 int get column => _column; |
22 int _column = 0; | 24 int _column = 0; |
23 | 25 |
24 /// The scanner's state, including line and column information. | |
25 /// | |
26 /// This can be used to efficiently save and restore the state of the scanner | |
27 /// when backtracking. A given [LineScannerState] is only valid for the | |
28 /// [LineScanner] that created it. | |
29 LineScannerState get state => | 26 LineScannerState get state => |
30 new LineScannerState._(this, position, line, column); | 27 new _EagerSpanScannerState(this, position, line, column); |
31 | 28 |
32 /// Whether the current position is between a CR character and an LF | |
33 /// charactet. | |
34 bool get _betweenCRLF => peekChar(-1) == $cr && peekChar() == $lf; | 29 bool get _betweenCRLF => peekChar(-1) == $cr && peekChar() == $lf; |
35 | 30 |
36 set state(LineScannerState state) { | 31 set state(LineScannerState state) { |
37 if (!identical(state._scanner, this)) { | 32 if (state is! _EagerSpanScannerState || |
| 33 !identical((state as _EagerSpanScannerState)._scanner, this)) { |
38 throw new ArgumentError("The given LineScannerState was not returned by " | 34 throw new ArgumentError("The given LineScannerState was not returned by " |
39 "this LineScanner."); | 35 "this LineScanner."); |
40 } | 36 } |
41 | 37 |
42 super.position = state.position; | 38 super.position = state.position; |
43 _line = state.line; | 39 _line = state.line; |
44 _column = state.column; | 40 _column = state.column; |
45 } | 41 } |
46 | 42 |
47 set position(int newPosition) { | 43 set position(int newPosition) { |
(...skipping 15 matching lines...) Expand all Loading... |
63 _line -= newlines.length; | 59 _line -= newlines.length; |
64 if (newlines.isEmpty) { | 60 if (newlines.isEmpty) { |
65 _column -= oldPosition - newPosition; | 61 _column -= oldPosition - newPosition; |
66 } else { | 62 } else { |
67 _column = newPosition - | 63 _column = newPosition - |
68 string.lastIndexOf(_newlineRegExp, newPosition) - 1; | 64 string.lastIndexOf(_newlineRegExp, newPosition) - 1; |
69 } | 65 } |
70 } | 66 } |
71 } | 67 } |
72 | 68 |
73 LineScanner(String string, {sourceUrl, int position}) | 69 EagerSpanScanner(String string, {sourceUrl, int position}) |
74 : super(string, sourceUrl: sourceUrl, position: position); | 70 : super(string, sourceUrl: sourceUrl, position: position); |
75 | 71 |
76 int readChar() { | 72 int readChar() { |
77 var char = super.readChar(); | 73 var char = super.readChar(); |
78 if (char == $lf || (char == $cr && peekChar() != $lf)) { | 74 if (char == $lf || (char == $cr && peekChar() != $lf)) { |
79 _line += 1; | 75 _line += 1; |
80 _column = 0; | 76 _column = 0; |
81 } else { | 77 } else { |
82 _column += 1; | 78 _column += 1; |
83 } | 79 } |
(...skipping 16 matching lines...) Expand all Loading... |
100 | 96 |
101 /// Returns a list of [Match]es describing all the newlines in [text], which | 97 /// Returns a list of [Match]es describing all the newlines in [text], which |
102 /// is assumed to end at [position]. | 98 /// is assumed to end at [position]. |
103 List<Match> _newlinesIn(String text) { | 99 List<Match> _newlinesIn(String text) { |
104 var newlines = _newlineRegExp.allMatches(text).toList(); | 100 var newlines = _newlineRegExp.allMatches(text).toList(); |
105 if (_betweenCRLF) newlines.removeLast(); | 101 if (_betweenCRLF) newlines.removeLast(); |
106 return newlines; | 102 return newlines; |
107 } | 103 } |
108 } | 104 } |
109 | 105 |
110 /// A class representing the state of a [LineScanner]. | 106 /// A class representing the state of an [EagerSpanScanner]. |
111 class LineScannerState { | 107 class _EagerSpanScannerState implements LineScannerState { |
112 /// The [LineScanner] that created this. | 108 final EagerSpanScanner _scanner; |
113 final LineScanner _scanner; | |
114 | |
115 /// The position of the scanner in this state. | |
116 final int position; | 109 final int position; |
117 | |
118 /// The zero-based line number of the scanner in this state. | |
119 final int line; | 110 final int line; |
120 | |
121 /// The zero-based column number of the scanner in this state. | |
122 final int column; | 111 final int column; |
123 | 112 |
124 LineScannerState._(this._scanner, this.position, this.line, this.column); | 113 _EagerSpanScannerState(this._scanner, this.position, this.line, this.column); |
125 } | 114 } |
| 115 |
OLD | NEW |