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'

# (extension) auto-ignore
X <  e1 e2   # define a rule 'X' with auto-ignore

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

Auto-ignore

The grammar can be defined such that some rules ignore occurrences of a pattern between sequence items. Most commonly, this is used to ignore whitespace, so the default ignore pattern is simple whitespace.

>>> pe.match("X <- 'a' 'b'", "a b")  # regular rule does not match
>>> pe.match("X <  'a' 'b'", "a b")  # auto-ignore rule matches
<Match object; span=(0, 3), match='a b'>

This feature can help to make grammars more readable.

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.5.0.tar.gz (160.9 kB view details)

Uploaded Source

Built Distributions

pe-0.5.0-cp311-cp311-win_amd64.whl (123.1 kB view details)

Uploaded CPython 3.11 Windows x86-64

pe-0.5.0-cp311-cp311-musllinux_1_1_x86_64.whl (738.0 kB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

pe-0.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (750.1 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pe-0.5.0-cp311-cp311-macosx_10_9_x86_64.whl (155.8 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

pe-0.5.0-cp310-cp310-win_amd64.whl (124.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

pe-0.5.0-cp310-cp310-musllinux_1_1_x86_64.whl (687.0 kB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

pe-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (684.9 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pe-0.5.0-cp310-cp310-macosx_10_9_x86_64.whl (156.6 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pe-0.5.0-cp39-cp39-win_amd64.whl (126.6 kB view details)

Uploaded CPython 3.9 Windows x86-64

pe-0.5.0-cp39-cp39-musllinux_1_1_x86_64.whl (700.3 kB view details)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

pe-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (701.0 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pe-0.5.0-cp39-cp39-macosx_10_9_x86_64.whl (159.8 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

pe-0.5.0-cp38-cp38-win_amd64.whl (127.2 kB view details)

Uploaded CPython 3.8 Windows x86-64

pe-0.5.0-cp38-cp38-musllinux_1_1_x86_64.whl (761.2 kB view details)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

pe-0.5.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (718.2 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pe-0.5.0-cp38-cp38-macosx_10_9_x86_64.whl (157.3 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: pe-0.5.0.tar.gz
  • Upload date:
  • Size: 160.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pe-0.5.0.tar.gz
Algorithm Hash digest
SHA256 b40bff75eef098e659217cb10bed5050fdf0e32e06de721a58370a901342c644
MD5 35858c6f967baf250aa351a961030438
BLAKE2b-256 5729ce983f18b44b7704fce05ec61259b39830a85662fbe815df7202f1b7a1cd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 123.1 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pe-0.5.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2c8be780d9ddf74cf04ee46da10da670c60be2f5b48e02e31ce413821ced9c80
MD5 85af6957af26c2cd610e47137509720e
BLAKE2b-256 815217a637e7aeda935f43ead7c10f92ca7d4040d86cf3995c8a9a28e7632ba5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 f3066f472c51d6bb64821a326b50b15f3451ce48694dbf082e002fe89a6e0a0c
MD5 35002f53e619e4bca6db1048bb82e9a1
BLAKE2b-256 2771cd6fe5158cf39eae38f9bb8d9d4bdc4f144124ed42704e3d6361573105e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c78c1427b421dd31ceb7deb1644daa5fb038844ae1bf682c5afdb4cb19516cee
MD5 9cb5d30d30ef58a2c85a0bf63d7918e0
BLAKE2b-256 3652f99797bfd75a20946a1f65addcd94fd13ddd55a51c428aad4f2be73bc27c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 823ca27704037c2d43782caa5d447ed0adde5054a28ee0a2b2ad709aa6b404f0
MD5 224db061ada6d2544d7f085fbdcf296e
BLAKE2b-256 c0dee3d5045a921d331a09ffcb8353275bd78a929508e6b4ea076fed87dda02f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 124.2 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pe-0.5.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3e46be158b771df721d994000fd7483583d37e1bacdb7508291a029aaa38a15b
MD5 11482946e06701b808155d5ee6098cb3
BLAKE2b-256 f539b32f30f98cddcf911e7ecec3f9aba5a557018c1635f85af0dd8652f1c0d6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 dc8e143819b08d166bf5e1c003959b5db02b5f5537290963ce04a3b10a1dad10
MD5 4e18262678b3335c13f7818bb7d7e8d5
BLAKE2b-256 0d3231b0bfb270096e0cd8c4b4d5adfc117c4ed14f80b5586cdcbcf4cc5f4ca4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 37bfd1206e09acfec026e5b1143e2e1354b690daf97a558a646f3c12290abc4b
MD5 064ba4c46b0589c015a96fdab0a4038a
BLAKE2b-256 88e0325a55acb809aecca4ec31e822ffbe6163c441e82d2e0430bd0b6c9da485

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9853ca0e04ceb08e02e2ffb1632d67018be2c8e6d014533b16671681eb49ecf4
MD5 f48f33d20e08af50f2afe3412e3a2fac
BLAKE2b-256 a3e24407da4cea5dce5b71fd9a55c96a6145dbcf4158dbadea5d2ad4cdeac839

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 126.6 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pe-0.5.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 b28140903e558687b8a88ef27fd613a1d1223d34e85b0c36bc2c8849435728ff
MD5 41cc31a7d94b9f9147223050ddda0fda
BLAKE2b-256 5804f027e79af9d13484da98519c7c15fb853c2ba41bff7036c1f7a1ba7692a5

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pe-0.5.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 e6b544cafca289d59a207174db0957d7905e4f2de5c9019de5918f7b481bf315
MD5 dfa3de059bbf5e25f06ab3ebce98c0ca
BLAKE2b-256 4716a0d3adad57b0d96b4d8b8b766d6fd0099d0118389ca03633a28df7aff54a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7f67c7b58a77257eea5f80e5d8287122c18eefe294c6a0fa893d8778bf53119f
MD5 56a6d7eb2770dde353422e0543395742
BLAKE2b-256 e3e3ee65aa9625325f6a6ba2ae3f44d9d9e373d06979b7b5c93aa511c52f09d2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.0-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 159.8 kB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pe-0.5.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8a40acf40948352032b1b4b5a11ef616a54ac3f69d1c2df54bdab758930bcf5c
MD5 157818e2699b6225941acbe334728cb4
BLAKE2b-256 a73ef994290278c719f509406c96be13575b66471655190f0478b3f419e33e67

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 127.2 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pe-0.5.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 7cade0e6e229e05dd9ea51ade1899536d144abe9b703a165b2c7a87b7fc29063
MD5 3eeaf50a6b73530f3933d453946320a9
BLAKE2b-256 b0ee18d8030550f43593f7b68edc607ee08f7afa6a54c8b71196e5308f532318

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pe-0.5.0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 fb3ba14b5591fc9380e9900f9816a1642154591485f136cfe594a856d23b9f40
MD5 73c90de0a29d903ee4b7de9c9a893a82
BLAKE2b-256 d3de8faa82ea139f74cb4b5c37c33ef2d7e0bc42d81417948deb8a8873aec98a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9f0f006f9f2f065669cd7363ef3631809651badaba48c6ac6a7a128c1f5da869
MD5 e084cf5e1afe0dea4f4c31d37b102062
BLAKE2b-256 151e28a1ebd1af00b6d4dc43ba96db425f1a4be610d3b1e4571dff90f89c3e4c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.0-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 157.3 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pe-0.5.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1d53126c3315be7708d4a9551ea7e2baf9d6af32823b2f738ce06cc5f843d8ba
MD5 d32fea1d8bdb1ba903ba20a47ddbb33f
BLAKE2b-256 3e3890188bd73c79a03a91dfdd5912b163c3d1c04437b5727f80477f91fad448

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