A lightweight PEG parser combinator library for Python.
Project description
Parsil
A lightweight PEG parser combinator library for Python.
Parsil provides a small set of composable parsing rules for building recursive-descent parsers. It is designed to be simple, readable, and easy to extend.
Features
- Lightweight and dependency-free
- PEG-style parser combinators
- Recursive grammars
- Regular expression support
- Rule composition with operators
- Rule transformation with
Map - Structured parsing results (
Success/Failure) - Full-input parsing
- Helpful parse error reporting
Installation
Install from PyPI:
pip install parsil-peg
Or install the latest development version from source:
git clone https://github.com/shahilahmed/parsil.git
cd parsil
pip install -e .
Quick Start
from parsil import *
grammar = Grammar()
grammar["number"] = R(r"\d+").map(int)
parser = Parser(grammar, "123")
result = parser.parse("number")
if result.ok:
print(result.value)
Output:
123
Building Grammars
Parsil supports a concise DSL for composing parser rules.
| Expression | Equivalent |
|---|---|
a + b |
Sequence(a, b) |
a | b |
Choice(a, b) |
rule.star() |
Repeat(rule) |
rule.plus() |
Repeat(rule, minimum=1) |
rule.optional() |
Repeat(rule, maximum=1) |
rule.map(func) |
Map(rule, func) |
Helper functions:
| Function | Equivalent |
|---|---|
L(text) |
Literal(text) |
R(pattern) |
Regex(pattern) |
Ref_(name) |
Ref(name) |
Example
from parsil import *
grammar = Grammar()
grammar["word"] = (
L("hello")
+ L(" ")
+ L("world")
)
parser = Parser(grammar, "hello world")
result = parser.parse("word")
if result.ok:
print(result.value)
Output:
['hello', ' ', 'world']
Parse Results
Every parse returns either a Success or Failure.
Successful parse:
result = parser.parse("number")
if result.ok:
print(result.value)
Failed parse:
result = parser.parse("number")
if result.failed:
print(result.position)
print(result.expected)
Example:
Failure(position=4, expected=[')'])
Available Rules
| Rule | Description |
|---|---|
Literal |
Match an exact string |
Regex |
Match a regular expression |
Sequence |
Match rules in order |
Choice |
Match the first successful rule |
Repeat |
Match a rule repeatedly |
Ref |
Reference another grammar rule |
Map |
Transform a matched value |
Project Structure
parsil/
├── examples/
├── parsil/
│ ├── grammar.py
│ ├── parser.py
│ ├── result.py
│ └── rules/
│ ├── base.py
│ ├── literal.py
│ ├── regex.py
│ ├── sequence.py
│ ├── choice.py
│ ├── repeat.py
│ ├── ref.py
│ └── map.py
├── tests/
├── LICENSE
├── README.md
└── pyproject.toml
Running Tests
pytest
Requirements
- Python 3.8 or later
License
This project is licensed under the MIT License.
PyPI
https://pypi.org/project/parsil-peg/
Source Code
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file parsil_peg-0.1.1.tar.gz.
File metadata
- Download URL: parsil_peg-0.1.1.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
187424dfb1ef6bad2a57f03f1b8c208524480422674217924c4bed36ab620463
|
|
| MD5 |
0bc726e8743ad977a41a3078d57ff235
|
|
| BLAKE2b-256 |
f4633220de81d8cf02f8dcb9c6cdab881d6e94d44ef92b4467f019d6ec9568cb
|
File details
Details for the file parsil_peg-0.1.1-py3-none-any.whl.
File metadata
- Download URL: parsil_peg-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61c4da0e843eef8dddb7bce839ac76043798e721adf63791ae8c96c4f8648a4a
|
|
| MD5 |
6a708d2e7460b75f08936a0fc82c17d1
|
|
| BLAKE2b-256 |
603c0893dbf793cf36e30a099055e4dcf498d027650d3a597e7a2f8373aa6ac5
|