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

Uploaded Source

Built Distributions

pe-0.5.1-cp311-cp311-win_amd64.whl (305.4 kB view details)

Uploaded CPython 3.11 Windows x86-64

pe-0.5.1-cp311-cp311-musllinux_1_1_x86_64.whl (998.8 kB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

pe-0.5.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pe-0.5.1-cp311-cp311-macosx_10_9_x86_64.whl (331.8 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

pe-0.5.1-cp310-cp310-win_amd64.whl (305.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

pe-0.5.1-cp310-cp310-musllinux_1_1_x86_64.whl (935.0 kB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

pe-0.5.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (932.3 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pe-0.5.1-cp310-cp310-macosx_10_9_x86_64.whl (331.3 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pe-0.5.1-cp39-cp39-win_amd64.whl (305.9 kB view details)

Uploaded CPython 3.9 Windows x86-64

pe-0.5.1-cp39-cp39-musllinux_1_1_x86_64.whl (937.7 kB view details)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

pe-0.5.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (934.8 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pe-0.5.1-cp39-cp39-macosx_10_9_x86_64.whl (331.6 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

pe-0.5.1-cp38-cp38-win_amd64.whl (307.1 kB view details)

Uploaded CPython 3.8 Windows x86-64

pe-0.5.1-cp38-cp38-musllinux_1_1_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

pe-0.5.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (956.1 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pe-0.5.1-cp38-cp38-macosx_10_9_x86_64.whl (330.7 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: pe-0.5.1.tar.gz
  • Upload date:
  • Size: 197.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pe-0.5.1.tar.gz
Algorithm Hash digest
SHA256 2aa0909deef1044510fc1b24d1078e47402e9740e60f78f03b361c8ae0c6fa85
MD5 f70a52ab95cd4859cc7eb1efc031b084
BLAKE2b-256 2a84f690b2fd9442d96760bed658ede0614d89f8d5df29b2485f9a51cca0fc35

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 305.4 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pe-0.5.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 15c6f1c0e48d89877f8102a2b54a6445b123a33d44057860d28e344f88d6a07d
MD5 950a9391f62e31e415abb73f97f60b1c
BLAKE2b-256 e33be441858021749f724ea52c60956771f12461282e95801a102d893d3ca26f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.1-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 37971c0f7ae5b1ece6005a373ca7c282a2d9c2a840d6b84017ab3f19776a8227
MD5 f22462656c6f8fa8d8ac1729d7171cad
BLAKE2b-256 bcf8ee22b79adfb70750145d6959b3f632ca1b3228830994c9ba300529aceb52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 067b2e33249ff7902ad5342ce3c9e7bdbda6a80854baaaa15e412a190937f95b
MD5 16776c4820f8c570bbd3b462d7f100db
BLAKE2b-256 6f5e1f1c6e7d47f3d325fe37d4d955c113d4de2138aec05ed5f51b5a3faf205e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 15d951f93c4f3f08be632333d6c4403cc626abcf39e7e6103fc3f2658e6f5189
MD5 008e0df8ff0c633d0c39b5f20f5e37b5
BLAKE2b-256 56158371a2dea06b3df96ab88b2c0e8cd918766969171e55477b9bdc8158f720

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 305.7 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pe-0.5.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3d4ac8068ebee966cbcc3678ca30bd92d1cbf60eee038c2def4d0469c9de8e52
MD5 8d69f378277abf438c57a3af5fc58598
BLAKE2b-256 29f7dc7b1b5b1999fd8802ff9364d3bbbedaf1dde7a60f69fdaa3afa7e75af6f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.1-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 8fb42a8b5741bdd15eb0ebcb69203e22e6e8ef2746900c5937c034a85a23dbb0
MD5 2bd58a11e9a7b08aad7e218db18215c7
BLAKE2b-256 403790be95a613ce3d81378fc298f6566de0987b3002a4c6d1180eb5ffd5d9ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 beef9768832f8c36fbfc8607da5c16af0cdf18d632fcc7413020a7c18890aa22
MD5 e1785cc8d3e9b3e73010568ad3f750f7
BLAKE2b-256 6c2a439646495c2887b01ad6d2b102c7668753e5d5dbbea9584874b11eac477e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 95c36f0d3d39a9448ea8fb34b96601d194585dff92ef71c152cb32c237a584ac
MD5 774b2bf37d35bddcdc24f2112422995b
BLAKE2b-256 b509019a6aa92967e760bccda1e3d40fef54d848bc6cdc86803eaac087c84432

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 305.9 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pe-0.5.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c576d5344fc1426d36ef6188151accc55d8caf41d832fcb338d39414084fc57e
MD5 3dabf5d19ea4e3de8435e1f87e67ad3f
BLAKE2b-256 72894b29ef67b8962e3227aaa8fcb98edff0e30d51d39b8ac74d8be1db1081ab

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pe-0.5.1-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 2fbc90cee2db46e0aec60d46886d4b5afdb262db70b6fea873feed43f79f5db0
MD5 3ab5be5e7949031b16871c2945064170
BLAKE2b-256 5eba30d2d8cbd255062cd8aefcf5446cc56a72c9d09764a7f12f34016be20a52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4e7c1ed7c398f5c0aa091cd968f7c652f975c2ba549dce517ee667be621e2852
MD5 bdb9235e1fadc9ecdb320d485713cbaa
BLAKE2b-256 1d7ab8fb5b31cd87171249dd1f3c8d8a65a7aaa758398eda17c9164a58b04488

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pe-0.5.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 99039fbfe409128de725766382cc16971f16bf2bc41d378f61c947f4a63b0240
MD5 d1ae143bcea1da93f4296fb7a2da1959
BLAKE2b-256 3462d5655fc7cb6492fe8da6e813e391d385c06ba1f94df5b83790a34da8683c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 307.1 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pe-0.5.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 9884d55ae777e5898e54502c4fbf97cc119b96ecb7274da3555949998b55e999
MD5 3987e8c32a1458f3c682e55e1cb715b2
BLAKE2b-256 b011911d09b0db2db990b7e38055254b667cdb23895137738145146225c752e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.1-cp38-cp38-musllinux_1_1_x86_64.whl
  • Upload date:
  • Size: 1.0 MB
  • Tags: CPython 3.8, musllinux: musl 1.1+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pe-0.5.1-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 842edcd18addd70f661b5e599756418fb44ddd4ff0fcf2139e8db1cf42723ab7
MD5 52105cad78c88a54ad434da9727931ba
BLAKE2b-256 e060c7e7bdb9431f782e8f9769755503dbd0d43957e127cfe607c830cc7390eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 59cb7f25c671818cf82090b5615432c7af9d4162624c7290a6350c8c21642aa3
MD5 4b3f34252594caa312f17c32064b9bf0
BLAKE2b-256 4ac42383a5a2894c29c7f7c4dadd43632e732d572eb5c8534c9be391ad375af0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pe-0.5.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ee70618f7abcc7d568602092a929bdffbc4d19e757c32482159fa357c6c7635d
MD5 53b446a7141165b3876b77794464d9b7
BLAKE2b-256 e16a06a77403cb3996e5da5a0ac6bd73a0a7ee4d60a13624b81dfc22ec03d478

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