combinator 1.0.0+3

  • Example
  • Installing
  • Versions
  • 62


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)


  • then now always returns dynamic.


  • star now includes with a call to opt.
  • Added comments.
  • Enforce generics on separatedBy.
  • Enforce Dart 2 semantics.


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+3

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+3 Jun 23, 2018 Go to the documentation of combinator 1.0.0+3 Download combinator 1.0.0+3 archive
1.0.0+2 Jun 23, 2018 Go to the documentation of combinator 1.0.0+2 Download combinator 1.0.0+2 archive
1.0.0+1 Jun 23, 2018 Go to the documentation of combinator 1.0.0+1 Download combinator 1.0.0+1 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

All 12 versions...

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

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

  • Dart: 2.1.0
  • pana: 0.12.7


Detected platforms: Flutter, web, other

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

Health suggestions

Fix lib/src/combinator/combinator.dart. (-0.50 points)

Analysis of lib/src/combinator/combinator.dart reported 1 hint:

line 278 col 5: Dead code.

Format lib/src/combinator/any.dart.

Run dartfmt to format lib/src/combinator/any.dart.

Format lib/src/combinator/longest.dart.

Run dartfmt to format lib/src/combinator/longest.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0
code_buffer ^1.0.0 1.0.1
matcher ^0.12.0 0.12.4
source_span ^1.0.0 1.4.1
string_scanner ^1.0.0 1.0.4
tuple ^1.0.0 1.0.2
Transitive dependencies
charcode 1.1.2
meta 1.1.6
path 1.6.2
quiver 2.0.1
stack_trace 1.9.3
Dev dependencies
dart2_constant ^1.0.0
test ^0.12.0