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.span_scanner; | 5 library string_scanner.span_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 'line_scanner.dart'; | 10 import 'line_scanner.dart'; |
(...skipping 16 matching lines...) Expand all Loading... |
27 set state(LineScannerState state) { | 27 set state(LineScannerState state) { |
28 if (state is! _SpanScannerState || | 28 if (state is! _SpanScannerState || |
29 !identical((state as _SpanScannerState)._scanner, this)) { | 29 !identical((state as _SpanScannerState)._scanner, this)) { |
30 throw new ArgumentError("The given LineScannerState was not returned by " | 30 throw new ArgumentError("The given LineScannerState was not returned by " |
31 "this LineScanner."); | 31 "this LineScanner."); |
32 } | 32 } |
33 | 33 |
34 this.position = state.position; | 34 this.position = state.position; |
35 } | 35 } |
36 | 36 |
37 /// The [SourceSpan] for [lastMatch]. | 37 /// The [FileSpan] for [lastMatch]. |
38 /// | 38 /// |
39 /// This is the span for the entire match. There's no way to get spans for | 39 /// This is the span for the entire match. There's no way to get spans for |
40 /// subgroups since [Match] exposes no information about their positions. | 40 /// subgroups since [Match] exposes no information about their positions. |
41 SourceSpan get lastSpan => _lastSpan; | 41 FileSpan get lastSpan => _lastSpan; |
42 SourceSpan _lastSpan; | 42 FileSpan _lastSpan; |
| 43 |
| 44 /// The current location of the scanner. |
| 45 FileLocation get location => _sourceFile.location(position); |
43 | 46 |
44 /// Returns an empty span at the current location. | 47 /// Returns an empty span at the current location. |
45 SourceSpan get emptySpan => _sourceFile.location(position).pointSpan(); | 48 FileSpan get emptySpan => location.pointSpan(); |
46 | 49 |
47 /// Creates a new [SpanScanner] that starts scanning from [position]. | 50 /// Creates a new [SpanScanner] that starts scanning from [position]. |
48 /// | 51 /// |
49 /// [sourceUrl] is used as [SourceLocation.sourceUrl] for the returned | 52 /// [sourceUrl] is used as [SourceLocation.sourceUrl] for the returned |
50 /// [SourceSpan]s as well as for error reporting. It can be a [String], a | 53 /// [FileSpan]s as well as for error reporting. It can be a [String], a |
51 /// [Uri], or `null`. | 54 /// [Uri], or `null`. |
52 SpanScanner(String string, {sourceUrl, int position}) | 55 SpanScanner(String string, {sourceUrl, int position}) |
53 : _sourceFile = new SourceFile(string, url: sourceUrl), | 56 : _sourceFile = new SourceFile(string, url: sourceUrl), |
54 super(string, sourceUrl: sourceUrl, position: position); | 57 super(string, sourceUrl: sourceUrl, position: position); |
55 | 58 |
56 /// Creates a [SourceSpan] representing the source range between [startState] | 59 /// Creates a [FileSpan] representing the source range between [startState] |
57 /// and the current position. | 60 /// and the current position. |
58 SourceSpan spanFrom(LineScannerState startState) => | 61 FileSpan spanFrom(LineScannerState startState) => |
59 _sourceFile.span(startState.position, position); | 62 _sourceFile.span(startState.position, position); |
60 | 63 |
61 bool matches(Pattern pattern) { | 64 bool matches(Pattern pattern) { |
62 if (!super.matches(pattern)) { | 65 if (!super.matches(pattern)) { |
63 _lastSpan = null; | 66 _lastSpan = null; |
64 return false; | 67 return false; |
65 } | 68 } |
66 | 69 |
67 _lastSpan = _sourceFile.span(position, lastMatch.end); | 70 _lastSpan = _sourceFile.span(position, lastMatch.end); |
68 return true; | 71 return true; |
(...skipping 17 matching lines...) Expand all Loading... |
86 class _SpanScannerState implements LineScannerState { | 89 class _SpanScannerState implements LineScannerState { |
87 /// The [SpanScanner] that created this. | 90 /// The [SpanScanner] that created this. |
88 final SpanScanner _scanner; | 91 final SpanScanner _scanner; |
89 | 92 |
90 final int position; | 93 final int position; |
91 int get line => _scanner._sourceFile.getLine(position); | 94 int get line => _scanner._sourceFile.getLine(position); |
92 int get column => _scanner._sourceFile.getColumn(position); | 95 int get column => _scanner._sourceFile.getColumn(position); |
93 | 96 |
94 _SpanScannerState(this._scanner, this.position); | 97 _SpanScannerState(this._scanner, this.position); |
95 } | 98 } |
OLD | NEW |