combinator 1.0.0

  • Example
  • Installing
  • Versions
  • 74


version build status

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

RECOMMENDED: Check example/ for examples. The examples contain examples of using:

  • Generic typing
  • Reading FileSpan from ParseResult
  • More...

Basic Usage

void main() {
  // Parse a Pattern (usually String or RegExp).
  var foo = match('foo');
  var number = match(new RegExp(r'[0-9]+'), errorMessage: 'Expected a number.');
  // Set a value.
  var numWithValue = => int.parse(r.span.text));
  // Expect a pattern, or nothing.
  var optional = numWithValue.opt();
  // Expect a pattern zero or more times.
  var star =;
  // Expect one or more times.
  var plus =;
  // Expect an arbitrary number of times.
  var threeTimes = optional.times(3);
  // Expect a sequence of patterns.
  var doraTheExplorer = chain([
  // Choose exactly one of a set of patterns, whichever
  // appears first.
  var alt = any([
  // Choose the *longest* match for any of the given alternatives.
  var alt2 = longest([
  // Friendly operators
  var fooOrNumber = foo | number;
  var fooAndNumber = foo & number;
  var notFoo = ~foo;

Error Messages

Parsers without descriptive error messages can lead to frustrating dead-ends for end-users. Fortunately, combinator is built with error handling in mind.

void main(Parser parser) {
  // Append an arbitrary error message to a parser if it is not matched.
  var withError = parser.error(errorMessage: 'Hey!!! Wrong!!!');
  // You can also set the severity of an error.
  var asHint = parser.error(severity: SyntaxErrorSeverity.hint);
  // Constructs like `any`, `chain`, and `longest` support this as well.
  var foo = longest([
    parser.error(errorMessage: 'foo'),
    parser.error(errorMessage: 'bar')
  ], errorMessage: 'Expected a "foo" or a "bar"');
  // If multiple errors are present at one location,
  // it can create a lot of noise.
  // Use `foldErrors` to only take one error at a given location.
  var lessNoise = parser.foldErrors();


Handling optional whitespace is dead-easy:

void main(Parser parser) {
  var optionalSpace =;

For Programming Languages

combinator was conceived to make writing parsers for complex grammars easier, namely programming languages. Thus, there are functions built-in to make common constructs easier:

void main(Parser parser) {
  var array = parser
                .surroundedBySquareBrackets(defaultValue: []);
  var braces = parser.surroundedByCurlyBraces();
  var sep = parser.separatedBy(match('!').space());

Differences between this and Petitparser

  • combinator makes extensive use of Dart's dynamic typing
  • combinator supports detailed error messages (with configurable severity)
  • combinator keeps track of locations (ex. line 1: 3)


  • star now includes with a call to opt.
  • Added comments.


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

final Parser minus = match('-');

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

final Parser digits =;

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));

main() {
  while (true) {
    stdout.write('Enter a number: ');
    var line = stdin.readLineSync();
    var scanner = new SpanScanner(line, sourceUrl: 'stdin');
    var result = number.parse(scanner);

    if (!result.successful) {
      for (var error in result.errors) {
        stderr.writeln(error.span.highlight(color: true));
    } else

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  combinator: "^1.0.0"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

      import 'package:combinator/combinator.dart';
Version Uploaded Documentation Archive
1.0.0 May 30, 2018 Go to the documentation of combinator 1.0.0 Download combinator 1.0.0 archive
1.0.0-beta+7 Nov 26, 2017 Go to the documentation of combinator 1.0.0-beta+7 Download combinator 1.0.0-beta+7 archive
1.0.0-beta+6 Nov 20, 2017 Go to the documentation of combinator 1.0.0-beta+6 Download combinator 1.0.0-beta+6 archive
1.0.0-beta+5 Nov 20, 2017 Go to the documentation of combinator 1.0.0-beta+5 Download combinator 1.0.0-beta+5 archive
1.0.0-beta+4 Nov 20, 2017 Go to the documentation of combinator 1.0.0-beta+4 Download combinator 1.0.0-beta+4 archive
1.0.0-beta+3 Nov 20, 2017 Go to the documentation of combinator 1.0.0-beta+3 Download combinator 1.0.0-beta+3 archive
1.0.0-beta+2 Nov 6, 2017 Go to the documentation of combinator 1.0.0-beta+2 Download combinator 1.0.0-beta+2 archive
1.0.0-beta+1 Nov 4, 2017 Go to the documentation of combinator 1.0.0-beta+1 Download combinator 1.0.0-beta+1 archive
1.0.0-beta Nov 4, 2017 Go to the documentation of combinator 1.0.0-beta Download combinator 1.0.0-beta archive


We analyzed this package on Jun 12, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.60.0
  • pana: 0.11.3


Describes how popular the package is relative to other packages. [more]
47 / 100
Code health derived from static analysis. [more]
100 / 100
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
Learn more about scoring.


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:combinator/combinator.dart.


  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 1 hint.

    Strong-mode analysis of lib/src/combinator/combinator.dart gave the following hint:

    line: 278 col: 5
    Dead code.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0
code_buffer ^1.0.0 1.0.0
matcher ^0.12.0 0.12.2+1
source_span ^1.0.0 1.4.0
string_scanner ^1.0.0 1.0.2
tuple ^1.0.0 1.0.1
Transitive dependencies
charcode 1.1.1
path 1.6.0
quiver_hashcode 1.0.0
stack_trace 1.9.2
Dev dependencies
dart2_constant ^1.0.0
t_stats ^1.1.2
test ^0.12.0