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. - Context-aware tokenization support, including parser-guided terminal selection for ambiguous token sets.
Installation
Build and install the package from the swiftlet directory:
pip install swiftlet
Example
from swiftlet import Swiftlet, Transformer
class Calculate(Transformer):
def start(self, child):
return child[0]
def expr(self, child):
return child[0]
def add(self, child):
return child[0] + child[2]
def sub(self, child):
return child[0] - child[2]
def mul(self, child):
return child[0] * child[2]
def div(self, child):
return child[0] / child[1]
def terms(self, child):
return child[0]
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 * 2 + 4"
parser = Swiftlet(grammar)
parsed_text = parser.parse(text)
calculate = Calculate()
result = calculate(parsed_text)
parsed_text.pretty_print()
print("\nResult: ", result)
if __name__ == "__main__":
main()
Output
start
add
sub
add
expr
terms 12
+
terms 10
-
mul
terms 8
*
2
+
terms 4
Result: 10
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
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.2.0-cp310-abi3-win_amd64.whl.
File metadata
- Download URL: swiftlet-0.2.0-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 1.1 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 |
e11be7a3e6a4814a745fbf621d4733885ad09d35624171d4c1de53ae152564dc
|
|
| MD5 |
5877a8d2d0c563a76187aff2388436e8
|
|
| BLAKE2b-256 |
f283d8b33ea78d0267fbf31bdc1c6d5b5df9721f126127985129edcf111b1660
|
Provenance
The following attestation bundles were made for swiftlet-0.2.0-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.2.0-cp310-abi3-win_amd64.whl -
Subject digest:
e11be7a3e6a4814a745fbf621d4733885ad09d35624171d4c1de53ae152564dc - Sigstore transparency entry: 1236358177
- Sigstore integration time:
-
Permalink:
jmishra01/Swiftlet@7848f2e713fc943074e5253c0951da437fc993ea -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/jmishra01
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@7848f2e713fc943074e5253c0951da437fc993ea -
Trigger Event:
release
-
Statement type:
File details
Details for the file swiftlet-0.2.0-cp310-abi3-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: swiftlet-0.2.0-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 |
7b74521c76ea390d3d0fc651ae72389da3c0954edb5643e2106804d7ea71edc9
|
|
| MD5 |
002fa4b6bf669499df300c2708fef910
|
|
| BLAKE2b-256 |
27ecd63600bd5fc786968776b09c9ac2e09652e8d2a30dea2b4e52cc163c245d
|
Provenance
The following attestation bundles were made for swiftlet-0.2.0-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.2.0-cp310-abi3-manylinux_2_34_x86_64.whl -
Subject digest:
7b74521c76ea390d3d0fc651ae72389da3c0954edb5643e2106804d7ea71edc9 - Sigstore transparency entry: 1236358089
- Sigstore integration time:
-
Permalink:
jmishra01/Swiftlet@7848f2e713fc943074e5253c0951da437fc993ea -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/jmishra01
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@7848f2e713fc943074e5253c0951da437fc993ea -
Trigger Event:
release
-
Statement type:
File details
Details for the file swiftlet-0.2.0-cp310-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: swiftlet-0.2.0-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.2 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 |
778215ad6cbe286312b8e191fec8dd483df0077ab14f68b1a39d97455ddacadc
|
|
| MD5 |
98cded77c2f496414a4b872adafe5d7f
|
|
| BLAKE2b-256 |
e2c1f111ddb06a5e4f6b06262c22049e363a105772f72e8104776f224c56271d
|
Provenance
The following attestation bundles were made for swiftlet-0.2.0-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.2.0-cp310-abi3-macosx_11_0_arm64.whl -
Subject digest:
778215ad6cbe286312b8e191fec8dd483df0077ab14f68b1a39d97455ddacadc - Sigstore transparency entry: 1236358029
- Sigstore integration time:
-
Permalink:
jmishra01/Swiftlet@7848f2e713fc943074e5253c0951da437fc993ea -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/jmishra01
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@7848f2e713fc943074e5253c0951da437fc993ea -
Trigger Event:
release
-
Statement type: