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 library string_scanner.string_scanner; | 5 library string_scanner.string_scanner; |
6 | 6 |
7 import 'package:source_span/source_span.dart'; | 7 import 'package:source_span/source_span.dart'; |
8 | 8 |
9 import 'exception.dart'; | 9 import 'exception.dart'; |
10 import 'utils.dart'; | 10 import 'utils.dart'; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 | 124 |
125 /// Returns whether or not [pattern] matches at the current position of the | 125 /// Returns whether or not [pattern] matches at the current position of the |
126 /// string. | 126 /// string. |
127 /// | 127 /// |
128 /// This doesn't move the scan pointer forward. | 128 /// This doesn't move the scan pointer forward. |
129 bool matches(Pattern pattern) { | 129 bool matches(Pattern pattern) { |
130 _lastMatch = pattern.matchAsPrefix(string, position); | 130 _lastMatch = pattern.matchAsPrefix(string, position); |
131 return _lastMatch != null; | 131 return _lastMatch != null; |
132 } | 132 } |
133 | 133 |
| 134 /// Returns the substring of [string] between [start] and [end]. |
| 135 /// |
| 136 /// Unlike [String.substring], [end] defaults to [position] rather than the |
| 137 /// end of the string. |
| 138 String substring(int start, [int end]) { |
| 139 if (end == null) end = position; |
| 140 return string.substring(start, end); |
| 141 } |
| 142 |
134 /// Throws a [FormatException] with [message] as well as a detailed | 143 /// Throws a [FormatException] with [message] as well as a detailed |
135 /// description of the location of the error in the string. | 144 /// description of the location of the error in the string. |
136 /// | 145 /// |
137 /// [match] is the match information for the span of the string with which the | 146 /// [match] is the match information for the span of the string with which the |
138 /// error is associated. This should be a match returned by this scanner's | 147 /// error is associated. This should be a match returned by this scanner's |
139 /// [lastMatch] property. By default, the error is associated with the last | 148 /// [lastMatch] property. By default, the error is associated with the last |
140 /// match. | 149 /// match. |
141 /// | 150 /// |
142 /// If [position] and/or [length] are passed, they are used as the error span | 151 /// If [position] and/or [length] are passed, they are used as the error span |
143 /// instead. If only [length] is passed, [position] defaults to the current | 152 /// instead. If only [length] is passed, [position] defaults to the current |
(...skipping 14 matching lines...) Expand all Loading... |
158 throw new StringScannerException(message, span, string); | 167 throw new StringScannerException(message, span, string); |
159 } | 168 } |
160 | 169 |
161 // TODO(nweiz): Make this handle long lines more gracefully. | 170 // TODO(nweiz): Make this handle long lines more gracefully. |
162 /// Throws a [FormatException] describing that [name] is expected at the | 171 /// Throws a [FormatException] describing that [name] is expected at the |
163 /// current position in the string. | 172 /// current position in the string. |
164 void _fail(String name) { | 173 void _fail(String name) { |
165 error("expected $name.", position: this.position, length: 0); | 174 error("expected $name.", position: this.position, length: 0); |
166 } | 175 } |
167 } | 176 } |
OLD | NEW |