Skip to main content

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:

> 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)??

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

ppegpy-0.3.19.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ppegpy-0.3.19-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file ppegpy-0.3.19.tar.gz.

File metadata

  • Download URL: ppegpy-0.3.19.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.4

File hashes

Hashes for ppegpy-0.3.19.tar.gz
Algorithm Hash digest
SHA256 3fc15afb2cc33145c472171f6f2c66dbdfd86a9d61781a9777cde3d9102a837c
MD5 ec2bbee287052e2e4c6597d2e044f4d1
BLAKE2b-256 a3db46e9a5b9eee8ed36a05944942b7ec61a9a1cbd4b545f8e65436e0bca9d5d

See more details on using hashes here.

File details

Details for the file ppegpy-0.3.19-py3-none-any.whl.

File metadata

  • Download URL: ppegpy-0.3.19-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.4

File hashes

Hashes for ppegpy-0.3.19-py3-none-any.whl
Algorithm Hash digest
SHA256 3c766722add72e3813f14ad463bf7110d29f0e61c376658b732c77b353d1d327
MD5 57068d3de6611c0e5025e313086c4333
BLAKE2b-256 13a25c51e6908d398451a185b1804491281f1b88f6e7ae49ea19993e8a47ec4f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page