Tree-sitter Rust Grammar for Python
tree-sitter-rust provides the official Rust grammar for the Tree-sitter parsing library in Python. It enables fast, incremental parsing of Rust code, generating concrete syntax trees for advanced code analysis, tooling, and editor integrations. The library is actively maintained, with frequent releases to keep up with `tree-sitter` core and Rust language changes.
Warnings
- breaking The `Language.build_library()` method, used for compiling Tree-sitter grammars from source at runtime, was removed from the `tree-sitter` Python library (formerly `py-tree-sitter`) around version 0.22. This means on-the-fly grammar compilation is no longer directly supported by the Python bindings.
- gotcha The `tree-sitter-rust` package provides only the Rust grammar definition; it relies on the core `tree-sitter` Python library for the parser engine itself. Forgetting to install `tree-sitter` will lead to `ModuleNotFoundError` or `RuntimeError` when attempting to use the parser.
- gotcha The `parser.parse()` method expects a `bytes` object as input for the source code, not a `str`. Passing a Python string directly will result in a `TypeError`.
- gotcha Incompatibilities between the `tree-sitter` core library version and the `tree-sitter-rust` grammar version can lead to runtime errors or incorrect parsing, especially if mixing installation methods or manually compiling grammars.
Install
-
pip install tree-sitter tree-sitter-rust
Imports
- language
from tree_sitter_rust import language
Quickstart
import tree_sitter
from tree_sitter import Language, Parser
from tree_sitter_rust import language
# Example Rust code to parse
rust_code = '''
fn main() {
let message = "Hello, world!";
println!("{}", message);
}
'''
# 1. Load the Rust language grammar
# The `language` object imported is a callable that returns the actual LanguageFn.
RUST_LANGUAGE = Language(language())
# 2. Create a parser and set its language
parser = Parser()
parser.set_language(RUST_LANGUAGE)
# 3. Parse the code (input must be bytes)
tree = parser.parse(bytes(rust_code, "utf8"))
# 4. Get the root node of the syntax tree
root_node = tree.root_node
print(f"Root node type: {root_node.type}")
print(f"Root node text: {root_node.text}")
print(f"Tree S-expression: {root_node.sexp()}")
# Example of traversing the tree
print("\nWalking the tree nodes:")
cursor = tree.walk()
# Start at the root
def traverse(cursor, indent=0):
print(" " * indent + f"Node: {cursor.node.type} [start: {cursor.node.start_point}, end: {cursor.node.end_point}]")
if cursor.goto_first_child():
traverse(cursor, indent + 1)
while cursor.goto_next_sibling():
traverse(cursor, indent + 1)
cursor.goto_parent() # Go back up after visiting all siblings
traverse(cursor)