Skip to main content

Parser combinator library for Python

Project description

Ore

A simple WIP pythonic parser combinator library inspired by Haskell's attoparsec.

It supports two styles: declarative and imperative.

For example, declarative style looks like the following:

from ore.combinators import alphabet, transform
from ore.combinators import sequence, take_while_possible

join = lambda l: ''.join(l)

name = transform(
    sequence(
        alphabet,
        transform(
            take_while_possible(alphabet),
            join
        )
    ),
    join
)

The very same combinator could be written as function:

from typing import Tuple

from ore.combinators import alphabet
from ore.combinators import take_while_possible
from ore import combinator_function, ParserState, Result


@combinator_function()
def name(state: ParserState) -> Tuple[Result, ParserState]:
    first_symbol, state = alphabet(state)
    other_symbols, state = take_while_possible(alphabet)(state)

    return Result.make_value(
        first_symbol.value + ''.join(other_symbols.value),
        state
    )

To run a parser on a given text, use run or run_safe:

from typing import Tuple

from ore.combinators import alphabet
from ore.combinators import take_while_possible
from ore import ParserState, Result
from ore import run_safe, combinator_function


@combinator_function()
def name(state: ParserState) -> Tuple[Result, ParserState]:
    first_symbol, state = alphabet(state)
    other_symbols, state = take_while_possible(alphabet)(state)

    return Result.make_value(
        first_symbol.value + ''.join(other_symbols.value),
        state
    )

name_result = run_safe(name, "Ore     ")

assert name_result.value == "Ore"

The difference between run and run_safe is that run_safe returns result without raising exceptions. Exceptions saved in the result instead. run just throws exceptions without saving them into result.

Installation

To install this library, just type pip install ore-combinators in the console.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for ore-combinators, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size ore_combinators-0.0.1-py3-none-any.whl (6.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size ore-combinators-0.0.1.tar.gz (4.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page