parser
Project description
flytrap-py
parser
How to use
from typing import Dict, Union
from flytrap import IParser, attempt, choice, digit, lazy, many1, spaces, split_by, string, token, until
type Json = Union[str, int, bool, None, list[Json], Dict[str, Json]]
def ignore_spaces[O](p: IParser[str, O]) -> IParser[str, O]:
return attempt(spaces()).with_(p)
def jstring() -> IParser[str, Json]:
"""
support only simple string
"""
return ignore_spaces(token('"').with_(until(token('"'))).skip(token('"')).map(str))
def jnumber() -> IParser[str, Json]:
"""
support only simple number
"""
def _inner(values: list[str]):
return int("".join(values))
return ignore_spaces(many1(digit()).map(_inner))
def jboolean() -> IParser[str, Json]:
return ignore_spaces(choice(
string("true").map(lambda _: True),
string("false").map(lambda _: False)
))
def jnull() -> IParser[str, Json]:
return ignore_spaces(string("null").map(lambda _: None))
def jarray() -> IParser[str, Json]:
def _inner(v: list[Json] | None)->Json:
if v is None: return list([])
else: return list(v)
return ignore_spaces(token("[")).with_(
attempt(split_by(jvalue(), ignore_spaces(token(","))))
).skip(ignore_spaces(token("]"))).map(_inner)
def jobject() -> IParser[str, Json]:
def _inner(values: list[tuple[Json, Json]] | None)->Json:
result = {}
if values is None:
return result
else:
for (key, value) in values:
result[key] = value
return result
return ignore_spaces(token("{")).with_(
attempt(split_by(
jstring().skip(ignore_spaces(token(":"))).and_(jvalue()),
ignore_spaces(token(","))
))
).skip(ignore_spaces(token("}"))).map(_inner)
def jvalue() -> IParser[str, Json]:
return choice(
jstring(),
jnumber(),
jboolean(),
jnull(),
lazy(jarray),
lazy(jobject)
)
def parser(src: str) -> Json:
(j, _) = jvalue().parse(src)
return j
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
flytrap_py-0.1.4.tar.gz
(9.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flytrap_py-0.1.4.tar.gz.
File metadata
- Download URL: flytrap_py-0.1.4.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/5.15.133.1-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
209220f14e3f3a603b343124c2198a8e48cf5350dff52d324fc17608b39914de
|
|
| MD5 |
e44e9087f9cf1fff83be3bce40d1fcf5
|
|
| BLAKE2b-256 |
874d85628f891c8c28b8bdf077f124c13ffc87ae487f9cc6d47387bce94d9be0
|
File details
Details for the file flytrap_py-0.1.4-py3-none-any.whl.
File metadata
- Download URL: flytrap_py-0.1.4-py3-none-any.whl
- Upload date:
- Size: 21.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/5.15.133.1-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d61907be099e6e1d0010e5ef1783d49470e5b434f9b9725a2cbadb88b7143127
|
|
| MD5 |
d1551420b9d03fd12e2b5c12ae3cb83a
|
|
| BLAKE2b-256 |
6b5d06f3138dd13e7f4dc70a63b440e6e6ddc35c09920a709be3a5406eff5014
|