Swiftlet provides Python bindings for Swiftlet, a high-performance parsing library built in Rust. It accepts an EBNF-style grammar and parses input text into a tree of Tree and Token nodes.
Project description
Swiftlet
Swiftlet provides Python bindings for Swiftlet, a high-performance parsing library built in Rust.
It accepts an EBNF-style grammar and parses input text into a tree of Tree and Token nodes.
Features
- Parse context-free grammars from a string or file.
- Choose the parsing algorithm with
earleyorclr. - Control ambiguity handling with
resolveorexplicit. - Inspect the parse result as
TreeandTokenobjects from Python.
Installation
Build and install the package from the swiftlet directory:
pip install .
If you are developing the bindings locally, maturin also works:
maturin develop
Example
from swiftlet import Swiftlet
def main():
grammar = """
start: expr
expr: expr "+" terms -> add
| expr "-" terms -> sub
| terms
terms: terms "*" INT -> mul
| terms "/" INT -> div
| INT
%import (WS, INT)
%ignore WS
"""
text = "12 + 10 - 8 * 20 + 4"
parser = Swiftlet(grammar)
tree = parser.parse(text)
tree.pretty_print()
if __name__ == "__main__":
main()
Usage
Create a parser from grammar text:
from swiftlet import Swiftlet
parser = Swiftlet(
grammar,
start="start",
algorithm="earley",
ambiguity="resolve",
debug=False,
)
Create a parser from a grammar file:
from swiftlet import Swiftlet
parser = Swiftlet.from_file("file_name")
Parse text and inspect the returned tree:
tree = parser.parse("42")
print(tree.get_name())
print(tree.get_children()[0].get_children()[0].get_word())
Notes
algorithmacceptsearleyorclr.ambiguityacceptsresolveorexplicit.parse()returns aTreeon success and raisesValueErrororRuntimeErroron failure.
For more examples, please check notebooks and tests in this package.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swiftlet-0.1.1-cp310-abi3-win_amd64.whl.
File metadata
- Download URL: swiftlet-0.1.1-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.10+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee85e9c0a2f826e9240f86da088ce505bf1a75a1c8f14efcb48b6058c52706d5
|
|
| MD5 |
809d64ff608897f12e0c1ab69a05094a
|
|
| BLAKE2b-256 |
96e70e5158f2a9d3265d74a826af1de2d7fb8f4880944ccc63618edbcca9ea0c
|
Provenance
The following attestation bundles were made for swiftlet-0.1.1-cp310-abi3-win_amd64.whl:
Publisher:
python-publish.yml on jmishra01/Swiftlet
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
swiftlet-0.1.1-cp310-abi3-win_amd64.whl -
Subject digest:
ee85e9c0a2f826e9240f86da088ce505bf1a75a1c8f14efcb48b6058c52706d5 - Sigstore transparency entry: 1181270509
- Sigstore integration time:
-
Permalink:
jmishra01/Swiftlet@ef13f7fb7c7c5b1397c8225a5691d51793044dbe -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/jmishra01
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ef13f7fb7c7c5b1397c8225a5691d51793044dbe -
Trigger Event:
release
-
Statement type:
File details
Details for the file swiftlet-0.1.1-cp310-abi3-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: swiftlet-0.1.1-cp310-abi3-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.10+, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
726011580824f418b65032c152edbdd752a3df8723aa9a9132365ef90661a99a
|
|
| MD5 |
ac286abccecd670c993ad054942b2a66
|
|
| BLAKE2b-256 |
9ba78456b38743735221f8b087e7722d1cc033dfd5175968b37728df81d59902
|
Provenance
The following attestation bundles were made for swiftlet-0.1.1-cp310-abi3-manylinux_2_34_x86_64.whl:
Publisher:
python-publish.yml on jmishra01/Swiftlet
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
swiftlet-0.1.1-cp310-abi3-manylinux_2_34_x86_64.whl -
Subject digest:
726011580824f418b65032c152edbdd752a3df8723aa9a9132365ef90661a99a - Sigstore transparency entry: 1181270513
- Sigstore integration time:
-
Permalink:
jmishra01/Swiftlet@ef13f7fb7c7c5b1397c8225a5691d51793044dbe -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/jmishra01
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ef13f7fb7c7c5b1397c8225a5691d51793044dbe -
Trigger Event:
release
-
Statement type:
File details
Details for the file swiftlet-0.1.1-cp310-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: swiftlet-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.10+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9adfc61a307100af31d06f3b80dc346c70684a2c5537b1474e6d97d79bea2da
|
|
| MD5 |
e31f0756d80251e4daeae0c82df6e60a
|
|
| BLAKE2b-256 |
323b1033c823f353c366485f94f671f2b7332c5531c1bd9132be685716b48837
|
Provenance
The following attestation bundles were made for swiftlet-0.1.1-cp310-abi3-macosx_11_0_arm64.whl:
Publisher:
python-publish.yml on jmishra01/Swiftlet
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
swiftlet-0.1.1-cp310-abi3-macosx_11_0_arm64.whl -
Subject digest:
a9adfc61a307100af31d06f3b80dc346c70684a2c5537b1474e6d97d79bea2da - Sigstore transparency entry: 1181270522
- Sigstore integration time:
-
Permalink:
jmishra01/Swiftlet@ef13f7fb7c7c5b1397c8225a5691d51793044dbe -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/jmishra01
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ef13f7fb7c7c5b1397c8225a5691d51793044dbe -
Trigger Event:
release
-
Statement type: