Skip to main content

Parser

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 5)", recover=True)

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

print(result.unwrap(recover=True))

Output:

at 8: expected '(' (skipped 2 tokens), at 17: expected ')' (skipped 1 token)
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.4.1.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

reparsec-0.4.1-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: reparsec-0.4.1.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.1 Windows/10

File hashes

Hashes for reparsec-0.4.1.tar.gz
Algorithm Hash digest
SHA256 7648f655c9490fdc133b04813907aee815859134ffb50652de82f88e55fb3e9c
MD5 91c0b9f56c9f4ddecf59816f418f3895
BLAKE2b-256 e5c20c513cafd32fded6d4af0f91a163f9da0e5f52330467dc4827fae5f66c62

See more details on using hashes here.

File details

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

File metadata

  • Download URL: reparsec-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 26.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.1 Windows/10

File hashes

Hashes for reparsec-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2381996a8dd62bfaa2c5096267deadc9f4e3123589aa6ce20195a3f44f79264a
MD5 a3984b62d77e25b4b2b8ff3374721b47
BLAKE2b-256 2d732d4bd38ea868570adfc9bdee636467b6eb6291acb5c8f85d9b088bb9aacb

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