semvec/pypi-packages huggingface dataset:
json5kit 0.4.0
A Roundtrip parser and CST for JSON, JSONC and JSON5.
License: MIT
Requires Python: >=3.7

# json5kit

A Roundtrip parser and CST for JSON, JSONC and JSON5.

[JSON5](https://json5.org) is a superset of JSON, that allows trailing commas,
comments, unquoted and single-quoted object keys, and a lot more.

Currently supports parsing most JSON5 syntax, and converting it back to source.
Also supports single line `// comments`.

## Installation

```bash
pip install json5kit
```

## Usage

```python
>>> source = """
... {
...   items: [1, 2, 4],  // change this to 3
... }
... """
>>> import json5kit
>>> tree = json5kit.parse(source)
>>> print(tree.to_source())

{
  items: [1, 2, 4],  // change this to 3
}

>>> print(tree.to_json())
{"items":[1,2,4]}

>>> # Let's replace the `4` with `3` now:
>>> class ReplaceFourWithThree(json5kit.Json5Transformer):
...     def visit_Number(self, node):
...         if node.value == 4:
...             return node.replace(value=3)
...         return node
...
>>> ReplaceFourWithThree().visit(tree)
>>> print(tree.to_source())

{
  items: [1, 2, 3],  // change this to 3
}
>>> print(tree.to_json())
{"items":[1,2,3]}
```

## Development / Testing

- Clone the project:

  ```bash
  git clone https://github.com/tusharsadhwani/json5kit
  cd json5kit
  ```

- Setup a virtual environment:

  ```bash
  virtualenv venv
  . venv/bin/activate
  ```

- Do an editable install of the project, that way you don't have to keep
  reinstalling:

  ```bash
  pip install -r requirements-dev.txt
  ```

- Run tests:

  ```bash
  pytest
  ```

- Run type checking:

  ```bash
  mypy .
  ```
reparsec 0.4.3
Parser
License: MIT
Requires Python: >=3.7,<4.0

[![Build status](https://github.com/ethframe/reparsec/workflows/Tests/badge.svg?branch=master)](https://github.com/ethframe/reparsec/actions?query=workflow%3ATests+branch%3Amaster+event%3Apush)
[![codecov.io](https://codecov.io/gh/ethframe/reparsec/branch/master/graph/badge.svg)](https://codecov.io/gh/ethframe/reparsec)
[![Documentation status](https://readthedocs.org/projects/reparsec/badge/?version=latest)](https://reparsec.readthedocs.io/en/latest/?badge=latest)
[![PyPI version](https://img.shields.io/pypi/v/reparsec)](https://pypi.org/project/reparsec)

# reparsec

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

## Installation

```
pip install reparsec
```

## Usage

* [Tutorial](https://reparsec.readthedocs.io/en/latest/pages/tutorial.html)
* [Documentation](https://reparsec.readthedocs.io/en/latest/index.html)

## Example

With `reparsec`, simple arithmetic expression parser and evaluator could be written like this:

```python
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:
  ```python
  >>> parser.parse("1 + 2 * (3 + 4)").unwrap()
  15
  ```
* report first syntax error:
  ```python
  >>> 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:
  ```python
  >>> 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:
  ```python
  >>> parser.parse("1 + 2 * * (3 + 4 5)", recover=True).unwrap(recover=True)
  15
  ```
* track line and column numbers:
  ```python
  >>> 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:
  * [JSON parser](https://github.com/ethframe/reparsec/blob/master/tests/parsers/json.py)
  * [Scannerless JSON parser](https://github.com/ethframe/reparsec/blob/master/tests/parsers/json_scannerless.py)