Skip to content

Python

hyperlark is a fast parsing toolkit for Python: write a grammar in the .lark language and parse it with a native (Rust) engine — no runtime dependencies.

Terminal window
pip install hyperlark
import hyperlark
parser = hyperlark.Lark(r"""
start: "hello" NAME
NAME: /\w+/
%ignore " "
""", parser="lalr")
tree = parser.parse("hello world")
print(tree.pretty())

parse returns a Tree. Walk its .children, read a node’s .data name, and inspect leaf Tokens: they compare by value (tok == "world") and support the usual string operations, but — by default (fast_tokens=True) — are not str subclasses; pass fast_tokens=False for lark-identical str-subclass tokens:

tree = parser.parse("hello world")
name = tree.children[0] # Token('NAME', 'world')
print(name, name.line, name.column)

Transform the tree with the Transformer / Visitor / Interpreter toolkit — see the transformers guide.

The package ships py.typed, so mypy and pyright read its annotations rather than falling back to Any. Tree is generic in its leaf type and ParseTree is Tree[Token], which is the type parse() returns — the same shape lark uses, so annotations written against lark carry over unchanged:

from hyperlark import Lark, ParseTree, Token
def first_token(tree: ParseTree) -> Token:
leaf = tree.children[0]
assert isinstance(leaf, Token)
return leaf
parser = Lark("start: WORD+\n%import common.WORD\n%ignore \" \"\n")
print(first_token(parser.parse("hello world")))