PetitParser
PetitParser makes writing parsers fast, enjoyable, and fully type-safe.
Instead of configuring external code generators or writing cryptic regular expressions, PetitParser models grammars directly as composable objects in your programming language. Assemble small, specialized parsers as modular building blocks to model everything from configuration formats to full programming languages.
- Composable: Combine basic parsers using intuitive operators (
&,|,.optional(),.star()) without separate grammar files. - Type-Safe: Transform text directly into strongly typed models, syntax trees, records, and pattern-matched objects.
- High Performance: Optimized character predicates and dedicated fast-path parsing keep execution fast and memory-efficient.
- Debuggable: Step through parsers with your native IDE debugger, inspect tokens, and visualize execution trees with built-in tracing.
Quick Look
Building a parser with PetitParser is straightforward and readable. Here is an example in Dart that recognizes key-value assignment pairs and maps them into a typed record:
import 'package:petitparser/petitparser.dart';
void main() {
final key = letter().plus().flatten();
final value = digit().plus().flatten().map(int.parse);
final entry = key.trim().skip(after: char('=')).then(value.trim());
final result = entry.parse('port = 8080');
print(result.value); // ('port', 8080)
}
Supported Languages
PetitParser is available in many programming languages:
Examples & Grammars
Explore interactive browser playgrounds and example grammars:
Programming Languages
- Dart: Comprehensive grammar for modern Dart supporting records, pattern matching, switch expressions, class modifiers, and enhanced enums (demo, source).
- Smalltalk: Complete Smalltalk-80 grammar and AST generator, ported from the original Helvetia implementation (demo, source).
- Pascal: Classic Pascal language grammar implementing the 1978 Apple Pascal Standard (source).
Interpreters & Engines
- Lisp Interpreter: Scheme-like evaluator supporting lexical scoping, lambda closures, macros, and native functions (demo, source).
- Prolog Interpreter: Logic programming inference engine featuring knowledge base parsing, SLD-resolution search, and unification (demo, source).
- Regular Expressions: Automata compiler that parses regex syntax, compiles to Non-deterministic Finite Automata (NFA), and steps through matching with an interactive visualizer (demo, source).
Mathematics & Expressions
- Math Evaluator: Arithmetic expression evaluator built with
ExpressionBuilder, handling operator precedence, associativity, functions, and variables (demo, source). - Expression Plotter: Parses mathematical expressions and plots function curves across coordinates on an HTML5 canvas (demo, source).
Formats & Protocols
- URI Parser: RFC-3986 parser deconstructing URIs into scheme, authority, credentials, host, port, path, query parameters, and fragments (demo, source).
- BibTeX: Extracts bibliographic entries, citation keys, tags, and key-value fields from BibTeX database files (demo, source).
- Tabular Text (CSV / TSV): Configurable delimited text parser with support for custom separators, quotes, and escaped values (demo, source).
- JSON: RFC-8259 parser comparing PetitParser against native platform JSON parsing throughput and mapping (demo, source).
- XML & XPath: Parses XML event streams, produces traversable DOM syntax trees, formats XML, and executes XPath queries (demo, source).
Background & Theory
PetitParser combines foundational concepts from computer science into an approachable object-oriented architecture:
- Scannerless Parsing: Lexing and parsing happen in a single unified phase, eliminating the impedance mismatch between lexer tokens and parser grammar rules.
- Parser Combinators: Grammars are built by composing primitive parser functions into complex parsers using higher-order combinators.
- Parsing Expression Grammars (PEG): Unambiguous prioritized choices eliminate ambiguity and simplify grammar development.
- Packrat Parsing: Efficient memoization ensures linear parse time when needed.
- Dynamic Reconfiguration: Grammars and parsers are first-class runtime objects that can be dynamically inspected, transformed, extended, or rewritten.
PetitParser was originally created by Lukas Renggli as part of his research on the Helvetia Language Workbench at the University of Bern.