Skip to main content

Library for Parsing Expression Grammars (PEG)

Project description

pe logo
Parsing Expressions
PyPI link Python Support tests


pe is a library for parsing expressions, including parsing expression grammars (PEGs). It aims to join the expressive power of parsing expressions with the familiarity of regular expressions. For example:

>>> import pe
>>> pe.match(r'"-"? [0-9]+', '-38')  # match an integer
<Match object; span=(0, 3), match='-38'>

A grammar can be used for more complicated or recursive patterns:

>>> float_parser = pe.compile(r'''
...   Start    <- INTEGER FRACTION? EXPONENT?
...   INTEGER  <- "-"? ("0" / [1-9] [0-9]*)
...   FRACTION <- "." [0-9]+
...   EXPONENT <- [Ee] [-+]? [0-9]+
... ''')
>>> float_parser.match('6.02e23')
<Match object; span=(0, 7), match='6.02e23'>

Quick Links

Features and Goals

  • Grammar notation is backward-compatible with standard PEG with few extensions
  • A specification describes the semantic effect of parsing (e.g., for mapping expressions to function calls)
  • Parsers are often faster than other parsing libraries, sometimes by a lot; see the benchmarks
  • The API is intuitive and familiar; it's modeled on the standard API's re module
  • Grammar definitions and parser implementations are separate

Syntax Overview

pe is backward compatible with standard PEG syntax and it is conservative with extensions.

# terminals
.            # any single character
"abc"        # string literal
'abc'        # string literal
[abc]        # character class

# repeating expressions
e            # exactly one
e?           # zero or one (optional)
e*           # zero or more
e+           # one or more

# combining expressions
e1 e2        # sequence of e1 and e2
e1 / e2      # ordered choice of e1 and e2
(e)          # subexpression

# lookahead
&e           # positive lookahead
!e           # negative lookahead

# (extension) capture substring
~e           # result of e is matched substring

# (extension) binding
name:e       # bind result of e to 'name'

# grammars
Name <- ...  # define a rule named 'Name'
... <- Name  # refer to rule named 'Name'

Matching Inputs with Parsing Expressions

When a parsing expression matches an input, it returns a Match object, which is similar to those of Python's re module for regular expressions. By default, nothing is captured, but the capture operator (~) emits the substring of the matching expression, similar to regular expression's capturing groups:

>>> e = pe.compile(r'[0-9] [.] [0-9]')
>>> m = e.match('1.4')
>>> m.group()
'1.4'
>>> m.groups()
()
>>> e = pe.compile(r'~([0-9] [.] [0-9])')
>>> m = e.match('1.4')
>>> m.group()
'1.4'
>>> m.groups()
('1.4',)

Value Bindings

A value binding extracts the emitted values of a match and associates it with a name that is made available in the Match.groupdict() dictionary. This is similar to named-capture groups in regular expressions, except that it extracts the emitted values and not the substring of the bound expression.

>>> e = pe.compile(r'~[0-9] x:(~[.]) ~[0-9]')
>>> m = e.match('1.4')
>>> m.groups()
('1', '4')
>>> m.groupdict()
{'x': '.'}

Actions

Actions (also called "semantic actions") are callables that transform parse results. When an arbitrary function is given, it is called as follows:

func(*match.groups(), **match.groupdict())

The result of this function call becomes the only emitted value going forward and all bound values are cleared.

For more control, pe provides the Action class and a number of subclasses for various use-cases. These actions have access to more information about a parse result and more control over the match. For example, the Pack class takes a function and calls it with the emitted values packed into a list:

func(match.groups())

And the Join class joins all emitted strings with a separator:

func(sep.join(match.groups()), **match.groupdict())

Example

Here is one way to parse a list of comma-separated integers:

>>> from pe.actions import Pack
>>> p = pe.compile(
...   r'''
...     Start  <- "[" Values? "]"
...     Values <- Int ("," Int)*
...     Int    <- ~( "-"? ("0" / [1-9] [0-9]*) )
...   ''',
...   actions={'Values': Pack(list), 'Int': int})
>>> m = p.match('[5,10,-15]')
>>> m.value()
[5, 10, -15]

