Skip to main content

Parser combinators library with error recovery

Project description

Build status codecov.io Documentation status PyPI version

reparsec

Small parsec-like parser combinators library with semi-automatic error recovery.

Installation

pip install reparsec

Usage

Simple arithmetic expression parser and calculator:

from typing import Callable

from reparsec import Delay
from reparsec.scannerless import literal, 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]()
value = number | expr.between(l_paren, r_paren)
expr.define(value.chainl1(mul_op).chainl1(add_op))

parser = expr << eof()

print(parser.parse("1 + 2 * (3 + 4)").unwrap())

Output:

15

Out-of-the-box error recovery:

result = parser.parse("1 + 2 * * (3 + 4", recover=True)

try:
    result.unwrap()
except ParseError as e:
    print(e)

print(result.unwrap(recover=True))

Output:

at 8: expected '(' (skipped 2 tokens), at 16: expected ')' (inserted ')')
15

More examples:

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

reparsec-0.3.2.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

reparsec-0.3.2-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file reparsec-0.3.2.tar.gz.

File metadata

  • Download URL: reparsec-0.3.2.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.6

File hashes

Hashes for reparsec-0.3.2.tar.gz
Algorithm Hash digest
SHA256 bed60ff4353c268d3631c2b6bbd4598fe036a86611bd32a1420b35aaba5932d8
MD5 52f4ba7924c455f96eb4fe25cebe2b6d
BLAKE2b-256 46d38e8bfc68d2a8626ac753ee770f7f83caebd8199c2690d2d735a3c474fcee

See more details on using hashes here.

File details

Details for the file reparsec-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: reparsec-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 25.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.6

File hashes

Hashes for reparsec-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 db65194b9dbc8e649ff332522572a54ef493a013cc8856a30528009017822eda
MD5 835c140a62ffb2e41b924c8af5cc44a0
BLAKE2b-256 9ff2a30232f3cf841f0a3a7366fb960a924de096ce78af6a8003c5e7c4ee1548

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page