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
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.
- xmlBuilt on petitparserFull-featured XML and XPath library for DOM parsing, SAX streaming, and fluent building.
- tomlBuilt on petitparserParse and encode TOML configuration files in Dart.
- json_pathBuilt on petitparserDart implementation of RFC 9535 JSONPath for querying and manipulating JSON
- iregexpBuilt on petitparserDart implementation of RFC 9485 I-Regexp, validating and matching patterns.
- puppeteerBuilt on petitparserA Dart port of Puppeteer to automate and control Chrome over the DevTools Protocol.
- pemBuilt on petitparserPEM encoding and decoding of textual keys and certificates following RFC 7468.
- math_expressionsBuilt on petitparserParse and evaluate mathematical expressions in real, vector, and interval contexts.
- lokalise_flutter_sdkBuilt on petitparserLokalise Flutter SDK over-the-air translations updates. This package provides new translations from lokalise.com without a new app release.