Similar Projects

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

pe-0.4.0.tar.gz (157.6 kB view details)

Uploaded Source

Built Distributions

pe-0.4.0-cp311-cp311-win_amd64.whl (119.8 kB view details)

Uploaded CPython 3.11 Windows x86-64

pe-0.4.0-cp311-cp311-musllinux_1_1_x86_64.whl (731.9 kB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

pe-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (740.1 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pe-0.4.0-cp311-cp311-macosx_10_9_x86_64.whl (151.2 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

pe-0.4.0-cp310-cp310-win_amd64.whl (120.9 kB view details)

Uploaded CPython 3.10 Windows x86-64

pe-0.4.0-cp310-cp310-musllinux_1_1_x86_64.whl (680.1 kB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

pe-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (679.4 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pe-0.4.0-cp310-cp310-macosx_10_9_x86_64.whl (148.3 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pe-0.4.0-cp39-cp39-win_amd64.whl (123.1 kB view details)

Uploaded CPython 3.9 Windows x86-64

pe-0.4.0-cp39-cp39-musllinux_1_1_x86_64.whl (694.0 kB view details)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

pe-0.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (691.9 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pe-0.4.0-cp39-cp39-macosx_10_9_x86_64.whl (151.5 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

pe-0.4.0-cp38-cp38-win_amd64.whl (123.2 kB view details)

Uploaded CPython 3.8 Windows x86-64

pe-0.4.0-cp38-cp38-musllinux_1_1_x86_64.whl (750.4 kB view details)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

pe-0.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (708.7 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pe-0.4.0-cp38-cp38-macosx_10_9_x86_64.whl (149.1 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

Details for the file pe-0.4.0.tar.gz.

File metadata

  • Download URL: pe-0.4.0.tar.gz
  • Upload date:
  • Size: 157.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for pe-0.4.0.tar.gz
Algorithm Hash digest
SHA256 69a2415c5819f89b86e628aef988b6649277b35d93c95fb571d433225e1b37fc
MD5 559828f6bd01bdf72fe253d65111f918
BLAKE2b-256 f4fb61c60d97f9665dd94263bef8b9021d52014cb4e9402034a7241fe77ca916

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: pe-0.4.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 119.8 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for pe-0.4.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2c70641e9c30098ff676cc52e84d2ad54744371e065f9b22ec384b677e835279
MD5 71fd57c42f377b8d629a0ceea510244e
BLAKE2b-256 fcb094799d3ae58ca0f9bbb689dc20d158c24838383cefe2cdc4e57ede1f84e9

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp311-cp311-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.4.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 d93ee635cf3144944eb93ac51dc156c20131722326158cc08cab761a1fb6d604
MD5 1f27d293f25c18db8c2ace07aa50c22d
BLAKE2b-256 b2bf7bd8ba315cd9ac420b8ed3b53282ee0d373e899aedb603e92477976b2fb5

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 347b45175ea3e7196da4a559884a58f2d9a31b8bbc98f23a2b67ef50a79b23e7
MD5 2024c832148457396f7e14e56fc0ed2f
BLAKE2b-256 3a429f51cc453a182fbfacc0a3986485b4a4b18f97e6bcfec08b2a32951b5d75

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.4.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 68d77807d12e3a96e4ed561f47e336f5a40728f937d0a26b8734215c4a263c25
MD5 549eb2c165a95bfa61c17679034f4412
BLAKE2b-256 1bb6adba9f14b1596f513033949133c8dc5bb4ae15ca533a3cda0af5d441f8df

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: pe-0.4.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 120.9 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for pe-0.4.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d50f589aff67964e20fc4368474f20fa8b5afe035bacd750e815e4bdc9d3ef9b
MD5 70e39efbfa0bc99b44c51d62c29f4625
BLAKE2b-256 54df05ae863ef2b88c3f0df78809baef2b7d666e0733b811647c6f74c99e41e9

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp310-cp310-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.4.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 15a64050b8482116af5eb006e115bb230505733943f36ed0d2cdbbf29b7a87bd
MD5 01aa03864f7fa4a390c28e12a4100958
BLAKE2b-256 68b30fb039dea1eea77ca6831722428d145adb082047a9d4385fd3922a2e0185

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 99a2c888ca23fb692889b8ed4b875718fecddd4f5c5178ab581f885794efb5c2
MD5 d430791fcbf051725b6758a8c0120290
BLAKE2b-256 535a20f2cff2acb73216bc1c33ce868dd44b036df0306fc306dd4263aed56db9

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.4.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2d3f22e11e510aaf29cc6ac501c0c58781768b0e8b976e66def4a0447fddff22
MD5 e1a221716a4db18aa8b37fa42557cf20
BLAKE2b-256 931452ea5d5d07ca71850488c3f54eb0b1fec801b5139bcbba7742ad98a258c3

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: pe-0.4.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 123.1 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for pe-0.4.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 90d65157ea72ef1be9b21e25ed55a76a894c4675d482d022c705eb9e93425c4c
MD5 0e55d62731607335f4c78bf850b80401
BLAKE2b-256 f6462173cd0d8d122a29897ba8614bc19f9eb04d24e3ae80c1d89d605b674306

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp39-cp39-musllinux_1_1_x86_64.whl.

File metadata

  • Download URL: pe-0.4.0-cp39-cp39-musllinux_1_1_x86_64.whl
  • Upload date:
  • Size: 694.0 kB
  • Tags: CPython 3.9, musllinux: musl 1.1+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for pe-0.4.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 0375ed8179f8fbe50978a4e69a8b53b5af7c1d3bc7a71795511c065ce9d2851d
MD5 4067af2eabb2bfd74996f19644a4dd94
BLAKE2b-256 b8cb64620976c1a6ca309a6d7e19e8605a42818b4aab94d1c2fb51877e25695e

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 be4b1d308a3832e817605e5b2b93e2487729cced6d49a09f804546d82da89593
MD5 a0c0413bc4b75bf0de907e7d4cc3de3b
BLAKE2b-256 2ac54ba13a06a023c6e691fa7de692441e1c19e3633747828914c33a963c0839

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.4.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b63c39a3977c32fe7ccf16a951f221923b9907d763d7f034bac1ed7531047633
MD5 545110d36678b89c0760e08cdb35fa7e
BLAKE2b-256 f977ea80a61a39aa00d9e75a44ad00da503a4f5fcbe11d78b7c6418b10ff596c

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: pe-0.4.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 123.2 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for pe-0.4.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 0f6bee383ca6ed92d5b2c4902a4044ada7d3c1febe01a7980200c671223f770f
MD5 68c38ad89147c4b62f6c348fd2543250
BLAKE2b-256 1b4350443084351a6b8877fba3ab219a2a25dbee1af715b3ae64aa0583cc2a71

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp38-cp38-musllinux_1_1_x86_64.whl.

File metadata

  • Download URL: pe-0.4.0-cp38-cp38-musllinux_1_1_x86_64.whl
  • Upload date:
  • Size: 750.4 kB
  • Tags: CPython 3.8, musllinux: musl 1.1+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for pe-0.4.0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 3a96c0d94d911eb70c885223306cdd8ec43d6880e18ea5b1063163ba02993af4
MD5 5c014d8ad5b84cfe45529f2064dfcde7
BLAKE2b-256 f3fc53cbe4ea6a48970b86c0902b3c6f0a9043f63097c29a3678dccc94af2890

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f8975af7cd080bb280a8337f49b9fe2d7cfc75746c65f26d591b5d0e0f99f153
MD5 d262cd109947bc37039bec5fc2b07843
BLAKE2b-256 fba5a39b1440de7b42a3f2046acd9540e62bfb348826c1e1f775ef7ad405d18d

See more details on using hashes here.

File details

Details for the file pe-0.4.0-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.4.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1767e2b104344488187cbe982cbd1147af773f9099bcbeefcaddff3648e1d92a
MD5 d96c30de7b0386fb85c2d26b6ec1e700
BLAKE2b-256 0f0f7f92ff47af70af5f98bcf8619a6db060779a9b70d8d6eccb6dd6a6180c26

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