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

Uploaded Source

Built Distributions

pe-0.5.2-cp312-cp312-win_amd64.whl (301.7 kB view details)

Uploaded CPython 3.12 Windows x86-64

pe-0.5.2-cp312-cp312-musllinux_1_1_x86_64.whl (982.5 kB view details)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

pe-0.5.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (991.7 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

pe-0.5.2-cp312-cp312-macosx_10_9_x86_64.whl (326.0 kB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

pe-0.5.2-cp311-cp311-win_amd64.whl (306.2 kB view details)

Uploaded CPython 3.11 Windows x86-64

pe-0.5.2-cp311-cp311-musllinux_1_1_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

pe-0.5.2-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.2-cp311-cp311-macosx_10_9_x86_64.whl (333.3 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

pe-0.5.2-cp310-cp310-win_amd64.whl (306.5 kB view details)

Uploaded CPython 3.10 Windows x86-64

pe-0.5.2-cp310-cp310-musllinux_1_1_x86_64.whl (940.4 kB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

pe-0.5.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (940.6 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pe-0.5.2-cp310-cp310-macosx_10_9_x86_64.whl (332.7 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pe-0.5.2-cp39-cp39-win_amd64.whl (306.7 kB view details)

Uploaded CPython 3.9 Windows x86-64

pe-0.5.2-cp39-cp39-musllinux_1_1_x86_64.whl (942.7 kB view details)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

pe-0.5.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (937.9 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pe-0.5.2-cp39-cp39-macosx_10_9_x86_64.whl (333.0 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

pe-0.5.2-cp38-cp38-win_amd64.whl (307.8 kB view details)

Uploaded CPython 3.8 Windows x86-64

pe-0.5.2-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.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (965.4 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pe-0.5.2-cp38-cp38-macosx_10_9_x86_64.whl (332.2 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: pe-0.5.2.tar.gz
  • Upload date:
  • Size: 198.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for pe-0.5.2.tar.gz
Algorithm Hash digest
SHA256 80c37784e5511f7a29ea1c1e0f3f2ab3615278575d27620f322ac455af0e4c2a
MD5 461340243200d6f23121efb459a2000f
BLAKE2b-256 7c6c0e364a719797f5d6a25246aab636464ddc0c3cbe048deb0562fcb29fd38b

See more details on using hashes here.

File details

Details for the file pe-0.5.2-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: pe-0.5.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 301.7 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for pe-0.5.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e36ec5dda04f1088d1c5cdfc0b4d6a969eb48d276efdd9cb696485943394f560
MD5 c51d2fe2b9d300cb8c84cc31256e0f14
BLAKE2b-256 f3dcc8c4d64cf28c63a54bd3892de985fa46730c8f1cfcdae691b1255f6d2263

See more details on using hashes here.

File details

Details for the file pe-0.5.2-cp312-cp312-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.5.2-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 eaab524ccba215332e15dc9bffec53096da01f19366a64c8909b5ace7ec30e92
MD5 7d1e8054680cdc518123d84f93027b4b
BLAKE2b-256 d2bd6eb070b088a1e982b07bc1b2dba64d3da411420fca03e1330d66829047c0

See more details on using hashes here.

File details

Details for the file pe-0.5.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.5.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 728eb01069ced42ed61ee43860af419137b3e950ffc32141a5c70f3f7da1e0ca
MD5 680dcd51ed7a1a639c6a1a4514f7b854
BLAKE2b-256 af325418bdb7384c7ceab49d20c4deb303d2389090778d42abb500e636c7c68b

See more details on using hashes here.

File details

Details for the file pe-0.5.2-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pe-0.5.2-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ff1e24b301cb1a81d4de7b73ea85bc1f9b8ad484fdddb8392fb64c5bdacb33f8
MD5 e8db3d2ba74eb98fdde6f19cf4b3e609
BLAKE2b-256 938f25bf8ddf7e60d957258172d87da7e4cfc6256b8343460c9c5dd2b78d37b5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 306.2 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for pe-0.5.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 136f31fa1e3ba45d7992c7c1cb278d0c3cf27f50d5d88bf289a08c0b9fbc4e77
MD5 a6ac046e2ce58bf52d03863bb4e432e3
BLAKE2b-256 80200a134d884ff8ce083f441b065d647fb1bacb44c6c7c7a93fb529d2bc2cd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.2-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 4ba29dac69a53a37c399d32cca0871be2d49b2049f4616e219a3fb814a7f65d9
MD5 77e142707b7c065fddb0ce1bf11fc479
BLAKE2b-256 7ff37d56128c7e69f4f2dff53253be7df2e7c089f994134a19224ecddc4a30e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 420341d67cc610a9c5926a8e10dd891db2607e010c3048b6d1c0847abca9396d
MD5 d96acfc607f59b1c155d9b094efa2f0a
BLAKE2b-256 c4be081963eb09c5060fb4a7ec802ffc4c9916accb12a6b6330a314d1e23880c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3bede7a7aba7534b13b6525ae5acf6a9998e4b33a30191fb5cf461ffff3df0ec
MD5 30ddf98846469b77ce6d4311ad54765f
BLAKE2b-256 5e830a1a0d463ccf7019f7dea7b250c64d8a9d31d94ceb8a43ebaaf749deed7d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.2-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 306.5 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for pe-0.5.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 09e40da50b80f410cf4899dff4f9d488057bd5e8249ca6173e3675311d27fc30
MD5 22dbc5fc19a0dafb5ddfae944acf12e0
BLAKE2b-256 e18d2df68df9397f9fd51a30ddb62d967e4f406e8447316097e34be0835acc2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.2-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 2a9eeda6c411e7efb336fcd0e2426120fd08de0e4d1d64eb93985975935801f7
MD5 a1a2480ebdf8dd88938c5a4a3bf55b83
BLAKE2b-256 0a2ceafa0c1902c5d5dc554638ee62f53f3b28765c0d095b6a4d6bb6b41f4fe2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 92c2eccf684e923bbb96686df5a6aee2c9d6b292bac543ee411e9819922edc68
MD5 b802191d7092e96da7197f9816ee6f35
BLAKE2b-256 88986dce54d64f5ea3ea4b43bd5264a878da69185e8ae1129fe59aceafc21803

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 30ccf6b00af4153718fd7f5d1fb8816b8687f0228ae9d61ed7dba59bfb285f7e
MD5 d51b43ba31aba4dd70ee40173fc89ca5
BLAKE2b-256 5b2ecd86437c3f916ef9c9d5c93f1a989211802262f9ae0499f2ea7d64072f2c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.2-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 306.7 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for pe-0.5.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 7e9658d6716f2595e577069a09714e649b648b29275517bd9c7332603d9d92a4
MD5 8003a9c0a8df17f8952e6aac5e846e3e
BLAKE2b-256 ba15868302caf79532e451c36cd83113e8c0be56ca26013d5386fe0ec5b677d4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pe-0.5.2-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 5c9cd1b58d0f0284bdace64745a9fd636bf80e95e81d1ef7f855ef76a7defe9f
MD5 9253e073d02b854f62493588e7d7595b
BLAKE2b-256 6f82e02b3cd0fefc3ca8c1dbe7cefceb11e869e68d12126af5d3473c7e815cf4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 16f5ea37495caaf58a39f45d0888e3226885787a7aa70fe11a49adae0745ad02
MD5 ab71e907188beaf92f45bf2f66576795
BLAKE2b-256 43c7381786b14f480f03e442c157cbd01233ee4ac79287adde3b9fb82d0e5dbc

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pe-0.5.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0b3be315e4d15ad0560c446cd2b1e5e85a5490e2cf6cf012a1cb4ad84615f2e0
MD5 420bf9a284766c76cce4c34cfb609ae4
BLAKE2b-256 6aba666b132c237e00cd2a356602a25071dd6485e8f97abe4c2c92bee24d6eb7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pe-0.5.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 307.8 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for pe-0.5.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 08e23443c370b11497edbde138642ca02cba0680137255aa54a2023c34604554
MD5 1c7392ddbb21f3c91a9fe73bd5ad2546
BLAKE2b-256 6a811e44480e91d1762493b02036f658bb4325c86102231c9a5ac6919e166dc8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.2-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 cd375194e95de492d144e199549806b6dae215da738eda61a28bc64f7928bc57
MD5 a73aa3f623d7ccd0c3b1af492fdc2c40
BLAKE2b-256 af86c49a350e99f5cd5f6c0696174ee224cc2c0283b80b5f51add8a486c04d14

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pe-0.5.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c56df3d5e93440a206f1458c575664029f8ff6b5964de8a0d0ff82a4ddfe27e8
MD5 24af2494ce2bf526f0d93e38d7e397b3
BLAKE2b-256 9b88d06af98dca156fe28c84f7bc1a9fb15fb5fe2305262e426428339fdfe8b8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pe-0.5.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2d6085c1c476eca13b6699bb5aa62a15a8d5fc426e692d516d7b4aa62ec9262b
MD5 312c97aee887164090f10379c98b209c
BLAKE2b-256 95223e7c44ce00a55f6a0e5d65a1a1a13db4e82de621d7796a37a239ee376895

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