Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 4a8bf6e

Browse files
authored
Merge the null_safety branch into master (#22)
1 parent 42d1f44 commit 4a8bf6e

17 files changed

+153
-79
lines changed

.travis.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ language: dart
22

33
dart:
44
- dev
5-
- 2.0.0
65

76
dart_task:
87
- test: --platform vm,chrome
@@ -15,13 +14,10 @@ matrix:
1514
- dart: dev
1615
dart_task:
1716
dartanalyzer: --fatal-warnings --fatal-hints .
18-
- dart: 2.0.0
19-
dart_task:
20-
dartanalyzer: --fatal-warnings .
2117

2218
# Only building master means that we don't run two builds for each pull request.
2319
branches:
24-
only: [master]
20+
only: [master, null_safety]
2521

2622
cache:
2723
directories:

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.0-nullsafety
2+
3+
- Migrate to null safety.
4+
15
## 1.0.5
26

37
- Added an example.

analysis_options.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ analyzer:
44
strong-mode:
55
implicit-casts: false
66

7+
enable-experiment:
8+
- non-nullable
9+
710
linter:
811
rules:
912
- avoid_bool_literals_in_conditional_expressions

example/example.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ num parseNumber(String source) {
2424

2525
// [Scanner.lastMatch] holds the [MatchData] for the most recent call to
2626
// [Scanner.scan], [Scanner.expect], or [Scanner.matches].
27-
var number = num.parse(scanner.lastMatch[0]);
27+
var number = num.parse((scanner.lastMatch![0])!);
2828

2929
if (scanner.scan('.')) {
3030
scanner.expect(RegExp(r'\d+'));
31-
final decimal = scanner.lastMatch[0];
31+
final decimal = scanner.lastMatch![0]!;
3232
number += int.parse(decimal) / math.pow(10, decimal.length);
3333
}
3434

lib/src/eager_span_scanner.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class EagerSpanScanner extends SpanScanner {
3131

3232
@override
3333
set state(LineScannerState state) {
34-
if (state is! _EagerSpanScannerState ||
35-
!identical((state as _EagerSpanScannerState)._scanner, this)) {
34+
if (state is! _EagerSpanScannerState || !identical(state._scanner, this)) {
3635
throw ArgumentError('The given LineScannerState was not returned by '
3736
'this LineScanner.');
3837
}
@@ -69,7 +68,7 @@ class EagerSpanScanner extends SpanScanner {
6968
}
7069
}
7170

72-
EagerSpanScanner(String string, {sourceUrl, int position})
71+
EagerSpanScanner(String string, {sourceUrl, int? position})
7372
: super(string, sourceUrl: sourceUrl, position: position);
7473

7574
@override
@@ -99,13 +98,14 @@ class EagerSpanScanner extends SpanScanner {
9998
@override
10099
bool scan(Pattern pattern) {
101100
if (!super.scan(pattern)) return false;
101+
final firstMatch = (lastMatch![0])!;
102102

103-
final newlines = _newlinesIn(lastMatch[0]);
103+
final newlines = _newlinesIn(firstMatch);
104104
_line += newlines.length;
105105
if (newlines.isEmpty) {
106-
_column += lastMatch[0].length;
106+
_column += firstMatch.length;
107107
} else {
108-
_column = lastMatch[0].length - newlines.last.end;
108+
_column = firstMatch.length - newlines.last.end;
109109
}
110110

111111
return true;

lib/src/exception.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class StringScannerException extends SourceSpanFormatException {
1414
/// The URL of the source file being parsed.
1515
///
1616
/// This may be `null`, indicating that the source URL is unknown.
17-
Uri get sourceUrl => span.sourceUrl;
17+
Uri? get sourceUrl => span?.sourceUrl;
1818

1919
StringScannerException(String message, SourceSpan span, String source)
2020
: super(message, span, source);

lib/src/line_scanner.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class LineScanner extends StringScanner {
7373
}
7474
}
7575

76-
LineScanner(String string, {sourceUrl, int position})
76+
LineScanner(String string, {sourceUrl, int? position})
7777
: super(string, sourceUrl: sourceUrl, position: position);
7878

7979
@override
@@ -104,12 +104,12 @@ class LineScanner extends StringScanner {
104104
bool scan(Pattern pattern) {
105105
if (!super.scan(pattern)) return false;
106106

107-
final newlines = _newlinesIn(lastMatch[0]);
107+
final newlines = _newlinesIn(lastMatch![0]!);
108108
_line += newlines.length;
109109
if (newlines.isEmpty) {
110-
_column += lastMatch[0].length;
110+
_column += (lastMatch![0])!.length;
111111
} else {
112-
_column = lastMatch[0].length - newlines.last.end;
112+
_column = (lastMatch![0])!.length - newlines.last.end;
113113
}
114114

115115
return true;

lib/src/relative_span_scanner.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ class RelativeSpanScanner extends StringScanner implements SpanScanner {
4747

4848
@override
4949
set state(LineScannerState state) {
50-
if (state is! _SpanScannerState ||
51-
!identical((state as _SpanScannerState)._scanner, this)) {
50+
if (state is! _SpanScannerState || !identical(state._scanner, this)) {
5251
throw ArgumentError('The given LineScannerState was not returned by '
5352
'this LineScanner.');
5453
}
@@ -57,8 +56,8 @@ class RelativeSpanScanner extends StringScanner implements SpanScanner {
5756
}
5857

5958
@override
60-
FileSpan get lastSpan => _lastSpan;
61-
FileSpan _lastSpan;
59+
FileSpan? get lastSpan => _lastSpan;
60+
FileSpan? _lastSpan;
6261

6362
@override
6463
FileLocation get location =>
@@ -73,7 +72,7 @@ class RelativeSpanScanner extends StringScanner implements SpanScanner {
7372
super(span.text, sourceUrl: span.sourceUrl);
7473

7574
@override
76-
FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) {
75+
FileSpan spanFrom(LineScannerState startState, [LineScannerState? endState]) {
7776
final endPosition = endState == null ? position : endState.position;
7877
return _sourceFile.span(_startLocation.offset + startState.position,
7978
_startLocation.offset + endPosition);
@@ -87,12 +86,12 @@ class RelativeSpanScanner extends StringScanner implements SpanScanner {
8786
}
8887

8988
_lastSpan = _sourceFile.span(_startLocation.offset + position,
90-
_startLocation.offset + lastMatch.end);
89+
_startLocation.offset + lastMatch!.end);
9190
return true;
9291
}
9392

9493
@override
95-
void error(String message, {Match match, int position, int length}) {
94+
Never error(String message, {Match? match, int? position, int? length}) {
9695
validateErrorArgs(string, match, position, length);
9796

9897
if (match == null && position == null && length == null) match = lastMatch;

lib/src/span_scanner.dart

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class SpanScanner extends StringScanner implements LineScanner {
2929

3030
@override
3131
set state(LineScannerState state) {
32-
if (state is! _SpanScannerState ||
33-
!identical((state as _SpanScannerState)._scanner, this)) {
32+
if (state is! _SpanScannerState || !identical(state._scanner, this)) {
3433
throw ArgumentError('The given LineScannerState was not returned by '
3534
'this LineScanner.');
3635
}
@@ -42,12 +41,12 @@ class SpanScanner extends StringScanner implements LineScanner {
4241
///
4342
/// This is the span for the entire match. There's no way to get spans for
4443
/// subgroups since [Match] exposes no information about their positions.
45-
FileSpan get lastSpan {
44+
FileSpan? get lastSpan {
4645
if (lastMatch == null) _lastSpan = null;
4746
return _lastSpan;
4847
}
4948

50-
FileSpan _lastSpan;
49+
FileSpan? _lastSpan;
5150

5251
/// The current location of the scanner.
5352
FileLocation get location => _sourceFile.location(position);
@@ -60,7 +59,7 @@ class SpanScanner extends StringScanner implements LineScanner {
6059
/// [sourceUrl] is used as [SourceLocation.sourceUrl] for the returned
6160
/// [FileSpan]s as well as for error reporting. It can be a [String], a
6261
/// [Uri], or `null`.
63-
SpanScanner(String string, {sourceUrl, int position})
62+
SpanScanner(String string, {sourceUrl, int? position})
6463
: _sourceFile = SourceFile.fromString(string, url: sourceUrl),
6564
super(string, sourceUrl: sourceUrl, position: position);
6665

@@ -75,7 +74,7 @@ class SpanScanner extends StringScanner implements LineScanner {
7574
/// itself and its `LineScannerState` are eagerly computed. To limit their
7675
/// memory footprint, returned spans and locations will still lazily compute
7776
/// their line and column numbers.
78-
factory SpanScanner.eager(String string, {sourceUrl, int position}) =
77+
factory SpanScanner.eager(String string, {sourceUrl, int? position}) =
7978
EagerSpanScanner;
8079

8180
/// Creates a new [SpanScanner] that scans within [span].
@@ -88,7 +87,7 @@ class SpanScanner extends StringScanner implements LineScanner {
8887

8988
/// Creates a [FileSpan] representing the source range between [startState]
9089
/// and the current position.
91-
FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) {
90+
FileSpan spanFrom(LineScannerState startState, [LineScannerState? endState]) {
9291
final endPosition = endState == null ? position : endState.position;
9392
return _sourceFile.span(startState.position, endPosition);
9493
}
@@ -100,12 +99,12 @@ class SpanScanner extends StringScanner implements LineScanner {
10099
return false;
101100
}
102101

103-
_lastSpan = _sourceFile.span(position, lastMatch.end);
102+
_lastSpan = _sourceFile.span(position, lastMatch!.end);
104103
return true;
105104
}
106105

107106
@override
108-
void error(String message, {Match match, int position, int length}) {
107+
Never error(String message, {Match? match, int? position, int? length}) {
109108
validateErrorArgs(string, match, position, length);
110109

111110
if (match == null && position == null && length == null) match = lastMatch;

lib/src/string_scanner.dart

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:charcode/charcode.dart';
6-
import 'package:meta/meta.dart';
76
import 'package:source_span/source_span.dart';
87

98
import 'exception.dart';
@@ -15,7 +14,7 @@ class StringScanner {
1514
///
1615
/// This is used for error reporting. It may be `null`, indicating that the
1716
/// source URL is unknown or unavailable.
18-
final Uri sourceUrl;
17+
final Uri? sourceUrl;
1918

2019
/// The string being scanned through.
2120
final String string;
@@ -36,15 +35,15 @@ class StringScanner {
3635
/// The data about the previous match made by the scanner.
3736
///
3837
/// If the last match failed, this will be `null`.
39-
Match get lastMatch {
38+
Match? get lastMatch {
4039
// Lazily unset [_lastMatch] so that we avoid extra assignments in
4140
// character-by-character methods that are used in core loops.
4241
if (_position != _lastMatchPosition) _lastMatch = null;
4342
return _lastMatch;
4443
}
4544

46-
Match _lastMatch;
47-
int _lastMatchPosition;
45+
Match? _lastMatch;
46+
int? _lastMatchPosition;
4847

4948
/// The portion of the string that hasn't yet been scanned.
5049
String get rest => string.substring(position);
@@ -57,9 +56,10 @@ class StringScanner {
5756
/// [position] defaults to 0, the beginning of the string. [sourceUrl] is the
5857
/// URL of the source of the string being scanned, if available. It can be
5958
/// a [String], a [Uri], or `null`.
60-
StringScanner(this.string, {sourceUrl, int position})
61-
: sourceUrl =
62-
sourceUrl is String ? Uri.parse(sourceUrl) : sourceUrl as Uri {
59+
StringScanner(this.string, {sourceUrl, int? position})
60+
: sourceUrl = sourceUrl == null
61+
? null
62+
: sourceUrl is String ? Uri.parse(sourceUrl) : sourceUrl as Uri {
6363
if (position != null) this.position = position;
6464
}
6565

@@ -79,7 +79,7 @@ class StringScanner {
7979
///
8080
/// This returns `null` if [offset] points outside the string. It doesn't
8181
/// affect [lastMatch].
82-
int peekChar([int offset]) {
82+
int? peekChar([int? offset]) {
8383
offset ??= 0;
8484
final index = position + offset;
8585
if (index < 0 || index >= string.length) return null;
@@ -102,7 +102,7 @@ class StringScanner {
102102
/// describing the position of the failure. [name] is used in this error as
103103
/// the expected name of the character being matched; if it's `null`, the
104104
/// character itself is used instead.
105-
void expectChar(int character, {String name}) {
105+
void expectChar(int character, {String? name}) {
106106
if (scanChar(character)) return;
107107

108108
if (name == null) {
@@ -125,7 +125,7 @@ class StringScanner {
125125
bool scan(Pattern pattern) {
126126
final success = matches(pattern);
127127
if (success) {
128-
_position = _lastMatch.end;
128+
_position = _lastMatch!.end;
129129
_lastMatchPosition = _position;
130130
}
131131
return success;
@@ -138,7 +138,7 @@ class StringScanner {
138138
/// position of the failure. [name] is used in this error as the expected name
139139
/// of the pattern being matched; if it's `null`, the pattern itself is used
140140
/// instead.
141-
void expect(Pattern pattern, {String name}) {
141+
void expect(Pattern pattern, {String? name}) {
142142
if (scan(pattern)) return;
143143

144144
if (name == null) {
@@ -175,7 +175,7 @@ class StringScanner {
175175
///
176176
/// Unlike [String.substring], [end] defaults to [position] rather than the
177177
/// end of the string.
178-
String substring(int start, [int end]) {
178+
String substring(int start, [int? end]) {
179179
end ??= position;
180180
return string.substring(start, end);
181181
}
@@ -193,8 +193,7 @@ class StringScanner {
193193
/// position; if only [position] is passed, [length] defaults to 0.
194194
///
195195
/// It's an error to pass [match] at the same time as [position] or [length].
196-
@alwaysThrows
197-
void error(String message, {Match match, int position, int length}) {
196+
Never error(String message, {Match? match, int? position, int? length}) {
198197
validateErrorArgs(string, match, position, length);
199198

200199
if (match == null && position == null && length == null) match = lastMatch;
@@ -209,7 +208,7 @@ class StringScanner {
209208
// TODO(nweiz): Make this handle long lines more gracefully.
210209
/// Throws a [FormatException] describing that [name] is expected at the
211210
/// current position in the string.
212-
void _fail(String name) {
211+
Never _fail(String name) {
213212
error('expected $name.', position: position, length: 0);
214213
}
215214
}

0 commit comments

Comments
 (0)