reparsec
Small parsec-like parser combinators library with semi-automatic error recovery.
Installation
pip install reparsec
Usage
Example
With reparsec, simple arithmetic expression parser and evaluator could be written like this:
from typing import Callable
from reparsec import Delay
from reparsec.scannerless import literal, parse, regexp
from reparsec.sequence import eof
def op_action(op: str) -> Callable[[int, int], int]:
return {
"+": lambda a, b: a + b,
"-": lambda a, b: a - b,
"*": lambda a, b: a * b,
}[op]
spaces = regexp(r"\s*")
number = regexp(r"\d+").fmap(int) << spaces
mul_op = regexp(r"[*]").fmap(op_action) << spaces
add_op = regexp(r"[-+]").fmap(op_action) << spaces
l_paren = literal("(") << spaces
r_paren = literal(")") << spaces
expr = Delay[str, int]()
expr.define(
(
number |
expr.between(l_paren, r_paren)
)
.chainl1(mul_op)
.chainl1(add_op)
)
parser = expr << eof()
This parser can:
- evaluate an expression:
>>> parser.parse("1 + 2 * (3 + 4)").unwrap() 15
- report first syntax error:
>>> parser.parse("1 + 2 * * (3 + 4 5)").unwrap() Traceback (most recent call last): ... reparsec.types.ParseError: at 8: expected '('
- attempt to recover and report multiple syntax errors:
>>> parser.parse("1 + 2 * * (3 + 4 5)", recover=True).unwrap() Traceback (most recent call last): ... reparsec.types.ParseError: at 8: expected '(' (skipped 2 tokens), at 17: expected ')' (skipped 1 token)
- automatically repair input and return some result:
>>> parser.parse("1 + 2 * * (3 + 4 5)", recover=True).unwrap(recover=True) 15
- track line and column numbers:
>>> parse(parser, """1 + ... 2 * * ( ... 3 + 4 5)""", recover=True).unwrap() Traceback (most recent call last): ... reparsec.types.ParseError: at 2:5: expected '(' (skipped 2 tokens), at 3:7: expected ')' (skipped 1 token)
More examples:
Metadata
Release files for reparsec 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| reparsec-0.4.3.tar.gz | 19.3 kB | Without provenance | Details for reparsec-0.4.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| reparsec-0.4.3-py3-none-any.whl | Python 3 | none | any | Details for reparsec-0.4.3-py3-none-any.whl |
Total release size: 44.6 kB
Release files / reparsec-0.4.3.tar.gz
| Download URL | reparsec-0.4.3.tar.gz |
|---|---|
| Size | 19.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
287853b9775748f8960c9b5acccc4e43049612e2b34ea2bc3e2e4f7dd0538d2b
|
|
BLAKE2b-256 checksum How to use checksums |
9501285ac3d5e1c8bcdc7000ad0d5718d129ec475b8eadc3ca1a1405845b465d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.9
|
Release files / reparsec-0.4.3-py3-none-any.whl
| Download URL | reparsec-0.4.3-py3-none-any.whl |
|---|---|
| Size | 25.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ea1d1a080ccabc62bb2f3cf008e30c576b497bc647fd95a7a856de26a314bbd1
|
|
BLAKE2b-256 checksum How to use checksums |
ddc906c757ca0d2f1aa62153ca924b7a9b13c6929b1506b611d93cb88d87bc21
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.9
|
Security for reparsec 0.4.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.