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

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:

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.2.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

reparsec-0.4.2-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: reparsec-0.4.2.tar.gz
  • Upload date:
  • Size: 19.9 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.2.tar.gz
Algorithm Hash digest
SHA256 67c3e138c9ec3d09a75977be68ba67ec815e1dead4ce65f6c01d3ee0a64e8f57
MD5 d32b7a2eb6eb1ff2fd5783830cdcf464
BLAKE2b-256 50822a18b9a3f885862767a6067ab5597b36ac3f470545b31e8193c01fbc2658

See more details on using hashes here.

File details

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

File metadata

  • Download URL: reparsec-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 25.3 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ba7b9001359dfe3b001af9fa88d04b65da5647cc670deaf2c99ab20b31308a2b
MD5 478b88babad820241d431bd437e01183
BLAKE2b-256 7182eb9bc16ba4bba70ea429e33d469aee312ad0b71765b0e730f1cfcd364baa

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