Move a number of packages and some of pub over to using source_span.
R=efortuna@google.com, rnystrom@google.com, sigmund@google.com
BUG=19930
Review URL: https://codereview.chromium.org//401753002
git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart/pkg/string_scanner@38526 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a5d5518..f1d8763 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,16 @@
+## 0.1.0
+
+* Switch from `source_maps`' `Span` class to `source_span`'s `SourceSpan` class.
+
+* `new StringScanner()`'s `sourceUrl` parameter is now named to make it clear
+ that it can be safely `null`.
+
+* `new StringScannerException()` takes different arguments in a different order
+ to match `SpanFormatException`.
+
+* `StringScannerException.string` has been renamed to
+ `StringScannerException.source` to match the `FormatException` interface.
+
## 0.0.3
* Make `StringScannerException` inherit from source_map's
diff --git a/lib/src/exception.dart b/lib/src/exception.dart
index 44ec9a3..50177d6 100644
--- a/lib/src/exception.dart
+++ b/lib/src/exception.dart
@@ -4,18 +4,17 @@
library string_scanner.exception;
-import 'package:source_maps/source_maps.dart';
+import 'package:source_span/source_span.dart';
/// An exception thrown by a [StringScanner] that failed to parse a string.
-class StringScannerException extends SpanFormatException {
- /// The source string being parsed.
- final String string;
+class StringScannerException extends SourceSpanFormatException {
+ String get source => super.source;
/// The URL of the source file being parsed.
///
/// This may be `null`, indicating that the source URL is unknown.
- final Uri sourceUrl;
+ Uri get sourceUrl => span.sourceUrl;
- StringScannerException(String message, this.string, this.sourceUrl, Span span)
- : super(message, span);
+ StringScannerException(String message, SourceSpan span, String source)
+ : super(message, span, source);
}
diff --git a/lib/src/span_scanner.dart b/lib/src/span_scanner.dart
index 9060e62..d8dc230 100644
--- a/lib/src/span_scanner.dart
+++ b/lib/src/span_scanner.dart
@@ -4,7 +4,7 @@
library string_scanner.span_scanner;
-import 'package:source_maps/source_maps.dart';
+import 'package:source_span/source_span.dart';
import 'exception.dart';
import 'line_scanner.dart';
@@ -20,7 +20,7 @@
final SourceFile _sourceFile;
int get line => _sourceFile.getLine(position);
- int get column => _sourceFile.getColumn(line, position);
+ int get column => _sourceFile.getColumn(position);
LineScannerState get state => new _SpanScannerState(this, position);
@@ -34,28 +34,28 @@
this.position = state.position;
}
- /// The [Span] for [lastMatch].
+ /// The [SourceSpan] for [lastMatch].
///
/// This is the span for the entire match. There's no way to get spans for
/// subgroups since [Match] exposes no information about their positions.
- Span get lastSpan => _lastSpan;
- Span _lastSpan;
+ SourceSpan get lastSpan => _lastSpan;
+ SourceSpan _lastSpan;
/// Returns an empty span at the current location.
- Span get emptySpan => _sourceFile.span(position);
+ SourceSpan get emptySpan => _sourceFile.location(position).pointSpan();
/// Creates a new [SpanScanner] that starts scanning from [position].
///
- /// [sourceUrl] is used as [Location.sourceUrl] for the returned [Span]s as
- /// well as for error reporting.
- SpanScanner(String string, sourceUrl, {int position})
- : _sourceFile = new SourceFile.text(
- sourceUrl is Uri ? sourceUrl.toString() : sourceUrl, string),
+ /// [sourceUrl] is used as [SourceLocation.sourceUrl] for the returned
+ /// [SourceSpan]s as well as for error reporting. It can be a [String], a
+ /// [Uri], or `null`.
+ SpanScanner(String string, {sourceUrl, int position})
+ : _sourceFile = new SourceFile(string, url: sourceUrl),
super(string, sourceUrl: sourceUrl, position: position);
- /// Creates a [Span] representing the source range between [startState] and
- /// the current position.
- Span spanFrom(LineScannerState startState) =>
+ /// Creates a [SourceSpan] representing the source range between [startState]
+ /// and the current position.
+ SourceSpan spanFrom(LineScannerState startState) =>
_sourceFile.span(startState.position, position);
bool matches(Pattern pattern) {
@@ -78,7 +78,7 @@
if (length == null) length = match == null ? 1 : match.end - match.start;
var span = _sourceFile.span(position, position + length);
- throw new StringScannerException(message, string, sourceUrl, span);
+ throw new StringScannerException(message, span, string);
}
}
@@ -89,7 +89,7 @@
final int position;
int get line => _scanner._sourceFile.getLine(position);
- int get column => _scanner._sourceFile.getColumn(line, position);
+ int get column => _scanner._sourceFile.getColumn(position);
_SpanScannerState(this._scanner, this.position);
}
diff --git a/lib/src/string_scanner.dart b/lib/src/string_scanner.dart
index c9e7459..44d5d2d 100644
--- a/lib/src/string_scanner.dart
+++ b/lib/src/string_scanner.dart
@@ -4,7 +4,7 @@
library string_scanner.string_scanner;
-import 'package:source_maps/source_maps.dart';
+import 'package:source_span/source_span.dart';
import 'exception.dart';
import 'utils.dart';
@@ -52,7 +52,7 @@
///
/// [position] defaults to 0, the beginning of the string. [sourceUrl] is the
/// URL of the source of the string being scanned, if available. It can be
- /// either a [String] or a [Uri].
+ /// a [String], a [Uri], or `null`.
StringScanner(this.string, {sourceUrl, int position})
: sourceUrl = sourceUrl is String ? Uri.parse(sourceUrl) : sourceUrl {
if (position != null) this.position = position;
@@ -153,10 +153,9 @@
}
if (length == null) length = match == null ? 1 : match.end - match.start;
- var url = sourceUrl == null ? null : sourceUrl.toString();
- var sourceFile = new SourceFile.text(url, string);
+ var sourceFile = new SourceFile(string, url: sourceUrl);
var span = sourceFile.span(position, position + length);
- throw new StringScannerException(message, string, sourceUrl, span);
+ throw new StringScannerException(message, span, string);
}
// TODO(nweiz): Make this handle long lines more gracefully.
diff --git a/pubspec.yaml b/pubspec.yaml
index f6d7ab9..1451fc5 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,12 +1,12 @@
name: string_scanner
-version: 0.0.3
+version: 0.1.0
author: "Dart Team <misc@dartlang.org>"
homepage: http://www.dartlang.org
description: >
A class for parsing strings using a sequence of patterns.
dependencies:
path: ">=1.2.0 <2.0.0"
- source_maps: ">=0.9.2 <0.10.0"
+ source_span: ">=1.0.0 <2.0.0"
dev_dependencies:
unittest: ">=0.10.0 <0.11.0"
environment:
diff --git a/test/span_scanner_test.dart b/test/span_scanner_test.dart
index 93ba0b6..796df29 100644
--- a/test/span_scanner_test.dart
+++ b/test/span_scanner_test.dart
@@ -10,7 +10,7 @@
void main() {
var scanner;
setUp(() {
- scanner = new SpanScanner('foo\nbar\nbaz', 'source');
+ scanner = new SpanScanner('foo\nbar\nbaz', sourceUrl: 'source');
});
test("tracks the span for the last match", () {
@@ -21,12 +21,12 @@
expect(span.start.offset, equals(2));
expect(span.start.line, equals(0));
expect(span.start.column, equals(2));
- expect(span.start.sourceUrl, equals('source'));
+ expect(span.start.sourceUrl, equals(Uri.parse('source')));
expect(span.end.offset, equals(6));
expect(span.end.line, equals(1));
expect(span.end.column, equals(2));
- expect(span.start.sourceUrl, equals('source'));
+ expect(span.start.sourceUrl, equals(Uri.parse('source')));
expect(span.text, equals('o\nba'));
});
@@ -48,12 +48,12 @@
expect(span.start.offset, equals(6));
expect(span.start.line, equals(1));
expect(span.start.column, equals(2));
- expect(span.start.sourceUrl, equals('source'));
+ expect(span.start.sourceUrl, equals(Uri.parse('source')));
expect(span.end.offset, equals(6));
expect(span.end.line, equals(1));
expect(span.end.column, equals(2));
- expect(span.start.sourceUrl, equals('source'));
+ expect(span.start.sourceUrl, equals(Uri.parse('source')));
expect(span.text, equals(''));
});