Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kraglik/ore
A simple and pythonic parser combinator library
https://github.com/kraglik/ore
parser-combinators python
Last synced: about 1 month ago
JSON representation
A simple and pythonic parser combinator library
- Host: GitHub
- URL: https://github.com/kraglik/ore
- Owner: kraglik
- License: mit
- Created: 2021-06-09T13:17:43.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-06-10T13:18:44.000Z (over 3 years ago)
- Last Synced: 2024-11-30T04:49:09.923Z (about 2 months ago)
- Topics: parser-combinators, python
- Language: Python
- Homepage:
- Size: 30.3 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
```python3
from ore_combinators.combinators import alphabet, transform
from ore_combinators.combinators import sequence, take_while_possiblejoin = lambda l: ''.join(l)
name = transform(
sequence(
alphabet,
transform(
take_while_possible(alphabet),
join
)
),
join
)
```The very same combinator could be written as function:
```python3
from typing import Tuplefrom 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`:
```python3
from typing import Tuplefrom 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.