Tree-sitter Python Bindings

0.25.2 · active · verified Sat Mar 28

Tree-sitter provides Python bindings to the core Tree-sitter parsing library, enabling fast, incremental parsing and the generation of concrete syntax trees for various programming languages. It's actively maintained, with frequent updates (minor and patch releases typically every few weeks or months) to keep pace with the underlying C library. The current version is `0.25.2`.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a language grammar (Python in this case), create a parser, parse a code string into a syntax tree, and then use Tree-sitter's query system to find specific patterns within the tree, such as function names. It highlights the typical workflow of initializing a `Language` object from a pre-compiled grammar and then using a `Parser` to process source code. Ensure the relevant language package (e.g., `tree-sitter-python`) is installed alongside `tree-sitter`.

import os
from tree_sitter import Language, Parser

# NOTE: For this to work, you need 'tree-sitter-python' installed
# pip install tree-sitter-python
import tree_sitter_python as tspython

# Load the Python language grammar
PY_LANGUAGE = Language(tspython.language())

# Create a parser and set its language
parser = Parser()
parser.set_language(PY_LANGUAGE)

# Source code to parse
code = b""" 
def greet(name): # type: (str) -> None
    print(f"Hello, {name}!")

greet("World")
"""

# Parse the code
tree = parser.parse(code)

# Get the root node and print its type
root_node = tree.root_node
print(f"Root node type: {root_node.type}")

# Traverse the tree (example: find function definitions)
query_string = """ 
(function_definition
  name: (identifier) @function.name)
"""
query = PY_LANGUAGE.query(query_string)

captures = query.captures(root_node)
for node, name in captures:
    print(f"Found function: {node.text.decode('utf8')} (capture: {name})")

view raw JSON →