combinator 1.1.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 65

combinator #

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 = number.map((r) => int.parse(r.span.text));
  
  // Expect a pattern, or nothing.
  var optional = numWithValue.opt();
  
  // Expect a pattern zero or more times.
  var star = optional.star();
  
  // Expect one or more times.
  var plus = optional.plus();
  
  // Expect an arbitrary number of times.
  var threeTimes = optional.times(3);
  
  // Expect a sequence of patterns.
  var doraTheExplorer = chain([
    match('Dora').space(),
    match('the').space(),
    match('Explorer').space(),
  ]);
  
  // Choose exactly one of a set of patterns, whichever
  // appears first.
  var alt = any([
    match('1'),
    match('11'),
    match('111'),
  ]);
  
  // Choose the *longest* match for any of the given alternatives.
  var alt2 = longest([
    match('1'),
    match('11'),
    match('111'),
  ]);
  
  // 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();
}

Whitespaces #

Handling optional whitespace is dead-easy:

void main(Parser parser) {
  var optionalSpace = parser.space();
}

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
                .separatedByComma()
                .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)

1.1.0 #

  • Add tupleX parsers. Hooray for strong typing!

1.0.0+3 #

  • then now always returns dynamic.

1.0.0+2 #

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

example/main.dart

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

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.toolString);
        stderr.writeln(error.span.highlight(color: true));
      }
    } else
      print(result.value);
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  combinator: ^1.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub 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.1.0 Jan 8, 2019 Go to the documentation of combinator 1.1.0 Download combinator 1.1.0 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

All 13 versions...

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

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

  • Dart: 2.3.1
  • pana: 0.12.17

Platforms

Detected platforms: Flutter, web, other

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

Health suggestions

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

Analysis of lib/src/combinator/combinator.dart reported 8 hints, including:

line 76 col 7: DO use curly braces for all flow control structures.

line 79 col 7: DO use curly braces for all flow control structures.

line 177 col 26: Use = to separate a named parameter from its default value.

line 233 col 37: Use = to separate a named parameter from its default value.

line 253 col 18: Use = to separate a named parameter from its default value.

Fix lib/src/combinator/reference.dart. (-1.49 points)

Analysis of lib/src/combinator/reference.dart reported 3 hints:

line 11 col 3: Avoid return types on setters.

line 13 col 7: DO use curly braces for all flow control structures.

line 21 col 7: DO use curly braces for all flow control structures.

Fix lib/src/combinator/any.dart. (-1 points)

Analysis of lib/src/combinator/any.dart reported 2 hints:

line 10 col 20: Use = to separate a named parameter from its default value.

line 39 col 9: DO use curly braces for all flow control structures.

Fix additional 6 files with analysis or formatting issues. (-3 points)

Additional issues in the following files:

  • lib/src/combinator/chain.dart (1 hint)
  • lib/src/combinator/check.dart (1 hint)
  • lib/src/combinator/index.dart (1 hint)
  • lib/src/combinator/longest.dart (1 hint)
  • lib/src/combinator/match.dart (1 hint)
  • lib/src/combinator/reduce.dart (1 hint)

Dependencies

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.5
source_span ^1.0.0 1.5.5
string_scanner ^1.0.0 1.0.4
tuple ^1.0.0 1.0.2
Transitive dependencies
charcode 1.1.2
meta 1.1.7
path 1.6.2
quiver 2.0.3
stack_trace 1.9.3
term_glyph 1.1.0
Dev dependencies
test any