Skip to main content

Parser combinator library for Python

Project description

Parsita

Build status Code coverage Latest PyPI version License Supported Python versions Black Poetry Nox Ruff

The executable grammar of parsers combinators made available in the executable pseudocode of Python.

Parsita is a parser combinator library written in Python. Parser combinators provide an easy way to define a grammar using code so that the grammar itself effectively parses the source. They are not the fastest at parsing, but they are the easiest to write.

Like all good parser combinator libraries, Parsita abuses operators to provide a clean grammar-like syntax. The __or__ method is defined so that | tests between two alternatives. The __and__ method is defined so that & tests two parsers in sequence. Other operators are used as well.

In a technique that I think is new to Python, Parsita uses metaclass magic to allow for forward declarations of values. This is important for parser combinators because grammars are often recursive or mutually recursive, meaning that some components must be used in the definition of others before they themselves are defined.

See the Documentation for the full user guide.

Installation

The recommended means of installation is with pip from PyPI.

pip install parsita

Hello world

The following is a very basic parser for extracting the name from a Hello, {name}! string.

from parsita import *

class HelloWorldParsers(ParserContext, whitespace=r'[ ]*'):
    hello_world = lit('Hello') >> ',' >> reg(r'[A-Z][a-z]*') << '!'

# A successful parse produces the parsed value
name = HelloWorldParsers.hello_world.parse('Hello, David!').unwrap()
assert name == 'David'

# A parsing failure produces a useful error message
name = HelloWorldParsers.hello_world.parse('Hello David!').unwrap()
# parsita.state.ParseError: Expected ',' but found 'David'
# Line 1, character 7
#
# Hello David!
#       ^

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

parsita-2.2.0.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

parsita-2.2.0-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file parsita-2.2.0.tar.gz.

File metadata

  • Download URL: parsita-2.2.0.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for parsita-2.2.0.tar.gz
Algorithm Hash digest
SHA256 6976cb4944080c3ad23d4ccf797060da46aec933fde4f705fb6b3923d9c6f2fa
MD5 2c76517fb6efe5ea3a6a66ebd927e0ea
BLAKE2b-256 67f925656f7f3b5fac688bed028b74bbe8757eb0912577899f63847e63635093

See more details on using hashes here.

Provenance

The following attestation bundles were made for parsita-2.2.0.tar.gz:

Publisher: release.yml on drhagen/parsita

Attestations:

File details

Details for the file parsita-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: parsita-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for parsita-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0ba8f9867aa413dd3c1ba9104cf9003b2981dc44ac777cf33e78c82bcc02bc9
MD5 2b0442084bc4193fc2bb215fd5d6db33
BLAKE2b-256 34a0865dd936d655b9d6283468ef4c2d236c255737160d988a25b2773cf73c13

See more details on using hashes here.

Provenance

The following attestation bundles were made for parsita-2.2.0-py3-none-any.whl:

Publisher: release.yml on drhagen/parsita

Attestations:

Supported by

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