A parsing library for Parsing Expression Grammars (PEG), with a native engine written in Rust and a Transformer that turns parse trees into your own objects.
- A grammar is plain text, loaded at runtime. No code generation and no build step.
- The engine compiles a grammar to bytecode for a parsing machine and builds a compact, lossless tree with exact source positions.
- Errors say where a parse failed and what was expected there, a grammar can name its mistakes and go on past them, and limits bound the time and memory of every parse.
=
=
start
pair
key=x
value=1
pair
key=y
value=22
A Transformer turns the tree into values, with one method for each rule:
return
return ,
{'x': 1, 'y': 22}
Install
For Python 3.11 or newer:
For Rust 1.98 or newer, the library is the crate fpeg:
The fpeg command checks grammars, parses inputs and profiles a parse. Each release comes with it built for Linux, macOS and Windows, and Cargo builds it from source:
Benchmarks
The benchmarks page has the numbers of the newest recorded runs, and the benchmarks folder the scripts that take them.
Documentation
- Getting started: install fpeg, write a first grammar and turn its tree into values, in five minutes.
- Examples: a calculator, a configuration file, CSV, JSON and a filter language, each a grammar and a
Transformer. - Tutorials and guides on operator precedence, syntax trees, indentation, modules, errors and performance.
- The reference of the grammar language, the Python API and the command line.
What comes next
Editor support is planned for the next release: a language server, a formatter and highlighting for grammar files. The page of planned features lists what comes after.
Licence
Licensed under either of the MIT licence or the Apache License, Version 2.0, at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in fpeg by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.