Skip to main content

Parser combinator library for Python

Project description

Parsita

PyPI downloads Build status Code coverage Latest PyPI version License Supported Python versions

The executable grammar of parser 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.3.0.tar.gz (18.8 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.3.0-py3-none-any.whl (29.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for parsita-2.3.0.tar.gz
Algorithm Hash digest
SHA256 7f8b3af94e8ae4e7ca77e9ba8621d813a23f2e9cdd3ffdc5c767070eb050f062
MD5 afb01c1a631216e3463cfd12ad5b9de9
BLAKE2b-256 603274163980a400366e4f76639ec404610e88a9dc4ee0db3dfd87a69c71e0b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for parsita-2.3.0.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.3.0-py3-none-any.whl.

File metadata

  • Download URL: parsita-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 29.5 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a6808f4d15bff96236883e56759b604acc54b3d66d40fc0e26d74862907eb2d6
MD5 a29d6e56be00bca82ccf0ac7fd97cc9c
BLAKE2b-256 fec734e0f3f51d3eb3d49c3b6745a4f1ec95b6399d0a1858da41c17aa8c2c046

See more details on using hashes here.

Provenance

The following attestation bundles were made for parsita-2.3.0-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