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.

Source Distribution

ore-combinators-0.0.4.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

ore_combinators-0.0.4-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file ore-combinators-0.0.4.tar.gz.

File metadata

  • Download URL: ore-combinators-0.0.4.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.0

File hashes

Hashes for ore-combinators-0.0.4.tar.gz
Algorithm Hash digest
SHA256 5b03e9e0c2d1db2dff4de19cc105912466138726339ecc9c419b28b0bcf24ec6
MD5 34b4d80707a52f10ee1d55177c641071
BLAKE2b-256 708691117c94394fa7e6c004e90187d75484b92ca2b27bc2640f72f5907bdbf3

See more details on using hashes here.

File details

Details for the file ore_combinators-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: ore_combinators-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.0

File hashes

Hashes for ore_combinators-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bff9404282697d076d775f0220e98c89cf30e3c9396ee76ea0f5fadd8fbb1120
MD5 4b44a277b048494adf5fb01b98babd70
BLAKE2b-256 a5ce6afd034ba22be106f69d432911ca967dc71cd387ff43a4fb11fe587afe21

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