Skip to main content

A Python port of Lua's LPeg pattern matching library

Project description

PPeg is a pattern matching library for Python, based on Parsing Expression Grammars (PEGs). It’s a port of the LPeg library from Lua.

Usage

Unlike the re module [1], PPeg patterns can handle balanced sequences

>>> from _ppeg import Pattern as P
>>> pattern = P.Grammar('(' + ( (P(1)-P.Set('()')) | P.Var(0) )**0 + ')')
>>> pattern('(foo(bar()baz))').pos
15
>>> pattern('(foo(bar(baz)').pos
-1
>>> capture = P.Cap(pattern)
>>> capture('(foo(bar()baz))').captures
['(foo(bar()baz))']

This example corresponds roughly to the following LPeg example

> lpeg = require "lpeg"
> pattern = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" }
> pattern:match("(foo(bar()baz))") -- Lua indexes begin at 1
16
> pattern:match("(foo(bar(baz)")
nil
> capture = lpeg.C(pattern)
> capture:match("(foo(bar()baz))")
"(foo(bar()baz))"

Modules

  • _cpeg.c

  • _ppeg.c
    • includes lpeg.c

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

PPeg-0.9.2.tar.gz (51.3 kB view details)

Uploaded Source

File details

Details for the file PPeg-0.9.2.tar.gz.

File metadata

  • Download URL: PPeg-0.9.2.tar.gz
  • Upload date:
  • Size: 51.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PPeg-0.9.2.tar.gz
Algorithm Hash digest
SHA256 b4b9c0c49f6d724fb06a3cec734db645e1ab0ede598b48d8abd8cef19b464b8e
MD5 9c1542eeed3e169ef33d7ea0d78edf03
BLAKE2b-256 2d8022e2dc5169a2635ed0db0b8def2c1091be69ddac35986128df1deb43a4e4

See more details on using hashes here.

Supported by

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