pPEG -- portable PEG in Python
Project description
pPEGpy
This is an implementation of a portable PEG parser in Python.
For documentation see pPEG, the portable PEG project.
The pPEGpy package can be installed from PyPi with:
Woops, lost my PyPi access credentials, so can't update PyPi yet...
See Notes below for how to clone and install this repo.
> pip install pPEGpy # version 3.18+ but not yet!
Note the spelling of pPEGpy, there are unrelated packages with similar names.
For other ways to use the pPEGpy grammar-parser see the Package Notes below.
Examples
from pPEGpy import peg
print("Hello world!")
greet = peg.compile("""
greet = _ hail _ whom _
hail = 'Hello' / 'Hi'
whom = 'you' / 'world!'
_ = [ \t\n\r]*
""",
transforms = {"greet": dict}
)
ok, words = greet.read("Hello world!")
print(words) # => {'hail': 'Hello', 'whom': 'world!'}
Run this example, then edit to see what works or fails.
Comment out the transforms on line 11 to see the parse tree printed out as JSON.
More examples:
from pPEGpy import peg
# Equivalent to the regular expression for
# well-formed URI's in RFC 3986.
uri = peg.compile("""
URI = (scheme ':')? ('//' auth)?
path ('?' query)? ('#' frag)?
scheme = ~[:/?#]+
auth = ~[/?#]*
path = ~[?#]*
query = ~'#'*
frag = ~[ \t\n\r]*
""",
transforms = {'URI': dict}
)
test = "http://www.ics.uci.edu/pub/ietf/uri/#Related"
ok, data = uri.read(test)
print(data)
# => {'scheme': 'http', 'auth': 'www.ics.uci.edu',
# 'path': '/pub/ietf/uri/', 'frag': 'Related'}
import pPEGpy as peg
print("CSV example....")
csv = peg.compile("""
CSV = Row+
Row = field (',' field)* _nl
field = _string / _text
_text = ~[,\n\r]*
_string = '"' (~["] / '""')* '"'
_nl = '\n' / '\r' '\n'?
""",
transforms = {
'CSV':list, 'Row':list, 'field':str
}
)
test = """A,B,C
a1,b1,c1
a2,"b,2",c2
a3,b3,c3
"""
ok, data = csv.read(test)
print(data)
# [['A', 'B', 'C'],
# ['a1', 'b1', 'c1'],
# ['a2', '"b,2"', 'c2'],
# ['a3', 'b3', 'c3']]
# -- parse tree --------
p = csv.parse(test);
print(p)
# CSV
# │ Row
# │ │ field 'A'
# │ │ field 'B'
# │ │ field 'C'
# │ Row
# │ │ field 'a1'
# │ │ field 'b1'
# │ │ field 'c1'
# │ Row
# │ │ field 'a2'
# │ │ field '"b,2"'
# │ │ field 'c2'
# │ Row
# │ │ field 'a3'
# │ │ field 'b3'
# │ │ field 'c3'
Package Notes
To experiment you can clone the GitHub repository pPEGpy.
These command lines can be used to build a local package:
> cd <your pPEGpy directory>
> uv init --lib
> uv build
> pip install -e .
> python3 examples/date.py
> ...
The -e option allows local editing of the local files.
The repo includes an examples/ folder, try running the date.py for example.
Bare File
The peg.py file in: pPEGpy/src/pPEGy/peg.py is the only file you really need.
If you put a copy of this file into a folder together with your own programs you can import the grammar-parser directly with import peg. Very simple and easy.
But that does not work across directories, to import the bare peg.py file from another directory requires a hack like this:
import sys
sys.path.insert(1, <path to your copy of peg.py>)
import peg
To avoid that (at the cost of all the Python packaging complications!) you can build a package pPEGpy and install it with pip, as above.
Release Notes version 3.18
-
transforms in now in peg.compile(..), and not parse.transform(..) as before
-
Transforms use
{'x': fx, 'y:': fy, ...} -
parse.tree always constructed (pruned with faults ignored)
-
PEGpy-29.py 2026-05-29 from fossil/mycode/
TODO
- extras.py need to be re-written to use trace directly
- add extras, and transforms API
Future plans
- source code map
- binary (not Unicode) and/or compile(..., ASCII=True)??
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 ppegpy-0.3.18.tar.gz.
File metadata
- Download URL: ppegpy-0.3.18.tar.gz
- Upload date:
- Size: 27.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a12126b6298029b1306d4d2ac098914576d7da1a3e473dc8e2c5ea5a854e2d08
|
|
| MD5 |
30e0a04f0437b985de0964bc7a405e03
|
|
| BLAKE2b-256 |
47cdfd3f0eed1a96b0c29a4a2df50dbce643e3b6f5e254457cd3ca89d5dc4de8
|
File details
Details for the file ppegpy-0.3.18-py3-none-any.whl.
File metadata
- Download URL: ppegpy-0.3.18-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15bf3a1b73b6377d7332e7dc621380c336713463fd3a985bc62504a1d30e8b4a
|
|
| MD5 |
e31ad1a42409aaf9c0921014b2301931
|
|
| BLAKE2b-256 |
c0dcd94465a8a0b9978c75e4ea28c48b418015e3d737d057babadec2456adad4
|