From 26d9936a3f0baae708a48e696117e9ffa6369954 Mon Sep 17 00:00:00 2001 From: thomashii Date: Tue, 18 May 2021 21:15:28 +0800 Subject: [PATCH] Updated combinator --- packages/combinator/CHANGELOG.md | 3 +++ packages/combinator/README.md | 2 +- packages/combinator/analysis_options.yaml | 1 + packages/combinator/example/basic_auth.dart | 3 ++- packages/combinator/example/calculator.dart | 3 ++- packages/combinator/example/delimiter.dart | 5 ++-- packages/combinator/example/json.dart | 5 ++-- packages/combinator/example/main.dart | 5 ++-- packages/combinator/example/query_string.dart | 5 ++-- packages/combinator/example/sexp.dart | 7 ++--- .../combinator/lib/src/combinator/any.dart | 12 ++++----- .../combinator/lib/src/combinator/chain.dart | 8 +++--- .../combinator/lib/src/combinator/check.dart | 7 ++--- .../lib/src/combinator/combinator.dart | 27 ++++++++++--------- .../lib/src/combinator/compare.dart | 2 +- .../combinator/lib/src/combinator/index.dart | 5 ++-- .../lib/src/combinator/longest.dart | 17 ++++++------ .../combinator/lib/src/combinator/match.dart | 3 ++- .../combinator/lib/src/combinator/opt.dart | 2 +- .../combinator/lib/src/combinator/reduce.dart | 5 ++-- .../lib/src/combinator/reference.dart | 11 +++++--- .../combinator/lib/src/combinator/repeat.dart | 3 ++- .../lib/src/combinator/to_list.dart | 2 +- packages/combinator/pubspec.yaml | 5 ++-- packages/combinator/test/list_test.dart | 2 +- packages/combinator/test/recursion_test.dart | 4 --- 26 files changed, 85 insertions(+), 69 deletions(-) diff --git a/packages/combinator/CHANGELOG.md b/packages/combinator/CHANGELOG.md index e3710992..cea07a47 100644 --- a/packages/combinator/CHANGELOG.md +++ b/packages/combinator/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.2 +* Resolve static analysis warnings + # 2.0.1 * Updated README diff --git a/packages/combinator/README.md b/packages/combinator/README.md index bd61f1fd..0c0e509e 100644 --- a/packages/combinator/README.md +++ b/packages/combinator/README.md @@ -1,5 +1,5 @@ # angel3_combinator -[![version](https://img.shields.io/badge/pub-v2.0.1-brightgreen)](https://pub.dartlang.org/packages/angel3_combinator) +[![version](https://img.shields.io/badge/pub-v2.0.2-brightgreen)](https://pub.dartlang.org/packages/angel3_combinator) [![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) diff --git a/packages/combinator/analysis_options.yaml b/packages/combinator/analysis_options.yaml index bbc55fe4..8dc465d4 100644 --- a/packages/combinator/analysis_options.yaml +++ b/packages/combinator/analysis_options.yaml @@ -1,3 +1,4 @@ +include: package:pedantic/analysis_options.yaml analyzer: strong-mode: implicit-casts: false diff --git a/packages/combinator/example/basic_auth.dart b/packages/combinator/example/basic_auth.dart index 15128da6..623be5c7 100644 --- a/packages/combinator/example/basic_auth.dart +++ b/packages/combinator/example/basic_auth.dart @@ -49,7 +49,8 @@ void main() { print(error.toolString); print(error.span!.highlight(color: true)); } - } else + } else { print(result.value); + } } } diff --git a/packages/combinator/example/calculator.dart b/packages/combinator/example/calculator.dart index e0b46083..552bc744 100644 --- a/packages/combinator/example/calculator.dart +++ b/packages/combinator/example/calculator.dart @@ -64,7 +64,8 @@ void main() { stderr.writeln(error.toolString); stderr.writeln(error.span!.highlight(color: true)); } - } else + } else { print(result.value); + } } } diff --git a/packages/combinator/example/delimiter.dart b/packages/combinator/example/delimiter.dart index 71008e47..4571790c 100644 --- a/packages/combinator/example/delimiter.dart +++ b/packages/combinator/example/delimiter.dart @@ -10,7 +10,7 @@ final Parser id = // pattern. // // This is useful for parsing arrays or map literals. -main() { +void main() { while (true) { stdout.write('Enter a string (ex "a,b,c"): '); var line = stdin.readLineSync()!; @@ -22,7 +22,8 @@ main() { print(error.toolString); print(error.span!.highlight(color: true)); } - } else + } else { print(result.value); + } } } diff --git a/packages/combinator/example/json.dart b/packages/combinator/example/json.dart index 36eff7bf..caaba890 100644 --- a/packages/combinator/example/json.dart +++ b/packages/combinator/example/json.dart @@ -50,7 +50,7 @@ Parser jsonGrammar() { return expr.foldErrors(); } -main() { +void main() { var JSON = jsonGrammar(); while (true) { @@ -64,7 +64,8 @@ main() { print(error.toolString); print(error.span!.highlight(color: true)); } - } else + } else { print(result.value); + } } } diff --git a/packages/combinator/example/main.dart b/packages/combinator/example/main.dart index 2ed8d8b3..1ea328c6 100644 --- a/packages/combinator/example/main.dart +++ b/packages/combinator/example/main.dart @@ -19,7 +19,7 @@ final Parser number = // (minus.opt() & decimal) // minus?, decimal .map((r) => num.parse(r.span!.text)); -main() { +void main() { while (true) { stdout.write('Enter a number: '); var line = stdin.readLineSync()!; @@ -31,7 +31,8 @@ main() { stderr.writeln(error.toolString); stderr.writeln(error.span!.highlight(color: true)); } - } else + } else { print(result.value); + } } } diff --git a/packages/combinator/example/query_string.dart b/packages/combinator/example/query_string.dart index 4e7c3a6a..4a42fff7 100644 --- a/packages/combinator/example/query_string.dart +++ b/packages/combinator/example/query_string.dart @@ -26,7 +26,7 @@ final Parser pairs = pair final Parser queryString = pairs.opt(); -main() { +void main() { while (true) { stdout.write('Enter a query string: '); var line = stdin.readLineSync()!; @@ -38,7 +38,8 @@ main() { print(error.toolString); print(error.span!.highlight(color: true)); } - } else + } else { print(result.value); + } } } diff --git a/packages/combinator/example/sexp.dart b/packages/combinator/example/sexp.dart index 541d3c35..9b51d626 100644 --- a/packages/combinator/example/sexp.dart +++ b/packages/combinator/example/sexp.dart @@ -45,12 +45,13 @@ void main() { while (q.isNotEmpty) { var current = q.removeFirst(); - if (current is! Tuple2) + if (current is! Tuple2) { out.insert(0, current); - else { + } else { var args = []; - for (int i = 0; i < (current.item1 as num); i++) + for (var i = 0; i < (current.item1 as num); i++) { args.add(out.removeLast()); + } out.add(current.item2(args)); } } diff --git a/packages/combinator/lib/src/combinator/any.dart b/packages/combinator/lib/src/combinator/any.dart index 616bf29d..45bc107a 100644 --- a/packages/combinator/lib/src/combinator/any.dart +++ b/packages/combinator/lib/src/combinator/any.dart @@ -7,7 +7,7 @@ part of lex.src.combinator; /// You can provide a custom [errorMessage]. You can set it to `false` to not /// generate any error at all. Parser any(Iterable> parsers, - {bool backtrack: true, errorMessage, SyntaxErrorSeverity? severity}) { + {bool backtrack = true, errorMessage, SyntaxErrorSeverity? severity}) { return _Any(parsers, backtrack != false, errorMessage, severity ?? SyntaxErrorSeverity.error); } @@ -30,14 +30,14 @@ class _Any extends Parser { } var errors = []; - int replay = args.scanner.position; + var replay = args.scanner.position; for (var parser in inactive) { var result = parser._parse(args.increaseDepth()); - if (result.successful) + if (result.successful) { return result; - else { + } else { if (backtrack) args.scanner.position = replay; if (parser is _Alt) errors.addAll(result.errors); } @@ -60,7 +60,7 @@ class _Any extends Parser { @override ParseResult __parse(ParseArgs args) { // Never called - throw ArgumentError("[Combinator] Invalid method call"); + throw ArgumentError('[Combinator] Invalid method call'); } @override @@ -68,7 +68,7 @@ class _Any extends Parser { buffer ..writeln('any(${parsers.length}) (') ..indent(); - int i = 1; + var i = 1; for (var parser in parsers) { buffer diff --git a/packages/combinator/lib/src/combinator/chain.dart b/packages/combinator/lib/src/combinator/chain.dart index 39101df5..101836b4 100644 --- a/packages/combinator/lib/src/combinator/chain.dart +++ b/packages/combinator/lib/src/combinator/chain.dart @@ -4,7 +4,7 @@ part of lex.src.combinator; /// /// If [failFast] is `true` (default), then the first failure to parse will abort the parse. ListParser chain(Iterable> parsers, - {bool failFast: true, SyntaxErrorSeverity? severity}) { + {bool failFast = true, SyntaxErrorSeverity? severity}) { return _Chain( parsers, failFast != false, severity ?? SyntaxErrorSeverity.error); } @@ -45,7 +45,7 @@ class _Chain extends ListParser { var errors = []; var results = []; var spans = []; - bool successful = true; + var successful = true; for (var parser in parsers) { var result = parser._parse(args.increaseDepth()); @@ -64,7 +64,7 @@ class _Chain extends ListParser { if (result.value != null) { results.add(result.value!); } else { - results.add("NULL" as T); + results.add('NULL' as T); } if (result.span != null) { @@ -94,7 +94,7 @@ class _Chain extends ListParser { buffer ..writeln('chain(${parsers.length}) (') ..indent(); - int i = 1; + var i = 1; for (var parser in parsers) { buffer diff --git a/packages/combinator/lib/src/combinator/check.dart b/packages/combinator/lib/src/combinator/check.dart index c15d7051..a6f7bedc 100644 --- a/packages/combinator/lib/src/combinator/check.dart +++ b/packages/combinator/lib/src/combinator/check.dart @@ -12,9 +12,9 @@ class _Check extends Parser { ParseResult __parse(ParseArgs args) { var matchState = {}; var result = parser._parse(args.increaseDepth()).change(parser: this); - if (!result.successful) + if (!result.successful) { return result; - else if (!matcher.matches(result.value, matchState)) { + } else if (!matcher.matches(result.value, matchState)) { return result.change(successful: false).addErrors([ SyntaxError( severity, @@ -23,8 +23,9 @@ class _Check extends Parser { result.span, ), ]); - } else + } else { return result; + } } @override diff --git a/packages/combinator/lib/src/combinator/combinator.dart b/packages/combinator/lib/src/combinator/combinator.dart index 3b2fcc7a..717365ab 100644 --- a/packages/combinator/lib/src/combinator/combinator.dart +++ b/packages/combinator/lib/src/combinator/combinator.dart @@ -72,11 +72,13 @@ abstract class Parser { ParseResult _parse(ParseArgs args) { var pos = args.scanner.position; - if (args.trampoline.hasMemoized(this, pos)) + if (args.trampoline.hasMemoized(this, pos)) { return args.trampoline.getMemoized(this, pos); + } - if (args.trampoline.isActive(this, pos)) + if (args.trampoline.isActive(this, pos)) { return ParseResult(args.trampoline, args.scanner, this, false, []); + } args.trampoline.enter(this, pos); var result = __parse(args); @@ -103,7 +105,6 @@ abstract class Parser { /// Casts this parser to produce [dynamic] objects. Parser castDynamic() => _CastDynamic(this); - // TODO: Type issue /// Runs the given function, which changes the returned [ParseResult] into one relating to a [U] object. Parser change(ParseResult Function(ParseResult) f) { return _Change(this, f); @@ -122,7 +123,7 @@ abstract class Parser { _Alt(this, errorMessage, severity ?? SyntaxErrorSeverity.error); /// Removes multiple errors that occur in the same spot; this can reduce noise in parser output. - Parser foldErrors({bool equal(SyntaxError a, SyntaxError b)?}) { + Parser foldErrors({bool Function(SyntaxError a, SyntaxError b)? equal}) { equal ??= (b, e) => b.span?.start.offset == e.span?.start.offset; return _FoldErrors(this, equal); } @@ -177,8 +178,8 @@ abstract class Parser { /// /// The generated parser only runs once; repeated uses always exit eagerly. Parser safe( - {bool backtrack: true, - String errorMessage = "error", + {bool backtrack = true, + String errorMessage = 'error', SyntaxErrorSeverity? severity}) => _Safe( this, backtrack, errorMessage, severity ?? SyntaxErrorSeverity.error); @@ -191,14 +192,14 @@ abstract class Parser { /// Use this as a shortcut to parse arrays, parameter lists, etc. Parser> separatedBy(Parser other) { var suffix = other.then(this).index(1).cast(); - return this.then(suffix.star()).map((r) { + return then(suffix.star()).map((r) { var v = r.value; if (v == null || v.length < 2) { return []; } var preceding = v.isEmpty ? [] : (v[0] == null ? [] : [v[0]]); var out = List.from(preceding); - if (v[1] != null && v[1] != "NULL") { + if (v[1] != null && v[1] != 'NULL') { v[1].forEach((element) { out.add(element as T); }); @@ -240,7 +241,7 @@ abstract class Parser { Parser space() => trail(RegExp(r'[ \n\r\t]+')); /// Consumes 0 or more instance(s) of this parser. - ListParser star({bool backtrack: true}) => + ListParser star({bool backtrack = true}) => times(1, exact: false, backtrack: backtrack).opt(); /// Shortcut for [chain]-ing two parsers together. @@ -260,10 +261,10 @@ abstract class Parser { /// /// You can provide custom error messages for when there are [tooFew] or [tooMany] occurrences. ListParser times(int count, - {bool exact: true, + {bool exact = true, String tooFew = 'Too few', String tooMany = 'Too many', - bool backtrack: true, + bool backtrack = true, SyntaxErrorSeverity? severity}) { return _Repeat(this, count, exact, tooFew, tooMany, backtrack, severity ?? SyntaxErrorSeverity.error); @@ -273,7 +274,7 @@ abstract class Parser { /// /// If [backtrack] is `true` (default), then a failed parse will not /// modify the scanner state. - Parser opt({bool backtrack: true}) => _Opt(this, backtrack); + Parser opt({bool backtrack = true}) => _Opt(this, backtrack); /// Sets the value of the [ParseResult]. Parser value(T Function(ParseResult) f) { @@ -302,7 +303,7 @@ abstract class ListParser extends Parser> { ListParser sort(Comparator compare) => _Compare(this, compare); @override - ListParser opt({bool backtrack: true}) => _ListOpt(this, backtrack); + ListParser opt({bool backtrack = true}) => _ListOpt(this, backtrack); /// Modifies this parser, returning only the values that match a predicate. Parser> where(bool Function(T) f) => diff --git a/packages/combinator/lib/src/combinator/compare.dart b/packages/combinator/lib/src/combinator/compare.dart index 4ee0b813..3a5e36d5 100644 --- a/packages/combinator/lib/src/combinator/compare.dart +++ b/packages/combinator/lib/src/combinator/compare.dart @@ -8,7 +8,7 @@ class _Compare extends ListParser { @override ParseResult> __parse(ParseArgs args) { - ParseResult> result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth()); if (!result.successful) return result; result = result.change( diff --git a/packages/combinator/lib/src/combinator/index.dart b/packages/combinator/lib/src/combinator/index.dart index e52860a7..41d4460a 100644 --- a/packages/combinator/lib/src/combinator/index.dart +++ b/packages/combinator/lib/src/combinator/index.dart @@ -8,19 +8,18 @@ class _Index extends Parser { @override ParseResult __parse(ParseArgs args) { - ParseResult> result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth()); Object? value; if (result.successful) { var vList = result.value; if (vList == null) { - throw ArgumentError("ParseResult is null"); + throw ArgumentError('ParseResult is null'); } if (index == -1) { value = vList.last; } else { if (index < vList.length) { - //TODO: Look at this // print(">>>>Index: $index, Size: ${vList.length}"); // value = // index == -1 ? result.value!.last : result.value!.elementAt(index); diff --git a/packages/combinator/lib/src/combinator/longest.dart b/packages/combinator/lib/src/combinator/longest.dart index fa9124e7..36d9652e 100644 --- a/packages/combinator/lib/src/combinator/longest.dart +++ b/packages/combinator/lib/src/combinator/longest.dart @@ -25,16 +25,16 @@ class _Longest extends Parser { return ParseResult(args.trampoline, args.scanner, this, false, []); } - int replay = args.scanner.position; + var replay = args.scanner.position; var errors = []; var results = >[]; for (var parser in inactive) { var result = parser._parse(args.increaseDepth()); - if (result.successful && result.span != null) + if (result.successful && result.span != null) { results.add(result); - else if (parser is _Alt) errors.addAll(result.errors); + } else if (parser is _Alt) errors.addAll(result.errors); args.scanner.position = replay; } @@ -45,7 +45,7 @@ class _Longest extends Parser { return results.first; } - if (errorMessage != false) + if (errorMessage != false) { errors.add( SyntaxError( severity, @@ -54,22 +54,23 @@ class _Longest extends Parser { args.scanner.emptySpan, ), ); + } return ParseResult(args.trampoline, args.scanner, this, false, errors); } @override ParseResult __parse(ParseArgs args) { - int replay = args.scanner.position; + var replay = args.scanner.position; var errors = []; var results = >[]; for (var parser in parsers) { var result = parser._parse(args.increaseDepth()); - if (result.successful) + if (result.successful) { results.add(result); - else if (parser is _Alt) errors.addAll(result.errors); + } else if (parser is _Alt) errors.addAll(result.errors); args.scanner.position = replay; } @@ -97,7 +98,7 @@ class _Longest extends Parser { buffer ..writeln('longest(${parsers.length}) (') ..indent(); - int i = 1; + var i = 1; for (var parser in parsers) { buffer diff --git a/packages/combinator/lib/src/combinator/match.dart b/packages/combinator/lib/src/combinator/match.dart index 338faff6..837c306d 100644 --- a/packages/combinator/lib/src/combinator/match.dart +++ b/packages/combinator/lib/src/combinator/match.dart @@ -15,7 +15,7 @@ class _Match extends Parser { @override ParseResult __parse(ParseArgs args) { var scanner = args.scanner; - if (!scanner.scan(pattern)) + if (!scanner.scan(pattern)) { return ParseResult(args.trampoline, scanner, this, false, [ SyntaxError( severity, @@ -23,6 +23,7 @@ class _Match extends Parser { scanner.emptySpan, ), ]); + } return ParseResult( args.trampoline, scanner, diff --git a/packages/combinator/lib/src/combinator/opt.dart b/packages/combinator/lib/src/combinator/opt.dart index 12083ed7..3f31c995 100644 --- a/packages/combinator/lib/src/combinator/opt.dart +++ b/packages/combinator/lib/src/combinator/opt.dart @@ -37,7 +37,7 @@ class _ListOpt extends ListParser { @override ParseResult> __parse(ParseArgs args) { var replay = args.scanner.position; - ParseResult> result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth()); if (!result.successful) args.scanner.position = replay; diff --git a/packages/combinator/lib/src/combinator/reduce.dart b/packages/combinator/lib/src/combinator/reduce.dart index 29f1f1f3..a8779b4b 100644 --- a/packages/combinator/lib/src/combinator/reduce.dart +++ b/packages/combinator/lib/src/combinator/reduce.dart @@ -8,9 +8,9 @@ class _Reduce extends Parser { @override ParseResult __parse(ParseArgs args) { - ParseResult> result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth()); - if (!result.successful) + if (!result.successful) { return ParseResult( args.trampoline, args.scanner, @@ -18,6 +18,7 @@ class _Reduce extends Parser { false, result.errors, ); + } result = result.change( value: result.value?.isNotEmpty == true ? result.value : []); diff --git a/packages/combinator/lib/src/combinator/reference.dart b/packages/combinator/lib/src/combinator/reference.dart index 737b94d4..5088c4ac 100644 --- a/packages/combinator/lib/src/combinator/reference.dart +++ b/packages/combinator/lib/src/combinator/reference.dart @@ -8,23 +8,26 @@ class Reference extends Parser { Reference._(); - void set parser(Parser value) { - if (_parser != null) + set parser(Parser value) { + if (_parser != null) { throw StateError('There is already a parser assigned to this reference.'); + } _parser = value; } @override ParseResult __parse(ParseArgs args) { - if (_parser == null) + if (_parser == null) { throw StateError('There is no parser assigned to this reference.'); + } return _parser!._parse(args); } @override ParseResult _parse(ParseArgs args) { - if (_parser == null) + if (_parser == null) { throw StateError('There is no parser assigned to this reference.'); + } return _parser!._parse(args); } diff --git a/packages/combinator/lib/src/combinator/repeat.dart b/packages/combinator/lib/src/combinator/repeat.dart index c8b9891d..13dd9f24 100644 --- a/packages/combinator/lib/src/combinator/repeat.dart +++ b/packages/combinator/lib/src/combinator/repeat.dart @@ -16,7 +16,8 @@ class _Repeat extends ListParser { var errors = []; var results = []; var spans = []; - int success = 0, replay = args.scanner.position; + var success = 0; + var replay = args.scanner.position; ParseResult result; do { diff --git a/packages/combinator/lib/src/combinator/to_list.dart b/packages/combinator/lib/src/combinator/to_list.dart index c2809975..320be1fc 100644 --- a/packages/combinator/lib/src/combinator/to_list.dart +++ b/packages/combinator/lib/src/combinator/to_list.dart @@ -13,7 +13,7 @@ class _ToList extends ListParser { return (result as ParseResult>).change(parser: this); } - List values = []; + var values = []; if (result.value != null) { values.add(result.value!); } diff --git a/packages/combinator/pubspec.yaml b/packages/combinator/pubspec.yaml index c937548f..1f190118 100644 --- a/packages/combinator/pubspec.yaml +++ b/packages/combinator/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_combinator -version: 2.0.1 +version: 2.0.2 description: Packrat parser combinators that support static typing, generics, file spans, memoization, and more. homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/combinator environment: @@ -11,4 +11,5 @@ dependencies: string_scanner: ^1.1.0 tuple: ^2.0.0 dev_dependencies: - test: ^1.17.4 \ No newline at end of file + test: ^1.17.4 + pedantic: ^1.11.0 diff --git a/packages/combinator/test/list_test.dart b/packages/combinator/test/list_test.dart index c137eae0..9489bbee 100644 --- a/packages/combinator/test/list_test.dart +++ b/packages/combinator/test/list_test.dart @@ -2,7 +2,7 @@ import 'package:angel3_combinator/angel3_combinator.dart'; import 'package:test/test.dart'; import 'common.dart'; -main() { +void main() { var number = chain([ match(RegExp(r'[0-9]+')).value((r) => int.parse(r.span!.text)), match(',').opt(), diff --git a/packages/combinator/test/recursion_test.dart b/packages/combinator/test/recursion_test.dart index 748883d8..d91cebb9 100644 --- a/packages/combinator/test/recursion_test.dart +++ b/packages/combinator/test/recursion_test.dart @@ -1,7 +1,3 @@ -import 'package:angel3_combinator/angel3_combinator.dart'; -import 'package:string_scanner/string_scanner.dart'; -import 'package:test/test.dart'; - void main() {} /*