Skip to content
pubscout

petitparser

7.1.0, released 8 weeks ago by lukas-renggli.ch

Build fast, composable, and type-safe grammars and parsers in plain Dart code.

WASM-readyRock solid
$ flutter pub add petitparser
Source repository

What it does

PetitParser allows developers to build small, specialized parsers directly in plain Dart code and combine them like LEGO bricks to parse text, configuration values, or complex programming languages. It provides type-safe combinators, character predicates, and transformation methods without requiring external code generators or fragile regular expressions.

AI-written summary of version 7.1.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to parse custom text formats, configuration files, or DSLs directly in Dart.
  • You want a type-safe and composable alternative to complex regular expressions.
  • You want to build and test grammars in-memory without a separate code-generation step.

Skip it when

  • You are parsing simple JSON or formats with widely available dedicated serializers.
  • You prefer external generator tools (like ANTLR) over code-as-grammar libraries.

Strengths

  • Fully type-safe using modern Dart features and records.
  • Highly composable with a rich set of combinators and builders.
  • No external code generators required.
  • Built-in tools for debugging and viewing execution trees.

Weaknesses

  • Steeper learning curve for developers unfamiliar with parser combinators.
  • May be overkill for trivial string manipulation tasks.

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
2
import 'package:petitparser/petitparser.dart';

void main() {
  final key = letter().plus().flatten();
  final value = digit().plus().flatten().map(int.parse);
  final parser = key.trim().skip(after: char('=')).then(value.trim()).map2((k, v) => (key: k, value: v));
  final result = parser.parse('port = 8080');
  print(result.value); // (key: port, value: 8080)
}

Works with

Packages commonly used together with petitparser: ones it builds on, ones built on it, and its own family.