Skip to content

Commit

Permalink
Fix latest pedantic lints, remove deprecated author field in pubspec (f…
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo authored and natebosch committed Dec 5, 2019
1 parent a918e73 commit eaf5817
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 104 deletions.
2 changes: 0 additions & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ analyzer:
linter:
rules:
- always_declare_return_types
#- annotate_overrides
- avoid_bool_literals_in_conditional_expressions
- avoid_classes_with_only_static_members
- avoid_empty_else
Expand Down Expand Up @@ -68,7 +67,6 @@ linter:
- prefer_is_empty
- prefer_is_not_empty
- prefer_null_aware_operators
#- prefer_single_quotes
- prefer_typing_uninitialized_variables
- recursive_getters
- slash_for_doc_comments
Expand Down
8 changes: 4 additions & 4 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ num parseNumber(String source) {

// [Scanner.scan] tries to consume a [Pattern] and returns whether or not it
// succeeded. It will move the scan pointer past the end of the pattern.
var negative = scanner.scan("-");
var negative = scanner.scan('-');

// [Scanner.expect] consumes a [Pattern] and throws a [FormatError] if it
// fails. Like [Scanner.scan], it will move the scan pointer forward.
scanner.expect(RegExp(r"\d+"));
scanner.expect(RegExp(r'\d+'));

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

if (scanner.scan(".")) {
scanner.expect(RegExp(r"\d+"));
if (scanner.scan('.')) {
scanner.expect(RegExp(r'\d+'));
var decimal = scanner.lastMatch[0];
number += int.parse(decimal) / math.pow(10, decimal.length);
}
Expand Down
17 changes: 14 additions & 3 deletions lib/src/eager_span_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,38 @@ import 'span_scanner.dart';
// sdk#23770 is fully complete, we should move the shared code into a mixin.

/// A regular expression matching newlines across platforms.
final _newlineRegExp = RegExp(r"\r\n?|\n");
final _newlineRegExp = RegExp(r'\r\n?|\n');

/// A [SpanScanner] that tracks the line and column eagerly, like [LineScanner].
class EagerSpanScanner extends SpanScanner {
@override
int get line => _line;
int _line = 0;

@override
int get column => _column;
int _column = 0;

@override
LineScannerState get state =>
_EagerSpanScannerState(this, position, line, column);

bool get _betweenCRLF => peekChar(-1) == $cr && peekChar() == $lf;

@override
set state(LineScannerState state) {
if (state is! _EagerSpanScannerState ||
!identical((state as _EagerSpanScannerState)._scanner, this)) {
throw ArgumentError("The given LineScannerState was not returned by "
"this LineScanner.");
throw ArgumentError('The given LineScannerState was not returned by '
'this LineScanner.');
}

super.position = state.position;
_line = state.line;
_column = state.column;
}

@override
set position(int newPosition) {
var oldPosition = position;
super.position = newPosition;
Expand Down Expand Up @@ -67,12 +72,14 @@ class EagerSpanScanner extends SpanScanner {
EagerSpanScanner(String string, {sourceUrl, int position})
: super(string, sourceUrl: sourceUrl, position: position);

@override
bool scanChar(int character) {
if (!super.scanChar(character)) return false;
_adjustLineAndColumn(character);
return true;
}

@override
int readChar() {
var character = super.readChar();
_adjustLineAndColumn(character);
Expand All @@ -89,6 +96,7 @@ class EagerSpanScanner extends SpanScanner {
}
}

@override
bool scan(Pattern pattern) {
if (!super.scan(pattern)) return false;

Expand All @@ -115,8 +123,11 @@ class EagerSpanScanner extends SpanScanner {
/// A class representing the state of an [EagerSpanScanner].
class _EagerSpanScannerState implements LineScannerState {
final EagerSpanScanner _scanner;
@override
final int position;
@override
final int line;
@override
final int column;

_EagerSpanScannerState(this._scanner, this.position, this.line, this.column);
Expand Down
1 change: 1 addition & 0 deletions lib/src/exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'string_scanner.dart';

/// An exception thrown by a [StringScanner] that failed to parse a string.
class StringScannerException extends SourceSpanFormatException {
@override
String get source => super.source as String;

/// The URL of the source file being parsed.
Expand Down
10 changes: 7 additions & 3 deletions lib/src/line_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'string_scanner.dart';
// Note that much of this code is duplicated in eager_span_scanner.dart.

/// A regular expression matching newlines across platforms.
final _newlineRegExp = RegExp(r"\r\n?|\n");
final _newlineRegExp = RegExp(r'\r\n?|\n');

/// A subclass of [StringScanner] that tracks line and column information.
class LineScanner extends StringScanner {
Expand Down Expand Up @@ -37,15 +37,16 @@ class LineScanner extends StringScanner {

set state(LineScannerState state) {
if (!identical(state._scanner, this)) {
throw ArgumentError("The given LineScannerState was not returned by "
"this LineScanner.");
throw ArgumentError('The given LineScannerState was not returned by '
'this LineScanner.');
}

super.position = state.position;
_line = state.line;
_column = state.column;
}

@override
set position(int newPosition) {
var oldPosition = position;
super.position = newPosition;
Expand Down Expand Up @@ -75,12 +76,14 @@ class LineScanner extends StringScanner {
LineScanner(String string, {sourceUrl, int position})
: super(string, sourceUrl: sourceUrl, position: position);

@override
bool scanChar(int character) {
if (!super.scanChar(character)) return false;
_adjustLineAndColumn(character);
return true;
}

@override
int readChar() {
var character = super.readChar();
_adjustLineAndColumn(character);
Expand All @@ -97,6 +100,7 @@ class LineScanner extends StringScanner {
}
}

@override
bool scan(Pattern pattern) {
if (!super.scan(pattern)) return false;

Expand Down
17 changes: 15 additions & 2 deletions lib/src/relative_span_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ class RelativeSpanScanner extends StringScanner implements SpanScanner {
/// This is used to convert between span-relative and file-relative fields.
final FileLocation _startLocation;

@override
int get line =>
_sourceFile.getLine(_startLocation.offset + position) -
_startLocation.line;

@override
int get column {
var line = _sourceFile.getLine(_startLocation.offset + position);
var column =
Expand All @@ -40,37 +42,44 @@ class RelativeSpanScanner extends StringScanner implements SpanScanner {
: column;
}

@override
LineScannerState get state => _SpanScannerState(this, position);

@override
set state(LineScannerState state) {
if (state is! _SpanScannerState ||
!identical((state as _SpanScannerState)._scanner, this)) {
throw ArgumentError("The given LineScannerState was not returned by "
"this LineScanner.");
throw ArgumentError('The given LineScannerState was not returned by '
'this LineScanner.');
}

position = state.position;
}

@override
FileSpan get lastSpan => _lastSpan;
FileSpan _lastSpan;

@override
FileLocation get location =>
_sourceFile.location(_startLocation.offset + position);

@override
FileSpan get emptySpan => location.pointSpan();

RelativeSpanScanner(FileSpan span)
: _sourceFile = span.file,
_startLocation = span.start,
super(span.text, sourceUrl: span.sourceUrl);

@override
FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) {
var endPosition = endState == null ? position : endState.position;
return _sourceFile.span(_startLocation.offset + startState.position,
_startLocation.offset + endPosition);
}

@override
bool matches(Pattern pattern) {
if (!super.matches(pattern)) {
_lastSpan = null;
Expand All @@ -82,6 +91,7 @@ class RelativeSpanScanner extends StringScanner implements SpanScanner {
return true;
}

@override
void error(String message, {Match match, int position, int length}) {
validateErrorArgs(string, match, position, length);

Expand All @@ -100,8 +110,11 @@ class _SpanScannerState implements LineScannerState {
/// The [SpanScanner] that created this.
final RelativeSpanScanner _scanner;

@override
final int position;
@override
int get line => _scanner._sourceFile.getLine(position);
@override
int get column => _scanner._sourceFile.getColumn(position);

_SpanScannerState(this._scanner, this.position);
Expand Down
13 changes: 11 additions & 2 deletions lib/src/span_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ class SpanScanner extends StringScanner implements LineScanner {
/// This caches line break information and is used to generate [FileSpan]s.
final SourceFile _sourceFile;

@override
int get line => _sourceFile.getLine(position);
@override
int get column => _sourceFile.getColumn(position);

@override
LineScannerState get state => _SpanScannerState(this, position);

@override
set state(LineScannerState state) {
if (state is! _SpanScannerState ||
!identical((state as _SpanScannerState)._scanner, this)) {
throw ArgumentError("The given LineScannerState was not returned by "
"this LineScanner.");
throw ArgumentError('The given LineScannerState was not returned by '
'this LineScanner.');
}

position = state.position;
Expand Down Expand Up @@ -89,6 +93,7 @@ class SpanScanner extends StringScanner implements LineScanner {
return _sourceFile.span(startState.position, endPosition);
}

@override
bool matches(Pattern pattern) {
if (!super.matches(pattern)) {
_lastSpan = null;
Expand All @@ -99,6 +104,7 @@ class SpanScanner extends StringScanner implements LineScanner {
return true;
}

@override
void error(String message, {Match match, int position, int length}) {
validateErrorArgs(string, match, position, length);

Expand All @@ -116,8 +122,11 @@ class _SpanScannerState implements LineScannerState {
/// The [SpanScanner] that created this.
final SpanScanner _scanner;

@override
final int position;
@override
int get line => _scanner._sourceFile.getLine(position);
@override
int get column => _scanner._sourceFile.getColumn(position);

_SpanScannerState(this._scanner, this.position);
Expand Down
12 changes: 6 additions & 6 deletions lib/src/string_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class StringScanner {
int get position => _position;
set position(int position) {
if (position < 0 || position > string.length) {
throw ArgumentError("Invalid position $position");
throw ArgumentError('Invalid position $position');
}

_position = position;
Expand Down Expand Up @@ -68,7 +68,7 @@ class StringScanner {
/// This throws a [FormatException] if the string has been fully consumed. It
/// doesn't affect [lastMatch].
int readChar() {
if (isDone) _fail("more input");
if (isDone) _fail('more input');
return string.codeUnitAt(_position++);
}

Expand Down Expand Up @@ -144,10 +144,10 @@ class StringScanner {
if (name == null) {
if (pattern is RegExp) {
var source = pattern.pattern;
name = "/$source/";
name = '/$source/';
} else {
name =
pattern.toString().replaceAll("\\", "\\\\").replaceAll('"', '\\"');
pattern.toString().replaceAll('\\', '\\\\').replaceAll('"', '\\"');
name = '"$name"';
}
}
Expand All @@ -158,7 +158,7 @@ class StringScanner {
/// [FormatException].
void expectDone() {
if (isDone) return;
_fail("no more input");
_fail('no more input');
}

/// Returns whether or not [pattern] matches at the current position of the
Expand Down Expand Up @@ -210,6 +210,6 @@ class StringScanner {
/// Throws a [FormatException] describing that [name] is expected at the
/// current position in the string.
void _fail(String name) {
error("expected $name.", position: position, length: 0);
error('expected $name.', position: position, length: 0);
}
}
12 changes: 6 additions & 6 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ void validateErrorArgs(String string, Match match, int position, int length) {

if (position != null) {
if (position < 0) {
throw RangeError("position must be greater than or equal to 0.");
throw RangeError('position must be greater than or equal to 0.');
} else if (position > string.length) {
throw RangeError("position must be less than or equal to the "
"string length.");
throw RangeError('position must be less than or equal to the '
'string length.');
}
}

if (length != null && length < 0) {
throw RangeError("length must be greater than or equal to 0.");
throw RangeError('length must be greater than or equal to 0.');
}

if (position != null && length != null && position + length > string.length) {
throw RangeError("position plus length must not go beyond the end of "
"the string.");
throw RangeError('position plus length must not go beyond the end of '
'the string.');
}
}
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: string_scanner
version: 1.0.5

description: A class for parsing strings using a sequence of patterns.
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/string_scanner

environment:
Expand Down
Loading

0 comments on commit eaf5817

Please sign in to comment.