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.

Example

from pPEGpy import peg

sexp = peg.compile("""
    sexp  = _ list
    list  = '(' _ elem* ')' _
    elem  = list / atom _
    atom  = ~[() \t\n\r]+
    _     = [ \t\n\r]*
""")

test = """
    (foo bar (blat 42) (f(g(x))))
"""

p = sexp.parse(test)

print(p)

This prints a parse tree diagram:

list
│ atom 'foo'
│ atom 'bar'
│ list
│ │ atom 'blat'
│ │ atom '42'
│ list
│ │ atom 'f'
│ │ list
│ │ │ atom 'g'
│ │ │ atom 'x'

Application can use a ptree:

ptree = p.tree()

print(ptree)  # =>

["list",[["atom","foo"],["atom","bar"],
    ["list",[["atom","blat"],["atom","42"]]],
    ["list",[["atom","f"],
        ["list",[["atom","g"],["atom","x"]]]]]]]

Another example:

from pPEGpy import peg

# Equivalent to the regular expression for well-formed URI's in RFC 3986.

pURI = peg.compile("""
    URI     = (scheme ':')? ('//' auth)? path ('?' query)? ('#' frag)?
    scheme  = ~[:/?#]+
    auth    = ~[/?#]*
    path    = ~[?#]*
    query   = ~'#'*
    frag    = ~[ \t\n\r]*
""")

test = "http://www.ics.uci.edu/pub/ietf/uri/#Related"
uri = pURI.parse(test)

print(uri)
URI
│ scheme 'http'
│ auth 'www.ics.uci.edu'
│ path '/pub/ietf/uri/'
│ frag 'Related'

ptree:

["URI",[["scheme","http"],["auth","www.ics.uci.edu"],
        ["path","/pub/ietf/uri/"],["frag","Related"]]]

Usage

Common usage:

    from pPEGpy import peg

    my_parser = peg.compile(""... my pPEG grammar rules...""")

    # -- use my-parser in my application .......

    my_parse = my_parser.parse('...input string...}')

    if not my_parse.ok:
        print(my_parse.err)
        .... handle parse failure ... 
    else:    
        ptree = my_parse.tree()
        process(ptree)

The ptree parse tree type is JSON data, as defined in pPEG.

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 .

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.


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.8.tar.gz (23.6 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.8-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ppegpy-0.3.8.tar.gz
Algorithm Hash digest
SHA256 9f91ca7ff038be55316a9af702e2a1949a9faf96381ffa3c8dec7259706d32ce
MD5 2e1aeb28f15e0b4f252cf482df208d0b
BLAKE2b-256 3eb2ddcf2f5b7a4679f348c523bf861e880847c76c46f12e9b95334729b83fc4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ppegpy-0.3.8-py3-none-any.whl
Algorithm Hash digest
SHA256 4bd528f4e88bd0ec386a918aa41a77222052b85d2a6f0c0561287e5f4c511ee1
MD5 5c28cd645ab23dcf4c43c3f131c6c4e2
BLAKE2b-256 59e2a4b1cbaf2301adf020581100e1328bf54aa52c7c8fe36873b57b78d8e028

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