fpeg¶
fpeg is a general-purpose parsing library for Parsing Expression Grammars (PEG).
A grammar is plain text, loaded at runtime. No code generation and no build step.
A native engine written in Rust compiles the grammar to bytecode for a parsing machine.
The parse produces a compact, lossless tree with exact source positions.
From Python you read that tree or turn it into your own objects with a
Transformer, a class with one method per rule.
Getting started installs fpeg, writes a first grammar and turns the tree of a parse into Python values, in about five minutes.
Examples¶
Complete programs for common tasks, each a grammar and a Transformer, to copy and change.
A calculator: lines of arithmetic with Python’s operators, variables and functions.
A configuration file: sections, comments and typed values, read into dictionaries.
CSV: a header and rows of quoted and bare fields, into a dictionary for each row.
JSON to Python objects: the values
json.loadsgives, with a grammar of your own or the one that comes with fpeg.A filter language: conditions parsed into a syntax tree of dataclasses and evaluated against records.
Tutorials¶
A grammar for JSON: from nothing to Python objects.
From a tree to your own objects: a
Transformerthat builds a syntax tree and computes values.
Guides¶
Operator precedence: expressions with a precedence block, and a calculator in Python’s order of operators.
Building an AST: your own classes from a parse, by methods or by builders in the grammar, and running the result.
Indentation: blocks by columns, brackets that turn layout off, continuation lines and tabs.
Interpolated strings: expressions inside strings, quotes as parameters, and delimiters that the input chooses.
Composing grammars: a grammar that extends another, rules taken from others, modules of your own, and Python extended with its transformer.
A language over Python: ORD, a grammar over Python with its transformer to
ast, checked against the language’s own parser.Errors and recovery: what an error says and where it points, labels, messages for common mistakes, and parses that go on past mistakes.
Performance: seeing where a parse spends its work, and what to change in a grammar.
Reference¶
Grammar language: the syntax of grammars.
Semantics: what a grammar means.
Parse tree: nodes, tokens, fields and spans.
Python API:
Grammar,Tree,Token,Transformer,Interpreter,Visitorand the errors.Command line:
fpeg check,fpeg parseandfpeg profile.Diagnostics: every problem a grammar can have, with its code.
Known limits: what fpeg does not do, or does at a cost, and why.
Explanation¶
How fpeg works: what happens from the text of a grammar to your values, and what each step costs.
Benchmarks: what fpeg and other parsers take on the same inputs.
Related projects: what other parsing tools are for, and how a grammar and its transformer move to fpeg.
What later releases bring is on the page of planned features. To work on fpeg itself, Contributing says how to build it from a checkout and run its checks.