OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library string_scanner.span_scanner; | |
6 | |
7 import 'package:source_span/source_span.dart'; | |
8 | |
9 import 'eager_span_scanner.dart'; | |
10 import 'exception.dart'; | |
11 import 'line_scanner.dart'; | |
12 import 'string_scanner.dart'; | |
13 import 'utils.dart'; | |
14 | |
15 /// A subclass of [LineScanner] that exposes matched ranges as source map | |
16 /// [Span]s. | |
17 class SpanScanner extends StringScanner implements LineScanner { | |
18 /// The source of the scanner. | |
19 /// | |
20 /// This caches line break information and is used to generate [Span]s. | |
21 final SourceFile _sourceFile; | |
22 | |
23 int get line => _sourceFile.getLine(position); | |
24 int get column => _sourceFile.getColumn(position); | |
25 | |
26 LineScannerState get state => new _SpanScannerState(this, position); | |
27 | |
28 set state(LineScannerState state) { | |
29 if (state is! _SpanScannerState || | |
30 !identical((state as _SpanScannerState)._scanner, this)) { | |
31 throw new ArgumentError("The given LineScannerState was not returned by " | |
32 "this LineScanner."); | |
33 } | |
34 | |
35 this.position = state.position; | |
36 } | |
37 | |
38 /// The [FileSpan] for [lastMatch]. | |
39 /// | |
40 /// This is the span for the entire match. There's no way to get spans for | |
41 /// subgroups since [Match] exposes no information about their positions. | |
42 FileSpan get lastSpan => _lastSpan; | |
43 FileSpan _lastSpan; | |
44 | |
45 /// The current location of the scanner. | |
46 FileLocation get location => _sourceFile.location(position); | |
47 | |
48 /// Returns an empty span at the current location. | |
49 FileSpan get emptySpan => location.pointSpan(); | |
50 | |
51 /// Creates a new [SpanScanner] that starts scanning from [position]. | |
52 /// | |
53 /// [sourceUrl] is used as [SourceLocation.sourceUrl] for the returned | |
54 /// [FileSpan]s as well as for error reporting. It can be a [String], a | |
55 /// [Uri], or `null`. | |
56 SpanScanner(String string, {sourceUrl, int position}) | |
57 : _sourceFile = new SourceFile(string, url: sourceUrl), | |
58 super(string, sourceUrl: sourceUrl, position: position); | |
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 | |
74 /// Creates a [FileSpan] representing the source range between [startState] | |
75 /// and the current position. | |
76 FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) { | |
77 var endPosition = endState == null ? position : endState.position; | |
78 return _sourceFile.span(startState.position, endPosition); | |
79 } | |
80 | |
81 bool matches(Pattern pattern) { | |
82 if (!super.matches(pattern)) { | |
83 _lastSpan = null; | |
84 return false; | |
85 } | |
86 | |
87 _lastSpan = _sourceFile.span(position, lastMatch.end); | |
88 return true; | |
89 } | |
90 | |
91 void error(String message, {Match match, int position, int length}) { | |
92 validateErrorArgs(string, match, position, length); | |
93 | |
94 if (match == null && position == null && length == null) match = lastMatch; | |
95 if (position == null) { | |
96 position = match == null ? this.position : match.start; | |
97 } | |
98 if (length == null) length = match == null ? 1 : match.end - match.start; | |
99 | |
100 var span = _sourceFile.span(position, position + length); | |
101 throw new StringScannerException(message, span, string); | |
102 } | |
103 } | |
104 | |
105 /// A class representing the state of a [SpanScanner]. | |
106 class _SpanScannerState implements LineScannerState { | |
107 /// The [SpanScanner] that created this. | |
108 final SpanScanner _scanner; | |
109 | |
110 final int position; | |
111 int get line => _scanner._sourceFile.getLine(position); | |
112 int get column => _scanner._sourceFile.getColumn(position); | |
113 | |
114 _SpanScannerState(this._scanner, this.position); | |
115 } | |
OLD | NEW |