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 'eager_span_scanner.dart'; |
9 import 'exception.dart'; | 10 import 'exception.dart'; |
10 import 'line_scanner.dart'; | 11 import 'line_scanner.dart'; |
11 import 'string_scanner.dart'; | 12 import 'string_scanner.dart'; |
12 import 'utils.dart'; | 13 import 'utils.dart'; |
13 | 14 |
14 /// A subclass of [LineScanner] that exposes matched ranges as source map | 15 /// A subclass of [LineScanner] that exposes matched ranges as source map |
15 /// [Span]s. | 16 /// [Span]s. |
16 class SpanScanner extends StringScanner implements LineScanner { | 17 class SpanScanner extends StringScanner implements LineScanner { |
17 /// The source of the scanner. | 18 /// The source of the scanner. |
18 /// | 19 /// |
(...skipping 30 matching lines...) Expand all Loading... |
49 | 50 |
50 /// Creates a new [SpanScanner] that starts scanning from [position]. | 51 /// Creates a new [SpanScanner] that starts scanning from [position]. |
51 /// | 52 /// |
52 /// [sourceUrl] is used as [SourceLocation.sourceUrl] for the returned | 53 /// [sourceUrl] is used as [SourceLocation.sourceUrl] for the returned |
53 /// [FileSpan]s as well as for error reporting. It can be a [String], a | 54 /// [FileSpan]s as well as for error reporting. It can be a [String], a |
54 /// [Uri], or `null`. | 55 /// [Uri], or `null`. |
55 SpanScanner(String string, {sourceUrl, int position}) | 56 SpanScanner(String string, {sourceUrl, int position}) |
56 : _sourceFile = new SourceFile(string, url: sourceUrl), | 57 : _sourceFile = new SourceFile(string, url: sourceUrl), |
57 super(string, sourceUrl: sourceUrl, position: position); | 58 super(string, sourceUrl: sourceUrl, position: position); |
58 | 59 |
| 60 /// Creates a new [SpanScanner] that eagerly computes line and column numbers. |
| 61 /// |
| 62 /// In general [new SpanScanner] will be more efficient, since it avoids extra |
| 63 /// computation on every scan. However, eager scanning can be useful for |
| 64 /// situations where the normal course of parsing frequently involves |
| 65 /// accessing the current line and column numbers. |
| 66 /// |
| 67 /// Note that *only* the `line` and `column` fields on the `SpanScanner` |
| 68 /// itself and its `LineScannerState` are eagerly computed. To limit their |
| 69 /// memory footprint, returned spans and locations will still lazily compute |
| 70 /// their line and column numbers. |
| 71 factory SpanScanner.eager(String string, {sourceUrl, int position}) = |
| 72 EagerSpanScanner; |
| 73 |
59 /// Creates a [FileSpan] representing the source range between [startState] | 74 /// Creates a [FileSpan] representing the source range between [startState] |
60 /// and the current position. | 75 /// and the current position. |
61 FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) { | 76 FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) { |
62 var endPosition = endState == null ? position : endState.position; | 77 var endPosition = endState == null ? position : endState.position; |
63 return _sourceFile.span(startState.position, endPosition); | 78 return _sourceFile.span(startState.position, endPosition); |
64 } | 79 } |
65 | 80 |
66 bool matches(Pattern pattern) { | 81 bool matches(Pattern pattern) { |
67 if (!super.matches(pattern)) { | 82 if (!super.matches(pattern)) { |
68 _lastSpan = null; | 83 _lastSpan = null; |
(...skipping 22 matching lines...) Expand all Loading... |
91 class _SpanScannerState implements LineScannerState { | 106 class _SpanScannerState implements LineScannerState { |
92 /// The [SpanScanner] that created this. | 107 /// The [SpanScanner] that created this. |
93 final SpanScanner _scanner; | 108 final SpanScanner _scanner; |
94 | 109 |
95 final int position; | 110 final int position; |
96 int get line => _scanner._sourceFile.getLine(position); | 111 int get line => _scanner._sourceFile.getLine(position); |
97 int get column => _scanner._sourceFile.getColumn(position); | 112 int get column => _scanner._sourceFile.getColumn(position); |
98 | 113 |
99 _SpanScannerState(this._scanner, this.position); | 114 _SpanScannerState(this._scanner, this.position); |
100 } | 115 } |
OLD | NEW |