Skip to main content

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 earley or clr.
  • Control ambiguity handling with resolve or explicit.
  • Inspect the parse result as Tree and Token objects from Python.

Installation

Build and install the package from the swiftlet directory:

pip install swiftlet

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

  • algorithm accepts earley or clr.
  • ambiguity accepts resolve or explicit.
  • parse() returns a Tree on success and raises ValueError or RuntimeError on 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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

swiftlet-0.1.2-cp310-abi3-win_amd64.whl (1.0 MB view details)

Uploaded CPython 3.10+Windows x86-64

swiftlet-0.1.2-cp310-abi3-manylinux_2_34_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.34+ x86-64

swiftlet-0.1.2-cp310-abi3-macosx_11_0_arm64.whl (1.1 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file swiftlet-0.1.2-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: swiftlet-0.1.2-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

Hashes for swiftlet-0.1.2-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 25abd1eb3821f5cf1f7f5a4d0efe7c3a0e33693388abb318558c222c3c2b64e0
MD5 2f9ff2dc148b873bcdbb2146f3fcace4
BLAKE2b-256 aadffd82088e65bc2ff0b32810ed3f71620650326d454d8a882ce0e1a43f6804

See more details on using hashes here.

Provenance

The following attestation bundles were made for swiftlet-0.1.2-cp310-abi3-win_amd64.whl:

Publisher: python-publish.yml on jmishra01/Swiftlet

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file swiftlet-0.1.2-cp310-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for swiftlet-0.1.2-cp310-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 d3d643adb3b5ed10ffcabf06039accbb5bf41b630131949ad5ad2d20d9a6628a
MD5 b06f576e160a3044e2d97d28129ecec7
BLAKE2b-256 2fde65e03582368d83b900fcedcbb58eab490fd13e80b4c54fbe69b1deaaf3f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for swiftlet-0.1.2-cp310-abi3-manylinux_2_34_x86_64.whl:

Publisher: python-publish.yml on jmishra01/Swiftlet

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file swiftlet-0.1.2-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for swiftlet-0.1.2-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a0773b5b2161ac202930740d346aa08c2cb711568a649005c431a59ca5d3a5f6
MD5 fb77756657ffdbac5da16a2a366c16d1
BLAKE2b-256 d2d96b3bf0f66e20f7409ab2726179d05daa571e188367754b1ce4f9795556c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for swiftlet-0.1.2-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: python-publish.yml on jmishra01/Swiftlet

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page