fpeg 0.1.0

A PEG parsing library: grammars loaded at runtime, a bytecode parsing machine and a lossless parse tree
Documentation
  • Coverage
  • 100%
    333 out of 333 items documented3 out of 3 items with examples
  • Size
  • Source code size: 742.1 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 4.1 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 3s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • schwimmbeck/fpeg
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • schwimmbeck

CI Coverage Docs Licence: MIT OR Apache-2.0 REUSE status Python 3.11+ Rust 1.98+

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.
import fpeg

grammar = fpeg.Grammar(r"""
start: pair ** ","
pair: key=NAME "=" value=NUMBER
NAME: [a-z]+
NUMBER: [0-9]+
%ignore " "+
""")

tree = grammar.parse("x = 1, y = 22")
print(tree.pretty())
start
  pair
    key=x
    value=1
  pair
    key=y
    value=22

A Transformer turns the tree into values, with one method for each rule:

class ToDict(fpeg.Transformer):
    def start(self, *pairs):
        return dict(pairs)

    def pair(self, key, value):
        return str(key), int(value)


print(ToDict().transform(tree))
{'x': 1, 'y': 22}

Install

For Python 3.11 or newer:

pip install fpeg

For Rust 1.98 or newer, the library is the crate fpeg:

cargo add 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:

cargo install fpeg-cli

Benchmarks

fpeg and other parsers, time per input

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.