Skip to main content

Parser combinator library for Python

Project description

Parsita

Build status Code coverage Latest PyPI version Supported Python versions

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(TextParsers, 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!').or_die()
assert name == 'David'

# A parsing failure produces a useful error message
name = HelloWorldParsers.hello_world.parse('Hello David!').or_die()
# 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-1.8.0b1.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

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

parsita-1.8.0b1-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file parsita-1.8.0b1.tar.gz.

File metadata

  • Download URL: parsita-1.8.0b1.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-73-generic

File hashes

Hashes for parsita-1.8.0b1.tar.gz
Algorithm Hash digest
SHA256 53859c064c5cb0fd6050c4d51e57669593a1501825aced19e7e10d01fb2db4b0
MD5 6432b99c1ab61cb89071548d11959d03
BLAKE2b-256 267500284fa2da52675e8041b04ae7d73f701d3b8c6d98ef69c4d8097d503b2e

See more details on using hashes here.

File details

Details for the file parsita-1.8.0b1-py3-none-any.whl.

File metadata

  • Download URL: parsita-1.8.0b1-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-73-generic

File hashes

Hashes for parsita-1.8.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 148377dfa895b290972aa2c71894de6bc173cfb27ebf4615ffa0c00e5a22fce5
MD5 14841e60c331583350e400d2332d431d
BLAKE2b-256 3f4f97c8f69eae14480ed65dcb7d97eac42fe6017346929f20bf900c89bfc842

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