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:source_span/source_span.dart'; | 5 import 'package:source_span/source_span.dart'; |
6 | 6 |
7 import 'eager_span_scanner.dart'; | 7 import 'eager_span_scanner.dart'; |
8 import 'exception.dart'; | 8 import 'exception.dart'; |
9 import 'line_scanner.dart'; | 9 import 'line_scanner.dart'; |
| 10 import 'relative_span_scanner.dart'; |
10 import 'string_scanner.dart'; | 11 import 'string_scanner.dart'; |
11 import 'utils.dart'; | 12 import 'utils.dart'; |
12 | 13 |
13 /// A subclass of [LineScanner] that exposes matched ranges as source map | 14 /// A subclass of [LineScanner] that exposes matched ranges as source map |
14 /// [Span]s. | 15 /// [Span]s. |
15 class SpanScanner extends StringScanner implements LineScanner { | 16 class SpanScanner extends StringScanner implements LineScanner { |
16 /// The source of the scanner. | 17 /// The source of the scanner. |
17 /// | 18 /// |
18 /// This caches line break information and is used to generate [Span]s. | 19 /// This caches line break information and is used to generate [Span]s. |
19 final SourceFile _sourceFile; | 20 final SourceFile _sourceFile; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 /// situations where the normal course of parsing frequently involves | 63 /// situations where the normal course of parsing frequently involves |
63 /// accessing the current line and column numbers. | 64 /// accessing the current line and column numbers. |
64 /// | 65 /// |
65 /// Note that *only* the `line` and `column` fields on the `SpanScanner` | 66 /// Note that *only* the `line` and `column` fields on the `SpanScanner` |
66 /// itself and its `LineScannerState` are eagerly computed. To limit their | 67 /// itself and its `LineScannerState` are eagerly computed. To limit their |
67 /// memory footprint, returned spans and locations will still lazily compute | 68 /// memory footprint, returned spans and locations will still lazily compute |
68 /// their line and column numbers. | 69 /// their line and column numbers. |
69 factory SpanScanner.eager(String string, {sourceUrl, int position}) = | 70 factory SpanScanner.eager(String string, {sourceUrl, int position}) = |
70 EagerSpanScanner; | 71 EagerSpanScanner; |
71 | 72 |
| 73 /// Creates a new [SpanScanner] that scans within [span]. |
| 74 /// |
| 75 /// This scans through [span.text], but emits new spans from [span.file] in |
| 76 /// their appropriate relative positions. The [string] field contains only |
| 77 /// [span.text], and [position], [line], and [column] are all relative to the |
| 78 /// span. |
| 79 factory SpanScanner.within(FileSpan span) = RelativeSpanScanner; |
| 80 |
72 /// Creates a [FileSpan] representing the source range between [startState] | 81 /// Creates a [FileSpan] representing the source range between [startState] |
73 /// and the current position. | 82 /// and the current position. |
74 FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) { | 83 FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) { |
75 var endPosition = endState == null ? position : endState.position; | 84 var endPosition = endState == null ? position : endState.position; |
76 return _sourceFile.span(startState.position, endPosition); | 85 return _sourceFile.span(startState.position, endPosition); |
77 } | 86 } |
78 | 87 |
79 bool matches(Pattern pattern) { | 88 bool matches(Pattern pattern) { |
80 if (!super.matches(pattern)) { | 89 if (!super.matches(pattern)) { |
81 _lastSpan = null; | 90 _lastSpan = null; |
(...skipping 22 matching lines...) Expand all Loading... |
104 class _SpanScannerState implements LineScannerState { | 113 class _SpanScannerState implements LineScannerState { |
105 /// The [SpanScanner] that created this. | 114 /// The [SpanScanner] that created this. |
106 final SpanScanner _scanner; | 115 final SpanScanner _scanner; |
107 | 116 |
108 final int position; | 117 final int position; |
109 int get line => _scanner._sourceFile.getLine(position); | 118 int get line => _scanner._sourceFile.getLine(position); |
110 int get column => _scanner._sourceFile.getColumn(position); | 119 int get column => _scanner._sourceFile.getColumn(position); |
111 | 120 |
112 _SpanScannerState(this._scanner, this.position); | 121 _SpanScannerState(this._scanner, this.position); |
113 } | 122 } |
OLD | NEW |