Skip to main content

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
  • Rule transformation with Map
  • Full-input parsing

Installation

pip install parsil

Or install from source:

pip install -e .

Quick Start

from parsil import *

grammar = Grammar()

grammar["number"] = Regex(r"\d+")

parser = Parser(grammar, "123")

print(parser.parse("number"))

Output:

123

Example

from parsil import *

grammar = Grammar()

grammar["word"] = Sequence(
    Literal("hello"),
    Literal(" "),
    Literal("world"),
)

parser = Parser(grammar, "hello world")

print(parser.parse("word"))

Output:

["hello", " ", "world"]

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 Repeat a rule
Ref Reference another grammar rule
Map Transform a matched value

Project Structure

parsil/
├── grammar.py
├── parser.py
├── rules/
│   ├── literal.py
│   ├── regex.py
│   ├── sequence.py
│   ├── choice.py
│   ├── repeat.py
│   ├── ref.py
│   └── map.py
└── examples/
└── tests/

Running Tests

pytest

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

parsil_peg-0.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

parsil_peg-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file parsil_peg-0.1.0.tar.gz.

File metadata

  • Download URL: parsil_peg-0.1.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for parsil_peg-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7083a416e8f3c4a3951b3e84567520c7c18d6204b67b4757b7f172c995962a7c
MD5 6404172de6c4623c4dfce0d4c6ca1885
BLAKE2b-256 a516379db75361ceccd3d962b35ee69804c10993ebc2b35d18ef1eb1d6363279

See more details on using hashes here.

File details

Details for the file parsil_peg-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: parsil_peg-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for parsil_peg-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a11dcdaba2d3d854f2606a7cd14c31934b63549b115eca824e2c23582f4d215
MD5 6ecdfe294af5a158be45b71ccedbc99a
BLAKE2b-256 fd8ef2c8665d41a9d136baf837074a8c7e420714001f9b3fd73e89d04206a764

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page