tree-sitter-languages

1.10.2 · deprecated · verified Sat Mar 28

The `tree-sitter-languages` library provides pre-compiled binary Python wheels for various Tree-sitter language parsers. It simplifies the setup for `py-tree-sitter` users by bundling common language grammars, eliminating the need for manual compilation. As of its last updates, the library is largely unmaintained, with official recommendations to consider `tree-sitter-language-pack` for active development and broader compatibility.

Warnings

Install

Imports

Quickstart

Demonstrates how to load a language and obtain a parser, then use it to parse a simple Python code snippet. It highlights the crucial dependency on `tree-sitter` and the potential need for version pinning.

from tree_sitter_languages import get_language, get_parser
import tree_sitter # The underlying tree-sitter library

# IMPORTANT: This library has known compatibility issues with newer versions of `tree-sitter`.
# For versions of `tree-sitter-languages` up to 1.10.2, you might need to pin `tree-sitter`
# to an earlier version for functionality, e.g., `pip install tree-sitter==0.21.3`.

try:
    # Get a language object for Python
    python_language = get_language('python')
    print(f"Loaded language: {python_language}")

    # Get a parser instance for Python
    python_parser = get_parser('python')
    print(f"Created parser: {python_parser}")

    # Example usage: parse some Python code
    code_bytes = b"""
    def hello_world():
        print('Hello, Tree-sitter!')
    """
    tree = python_parser.parse(code_bytes)
    
    print("\nParsed code:")
    print(code_bytes.decode())
    print("\nRoot node of the syntax tree (S-expression form):")
    print(tree.root_node.sexp())

except ImportError as e:
    print(f"Error: {e}. Ensure 'tree-sitter-languages' and 'tree-sitter' are installed.")
except Exception as e:
    print(f"An error occurred during parsing: {e}. Check tree-sitter version compatibility.")

view raw JSON →