belatuk_combinator 5.3.0 copy "belatuk_combinator: ^5.3.0" to clipboard
belatuk_combinator: ^5.3.0 copied to clipboard

Packrat parser combinators that support static typing, generics, file spans, memoization, and more.

example/main.dart

import 'dart:io';
import 'package:belatuk_combinator/belatuk_combinator.dart';
import 'package:string_scanner/string_scanner.dart';

final Parser minus = match('-');

final Parser<int> digit =
    match(RegExp(r'[0-9]'), errorMessage: 'Expected a number');

final Parser digits = digit.plus();

final Parser dot = match('.');

final Parser decimal = ( // digits, (dot, digits)?
        digits & (dot & digits).opt() //
    );

final Parser number = //
    (minus.opt() & decimal) // minus?, decimal
        .map<num>((r) => num.parse(r.span!.text));

void main() {
  while (true) {
    stdout.write('Enter a number: ');
    var line = stdin.readLineSync()!;
    var scanner = SpanScanner(line, sourceUrl: 'stdin');
    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));
      }
    } else {
      print(result.value);
    }
  }
}
1
likes
160
pub points
41%
popularity

Publisher

verified publisherdukefirehawk.com

Packrat parser combinators that support static typing, generics, file spans, memoization, and more.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

belatuk_code_buffer, matcher, source_span, string_scanner, tuple

More

Packages that depend on belatuk_combinator