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.4.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

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

parsita-2.2.4-py3-none-any.whl (29.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: parsita-2.2.4.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for parsita-2.2.4.tar.gz
Algorithm Hash digest
SHA256 d9615df5bdc2c8354ba7918f9be0a81cd121113e0f5a4da3247ec3927babf5cd
MD5 0ecc6a63c50d3dd34584859c702e78a7
BLAKE2b-256 f9daaea43cab802f53b4b3095ccbf4328dc720b044a0017239dea3b93cd63812

See more details on using hashes here.

Provenance

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

Publisher: release.yml on drhagen/parsita

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: parsita-2.2.4-py3-none-any.whl
  • Upload date:
  • Size: 29.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for parsita-2.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b10cd614317b15ebed1ac4d0e6239110f9d3bf578c756b5c427bd69cd8d5bd05
MD5 39166d995ba1060e501d54c0638f8720
BLAKE2b-256 623e5d687570521779cd8f7ef63fc6f99adf80918d25f4f85996804f3d31023e

See more details on using hashes here.

Provenance

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

Publisher: release.yml on drhagen/parsita

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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