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.combinators import alphabet, transform
from ore_combinators.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.combinators import alphabet
from ore_combinators.combinators import take_while_possible
from ore_combinators import combinator_function, ParserState, Result
@combinator_function()
def name(state: ParserState) -> Tuple[str, ParserState]:
first_symbol, state = alphabet(state)
other_symbols, state = take_while_possible(alphabet)(state)
return Result.make_value(
first_symbol + ''.join(other_symbols),
state
)
To run a parser on a given text, use run or run_safe:
from typing import Tuple
from ore_combinators.combinators import alphabet
from ore_combinators.combinators import take_while_possible
from ore_combinators import ParserState, Result
from ore_combinators import run_safe, combinator_function
@combinator_function()
def name(state: ParserState) -> Tuple[str, ParserState]:
first_symbol, state = alphabet(state)
other_symbols, state = take_while_possible(alphabet)(state)
return Result.make_value(
first_symbol + ''.join(other_symbols),
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ore-combinators-0.0.5.tar.gz.
File metadata
- Download URL: ore-combinators-0.0.5.tar.gz
- Upload date:
- Size: 7.5 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
650fbbcfb979531b9de55f84eea8234ecdf868ad7ed8cddd83dcd5a575eca357
|
|
| MD5 |
209435ca4d0ef76c0e2b708de635545c
|
|
| BLAKE2b-256 |
efec0a9cdd91ab93f40ea2bd4eb4a46d9a60c112b60999df418e96bd4dbb5907
|
File details
Details for the file ore_combinators-0.0.5-py3-none-any.whl.
File metadata
- Download URL: ore_combinators-0.0.5-py3-none-any.whl
- Upload date:
- Size: 23.9 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f3cc7c9ba83ca89c1e705a384e3397b45071e422634d480a4f386ae36ce969f
|
|
| MD5 |
d174585d39d8b562ce309acf3d134bc7
|
|
| BLAKE2b-256 |
74b928d84ce3be3556c1365cffec04029aa4caa303c979b86c0ab31877cd03c3
|