33// BSD-style license that can be found in the LICENSE file.
44
55import 'package:charcode/charcode.dart' ;
6- import 'package:meta/meta.dart' ;
76import 'package:source_span/source_span.dart' ;
87
98import '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