| 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; | |
| 6 | |
| 7 import 'package:source_span/source_span.dart'; | 5 import 'package:source_span/source_span.dart'; |
| 8 | 6 |
| 9 import 'eager_span_scanner.dart'; | 7 import 'eager_span_scanner.dart'; |
| 10 import 'exception.dart'; | 8 import 'exception.dart'; |
| 11 import 'line_scanner.dart'; | 9 import 'line_scanner.dart'; |
| 10 import 'relative_span_scanner.dart'; |
| 12 import 'string_scanner.dart'; | 11 import 'string_scanner.dart'; |
| 13 import 'utils.dart'; | 12 import 'utils.dart'; |
| 14 | 13 |
| 15 /// A subclass of [LineScanner] that exposes matched ranges as source map | 14 /// A subclass of [LineScanner] that exposes matched ranges as source map |
| 16 /// [Span]s. | 15 /// [Span]s. |
| 17 class SpanScanner extends StringScanner implements LineScanner { | 16 class SpanScanner extends StringScanner implements LineScanner { |
| 18 /// The source of the scanner. | 17 /// The source of the scanner. |
| 19 /// | 18 /// |
| 20 /// 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. |
| 21 final SourceFile _sourceFile; | 20 final SourceFile _sourceFile; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 32 "this LineScanner."); | 31 "this LineScanner."); |
| 33 } | 32 } |
| 34 | 33 |
| 35 this.position = state.position; | 34 this.position = state.position; |
| 36 } | 35 } |
| 37 | 36 |
| 38 /// The [FileSpan] for [lastMatch]. | 37 /// The [FileSpan] for [lastMatch]. |
| 39 /// | 38 /// |
| 40 /// 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 |
| 41 /// subgroups since [Match] exposes no information about their positions. | 40 /// subgroups since [Match] exposes no information about their positions. |
| 42 FileSpan get lastSpan => _lastSpan; | 41 FileSpan get lastSpan { |
| 42 if (lastMatch == null) _lastSpan = null; |
| 43 return _lastSpan; |
| 44 } |
| 43 FileSpan _lastSpan; | 45 FileSpan _lastSpan; |
| 44 | 46 |
| 45 /// The current location of the scanner. | 47 /// The current location of the scanner. |
| 46 FileLocation get location => _sourceFile.location(position); | 48 FileLocation get location => _sourceFile.location(position); |
| 47 | 49 |
| 48 /// Returns an empty span at the current location. | 50 /// Returns an empty span at the current location. |
| 49 FileSpan get emptySpan => location.pointSpan(); | 51 FileSpan get emptySpan => location.pointSpan(); |
| 50 | 52 |
| 51 /// Creates a new [SpanScanner] that starts scanning from [position]. | 53 /// Creates a new [SpanScanner] that starts scanning from [position]. |
| 52 /// | 54 /// |
| 53 /// [sourceUrl] is used as [SourceLocation.sourceUrl] for the returned | 55 /// [sourceUrl] is used as [SourceLocation.sourceUrl] for the returned |
| 54 /// [FileSpan]s as well as for error reporting. It can be a [String], a | 56 /// [FileSpan]s as well as for error reporting. It can be a [String], a |
| 55 /// [Uri], or `null`. | 57 /// [Uri], or `null`. |
| 56 SpanScanner(String string, {sourceUrl, int position}) | 58 SpanScanner(String string, {sourceUrl, int position}) |
| 57 : _sourceFile = new SourceFile(string, url: sourceUrl), | 59 : _sourceFile = new SourceFile.fromString(string, url: sourceUrl), |
| 58 super(string, sourceUrl: sourceUrl, position: position); | 60 super(string, sourceUrl: sourceUrl, position: position); |
| 59 | 61 |
| 60 /// Creates a new [SpanScanner] that eagerly computes line and column numbers. | 62 /// Creates a new [SpanScanner] that eagerly computes line and column numbers. |
| 61 /// | 63 /// |
| 62 /// In general [new SpanScanner] will be more efficient, since it avoids extra | 64 /// In general [new SpanScanner] will be more efficient, since it avoids extra |
| 63 /// computation on every scan. However, eager scanning can be useful for | 65 /// computation on every scan. However, eager scanning can be useful for |
| 64 /// situations where the normal course of parsing frequently involves | 66 /// situations where the normal course of parsing frequently involves |
| 65 /// accessing the current line and column numbers. | 67 /// accessing the current line and column numbers. |
| 66 /// | 68 /// |
| 67 /// Note that *only* the `line` and `column` fields on the `SpanScanner` | 69 /// Note that *only* the `line` and `column` fields on the `SpanScanner` |
| 68 /// itself and its `LineScannerState` are eagerly computed. To limit their | 70 /// itself and its `LineScannerState` are eagerly computed. To limit their |
| 69 /// memory footprint, returned spans and locations will still lazily compute | 71 /// memory footprint, returned spans and locations will still lazily compute |
| 70 /// their line and column numbers. | 72 /// their line and column numbers. |
| 71 factory SpanScanner.eager(String string, {sourceUrl, int position}) = | 73 factory SpanScanner.eager(String string, {sourceUrl, int position}) = |
| 72 EagerSpanScanner; | 74 EagerSpanScanner; |
| 73 | 75 |
| 76 /// Creates a new [SpanScanner] that scans within [span]. |
| 77 /// |
| 78 /// This scans through [span.text], but emits new spans from [span.file] in |
| 79 /// their appropriate relative positions. The [string] field contains only |
| 80 /// [span.text], and [position], [line], and [column] are all relative to the |
| 81 /// span. |
| 82 factory SpanScanner.within(FileSpan span) = RelativeSpanScanner; |
| 83 |
| 74 /// Creates a [FileSpan] representing the source range between [startState] | 84 /// Creates a [FileSpan] representing the source range between [startState] |
| 75 /// and the current position. | 85 /// and the current position. |
| 76 FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) { | 86 FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) { |
| 77 var endPosition = endState == null ? position : endState.position; | 87 var endPosition = endState == null ? position : endState.position; |
| 78 return _sourceFile.span(startState.position, endPosition); | 88 return _sourceFile.span(startState.position, endPosition); |
| 79 } | 89 } |
| 80 | 90 |
| 81 bool matches(Pattern pattern) { | 91 bool matches(Pattern pattern) { |
| 82 if (!super.matches(pattern)) { | 92 if (!super.matches(pattern)) { |
| 83 _lastSpan = null; | 93 _lastSpan = null; |
| 84 return false; | 94 return false; |
| 85 } | 95 } |
| 86 | 96 |
| 87 _lastSpan = _sourceFile.span(position, lastMatch.end); | 97 _lastSpan = _sourceFile.span(position, lastMatch.end); |
| 88 return true; | 98 return true; |
| 89 } | 99 } |
| 90 | 100 |
| 91 void error(String message, {Match match, int position, int length}) { | 101 void error(String message, {Match match, int position, int length}) { |
| 92 validateErrorArgs(string, match, position, length); | 102 validateErrorArgs(string, match, position, length); |
| 93 | 103 |
| 94 if (match == null && position == null && length == null) match = lastMatch; | 104 if (match == null && position == null && length == null) match = lastMatch; |
| 95 if (position == null) { | 105 if (position == null) { |
| 96 position = match == null ? this.position : match.start; | 106 position = match == null ? this.position : match.start; |
| 97 } | 107 } |
| 98 if (length == null) length = match == null ? 1 : match.end - match.start; | 108 if (length == null) length = match == null ? 0 : match.end - match.start; |
| 99 | 109 |
| 100 var span = _sourceFile.span(position, position + length); | 110 var span = _sourceFile.span(position, position + length); |
| 101 throw new StringScannerException(message, span, string); | 111 throw new StringScannerException(message, span, string); |
| 102 } | 112 } |
| 103 } | 113 } |
| 104 | 114 |
| 105 /// A class representing the state of a [SpanScanner]. | 115 /// A class representing the state of a [SpanScanner]. |
| 106 class _SpanScannerState implements LineScannerState { | 116 class _SpanScannerState implements LineScannerState { |
| 107 /// The [SpanScanner] that created this. | 117 /// The [SpanScanner] that created this. |
| 108 final SpanScanner _scanner; | 118 final SpanScanner _scanner; |
| 109 | 119 |
| 110 final int position; | 120 final int position; |
| 111 int get line => _scanner._sourceFile.getLine(position); | 121 int get line => _scanner._sourceFile.getLine(position); |
| 112 int get column => _scanner._sourceFile.getColumn(position); | 122 int get column => _scanner._sourceFile.getColumn(position); |
| 113 | 123 |
| 114 _SpanScannerState(this._scanner, this.position); | 124 _SpanScannerState(this._scanner, this.position); |
| 115 } | 125 } |
| OLD | NEW |