Skip to main content

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:

> pip install pPEGpy

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.19

  • extensions and palindrome examples implemented.

Future plans

  • source code map
  • binary (not Unicode) and/or compile(..., ASCII=True)??

Metadata

Release files for pPEGpy 0.3.19

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pPEGpy 0.3.19
File Size Uploaded Provenance Actions
ppegpy-0.3.19.tar.gz 27.9 kB Without provenance Details for ppegpy-0.3.19.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pPEGpy 0.3.19
File Interpreter ABI Platform Actions
ppegpy-0.3.19-py3-none-any.whl Python 3 none any Details for ppegpy-0.3.19-py3-none-any.whl

Total release size: 40.1 kB

Release files / ppegpy-0.3.19.tar.gz

Download URL ppegpy-0.3.19.tar.gz
Size 27.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3fc15afb2cc33145c472171f6f2c66dbdfd86a9d61781a9777cde3d9102a837c
BLAKE2b-256 checksum
How to use checksums
a3db46e9a5b9eee8ed36a05944942b7ec61a9a1cbd4b545f8e65436e0bca9d5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.4

Release files / ppegpy-0.3.19-py3-none-any.whl

Download URL ppegpy-0.3.19-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c766722add72e3813f14ad463bf7110d29f0e61c376658b732c77b353d1d327
BLAKE2b-256 checksum
How to use checksums
13a25c51e6908d398451a185b1804491281f1b88f6e7ae49ea19993e8a47ec4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.4

Security for pPEGpy 0.3.19

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page