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_maps/source_maps.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'; |
11 import 'string_scanner.dart'; | 11 import 'string_scanner.dart'; |
12 import 'utils.dart'; | 12 import 'utils.dart'; |
13 | 13 |
14 /// A subclass of [LineScanner] that exposes matched ranges as source map | 14 /// A subclass of [LineScanner] that exposes matched ranges as source map |
15 /// [Span]s. | 15 /// [Span]s. |
16 class SpanScanner extends StringScanner implements LineScanner { | 16 class SpanScanner extends StringScanner implements LineScanner { |
17 /// The source of the scanner. | 17 /// The source of the scanner. |
18 /// | 18 /// |
19 /// 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. |
20 final SourceFile _sourceFile; | 20 final SourceFile _sourceFile; |
21 | 21 |
22 int get line => _sourceFile.getLine(position); | 22 int get line => _sourceFile.getLine(position); |
23 int get column => _sourceFile.getColumn(line, position); | 23 int get column => _sourceFile.getColumn(position); |
24 | 24 |
25 LineScannerState get state => new _SpanScannerState(this, position); | 25 LineScannerState get state => new _SpanScannerState(this, position); |
26 | 26 |
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 [Span] for [lastMatch]. | 37 /// The [SourceSpan] 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 Span get lastSpan => _lastSpan; | 41 SourceSpan get lastSpan => _lastSpan; |
42 Span _lastSpan; | 42 SourceSpan _lastSpan; |
43 | 43 |
44 /// Returns an empty span at the current location. | 44 /// Returns an empty span at the current location. |
45 Span get emptySpan => _sourceFile.span(position); | 45 SourceSpan get emptySpan => _sourceFile.location(position).pointSpan(); |
46 | 46 |
47 /// Creates a new [SpanScanner] that starts scanning from [position]. | 47 /// Creates a new [SpanScanner] that starts scanning from [position]. |
48 /// | 48 /// |
49 /// [sourceUrl] is used as [Location.sourceUrl] for the returned [Span]s as | 49 /// [sourceUrl] is used as [SourceLocation.sourceUrl] for the returned |
50 /// well as for error reporting. | 50 /// [SourceSpan]s as well as for error reporting. It can be a [String], a |
51 SpanScanner(String string, sourceUrl, {int position}) | 51 /// [Uri], or `null`. |
52 : _sourceFile = new SourceFile.text( | 52 SpanScanner(String string, {sourceUrl, int position}) |
53 sourceUrl is Uri ? sourceUrl.toString() : sourceUrl, string), | 53 : _sourceFile = new SourceFile(string, url: sourceUrl), |
54 super(string, sourceUrl: sourceUrl, position: position); | 54 super(string, sourceUrl: sourceUrl, position: position); |
55 | 55 |
56 /// Creates a [Span] representing the source range between [startState] and | 56 /// Creates a [SourceSpan] representing the source range between [startState] |
57 /// the current position. | 57 /// and the current position. |
58 Span spanFrom(LineScannerState startState) => | 58 SourceSpan spanFrom(LineScannerState startState) => |
59 _sourceFile.span(startState.position, position); | 59 _sourceFile.span(startState.position, position); |
60 | 60 |
61 bool matches(Pattern pattern) { | 61 bool matches(Pattern pattern) { |
62 if (!super.matches(pattern)) { | 62 if (!super.matches(pattern)) { |
63 _lastSpan = null; | 63 _lastSpan = null; |
64 return false; | 64 return false; |
65 } | 65 } |
66 | 66 |
67 _lastSpan = _sourceFile.span(position, lastMatch.end); | 67 _lastSpan = _sourceFile.span(position, lastMatch.end); |
68 return true; | 68 return true; |
69 } | 69 } |
70 | 70 |
71 void error(String message, {Match match, int position, int length}) { | 71 void error(String message, {Match match, int position, int length}) { |
72 validateErrorArgs(string, match, position, length); | 72 validateErrorArgs(string, match, position, length); |
73 | 73 |
74 if (match == null && position == null && length == null) match = lastMatch; | 74 if (match == null && position == null && length == null) match = lastMatch; |
75 if (position == null) { | 75 if (position == null) { |
76 position = match == null ? this.position : match.start; | 76 position = match == null ? this.position : match.start; |
77 } | 77 } |
78 if (length == null) length = match == null ? 1 : match.end - match.start; | 78 if (length == null) length = match == null ? 1 : match.end - match.start; |
79 | 79 |
80 var span = _sourceFile.span(position, position + length); | 80 var span = _sourceFile.span(position, position + length); |
81 throw new StringScannerException(message, string, sourceUrl, span); | 81 throw new StringScannerException(message, span, string); |
82 } | 82 } |
83 } | 83 } |
84 | 84 |
85 /// A class representing the state of a [SpanScanner]. | 85 /// A class representing the state of a [SpanScanner]. |
86 class _SpanScannerState implements LineScannerState { | 86 class _SpanScannerState implements LineScannerState { |
87 /// The [SpanScanner] that created this. | 87 /// The [SpanScanner] that created this. |
88 final SpanScanner _scanner; | 88 final SpanScanner _scanner; |
89 | 89 |
90 final int position; | 90 final int position; |
91 int get line => _scanner._sourceFile.getLine(position); | 91 int get line => _scanner._sourceFile.getLine(position); |
92 int get column => _scanner._sourceFile.getColumn(line, position); | 92 int get column => _scanner._sourceFile.getColumn(position); |
93 | 93 |
94 _SpanScannerState(this._scanner, this.position); | 94 _SpanScannerState(this._scanner, this.position); |
95 } | 95 } |
OLD | NEW |