platform/packages/combinator/example/delimiter.dart

30 lines
871 B
Dart
Raw Normal View History

2021-03-17 23:04:36 +00:00
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> id =
2021-03-18 00:00:56 +00:00
match<String>(RegExp(r'[A-Za-z]+')).value((r) => r.span!.text);
2021-03-17 23:04:36 +00:00
// We can use `separatedBy` to easily construct parser
// that can be matched multiple times, separated by another
// pattern.
//
// This is useful for parsing arrays or map literals.
2021-05-18 13:15:28 +00:00
void main() {
2021-03-17 23:04:36 +00:00
while (true) {
stdout.write('Enter a string (ex "a,b,c"): ');
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 = id.separatedBy(match(',').space()).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
}
2021-05-18 13:15:28 +00:00
} else {
2021-03-17 23:04:36 +00:00
print(result.value);
2021-05-18 13:15:28 +00:00
}
2021-03-17 23:04:36 +00:00
}
}