https://github.com/true-grue/peco
Tiny parser combinators library written in Python.
https://github.com/true-grue/peco
minmalist parser-combinators peg python
Last synced: about 1 month ago
JSON representation
Tiny parser combinators library written in Python.
- Host: GitHub
- URL: https://github.com/true-grue/peco
- Owner: true-grue
- License: mit
- Created: 2024-02-29T10:20:20.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-05-20T21:39:35.000Z (8 months ago)
- Last Synced: 2025-08-25T21:05:28.530Z (5 months ago)
- Topics: minmalist, parser-combinators, peg, python
- Language: Python
- Homepage:
- Size: 41 KB
- Stars: 15
- Watchers: 3
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# peco
This is a tiny (100 LOC) parser combinator library in Python.
Installation:
```
pip install pecolib
```
Main features:
* Combined lexical and syntactic parsing using the PEG formalism.
* Lexical rules with regular expressions (see `eat`).
* Stack-based implementation of semantic actions (see `push` and `to`).
* Selective memoization for performance (see `memo`).
* Support for left recursion (see `left`).
Parsers are built from combinators and operate on a `Peco` namedtuple:
* `text: str`. Source text.
* `pos: int`. Position in the `text`.
* `ok: bool`. Parsing result.
* `stack: tuple | None`. Semantic result stack.
* `glob: dict`. Global data, including the error position field (`err`).
Most combinators follow PEG constructs:
* `empty`. Empty string.
* `seq`. Sequence.
* `alt`. Ordered choice.
* `many`. Zero-or-more.
* `some`. One-or-more.
* `opt`. Optional.
* `peek`. And-predicate.
* `npeek`. Not-predicate.
Support for semantic actions:
* `push(f)`. Pushes a text fragment parsed by the `f` combinator onto the `stack`.
* `to(f)`. Pops `n` elements from the `stack` and passes them as arguments to function `f` with arity `n`. The result is pushed back onto the `stack`.
* `group(f)`. Combines all elements pushed onto the `stack` by `f` into a single tuple.
For examples of using peco's combinators, see the tests.