Skip to main content

A Python Generator based Parser Combinator library

Project description

PGPC

Overview

PGPC is a parser combinator library. Its name is an acronym, which stands for Python Generator based Parser Combinator library.

The library was heavily influenced by the Parsec monadic parser combinator library, so the transition from Parsec (and other parser combinators) to PGPC should be relatively easy.

The original idea of the library is emulating the do-notation with the yield Python keyword.

Quick start

The @topology decorator over a function allows the yield keyword work with parsers in a do-notation-like fashion:

  1. install the package: pip install pgpc
  2. save the following code into main.py:
from pgpc.scanner import TextScanner
from pgpc.parser import Parser, topology, char, position, content


@topology
def parse_hello_world():
    start = yield position()

    for c in "Hello":
        yield char(c)

    yield char(",")
    yield char(" ")

    for c in "World":
        yield char(c)

    yield char("!")

    end = yield position()
    source = yield content()

    return f"Parsed '{source[start.offset:end.offset]}' which started at {start} and ended at {end}"


if __name__ == '__main__':
    text = "Hello, World!"

    hw_parser: Parser[str] = parse_hello_world()

    result = hw_parser(TextScanner(text))

    print(result)
  1. run main.py: python main.py

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

pgpc-0.0.4.tar.gz (8.2 kB view hashes)

Uploaded Source

Built Distribution

pgpc-0.0.4-py3-none-any.whl (9.3 kB view hashes)

Uploaded Python 3

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