Tree Sitter is a framework for building grammars for programming languages. It generates and uses syntax trees from source files, which are useful for code analysis, tooling, and syntax highlighting.
Python bindings for Tree Sitter grammars are provided through the py-tree-sitter module. The Nix package python3Packages.tree-sitter-grammars provides pre-built grammars for various languages.
For example, to experiment with the Rust grammar, you can create a shell environment with the following configuration:
{ pkgs ? import <nixpkgs> { },}:
pkgs.mkShell { name = "py-tree-sitter-dev-shell";
buildInputs = with pkgs; [ (python3.withPackages ( ps: with ps; [ tree-sitter tree-sitter-grammars.tree-sitter-rust ] )) ];}Once inside the shell, the following Python code demonstrates how to parse a Rust code snippet:
# Import the Tree Sitter library and Rust grammarimport tree_sitterimport tree_sitter_rust
# Load the Rust grammar and initialize the parserrust = tree_sitter.Language(tree_sitter_rust.language())parser = tree_sitter.Parser(rust)
# Parse a Rust snippettree = parser.parse( bytes( """ fn main() { println!("Hello, world!"); } """, "utf8" ))
# Display the resulting syntax treeprint(tree.root_node)The tree_sitter_rust.language() function references the Rust grammar loaded in the Nix shell. The resulting tree allows you to programmatically inspect the structure of the code.