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