Skip to main content

No project description provided

Project description

lakai

Lakai is a wrapper around Lark that provides a convenient API.

Installation

$ pip install lakai

Usage

import lakai
grammar = r"""
    %ignore /\s+/
    %import common.INT
    ?atom: INT
    ?!product: atom | product "*" atom | product "/" atom
    ?!sum: product | sum "+" product | sum "-" product
"""
parser = lakai.from_string(grammar, start="sum")
tree = parser.parse("1 + 3 * 2 + 4 / 2")
lakai.pprint(tree)

Gives

sum
    sum
        INT: '1'
        PLUS: '+'
        product
            INT: '3'
            STAR: '*'
            INT: '2'
    PLUS: '+'
    product
        INT: '4'
        SLASH: '/'
        INT: '2'

To evaluate the expression, you can use a lakai.Transformer:

import operator

class Computer(lakai.Transformer):
    operators = {"/": operator.truediv, "*": operator.mul, "-": operator.sub, "+": operator.add}

    def visit_INT(self, leaf: lakai.Leaf) -> int:
        return int(leaf.value)

    def visit_product(self, node: lakai.Node[Any]) -> int:
        return self.operators[node.children[1].value](node.children[0], node.children[2])

    visit_sum = visit_product

assert Computer().visit(tree) == 9

To use Lakai with a Lark standalone parser:

import lakai
from ._standalone import Lark_Standalone
parser = lakai.Lakai(Lark_Standalone())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lakai-0.1.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

lakai-0.1.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file lakai-0.1.1.tar.gz.

File metadata

  • Download URL: lakai-0.1.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for lakai-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e6329c0939d77949d5be0ae24bbd71671792e5a02146828aa25f42f92f382eec
MD5 bf082bd7ff5391c02f9cedd78e58f00b
BLAKE2b-256 edf8dd63d812607c8ceb38182df305b2820873ee651f792182eb845828233218

See more details on using hashes here.

File details

Details for the file lakai-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: lakai-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for lakai-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8c45d545d6fdb7ee912a549f5c305b4d5a0123b4442a65e3ed8e1b1b4d378c3d
MD5 628952180412c59de2f2e5026ee73ced
BLAKE2b-256 3e081c5f865df4d8fcb376134d0df06b0851b3007952fd88c71e489c78d1a898

See more details on using hashes here.

Supported by

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