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 PyPI Downloads

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.
  • 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

  • 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.2.1-cp310-abi3-win_amd64.whl (896.1 kB view details)

Uploaded CPython 3.10+Windows x86-64

swiftlet-0.2.1-cp310-abi3-manylinux_2_34_x86_64.whl (1.0 MB view details)

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

swiftlet-0.2.1-cp310-abi3-macosx_11_0_arm64.whl (899.7 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: swiftlet-0.2.1-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 896.1 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for swiftlet-0.2.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 18291e2a2902eed9c18ceb81a792687e6cfdcdbf2561b160bb16e962e7dc1542
MD5 fe4e650949cd5030eb00fbdf304b49c2
BLAKE2b-256 28a43aa2e3b878b5b816ca0ce54a158eeab598be49d254482e3759ba5bba01c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for swiftlet-0.2.1-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.2.1-cp310-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for swiftlet-0.2.1-cp310-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 5258ed4c5d158ae727d4049431e37b8108f03b0933da8f0254e7a61add79fc71
MD5 e461d2b178713714624ca0a851fcaf58
BLAKE2b-256 291d530997dfd0e1a790cd3e2de483fe3ed06afe0d2f550eface727abc0628c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for swiftlet-0.2.1-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.2.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for swiftlet-0.2.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 aa3144cfffce08490263bb2a37c388386c397648af6a437328317bf01aab0546
MD5 5ab36e4a5871151ff9ddef28a6f9d56e
BLAKE2b-256 3a79b8209969735dfd969fb64a7d281b3f5ba8a725c8460e6f208d5751d8125b

See more details on using hashes here.

Provenance

The following attestation bundles were made for swiftlet-0.2.1-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