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/charcode.dart'; | 5 import 'package:charcode/charcode.dart'; |
6 import 'package:source_span/source_span.dart'; | 6 import 'package:source_span/source_span.dart'; |
7 | 7 |
8 import 'exception.dart'; | 8 import 'exception.dart'; |
9 import 'utils.dart'; | 9 import 'utils.dart'; |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 final String string; | 25 final String string; |
26 | 26 |
27 /// The current position of the scanner in the string, in characters. | 27 /// The current position of the scanner in the string, in characters. |
28 int get position => _position; | 28 int get position => _position; |
29 set position(int position) { | 29 set position(int position) { |
30 if (position < 0 || position > string.length) { | 30 if (position < 0 || position > string.length) { |
31 throw new ArgumentError("Invalid position $position"); | 31 throw new ArgumentError("Invalid position $position"); |
32 } | 32 } |
33 | 33 |
34 _position = position; | 34 _position = position; |
| 35 _lastMatch = null; |
35 } | 36 } |
36 int _position = 0; | 37 int _position = 0; |
37 | 38 |
38 /// The data about the previous match made by the scanner. | 39 /// The data about the previous match made by the scanner. |
39 /// | 40 /// |
40 /// If the last match failed, this will be `null`. | 41 /// If the last match failed, this will be `null`. |
41 Match get lastMatch => _lastMatch; | 42 Match get lastMatch { |
| 43 // Lazily unset [_lastMatch] so that we avoid extra assignments in |
| 44 // character-by-character methods that are used in core loops. |
| 45 if (_position != _lastMatchPosition) _lastMatch = null; |
| 46 return _lastMatch; |
| 47 } |
42 Match _lastMatch; | 48 Match _lastMatch; |
| 49 int _lastMatchPosition; |
43 | 50 |
44 /// The portion of the string that hasn't yet been scanned. | 51 /// The portion of the string that hasn't yet been scanned. |
45 String get rest => string.substring(position); | 52 String get rest => string.substring(position); |
46 | 53 |
47 /// Whether the scanner has completely consumed [string]. | 54 /// Whether the scanner has completely consumed [string]. |
48 bool get isDone => position == string.length; | 55 bool get isDone => position == string.length; |
49 | 56 |
50 /// Creates a new [StringScanner] that starts scanning from [position]. | 57 /// Creates a new [StringScanner] that starts scanning from [position]. |
51 /// | 58 /// |
52 /// [position] defaults to 0, the beginning of the string. [sourceUrl] is the | 59 /// [position] defaults to 0, the beginning of the string. [sourceUrl] is the |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 118 |
112 _fail('Expected $name.'); | 119 _fail('Expected $name.'); |
113 } | 120 } |
114 | 121 |
115 /// If [pattern] matches at the current position of the string, scans forward | 122 /// If [pattern] matches at the current position of the string, scans forward |
116 /// until the end of the match. | 123 /// until the end of the match. |
117 /// | 124 /// |
118 /// Returns whether or not [pattern] matched. | 125 /// Returns whether or not [pattern] matched. |
119 bool scan(Pattern pattern) { | 126 bool scan(Pattern pattern) { |
120 var success = matches(pattern); | 127 var success = matches(pattern); |
121 if (success) _position = _lastMatch.end; | 128 if (success) { |
| 129 _position = _lastMatch.end; |
| 130 _lastMatchPosition = _position; |
| 131 } |
122 return success; | 132 return success; |
123 } | 133 } |
124 | 134 |
125 /// If [pattern] matches at the current position of the string, scans forward | 135 /// If [pattern] matches at the current position of the string, scans forward |
126 /// until the end of the match. | 136 /// until the end of the match. |
127 /// | 137 /// |
128 /// If [pattern] did not match, throws a [FormatException] describing the | 138 /// If [pattern] did not match, throws a [FormatException] describing the |
129 /// position of the failure. [name] is used in this error as the expected name | 139 /// position of the failure. [name] is used in this error as the expected name |
130 /// of the pattern being matched; if it's `null`, the pattern itself is used | 140 /// of the pattern being matched; if it's `null`, the pattern itself is used |
131 /// instead. | 141 /// instead. |
(...skipping 20 matching lines...) Expand all Loading... |
152 if (isDone) return; | 162 if (isDone) return; |
153 _fail("no more input"); | 163 _fail("no more input"); |
154 } | 164 } |
155 | 165 |
156 /// Returns whether or not [pattern] matches at the current position of the | 166 /// Returns whether or not [pattern] matches at the current position of the |
157 /// string. | 167 /// string. |
158 /// | 168 /// |
159 /// This doesn't move the scan pointer forward. | 169 /// This doesn't move the scan pointer forward. |
160 bool matches(Pattern pattern) { | 170 bool matches(Pattern pattern) { |
161 _lastMatch = pattern.matchAsPrefix(string, position); | 171 _lastMatch = pattern.matchAsPrefix(string, position); |
| 172 _lastMatchPosition = _position; |
162 return _lastMatch != null; | 173 return _lastMatch != null; |
163 } | 174 } |
164 | 175 |
165 /// Returns the substring of [string] between [start] and [end]. | 176 /// Returns the substring of [string] between [start] and [end]. |
166 /// | 177 /// |
167 /// Unlike [String.substring], [end] defaults to [position] rather than the | 178 /// Unlike [String.substring], [end] defaults to [position] rather than the |
168 /// end of the string. | 179 /// end of the string. |
169 String substring(int start, [int end]) { | 180 String substring(int start, [int end]) { |
170 if (end == null) end = position; | 181 if (end == null) end = position; |
171 return string.substring(start, end); | 182 return string.substring(start, end); |
172 } | 183 } |
173 | 184 |
174 /// Throws a [FormatException] with [message] as well as a detailed | 185 /// Throws a [FormatException] with [message] as well as a detailed |
175 /// description of the location of the error in the string. | 186 /// description of the location of the error in the string. |
176 /// | 187 /// |
177 /// [match] is the match information for the span of the string with which the | 188 /// [match] is the match information for the span of the string with which the |
178 /// error is associated. This should be a match returned by this scanner's | 189 /// error is associated. This should be a match returned by this scanner's |
179 /// [lastMatch] property. By default, the error is associated with the last | 190 /// [lastMatch] property. By default, the error is associated with the last |
180 /// match. | 191 /// match. |
181 /// | 192 /// |
182 /// If [position] and/or [length] are passed, they are used as the error span | 193 /// If [position] and/or [length] are passed, they are used as the error span |
183 /// instead. If only [length] is passed, [position] defaults to the current | 194 /// instead. If only [length] is passed, [position] defaults to the current |
184 /// position; if only [position] is passed, [length] defaults to 1. | 195 /// position; if only [position] is passed, [length] defaults to 0. |
185 /// | 196 /// |
186 /// It's an error to pass [match] at the same time as [position] or [length]. | 197 /// It's an error to pass [match] at the same time as [position] or [length]. |
187 void error(String message, {Match match, int position, int length}) { | 198 void error(String message, {Match match, int position, int length}) { |
188 validateErrorArgs(string, match, position, length); | 199 validateErrorArgs(string, match, position, length); |
189 | 200 |
190 if (match == null && position == null && length == null) match = lastMatch; | 201 if (match == null && position == null && length == null) match = lastMatch; |
191 if (position == null) { | 202 if (position == null) { |
192 position = match == null ? this.position : match.start; | 203 position = match == null ? this.position : match.start; |
193 } | 204 } |
194 if (length == null) length = match == null ? 1 : match.end - match.start; | 205 if (length == null) length = match == null ? 0 : match.end - match.start; |
195 | 206 |
196 var sourceFile = new SourceFile(string, url: sourceUrl); | 207 var sourceFile = new SourceFile(string, url: sourceUrl); |
197 var span = sourceFile.span(position, position + length); | 208 var span = sourceFile.span(position, position + length); |
198 throw new StringScannerException(message, span, string); | 209 throw new StringScannerException(message, span, string); |
199 } | 210 } |
200 | 211 |
201 // TODO(nweiz): Make this handle long lines more gracefully. | 212 // TODO(nweiz): Make this handle long lines more gracefully. |
202 /// Throws a [FormatException] describing that [name] is expected at the | 213 /// Throws a [FormatException] describing that [name] is expected at the |
203 /// current position in the string. | 214 /// current position in the string. |
204 void _fail(String name) { | 215 void _fail(String name) { |
205 error("expected $name.", position: this.position, length: 0); | 216 error("expected $name.", position: this.position, length: 0); |
206 } | 217 } |
207 } | 218 } |
OLD | NEW |