A Python Generator based Parser Combinator library
Project description
PGPC
Overview
PGPC is a parser combinator library. Its name is an acronym, which stands for Python Generator based Parser Combinator library.
The library was heavily influenced by the Parsec monadic parser combinator library,
so the transition from Parsec (and other parser combinators) to PGPC should be relatively easy.
The original idea of the library is emulating the do-notation with the yield Python keyword.
Quick start
The @topology decorator over a function allows the yield keyword work with parsers in a do-notation-like fashion:
- install the package:
pip install pgpc - save the following code into
main.py:
from pgpc.scanner import TextScanner
from pgpc.parser import Parser, topology, char, position, content
@topology
def parse_hello_world():
start = yield position()
for c in "Hello":
yield char(c)
yield char(",")
yield char(" ")
for c in "World":
yield char(c)
yield char("!")
end = yield position()
source = yield content()
return f"Parsed '{source[start.offset:end.offset]}' which started at {start} and ended at {end}"
if __name__ == '__main__':
text = "Hello, World!"
hw_parser: Parser[str] = parse_hello_world()
result = hw_parser(TextScanner(text))
print(result)
- run
main.py:python main.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 pgpc-0.0.4.tar.gz.
File metadata
- Download URL: pgpc-0.0.4.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0aee1541d06204ca4df60ae939b1603a68c3cd8cde472bc05c00378547234580
|
|
| MD5 |
a6c466bd8ad26813b8a64ede1ff0d88b
|
|
| BLAKE2b-256 |
e382218dc670b4cd48ef28bcf5b3b9efe418e8072deef884a2ab7705ebd82fba
|
File details
Details for the file pgpc-0.0.4-py3-none-any.whl.
File metadata
- Download URL: pgpc-0.0.4-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a64e35057d5dd8339f98ee0a023737735a1dd5266bf4d9a70a13325825e1630
|
|
| MD5 |
e993229e99fb820eb875a673295890d1
|
|
| BLAKE2b-256 |
bc4e55fe0ece125dfe0b28ee8c928b30da5a08ec886aa108bf4856afb14142cd
|