Library for Parsing Expression Grammars (PEG)
Project description
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
- Optimizations target the abstract grammar definitions
- Multiple parsers are available (currently packrat for recursive descent and machine for an iterative "parsing machine" as from Medeiros and Ierusalimschy, 2008 and implemented in LPeg).
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
e{5} # exactly 5
e{3,5} # three to five
# 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file pe-0.5.3.tar.gz
.
File metadata
- Download URL: pe-0.5.3.tar.gz
- Upload date:
- Size: 200.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a772dccb588b12e21668a6e0ad43669faaf81541cfcab177326efe46ffdcc336 |
|
MD5 | 35c181d99c1a2fd48ce8a5d245217c7b |
|
BLAKE2b-256 | cec0c0531092e28bde1e132698b52da432b4483d5cd35c55a5359ea8f03839ae |
File details
Details for the file pe-0.5.3-cp312-cp312-win_amd64.whl
.
File metadata
- Download URL: pe-0.5.3-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 305.4 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f4f0b867a9de95a4e0cbb7308cb29f58dc7ac9c034fed4ef3e1ccd5d3ad2ba9 |
|
MD5 | 91be49166092cd5c91a950112e3fc38c |
|
BLAKE2b-256 | 3030e67740c9e745ffdf57413382c2e7cfc82bce4000019654839a6eccb546bd |
File details
Details for the file pe-0.5.3-cp312-cp312-musllinux_1_1_x86_64.whl
.
File metadata
- Download URL: pe-0.5.3-cp312-cp312-musllinux_1_1_x86_64.whl
- Upload date:
- Size: 995.0 kB
- Tags: CPython 3.12, musllinux: musl 1.1+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9cde71eee35468a079b7ca449406797a436cabbd08be4671e58da7f48ae13a93 |
|
MD5 | ab0df6efc986fa89f384007a9ab4c165 |
|
BLAKE2b-256 | 547b819ebed5d4b54fa95ed7a3282251d40afffbcf566bb303de6756839c69fc |
File details
Details for the file pe-0.5.3-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pe-0.5.3-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26f39bf8b239a3fd278be809b129bcc1429237ae4c12d902c84c91fd102e8d0a |
|
MD5 | ef3566195187af60b3fba8b2cff98d3c |
|
BLAKE2b-256 | d6c360643f28c0cea9d3536fdba6607a8812e22e0027d0b83f89acc641740a45 |
File details
Details for the file pe-0.5.3-cp312-cp312-macosx_11_0_arm64.whl
.
File metadata
- Download URL: pe-0.5.3-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 315.5 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d23407b56bd7e2c2c21e5260cec2bd51776e21c76aedef8a62cec9a53f52a2d9 |
|
MD5 | 1b97976de76ecb1c0dcdfe423969973e |
|
BLAKE2b-256 | 5716890384731b53ff8c4d193b2a1e53dfc097027bcfff8db93806b23743e755 |
File details
Details for the file pe-0.5.3-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: pe-0.5.3-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 309.9 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 236d25db3619abdf2d3904e45b57b46b7933d780a09c436365a06a2a41504f42 |
|
MD5 | 3940f55721ad44aee980e9b990a8e691 |
|
BLAKE2b-256 | d2494cad3236b5f54cca71d69c0e7f2a19624bd288c4693c782a13cf6efd1c55 |
File details
Details for the file pe-0.5.3-cp311-cp311-musllinux_1_1_x86_64.whl
.
File metadata
- Download URL: pe-0.5.3-cp311-cp311-musllinux_1_1_x86_64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.11, musllinux: musl 1.1+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 329b971d070cf81270f83ae361f7eb35d04ff65df871fab03667d0a9202bc890 |
|
MD5 | 5fc00a983025f72daef44d9336f058d8 |
|
BLAKE2b-256 | d7aa22354684b03b54ff6d3e88cc29d8e7d08f1ac4aec86b0125d818224301cb |
File details
Details for the file pe-0.5.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pe-0.5.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 058506986a523983c3a868a1b395b96554432e3ee945439c3fee29f317b75b5f |
|
MD5 | ea82c38ae51b8aeeaa69b7c3501d21d6 |
|
BLAKE2b-256 | a96961365b5194b10bd5e34561ae912720039912277cd0e0c4dd50a1adc5a509 |
File details
Details for the file pe-0.5.3-cp311-cp311-macosx_11_0_arm64.whl
.
File metadata
- Download URL: pe-0.5.3-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 317.4 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6d5ee4290b05815abb401c713c54087bbf000fc132b0c5aea714d04337f54d9 |
|
MD5 | 923c4a4922faeb9c9bdebdefa5b620d1 |
|
BLAKE2b-256 | bee6319ee417cb40e63cbf5c14addc7bc0b9f62368fba312e7e4638259f2a3ec |
File details
Details for the file pe-0.5.3-cp310-cp310-win_amd64.whl
.
File metadata
- Download URL: pe-0.5.3-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 310.2 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc975f558fb8e46100215f0404ab9b5b71a28f3241a5665beb8f296431df8878 |
|
MD5 | c68e13d7d50cad6069b2fbe69dd97d65 |
|
BLAKE2b-256 | 17afbc449d6559167e913355d1bc62dc8cb5bff0d7f286e94198e7812c0e95d6 |
File details
Details for the file pe-0.5.3-cp310-cp310-musllinux_1_1_x86_64.whl
.
File metadata
- Download URL: pe-0.5.3-cp310-cp310-musllinux_1_1_x86_64.whl
- Upload date:
- Size: 951.2 kB
- Tags: CPython 3.10, musllinux: musl 1.1+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d19044156616a6494394cebfa7f824fa53a2f79a9c9d9d2dc94ca9629d7859e |
|
MD5 | 4903d02562f2bce24816649fb4825174 |
|
BLAKE2b-256 | e73ffa5a43895da50a8218d77d419c09a611a6cf206e341e0498d13318d4173e |
File details
Details for the file pe-0.5.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pe-0.5.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 952.5 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2dc1fab2eda5525beec5a8cb437e404c3fe962aa2d12ab06111d93b9bf463976 |
|
MD5 | a612ed437eab0278bf750910ca93ccca |
|
BLAKE2b-256 | 1d7f77769944156bae69f518935a62a6d50f2185d9bca07ea7a0053948be47a5 |
File details
Details for the file pe-0.5.3-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: pe-0.5.3-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 316.6 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6eaabcf08e2b21bbed23a5a49fa2ab81aa1d7651a8f73c065032408ff4f14c29 |
|
MD5 | d8b963cc68d71e127e1c38bc60e756eb |
|
BLAKE2b-256 | 0f592241cdf1a7fbb0289a5bba1cad45641df54d789a999222be5e69081ccf8d |
File details
Details for the file pe-0.5.3-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: pe-0.5.3-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 310.4 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c8bab1db5cda64ab30ad60de0a288d1005d2480f7e3e9254fedd99628b1235c |
|
MD5 | 5b4543b54949eba0df44a5e995f52667 |
|
BLAKE2b-256 | 6eecc3ea7e21ac763b63f649730fe51c849834786440eba73f8677caa3d41645 |
File details
Details for the file pe-0.5.3-cp39-cp39-musllinux_1_1_x86_64.whl
.
File metadata
- Download URL: pe-0.5.3-cp39-cp39-musllinux_1_1_x86_64.whl
- Upload date:
- Size: 953.4 kB
- Tags: CPython 3.9, musllinux: musl 1.1+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1bb402ad5b62c2c973427344d5f4810e53cb469dde5ff89c149b3525c4b495d6 |
|
MD5 | 8e2ac1d0628c5a2f8ba7da9cddb5dbe6 |
|
BLAKE2b-256 | a10297fc5019f97f085332d049e736936647c99194477c57e9fdaeecfd7c2696 |
File details
Details for the file pe-0.5.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pe-0.5.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 956.3 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 445236aa4c0d714248e0bcdfd17c57562cbfb9f17d4b9543305dd3ec95897e77 |
|
MD5 | 3f2377effe3a9b148643412c11f60841 |
|
BLAKE2b-256 | 456085d2528de684df2101eb599b5c360e288d8e02c1b0090b9c116b682c59b2 |
File details
Details for the file pe-0.5.3-cp39-cp39-macosx_11_0_arm64.whl
.
File metadata
- Download URL: pe-0.5.3-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 317.0 kB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6279e2b3a99177515b3b0b1aa9884dcdda3a2c2e9eb9b55337e85bd3321462c4 |
|
MD5 | 7814ca63c00e972e65d50c5771b14417 |
|
BLAKE2b-256 | ecdde61125953ce63d7c83808e5ea94186f2c8e085d03e36033e19b4e92bdde1 |
File details
Details for the file pe-0.5.3-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: pe-0.5.3-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 311.6 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e11cfdbc918b5d90140c1da0a8399b5fd7122831f88fd0308b1acf828f7c944 |
|
MD5 | cde69746610ce3a05dc12405d615cace |
|
BLAKE2b-256 | ed56382e81d528f173f745208a6f70ad3ae457c032cc853ac931eb12dbe4782e |
File details
Details for the file pe-0.5.3-cp38-cp38-musllinux_1_1_x86_64.whl
.
File metadata
- Download URL: pe-0.5.3-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/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a29f3e79f0821e7bd090b940027a81825cde77dfcefa49af21ce8b127d53ca87 |
|
MD5 | bedc52983ab3ff0cc98586108d5c8913 |
|
BLAKE2b-256 | 298faecad0548c55d837c9f0c3d2b8c8ca0704eacaf9d77123dfdae6b48570fd |
File details
Details for the file pe-0.5.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pe-0.5.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 977.9 kB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb6e467762dcbb4b9c0a27f027eb48834c4d072a2f5000d7a3594e0fa74ab43d |
|
MD5 | 99745734661701fbe7d7755aeb292a3e |
|
BLAKE2b-256 | b990044277138be874f76cbc322ffcaa4d56a6662ff3639068b478328ff10fd6 |
File details
Details for the file pe-0.5.3-cp38-cp38-macosx_11_0_arm64.whl
.
File metadata
- Download URL: pe-0.5.3-cp38-cp38-macosx_11_0_arm64.whl
- Upload date:
- Size: 316.0 kB
- Tags: CPython 3.8, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84ebbd88ab1edb85f57a4c3115ee0b6666f8d74e416c19818854b697b109c89c |
|
MD5 | 90932b9341232ac0988e2daafab2c0f1 |
|
BLAKE2b-256 | 0d4485196b7f73f1484c7d0280f39a518309e20d8e99544d0be848ba6c3a78dc |