2021-03-17 23:04:36 +00:00
|
|
|
// For some reason, this cannot be run in checked mode???
|
|
|
|
|
|
|
|
import 'dart:io';
|
2021-05-14 06:11:50 +00:00
|
|
|
import 'package:angel3_combinator/angel3_combinator.dart';
|
2021-03-17 23:04:36 +00:00
|
|
|
import 'package:string_scanner/string_scanner.dart';
|
|
|
|
|
|
|
|
final Parser<String> key =
|
|
|
|
match<String>(RegExp(r'[^=&\n]+'), errorMessage: 'Missing k/v')
|
2021-03-18 00:00:56 +00:00
|
|
|
.value((r) => r.span!.text);
|
2021-03-17 23:04:36 +00:00
|
|
|
|
2021-03-18 00:00:56 +00:00
|
|
|
final Parser value = key.map((r) => Uri.decodeQueryComponent(r.value!));
|
2021-03-17 23:04:36 +00:00
|
|
|
|
|
|
|
final Parser pair = chain([
|
|
|
|
key,
|
|
|
|
match('='),
|
|
|
|
value,
|
|
|
|
]).map((r) {
|
|
|
|
return {
|
2021-03-18 00:00:56 +00:00
|
|
|
r.value![0]: r.value![2],
|
2021-03-17 23:04:36 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
final Parser pairs = pair
|
|
|
|
.separatedBy(match(r'&'))
|
2021-03-18 00:00:56 +00:00
|
|
|
.map((r) => r.value!.reduce((a, b) => a..addAll(b)));
|
2021-03-17 23:04:36 +00:00
|
|
|
|
|
|
|
final Parser queryString = pairs.opt();
|
|
|
|
|
|
|
|
main() {
|
|
|
|
while (true) {
|
|
|
|
stdout.write('Enter a query string: ');
|
2021-03-18 00:00:56 +00:00
|
|
|
var line = stdin.readLineSync()!;
|
2021-05-14 06:11:50 +00:00
|
|
|
var scanner = SpanScanner(line, sourceUrl: 'stdin');
|
2021-05-02 04:12:43 +00:00
|
|
|
var result = pairs.parse(scanner);
|
2021-03-17 23:04:36 +00:00
|
|
|
|
|
|
|
if (!result.successful) {
|
|
|
|
for (var error in result.errors) {
|
|
|
|
print(error.toolString);
|
2021-03-18 00:00:56 +00:00
|
|
|
print(error.span!.highlight(color: true));
|
2021-03-17 23:04:36 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
print(result.value);
|
|
|
|
}
|
|
|
|
}
|