From d62a4aa7ac8bec3d7290cb53f5f244a763e320f5 Mon Sep 17 00:00:00 2001 From: thomashii Date: Thu, 18 Mar 2021 08:00:56 +0800 Subject: [PATCH] Updated to Nullsafety --- packages/combinator/example/basic_auth.dart | 16 +- packages/combinator/example/calculator.dart | 16 +- packages/combinator/example/delimiter.dart | 8 +- packages/combinator/example/json.dart | 12 +- packages/combinator/example/main.dart | 8 +- packages/combinator/example/query_string.dart | 14 +- packages/combinator/example/sexp.dart | 10 +- packages/combinator/example/tuple.dart | 4 +- .../lib/src/combinator/advance.dart | 2 +- .../combinator/lib/src/combinator/any.dart | 6 +- .../combinator/lib/src/combinator/cache.dart | 4 +- .../combinator/lib/src/combinator/cast.dart | 6 +- .../combinator/lib/src/combinator/chain.dart | 16 +- .../combinator/lib/src/combinator/check.dart | 4 +- .../lib/src/combinator/combinator.dart | 137 +++++++++--------- .../lib/src/combinator/compare.dart | 6 +- .../lib/src/combinator/fold_errors.dart | 2 +- .../combinator/lib/src/combinator/index.dart | 8 +- .../lib/src/combinator/longest.dart | 16 +- .../combinator/lib/src/combinator/map.dart | 4 +- .../combinator/lib/src/combinator/match.dart | 4 +- .../lib/src/combinator/max_depth.dart | 2 +- .../combinator/lib/src/combinator/negate.dart | 4 +- .../combinator/lib/src/combinator/opt.dart | 4 +- .../combinator/lib/src/combinator/reduce.dart | 4 +- .../lib/src/combinator/reference.dart | 12 +- .../combinator/lib/src/combinator/repeat.dart | 22 +-- .../combinator/lib/src/combinator/safe.dart | 6 +- .../lib/src/combinator/to_list.dart | 8 +- .../combinator/lib/src/combinator/util.dart | 30 ++-- .../combinator/lib/src/combinator/value.dart | 2 +- packages/combinator/lib/src/error.dart | 10 +- packages/combinator/pubspec.yaml | 2 +- packages/combinator/test/list_test.dart | 12 +- packages/combinator/test/match_test.dart | 6 +- packages/combinator/test/misc_test.dart | 29 ++-- packages/combinator/test/value_test.dart | 4 +- 37 files changed, 231 insertions(+), 229 deletions(-) diff --git a/packages/combinator/example/basic_auth.dart b/packages/combinator/example/basic_auth.dart index e6697590..9c64e7e0 100644 --- a/packages/combinator/example/basic_auth.dart +++ b/packages/combinator/example/basic_auth.dart @@ -10,7 +10,7 @@ import 'package:string_scanner/string_scanner.dart'; /// Namely, the `username` or `password` in `{username}:{password}`. final Parser string = match(new RegExp(r'[^:$]+'), errorMessage: 'Expected a string.') - .value((r) => r.span.text); + .value((r) => r.span!.text); /// Transforms `{username}:{password}` to `{"username": username, "password": password}`. final Parser> credentials = chain([ @@ -18,19 +18,19 @@ final Parser> credentials = chain([ match(':'), string.opt(), ]).map>( - (r) => {'username': r.value[0], 'password': r.value[2]}); + (r) => {'username': r.value![0], 'password': r.value![2]}); /// We can actually embed a parser within another parser. /// /// This is used here to BASE64URL-decode a string, and then /// parse the decoded string. -final Parser credentialString = match>( +final Parser credentialString = match?>( new RegExp(r'([^\n$]+)'), errorMessage: 'Expected a credential string.') .value((r) { - var decoded = utf8.decode(base64Url.decode(r.span.text)); + var decoded = utf8.decode(base64Url.decode(r.span!.text)); var scanner = new SpanScanner(decoded); - return credentials.parse(scanner).value; + return credentials.parse(scanner)!.value; }); final Parser basic = match('Basic').space(); @@ -40,14 +40,14 @@ final Parser basicAuth = basic.then(credentialString).index(1); void main() { while (true) { stdout.write('Enter a basic auth value: '); - var line = stdin.readLineSync(); + var line = stdin.readLineSync()!; var scanner = new SpanScanner(line, sourceUrl: 'stdin'); - var result = basicAuth.parse(scanner); + var result = basicAuth.parse(scanner)!; if (!result.successful) { for (var error in result.errors) { print(error.toolString); - print(error.span.highlight(color: true)); + print(error.span!.highlight(color: true)); } } else print(result.value); diff --git a/packages/combinator/example/calculator.dart b/packages/combinator/example/calculator.dart index 59a436ee..34e799ea 100644 --- a/packages/combinator/example/calculator.dart +++ b/packages/combinator/example/calculator.dart @@ -8,13 +8,13 @@ Parser calculatorGrammar() { var expr = reference(); var number = match(new RegExp(r'-?[0-9]+(\.[0-9]+)?')) - .value((r) => num.parse(r.span.text)); + .value((r) => num.parse(r.span!.text)); var hex = match(new RegExp(r'0x([A-Fa-f0-9]+)')) - .map((r) => int.parse(r.scanner.lastMatch[1], radix: 16)); + .map((r) => int.parse(r.scanner.lastMatch![1]!, radix: 16)); var binary = match(new RegExp(r'([0-1]+)b')) - .map((r) => int.parse(r.scanner.lastMatch[1], radix: 2)); + .map((r) => int.parse(r.scanner.lastMatch![1]!, radix: 2)); var alternatives = >[]; @@ -22,9 +22,9 @@ Parser calculatorGrammar() { alternatives.add( chain([ expr.space(), - match(op).space(), + match(op).space() as Parser, expr.space(), - ]).map((r) => f(r.value[0], r.value[2])), + ]).map((r) => f(r.value![0], r.value![2])), ); } @@ -55,14 +55,14 @@ void main() { while (true) { stdout.write('Enter an expression: '); - var line = stdin.readLineSync(); + var line = stdin.readLineSync()!; var scanner = new SpanScanner(line, sourceUrl: 'stdin'); - var result = calculator.parse(scanner); + var result = calculator.parse(scanner)!; if (!result.successful) { for (var error in result.errors) { stderr.writeln(error.toolString); - stderr.writeln(error.span.highlight(color: true)); + stderr.writeln(error.span!.highlight(color: true)); } } else print(result.value); diff --git a/packages/combinator/example/delimiter.dart b/packages/combinator/example/delimiter.dart index 0028accc..03213de0 100644 --- a/packages/combinator/example/delimiter.dart +++ b/packages/combinator/example/delimiter.dart @@ -3,7 +3,7 @@ import 'package:combinator/combinator.dart'; import 'package:string_scanner/string_scanner.dart'; final Parser id = - match(RegExp(r'[A-Za-z]+')).value((r) => r.span.text); + match(RegExp(r'[A-Za-z]+')).value((r) => r.span!.text); // We can use `separatedBy` to easily construct parser // that can be matched multiple times, separated by another @@ -13,14 +13,14 @@ final Parser id = main() { while (true) { stdout.write('Enter a string (ex "a,b,c"): '); - var line = stdin.readLineSync(); + var line = stdin.readLineSync()!; var scanner = new SpanScanner(line, sourceUrl: 'stdin'); - var result = id.separatedBy(match(',').space()).parse(scanner); + var result = id.separatedBy(match(',').space()).parse(scanner)!; if (!result.successful) { for (var error in result.errors) { print(error.toolString); - print(error.span.highlight(color: true)); + print(error.span!.highlight(color: true)); } } else print(result.value); diff --git a/packages/combinator/example/json.dart b/packages/combinator/example/json.dart index 10dd0612..34d1e838 100644 --- a/packages/combinator/example/json.dart +++ b/packages/combinator/example/json.dart @@ -9,13 +9,13 @@ Parser jsonGrammar() { var number = match(new RegExp(r'-?[0-9]+(\.[0-9]+)?'), errorMessage: 'Expected a number.') .value( - (r) => num.parse(r.span.text), + (r) => num.parse(r.span!.text), ); // Parse a string (no escapes supported, because lazy). var string = match(new RegExp(r'"[^"]*"'), errorMessage: 'Expected a string.').value( - (r) => r.span.text.substring(1, r.span.text.length - 1), + (r) => r.span!.text.substring(1, r.span!.text.length - 1), ); // Parse an array @@ -29,7 +29,7 @@ Parser jsonGrammar() { string.space(), match(':').space(), expr.error(errorMessage: 'Missing expression.'), - ]).castDynamic().cast().value((r) => {r.value[0]: r.value[2]}); + ]).castDynamic().cast().value((r) => {r.value![0]: r.value![2]}); // Parse an object. var object = keyValuePair @@ -55,14 +55,14 @@ main() { while (true) { stdout.write('Enter some JSON: '); - var line = stdin.readLineSync(); + var line = stdin.readLineSync()!; var scanner = new SpanScanner(line, sourceUrl: 'stdin'); - var result = JSON.parse(scanner); + var result = JSON.parse(scanner)!; if (!result.successful) { for (var error in result.errors) { print(error.toolString); - print(error.span.highlight(color: true)); + print(error.span!.highlight(color: true)); } } else print(result.value); diff --git a/packages/combinator/example/main.dart b/packages/combinator/example/main.dart index 96dfc233..c6c15649 100644 --- a/packages/combinator/example/main.dart +++ b/packages/combinator/example/main.dart @@ -17,19 +17,19 @@ final Parser decimal = ( // digits, (dot, digits)? final Parser number = // (minus.opt() & decimal) // minus?, decimal - .map((r) => num.parse(r.span.text)); + .map((r) => num.parse(r.span!.text)); main() { while (true) { stdout.write('Enter a number: '); - var line = stdin.readLineSync(); + var line = stdin.readLineSync()!; var scanner = new SpanScanner(line, sourceUrl: 'stdin'); - var result = number.parse(scanner); + var result = number.parse(scanner)!; if (!result.successful) { for (var error in result.errors) { stderr.writeln(error.toolString); - stderr.writeln(error.span.highlight(color: true)); + stderr.writeln(error.span!.highlight(color: true)); } } else print(result.value); diff --git a/packages/combinator/example/query_string.dart b/packages/combinator/example/query_string.dart index a1f8a3f2..87d8e8e7 100644 --- a/packages/combinator/example/query_string.dart +++ b/packages/combinator/example/query_string.dart @@ -6,9 +6,9 @@ import 'package:string_scanner/string_scanner.dart'; final Parser key = match(RegExp(r'[^=&\n]+'), errorMessage: 'Missing k/v') - .value((r) => r.span.text); + .value((r) => r.span!.text); -final Parser value = key.map((r) => Uri.decodeQueryComponent(r.value)); +final Parser value = key.map((r) => Uri.decodeQueryComponent(r.value!)); final Parser pair = chain([ key, @@ -16,27 +16,27 @@ final Parser pair = chain([ value, ]).map((r) { return { - r.value[0]: r.value[2], + r.value![0]: r.value![2], }; }); final Parser pairs = pair .separatedBy(match(r'&')) - .map((r) => r.value.reduce((a, b) => a..addAll(b))); + .map((r) => r.value!.reduce((a, b) => a..addAll(b))); final Parser queryString = pairs.opt(); main() { while (true) { stdout.write('Enter a query string: '); - var line = stdin.readLineSync(); + var line = stdin.readLineSync()!; var scanner = new SpanScanner(line, sourceUrl: 'stdin'); - var result = pairs.parse(scanner); + var result = pairs.parse(scanner)!; if (!result.successful) { for (var error in result.errors) { print(error.toolString); - print(error.span.highlight(color: true)); + print(error.span!.highlight(color: true)); } } else print(result.value); diff --git a/packages/combinator/example/sexp.dart b/packages/combinator/example/sexp.dart index 5e7d4083..1c8add58 100644 --- a/packages/combinator/example/sexp.dart +++ b/packages/combinator/example/sexp.dart @@ -27,21 +27,21 @@ void main() { var number = match(new RegExp(r'[0-9]+(\.[0-9]+)?'), errorMessage: 'Expected a number.') - .map((r) => num.parse(r.span.text)); + .map((r) => num.parse(r.span!.text)); var id = match( new RegExp( r'[A-Za-z_!\\$",\\+-\\./:;\\?<>%&\\*@\[\]\\{\}\\|`\\^~][A-Za-z0-9_!\\$",\\+-\\./:;\\?<>%&\*@\[\]\\{\}\\|`\\^~]*'), errorMessage: 'Expected an ID') .map((r) => - symbols[r.span.text] ??= throw "Undefined symbol: '${r.span.text}'"); + symbols[r.span!.text] ??= throw "Undefined symbol: '${r.span!.text}'"); var atom = number.castDynamic().or(id); var list = expr.space().times(2, exact: false).map((r) { try { var out = []; - var q = new Queue.from(r.value.reversed); + var q = new Queue.from(r.value!.reversed); while (q.isNotEmpty) { var current = q.removeFirst(); @@ -69,8 +69,8 @@ void main() { while (true) { stdout.write('> '); - var line = stdin.readLineSync(); - var result = expr.parse(new SpanScanner(line)); + var line = stdin.readLineSync()!; + var result = expr.parse(new SpanScanner(line))!; if (result.errors.isNotEmpty) { for (var error in result.errors) { diff --git a/packages/combinator/example/tuple.dart b/packages/combinator/example/tuple.dart index 453c7e61..b92f4262 100644 --- a/packages/combinator/example/tuple.dart +++ b/packages/combinator/example/tuple.dart @@ -2,7 +2,7 @@ import 'package:combinator/combinator.dart'; import 'package:string_scanner/string_scanner.dart'; void main() { - var pub = match('pub').map((r) => r.span.text).space(); + var pub = match('pub').map((r) => r.span!.text).space(); var dart = match('dart').map((r) => 24).space(); var lang = match('lang').map((r) => true).space(); @@ -10,5 +10,5 @@ void main() { var grammar = tuple3(pub, dart, lang); var scanner = SpanScanner('pub dart lang'); - print(grammar.parse(scanner).value); + print(grammar.parse(scanner)!.value); } diff --git a/packages/combinator/lib/src/combinator/advance.dart b/packages/combinator/lib/src/combinator/advance.dart index 01506dc2..49fcdb88 100644 --- a/packages/combinator/lib/src/combinator/advance.dart +++ b/packages/combinator/lib/src/combinator/advance.dart @@ -8,7 +8,7 @@ class _Advance extends Parser { @override ParseResult __parse(ParseArgs args) { - var result = parser._parse(args.increaseDepth()).change(parser: this); + var result = parser._parse(args.increaseDepth())!.change(parser: this); if (result.successful) args.scanner.position += amount; return result; } diff --git a/packages/combinator/lib/src/combinator/any.dart b/packages/combinator/lib/src/combinator/any.dart index 618d2b58..0c16a81d 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 new _Any(parsers, backtrack != false, errorMessage, severity ?? SyntaxErrorSeverity.error); } @@ -33,7 +33,7 @@ class _Any extends Parser { int replay = args.scanner.position; for (var parser in inactive) { - var result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth())!; if (result.successful) return result; @@ -58,7 +58,7 @@ class _Any extends Parser { } @override - ParseResult __parse(ParseArgs args) { + ParseResult? __parse(ParseArgs args) { // Never called return null; } diff --git a/packages/combinator/lib/src/combinator/cache.dart b/packages/combinator/lib/src/combinator/cache.dart index b07b38e0..f57569f0 100644 --- a/packages/combinator/lib/src/combinator/cache.dart +++ b/packages/combinator/lib/src/combinator/cache.dart @@ -1,7 +1,7 @@ part of lex.src.combinator; class _Cache extends Parser { - final Map> _cache = {}; + final Map?> _cache = {}; final Parser parser; _Cache(this.parser); @@ -10,7 +10,7 @@ class _Cache extends Parser { ParseResult __parse(ParseArgs args) { return _cache.putIfAbsent(args.scanner.position, () { return parser._parse(args.increaseDepth()); - }).change(parser: this); + })!.change(parser: this); } @override diff --git a/packages/combinator/lib/src/combinator/cast.dart b/packages/combinator/lib/src/combinator/cast.dart index b1e91d0e..6bd44f80 100644 --- a/packages/combinator/lib/src/combinator/cast.dart +++ b/packages/combinator/lib/src/combinator/cast.dart @@ -7,7 +7,7 @@ class _Cast extends Parser { @override ParseResult __parse(ParseArgs args) { - var result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth())!; return new ParseResult( args.trampoline, args.scanner, @@ -15,7 +15,7 @@ class _Cast extends Parser { result.successful, result.errors, span: result.span, - value: result.value as U, + value: result.value as U?, ); } @@ -38,7 +38,7 @@ class _CastDynamic extends Parser { @override ParseResult __parse(ParseArgs args) { - var result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth())!; return new ParseResult( args.trampoline, args.scanner, diff --git a/packages/combinator/lib/src/combinator/chain.dart b/packages/combinator/lib/src/combinator/chain.dart index 3925aed4..8e0552c8 100644 --- a/packages/combinator/lib/src/combinator/chain.dart +++ b/packages/combinator/lib/src/combinator/chain.dart @@ -4,21 +4,21 @@ 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 new _Chain( parsers, failFast != false, severity ?? SyntaxErrorSeverity.error); } class _Alt extends Parser { final Parser parser; - final String errorMessage; + final String? errorMessage; final SyntaxErrorSeverity severity; _Alt(this.parser, this.errorMessage, this.severity); @override ParseResult __parse(ParseArgs args) { - var result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth())!; return result.successful ? result : result.addErrors([ @@ -43,12 +43,12 @@ class _Chain extends ListParser { @override ParseResult> __parse(ParseArgs args) { var errors = []; - var results = []; - var spans = []; + var results = []; + var spans = []; bool successful = true; for (var parser in parsers) { - var result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth())!; if (!result.successful) { if (parser is _Alt) errors.addAll(result.errors); @@ -66,10 +66,10 @@ class _Chain extends ListParser { if (result.span != null) spans.add(result.span); } - FileSpan span; + FileSpan? span; if (spans.isNotEmpty) { - span = spans.reduce((a, b) => a.expand(b)); + span = spans.reduce((a, b) => a!.expand(b!)); } return new ParseResult>( diff --git a/packages/combinator/lib/src/combinator/check.dart b/packages/combinator/lib/src/combinator/check.dart index 1f2159f0..58416040 100644 --- a/packages/combinator/lib/src/combinator/check.dart +++ b/packages/combinator/lib/src/combinator/check.dart @@ -3,7 +3,7 @@ part of lex.src.combinator; class _Check extends Parser { final Parser parser; final Matcher matcher; - final String errorMessage; + final String? errorMessage; final SyntaxErrorSeverity severity; _Check(this.parser, this.matcher, this.errorMessage, this.severity); @@ -11,7 +11,7 @@ class _Check extends Parser { @override ParseResult __parse(ParseArgs args) { var matchState = {}; - var result = parser._parse(args.increaseDepth()).change(parser: this); + var result = parser._parse(args.increaseDepth())!.change(parser: this); if (!result.successful) return result; else if (!matcher.matches(result.value, matchState)) { diff --git a/packages/combinator/lib/src/combinator/combinator.dart b/packages/combinator/lib/src/combinator/combinator.dart index 2b083a25..3b6d9b14 100644 --- a/packages/combinator/lib/src/combinator/combinator.dart +++ b/packages/combinator/lib/src/combinator/combinator.dart @@ -62,21 +62,21 @@ class ParseArgs { ParseArgs(this.trampoline, this.scanner, this.depth); - ParseArgs increaseDepth() => new ParseArgs(trampoline, scanner, depth + 1); + ParseArgs increaseDepth() => ParseArgs(trampoline, scanner, depth + 1); } /// A parser combinator, which can parse very complicated grammars in a manageable manner. abstract class Parser { - ParseResult __parse(ParseArgs args); + ParseResult? __parse(ParseArgs args); - ParseResult _parse(ParseArgs args) { + ParseResult? _parse(ParseArgs args) { var pos = args.scanner.position; if (args.trampoline.hasMemoized(this, pos)) return args.trampoline.getMemoized(this, pos); if (args.trampoline.isActive(this, pos)) - return new ParseResult(args.trampoline, args.scanner, this, false, []); + return ParseResult(args.trampoline, args.scanner, this, false, []); args.trampoline.enter(this, pos); var result = __parse(args); @@ -86,79 +86,80 @@ abstract class Parser { } /// Parses text from a [SpanScanner]. - ParseResult parse(SpanScanner scanner, [int depth = 1]) { - var args = new ParseArgs(new Trampoline(), scanner, depth); + ParseResult? parse(SpanScanner scanner, [int depth = 1]) { + var args = ParseArgs(Trampoline(), scanner, depth); return _parse(args); } /// Skips forward a certain amount of steps after parsing, if it was successful. - Parser forward(int amount) => new _Advance(this, amount); + Parser forward(int amount) => _Advance(this, amount); /// Moves backward a certain amount of steps after parsing, if it was successful. - Parser back(int amount) => new _Advance(this, amount * -1); + Parser back(int amount) => _Advance(this, amount * -1); /// Casts this parser to produce [U] objects. - Parser cast() => new _Cast(this); + Parser cast() => _Cast(this); /// Casts this parser to produce [dynamic] objects. - Parser castDynamic() => new _CastDynamic(this); + 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 new _Change(this, f); + Parser change(ParseResult Function(ParseResult?) f) { + return _Change(this, f); } /// Validates the parse result against a [Matcher]. /// /// You can provide a custom [errorMessage]. Parser check(Matcher matcher, - {String errorMessage, SyntaxErrorSeverity severity}) => - new _Check( + {String? errorMessage, SyntaxErrorSeverity? severity}) => + _Check( this, matcher, errorMessage, severity ?? SyntaxErrorSeverity.error); /// Binds an [errorMessage] to a copy of this parser. - Parser error({String errorMessage, SyntaxErrorSeverity severity}) => - new _Alt(this, errorMessage, severity ?? SyntaxErrorSeverity.error); + Parser error({String? errorMessage, SyntaxErrorSeverity? severity}) => + _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)}) { - equal ??= (b, e) => b.span.start.offset == e.span.start.offset; - return new _FoldErrors(this, equal); + Parser foldErrors({bool equal(SyntaxError a, SyntaxError b)?}) { + equal ??= (b, e) => b.span!.start.offset == e.span!.start.offset; + return _FoldErrors(this, equal); } /// Transforms the parse result using a unary function. Parser map(U Function(ParseResult) f) { - return new _Map(this, f); + return _Map(this, f); } /// Prevents recursion past a certain [depth], preventing stack overflow errors. - Parser maxDepth(int depth) => new _MaxDepth(this, depth); + Parser maxDepth(int depth) => _MaxDepth(this, depth); Parser operator ~() => negate(); /// Ensures this pattern is not matched. /// /// You can provide an [errorMessage]. - Parser negate({String errorMessage, SyntaxErrorSeverity severity}) => - new _Negate(this, errorMessage, severity ?? SyntaxErrorSeverity.error); + Parser negate({String? errorMessage, SyntaxErrorSeverity? severity}) => + _Negate(this, errorMessage, severity ?? SyntaxErrorSeverity.error); /// Caches the results of parse attempts at various locations within the source text. /// /// Use this to prevent excessive recursion. - Parser cache() => new _Cache(this); + Parser cache() => _Cache(this); Parser operator &(Parser other) => and(other); /// Consumes `this` and another parser, but only considers the result of `this` parser. Parser and(Parser other) => then(other).change((r) { - return new ParseResult( - r.trampoline, + return ParseResult( + r!.trampoline, r.scanner, this, r.successful, r.errors, span: r.span, - value: (r.value != null ? r.value[0] : r.value) as T, + value: (r.value != null ? r.value![0] : r.value) as T?, ); }); @@ -168,16 +169,16 @@ abstract class Parser { Parser or(Parser other) => any([this, other]); /// Parses this sequence one or more times. - ListParser plus() => times(1, exact: false); + ListParser plus() => times(1, exact: false); /// Safely escapes this parser when an error occurs. /// /// The generated parser only runs once; repeated uses always exit eagerly. Parser safe( {bool backtrack: true, - String errorMessage, - SyntaxErrorSeverity severity}) => - new _Safe( + String? errorMessage, + SyntaxErrorSeverity? severity}) => + _Safe( this, backtrack, errorMessage, severity ?? SyntaxErrorSeverity.error); Parser> separatedByComma() => @@ -190,18 +191,18 @@ abstract class Parser { var suffix = other.then(this).index(1).cast(); return this.then(suffix.star()).map((r) { var preceding = - r.value.isEmpty ? [] : (r.value[0] == null ? [] : [r.value[0]]); - var out = new List.from(preceding); - if (r.value[1] != null) out.addAll(r.value[1] as Iterable); + r.value!.isEmpty ? [] : (r.value![0] == null ? [] : [r.value![0]]); + var out = List.from(preceding); + if (r.value![1] != null) out.addAll(r.value![1] as Iterable); return out; }); } - Parser surroundedByCurlyBraces({T defaultValue}) => opt() + Parser surroundedByCurlyBraces({T? defaultValue}) => opt() .surroundedBy(match('{').space(), match('}').space()) .map((r) => r.value ?? defaultValue); - Parser surroundedBySquareBrackets({T defaultValue}) => opt() + Parser surroundedBySquareBrackets({T? defaultValue}) => opt() .surroundedBy(match('[').space(), match(']').space()) .map((r) => r.value ?? defaultValue); @@ -209,7 +210,7 @@ abstract class Parser { /// /// If no [right] is provided, it expects to see the same pattern on both sides. /// Use this parse things like parenthesized expressions, arrays, etc. - Parser surroundedBy(Parser left, [Parser right]) { + Parser surroundedBy(Parser left, [Parser? right]) { return chain([ left, this, @@ -227,17 +228,17 @@ abstract class Parser { surroundedBy(match('(').space(), match(')').space()); /// Consumes any trailing whitespace. - Parser space() => trail(new RegExp(r'[ \n\r\t]+')); + 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. ListParser then(Parser other) => chain([this, other]); /// Casts this instance into a [ListParser]. - ListParser toList() => new _ToList(this); + ListParser toList() => _ToList(this); /// Consumes and ignores any trailing occurrences of [pattern]. Parser trail(Pattern pattern) => @@ -249,13 +250,13 @@ abstract class Parser { /// an infinite amount of occurrences after the specified [count]. /// /// You can provide custom error messages for when there are [tooFew] or [tooMany] occurrences. - ListParser times(int count, + ListParser times(int count, {bool exact: true, - String tooFew, - String tooMany, + String? tooFew, + String? tooMany, bool backtrack: true, - SyntaxErrorSeverity severity}) { - return new _Repeat(this, count, exact, tooFew, tooMany, backtrack, + SyntaxErrorSeverity? severity}) { + return _Repeat(this, count, exact, tooFew, tooMany, backtrack, severity ?? SyntaxErrorSeverity.error); } @@ -263,11 +264,11 @@ abstract class Parser { /// /// If [backtrack] is `true` (default), then a failed parse will not /// modify the scanner state. - Parser opt({bool backtrack: true}) => new _Opt(this, backtrack); + Parser opt({bool backtrack: true}) => _Opt(this, backtrack); /// Sets the value of the [ParseResult]. Parser value(T Function(ParseResult) f) { - return new _Value(this, f); + return _Value(this, f); } /// Prints a representation of this parser, ideally without causing a stack overflow. @@ -280,26 +281,26 @@ abstract class ListParser extends Parser> { Parser first() => index(0); /// Modifies this parser to only return the value at the given index [i]. - Parser index(int i) => new _Index(this, i); + Parser index(int i) => _Index(this, i); /// Shortcut for calling [index] with the greatest-possible index. Parser last() => index(-1); /// Modifies this parser to call `List.reduce` on the parsed values. - Parser reduce(T Function(T, T) combine) => new _Reduce(this, combine); + Parser reduce(T Function(T, T) combine) => _Reduce(this, combine); /// Sorts the parsed values, using the given [Comparator]. - ListParser sort(Comparator compare) => new _Compare(this, compare); + ListParser sort(Comparator compare) => _Compare(this, compare); @override - ListParser opt({bool backtrack: true}) => new _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) => - map>((r) => r.value.where(f).toList()); + map>((r) => r.value!.where(f).toList()); /// Condenses a [ListParser] into having a value of the combined span's text. - Parser flatten() => map((r) => r.span.text); + Parser flatten() => map((r) => r.span!.text); } /// Prevents stack overflow in recursive parsers. @@ -313,32 +314,32 @@ class Trampoline { } ParseResult getMemoized(Parser parser, int position) { - return _memo[parser].firstWhere((t) => t.item1 == position).item2 + return _memo[parser]!.firstWhere((t) => t.item1 == position).item2 as ParseResult; } - void memoize(Parser parser, int position, ParseResult result) { + void memoize(Parser parser, int position, ParseResult? result) { if (result != null) { var list = _memo.putIfAbsent(parser, () => []); - var tuple = new Tuple2(position, result); + var tuple = Tuple2(position, result); if (!list.contains(tuple)) list.add(tuple); } } bool isActive(Parser parser, int position) { if (!_active.containsKey(parser)) return false; - var q = _active[parser]; + var q = _active[parser]!; if (q.isEmpty) return false; //return q.contains(position); return q.first == position; } void enter(Parser parser, int position) { - _active.putIfAbsent(parser, () => new Queue()).addFirst(position); + _active.putIfAbsent(parser, () => Queue()).addFirst(position); } void exit(Parser parser) { - if (_active.containsKey(parser)) _active[parser].removeFirst(); + if (_active.containsKey(parser)) _active[parser]!.removeFirst(); } } @@ -347,8 +348,8 @@ class ParseResult { final Parser parser; final bool successful; final Iterable errors; - final FileSpan span; - final T value; + final FileSpan? span; + final T? value; final SpanScanner scanner; final Trampoline trampoline; @@ -357,12 +358,12 @@ class ParseResult { {this.span, this.value}); ParseResult change( - {Parser parser, - bool successful, - Iterable errors, - FileSpan span, - T value}) { - return new ParseResult( + {Parser? parser, + bool? successful, + Iterable? errors, + FileSpan? span, + T? value}) { + return ParseResult( trampoline, scanner, parser ?? this.parser, @@ -375,7 +376,7 @@ class ParseResult { ParseResult addErrors(Iterable errors) { return change( - errors: new List.from(this.errors)..addAll(errors), + errors: List.from(this.errors)..addAll(errors), ); } } diff --git a/packages/combinator/lib/src/combinator/compare.dart b/packages/combinator/lib/src/combinator/compare.dart index 99447ac1..49a39701 100644 --- a/packages/combinator/lib/src/combinator/compare.dart +++ b/packages/combinator/lib/src/combinator/compare.dart @@ -8,12 +8,12 @@ class _Compare extends ListParser { @override ParseResult> __parse(ParseArgs args) { - var result = parser._parse(args.increaseDepth()); + ParseResult> result = parser._parse(args.increaseDepth())!; if (!result.successful) return result; result = result.change( value: result.value?.isNotEmpty == true ? result.value : []); - result = result.change(value: new List.from(result.value)); + result = result.change(value: new List.from(result.value!)); return new ParseResult>( args.trampoline, args.scanner, @@ -21,7 +21,7 @@ class _Compare extends ListParser { true, [], span: result.span, - value: result.value..sort(compare), + value: result.value?..sort(compare), ); } diff --git a/packages/combinator/lib/src/combinator/fold_errors.dart b/packages/combinator/lib/src/combinator/fold_errors.dart index 6d15c69a..5b838e6f 100644 --- a/packages/combinator/lib/src/combinator/fold_errors.dart +++ b/packages/combinator/lib/src/combinator/fold_errors.dart @@ -8,7 +8,7 @@ class _FoldErrors extends Parser { @override ParseResult __parse(ParseArgs args) { - var result = parser._parse(args.increaseDepth()).change(parser: this); + var result = parser._parse(args.increaseDepth())!.change(parser: this); var errors = result.errors.fold>([], (out, e) { if (!out.any((b) => equal(e, b))) out.add(e); return out; diff --git a/packages/combinator/lib/src/combinator/index.dart b/packages/combinator/lib/src/combinator/index.dart index 0a57c3d7..a3791333 100644 --- a/packages/combinator/lib/src/combinator/index.dart +++ b/packages/combinator/lib/src/combinator/index.dart @@ -8,11 +8,11 @@ class _Index extends Parser { @override ParseResult __parse(ParseArgs args) { - var result = parser._parse(args.increaseDepth()); - Object value; + ParseResult> result = parser._parse(args.increaseDepth())!; + Object? value; if (result.successful) - value = index == -1 ? result.value.last : result.value.elementAt(index); + value = index == -1 ? result.value!.last : result.value!.elementAt(index); return new ParseResult( args.trampoline, @@ -21,7 +21,7 @@ class _Index extends Parser { result.successful, result.errors, span: result.span, - value: value as T, + value: value as T?, ); } diff --git a/packages/combinator/lib/src/combinator/longest.dart b/packages/combinator/lib/src/combinator/longest.dart index dec48df9..ae00c2c8 100644 --- a/packages/combinator/lib/src/combinator/longest.dart +++ b/packages/combinator/lib/src/combinator/longest.dart @@ -4,14 +4,14 @@ part of lex.src.combinator; /// /// You can provide a custom [errorMessage]. Parser longest(Iterable> parsers, - {Object errorMessage, SyntaxErrorSeverity severity}) { + {Object? errorMessage, SyntaxErrorSeverity? severity}) { return new _Longest( parsers, errorMessage, severity ?? SyntaxErrorSeverity.error); } class _Longest extends Parser { final Iterable> parsers; - final Object errorMessage; + final Object? errorMessage; final SyntaxErrorSeverity severity; _Longest(this.parsers, this.errorMessage, this.severity); @@ -31,7 +31,7 @@ class _Longest extends Parser { var results = >[]; for (var parser in inactive) { - var result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth())!; if (result.successful && result.span != null) results.add(result); @@ -41,8 +41,8 @@ class _Longest extends Parser { } if (results.isNotEmpty) { - results.sort((a, b) => b.span.length.compareTo(a.span.length)); - args.scanner.scan(results.first.span.text); + results.sort((a, b) => b.span!.length.compareTo(a.span!.length)); + args.scanner.scan(results.first.span!.text); return results.first; } @@ -66,7 +66,7 @@ class _Longest extends Parser { var results = >[]; for (var parser in parsers) { - var result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth())!; if (result.successful) results.add(result); @@ -76,8 +76,8 @@ class _Longest extends Parser { } if (results.isNotEmpty) { - results.sort((a, b) => b.span.length.compareTo(a.span.length)); - args.scanner.scan(results.first.span.text); + results.sort((a, b) => b.span!.length.compareTo(a.span!.length)); + args.scanner.scan(results.first.span!.text); return results.first; } diff --git a/packages/combinator/lib/src/combinator/map.dart b/packages/combinator/lib/src/combinator/map.dart index b1e210a3..37acddd5 100644 --- a/packages/combinator/lib/src/combinator/map.dart +++ b/packages/combinator/lib/src/combinator/map.dart @@ -8,7 +8,7 @@ class _Map extends Parser { @override ParseResult __parse(ParseArgs args) { - var result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth())!; return new ParseResult( args.trampoline, args.scanner, @@ -34,7 +34,7 @@ class _Map extends Parser { class _Change extends Parser { final Parser parser; - final ParseResult Function(ParseResult) f; + final ParseResult Function(ParseResult?) f; _Change(this.parser, this.f); diff --git a/packages/combinator/lib/src/combinator/match.dart b/packages/combinator/lib/src/combinator/match.dart index 4ebfc1c6..e533a503 100644 --- a/packages/combinator/lib/src/combinator/match.dart +++ b/packages/combinator/lib/src/combinator/match.dart @@ -2,12 +2,12 @@ part of lex.src.combinator; /// Expects to match a given [pattern]. If it is not matched, you can provide a custom [errorMessage]. Parser match(Pattern pattern, - {String errorMessage, SyntaxErrorSeverity severity}) => + {String? errorMessage, SyntaxErrorSeverity? severity}) => new _Match(pattern, errorMessage, severity ?? SyntaxErrorSeverity.error); class _Match extends Parser { final Pattern pattern; - final String errorMessage; + final String? errorMessage; final SyntaxErrorSeverity severity; _Match(this.pattern, this.errorMessage, this.severity); diff --git a/packages/combinator/lib/src/combinator/max_depth.dart b/packages/combinator/lib/src/combinator/max_depth.dart index 1c28a9d2..f9054132 100644 --- a/packages/combinator/lib/src/combinator/max_depth.dart +++ b/packages/combinator/lib/src/combinator/max_depth.dart @@ -7,7 +7,7 @@ class _MaxDepth extends Parser { _MaxDepth(this.parser, this.cap); @override - ParseResult __parse(ParseArgs args) { + ParseResult? __parse(ParseArgs args) { if (args.depth > cap) { return new ParseResult(args.trampoline, args.scanner, this, false, []); } diff --git a/packages/combinator/lib/src/combinator/negate.dart b/packages/combinator/lib/src/combinator/negate.dart index db255afc..30a8c377 100644 --- a/packages/combinator/lib/src/combinator/negate.dart +++ b/packages/combinator/lib/src/combinator/negate.dart @@ -2,14 +2,14 @@ part of lex.src.combinator; class _Negate extends Parser { final Parser parser; - final String errorMessage; + final String? errorMessage; final SyntaxErrorSeverity severity; _Negate(this.parser, this.errorMessage, this.severity); @override ParseResult __parse(ParseArgs args) { - var result = parser._parse(args.increaseDepth()).change(parser: this); + var result = parser._parse(args.increaseDepth())!.change(parser: this); if (!result.successful) { return new ParseResult( diff --git a/packages/combinator/lib/src/combinator/opt.dart b/packages/combinator/lib/src/combinator/opt.dart index 3f31c995..ac2b31c2 100644 --- a/packages/combinator/lib/src/combinator/opt.dart +++ b/packages/combinator/lib/src/combinator/opt.dart @@ -9,7 +9,7 @@ class _Opt extends Parser { @override ParseResult __parse(ParseArgs args) { var replay = args.scanner.position; - var result = parser._parse(args.increaseDepth()); + var result = parser._parse(args.increaseDepth())!; if (!result.successful) args.scanner.position = replay; @@ -37,7 +37,7 @@ class _ListOpt extends ListParser { @override ParseResult> __parse(ParseArgs args) { var replay = args.scanner.position; - var result = parser._parse(args.increaseDepth()); + ParseResult> 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 cb88f94b..ae2d7d14 100644 --- a/packages/combinator/lib/src/combinator/reduce.dart +++ b/packages/combinator/lib/src/combinator/reduce.dart @@ -8,7 +8,7 @@ class _Reduce extends Parser { @override ParseResult __parse(ParseArgs args) { - var result = parser._parse(args.increaseDepth()); + ParseResult> result = parser._parse(args.increaseDepth())!; if (!result.successful) return new ParseResult( @@ -28,7 +28,7 @@ class _Reduce extends Parser { result.successful, [], span: result.span, - value: result.value.isEmpty ? null : result.value.reduce(combine), + value: result.value!.isEmpty ? null : result.value!.reduce(combine), ); } diff --git a/packages/combinator/lib/src/combinator/reference.dart b/packages/combinator/lib/src/combinator/reference.dart index d46f5a6e..dd2b6b9b 100644 --- a/packages/combinator/lib/src/combinator/reference.dart +++ b/packages/combinator/lib/src/combinator/reference.dart @@ -3,7 +3,7 @@ part of lex.src.combinator; Reference reference() => new Reference._(); class Reference extends Parser { - Parser _parser; + Parser? _parser; bool printed = false; Reference._(); @@ -16,22 +16,22 @@ class Reference extends Parser { } @override - ParseResult __parse(ParseArgs args) { + ParseResult? __parse(ParseArgs args) { if (_parser == null) throw new StateError('There is no parser assigned to this reference.'); - return _parser._parse(args); + return _parser!._parse(args); } @override - ParseResult _parse(ParseArgs args) { - return _parser._parse(args); + ParseResult? _parse(ParseArgs args) { + return _parser!._parse(args); } @override void stringify(CodeBuffer buffer) { if (_parser == null) buffer.writeln('(undefined reference <$T>)'); - else if (!printed) _parser.stringify(buffer); + else if (!printed) _parser!.stringify(buffer); printed = true; buffer.writeln('(previously printed reference)'); } diff --git a/packages/combinator/lib/src/combinator/repeat.dart b/packages/combinator/lib/src/combinator/repeat.dart index 4f62b5e4..be8b9c57 100644 --- a/packages/combinator/lib/src/combinator/repeat.dart +++ b/packages/combinator/lib/src/combinator/repeat.dart @@ -1,26 +1,26 @@ part of lex.src.combinator; -class _Repeat extends ListParser { +class _Repeat extends ListParser { final Parser parser; final int count; final bool exact, backtrack; - final String tooFew, tooMany; + final String? tooFew, tooMany; final SyntaxErrorSeverity severity; _Repeat(this.parser, this.count, this.exact, this.tooFew, this.tooMany, this.backtrack, this.severity); @override - ParseResult> __parse(ParseArgs args) { + ParseResult> __parse(ParseArgs args) { var errors = []; - var results = []; - var spans = []; + var results = []; + var spans = []; int success = 0, replay = args.scanner.position; - ParseResult result; + ParseResult? result; do { result = parser._parse(args.increaseDepth()); - if (result.successful) { + if (result!.successful) { success++; results.add(result.value); replay = args.scanner.position; @@ -41,12 +41,12 @@ class _Repeat extends ListParser { if (backtrack) args.scanner.position = replay; - return new ParseResult>( + return new ParseResult>( args.trampoline, args.scanner, this, false, errors); } else if (success > count && exact) { if (backtrack) args.scanner.position = replay; - return new ParseResult>( + return new ParseResult>( args.trampoline, args.scanner, this, false, [ new SyntaxError( severity, @@ -56,8 +56,8 @@ class _Repeat extends ListParser { ]); } - var span = spans.reduce((a, b) => a.expand(b)); - return new ParseResult>( + var span = spans.reduce((a, b) => a!.expand(b!)); + return new ParseResult>( args.trampoline, args.scanner, this, diff --git a/packages/combinator/lib/src/combinator/safe.dart b/packages/combinator/lib/src/combinator/safe.dart index 02127275..8bac100e 100644 --- a/packages/combinator/lib/src/combinator/safe.dart +++ b/packages/combinator/lib/src/combinator/safe.dart @@ -3,18 +3,18 @@ part of lex.src.combinator; class _Safe extends Parser { final Parser parser; final bool backtrack; - final String errorMessage; + final String? errorMessage; final SyntaxErrorSeverity severity; bool _triggered = false; _Safe(this.parser, this.backtrack, this.errorMessage, this.severity); @override - ParseResult __parse(ParseArgs args) { + ParseResult? __parse(ParseArgs args) { var replay = args.scanner.position; try { - if (_triggered) throw null; + if (_triggered) throw Exception(); return parser._parse(args.increaseDepth()); } catch (_) { _triggered = true; diff --git a/packages/combinator/lib/src/combinator/to_list.dart b/packages/combinator/lib/src/combinator/to_list.dart index eb54b5c4..a339db50 100644 --- a/packages/combinator/lib/src/combinator/to_list.dart +++ b/packages/combinator/lib/src/combinator/to_list.dart @@ -1,16 +1,16 @@ part of lex.src.combinator; -class _ToList extends ListParser { +class _ToList extends ListParser { final Parser parser; _ToList(this.parser); @override - ParseResult> __parse(ParseArgs args) { - var result = parser._parse(args.increaseDepth()); + ParseResult> __parse(ParseArgs args) { + var result = parser._parse(args.increaseDepth())!; if (result.value is List) { - return (result as ParseResult>).change(parser: this); + return (result as ParseResult>).change(parser: this); } return new ParseResult( diff --git a/packages/combinator/lib/src/combinator/util.dart b/packages/combinator/lib/src/combinator/util.dart index 7e0b56c4..e10d7f5f 100644 --- a/packages/combinator/lib/src/combinator/util.dart +++ b/packages/combinator/lib/src/combinator/util.dart @@ -1,48 +1,48 @@ part of lex.src.combinator; /// A typed parser that parses a sequence of 2 values of different types. -Parser> tuple2(Parser a, Parser b) { +Parser> tuple2(Parser a, Parser b) { return chain([a, b]).map((r) { - return Tuple2(r.value[0] as A, r.value[1] as B); + return Tuple2(r.value![0] as A?, r.value![1] as B?); }); } /// A typed parser that parses a sequence of 3 values of different types. -Parser> tuple3(Parser a, Parser b, Parser c) { +Parser> tuple3(Parser a, Parser b, Parser c) { return chain([a, b, c]).map((r) { - return Tuple3(r.value[0] as A, r.value[1] as B, r.value[2] as C); + return Tuple3(r.value![0] as A?, r.value![1] as B?, r.value![2] as C?); }); } /// A typed parser that parses a sequence of 4 values of different types. -Parser> tuple4( +Parser> tuple4( Parser a, Parser b, Parser c, Parser d) { return chain([a, b, c, d]).map((r) { return Tuple4( - r.value[0] as A, r.value[1] as B, r.value[2] as C, r.value[3] as D); + r.value![0] as A?, r.value![1] as B?, r.value![2] as C?, r.value![3] as D?); }); } /// A typed parser that parses a sequence of 5 values of different types. -Parser> tuple5( +Parser> tuple5( Parser a, Parser b, Parser c, Parser d, Parser e) { return chain([a, b, c, d, e]).map((r) { - return Tuple5(r.value[0] as A, r.value[1] as B, r.value[2] as C, - r.value[3] as D, r.value[4] as E); + return Tuple5(r.value![0] as A?, r.value![1] as B?, r.value![2] as C?, + r.value![3] as D?, r.value![4] as E?); }); } /// A typed parser that parses a sequence of 6 values of different types. -Parser> tuple6(Parser a, +Parser> tuple6(Parser a, Parser b, Parser c, Parser d, Parser e, Parser f) { return chain([a, b, c, d, e, f]).map((r) { - return Tuple6(r.value[0] as A, r.value[1] as B, r.value[2] as C, - r.value[3] as D, r.value[4] as E, r.value[5] as F); + return Tuple6(r.value![0] as A?, r.value![1] as B?, r.value![2] as C?, + r.value![3] as D?, r.value![4] as E?, r.value![5] as F?); }); } /// A typed parser that parses a sequence of 7 values of different types. -Parser> tuple7( +Parser> tuple7( Parser a, Parser b, Parser c, @@ -51,7 +51,7 @@ Parser> tuple7( Parser f, Parser g) { return chain([a, b, c, d, e, f, g]).map((r) { - return Tuple7(r.value[0] as A, r.value[1] as B, r.value[2] as C, - r.value[3] as D, r.value[4] as E, r.value[5] as F, r.value[6] as G); + return Tuple7(r.value![0] as A?, r.value![1] as B?, r.value![2] as C?, + r.value![3] as D?, r.value![4] as E?, r.value![5] as F?, r.value![6] as G?); }); } diff --git a/packages/combinator/lib/src/combinator/value.dart b/packages/combinator/lib/src/combinator/value.dart index b0612073..ab206187 100644 --- a/packages/combinator/lib/src/combinator/value.dart +++ b/packages/combinator/lib/src/combinator/value.dart @@ -8,7 +8,7 @@ class _Value extends Parser { @override ParseResult __parse(ParseArgs args) { - var result = parser._parse(args.increaseDepth()).change(parser: this); + var result = parser._parse(args.increaseDepth())!.change(parser: this); return result.successful ? result.change(value: f(result)) : result; } diff --git a/packages/combinator/lib/src/error.dart b/packages/combinator/lib/src/error.dart index e6b4de09..6c7b5f51 100644 --- a/packages/combinator/lib/src/error.dart +++ b/packages/combinator/lib/src/error.dart @@ -2,16 +2,16 @@ import 'package:source_span/source_span.dart'; class SyntaxError implements Exception { final SyntaxErrorSeverity severity; - final String message; - final FileSpan span; - String _toolString; + final String? message; + final FileSpan? span; + String? _toolString; SyntaxError(this.severity, this.message, this.span); - String get toolString { + String? get toolString { if (_toolString != null) return _toolString; var type = severity == SyntaxErrorSeverity.warning ? 'warning' : 'error'; - return _toolString = '$type: ${span.start.toolString}: $message'; + return _toolString = '$type: ${span!.start.toolString}: $message'; } } diff --git a/packages/combinator/pubspec.yaml b/packages/combinator/pubspec.yaml index 122c162c..c0894fdb 100644 --- a/packages/combinator/pubspec.yaml +++ b/packages/combinator/pubspec.yaml @@ -5,7 +5,7 @@ author: Tobe O homepage: https://github.com/thosakwe/combinator.git publish_to: none environment: - sdk: ">=2.10.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: code_buffer: git: diff --git a/packages/combinator/test/list_test.dart b/packages/combinator/test/list_test.dart index 06e7dd4b..64bf8fba 100644 --- a/packages/combinator/test/list_test.dart +++ b/packages/combinator/test/list_test.dart @@ -4,19 +4,19 @@ import 'common.dart'; main() { var number = chain([ - match(new RegExp(r'[0-9]+')).value((r) => int.parse(r.span.text)), + match(new RegExp(r'[0-9]+')).value((r) => int.parse(r.span!.text)), match(',').opt(), ]).first().cast(); var numbers = number.plus(); test('sort', () { - var parser = numbers.sort((a, b) => a.compareTo(b)); - expect(parser.parse(scan('21,2,3,34,20')).value, [2, 3, 20, 21, 34]); + var parser = numbers.sort((a, b) => a!.compareTo(b!)); + expect(parser.parse(scan('21,2,3,34,20'))!.value, [2, 3, 20, 21, 34]); }); test('reduce', () { - var parser = numbers.reduce((a, b) => a + b); - expect(parser.parse(scan('21,2,3,34,20')).value, 80); - expect(parser.parse(scan('not numbers')).value, isNull); + var parser = numbers.reduce((a, b) => a! + b!); + expect(parser.parse(scan('21,2,3,34,20'))!.value, 80); + expect(parser.parse(scan('not numbers'))!.value, isNull); }); } diff --git a/packages/combinator/test/match_test.dart b/packages/combinator/test/match_test.dart index 60504d93..d1406276 100644 --- a/packages/combinator/test/match_test.dart +++ b/packages/combinator/test/match_test.dart @@ -4,13 +4,13 @@ import 'common.dart'; main() { test('match string', () { - expect(match('hello').parse(scan('hello world')).successful, isTrue); + expect(match('hello').parse(scan('hello world'))!.successful, isTrue); }); test('match start only', () { - expect(match('hello').parse(scan('goodbye hello')).successful, isFalse); + expect(match('hello').parse(scan('goodbye hello'))!.successful, isFalse); }); test('fail if no match', () { - expect(match('hello').parse(scan('world')).successful, isFalse); + expect(match('hello').parse(scan('world'))!.successful, isFalse); }); } diff --git a/packages/combinator/test/misc_test.dart b/packages/combinator/test/misc_test.dart index aca09d50..f90d5d83 100644 --- a/packages/combinator/test/misc_test.dart +++ b/packages/combinator/test/misc_test.dart @@ -14,28 +14,29 @@ main() { }); test('change', () { - var parser = match('hello').change((r) => r.change(value: 23)); - expect(parser.parse(scan('helloworld')).value, 23); + var parser = match('hello').change((r) => r!.change(value: 23)); + expect(parser.parse(scan('helloworld'))!.value, 23); }); test('check', () { var parser = match(new RegExp(r'[A-Za-z]+')) - .value((r) => r.span.length) + .value((r) => r.span!.length) .check(greaterThan(3)); - expect(parser.parse(scan('helloworld')).successful, isTrue); - expect(parser.parse(scan('yo')).successful, isFalse); + expect(parser.parse(scan('helloworld'))!.successful, isTrue); + expect(parser.parse(scan('yo'))!.successful, isFalse); }); test('map', () { - var parser = match(new RegExp(r'[A-Za-z]+')).map((r) => r.span.length); - expect(parser.parse(scan('hello')).value, 5); + var parser = + match(new RegExp(r'[A-Za-z]+')).map((r) => r.span!.length); + expect(parser.parse(scan('hello'))!.value, 5); }); test('negate', () { var parser = match('hello').negate(errorMessage: 'world'); - expect(parser.parse(scan('goodbye world')).successful, isTrue); - expect(parser.parse(scan('hello world')).successful, isFalse); - expect(parser.parse(scan('hello world')).errors.first.message, 'world'); + expect(parser.parse(scan('goodbye world'))!.successful, isTrue); + expect(parser.parse(scan('hello world'))!.successful, isFalse); + expect(parser.parse(scan('hello world'))!.errors.first.message, 'world'); }); group('opt', () { @@ -43,13 +44,13 @@ main() { var list = match('hel').then(match('lo')).opt(); test('succeeds if present', () { - expect(single.parse(scan('hello')).successful, isTrue); - expect(list.parse(scan('hello')).successful, isTrue); + expect(single.parse(scan('hello'))!.successful, isTrue); + expect(list.parse(scan('hello'))!.successful, isTrue); }); test('succeeds if not present', () { - expect(single.parse(scan('goodbye')).successful, isTrue); - expect(list.parse(scan('goodbye')).successful, isTrue); + expect(single.parse(scan('goodbye'))!.successful, isTrue); + expect(list.parse(scan('goodbye'))!.successful, isTrue); }); test('backtracks if not present', () { diff --git a/packages/combinator/test/value_test.dart b/packages/combinator/test/value_test.dart index c987d10f..97ab3543 100644 --- a/packages/combinator/test/value_test.dart +++ b/packages/combinator/test/value_test.dart @@ -6,10 +6,10 @@ main() { var parser = match('hello').value((r) => 'world'); test('sets value', () { - expect(parser.parse(scan('hello world')).value, 'world'); + expect(parser.parse(scan('hello world'))!.value, 'world'); }); test('no value if no match', () { - expect(parser.parse(scan('goodbye world')).value, isNull); + expect(parser.parse(scan('goodbye world'))!.value, isNull); }); }