Tree-sitter Go Grammar for Python
tree-sitter-go provides Python bindings for the Go programming language grammar, enabling the tree-sitter parsing library to generate incremental syntax trees for Go code. This allows for robust code analysis, highlighting, and manipulation. The library is actively maintained, with releases generally coinciding with updates to the core tree-sitter project.
Warnings
- breaking The `tree-sitter` core library (a dependency) introduced significant breaking changes in version `0.25.0`, including an ABI bump to 15, removal of `ts_node_child_containing_descendant`, and changes to parsing/query cancellation APIs. Users upgrading the core `tree-sitter` package should consult its changelog for compatibility.
- deprecated The `Language.build_library` static method was removed from the `tree-sitter` Python bindings around version `0.21.x`. Attempting to build grammars dynamically from source within Python code will now fail.
- gotcha Users on Windows may encounter challenges when installing `tree-sitter` or language grammars due to underlying C/C++ compilation requirements. Error messages related to missing compilers (e.g., `cl.exe`) are common.
Install
-
pip install tree-sitter-go tree-sitter
Imports
- language
import tree_sitter_go as tsgo from tree_sitter import Language, Parser GO_LANGUAGE = Language(tsgo.language())
- Parser
from tree_sitter import Parser
Quickstart
import tree_sitter_go as tsgo
from tree_sitter import Language, Parser
# Load the Go language grammar
GO_LANGUAGE = Language(tsgo.language())
# Create a parser and set its language
parser = Parser()
parser.set_language(GO_LANGUAGE)
# Example Go code (must be bytes for tree-sitter)
go_code = b"""
package main
import (
"fmt"
"os"
)
func main() {
// A simple 'Hello World' in Go
name := os.Getenv("USER_NAME")
if name == "" {
name = "Tree-sitter User"
}
fm.Println("Hello,", name + "!")
}
"""
# Parse the code
tree = parser.parse(go_code)
# Get the root node of the syntax tree
root_node = tree.root_node
# Print basic information about the root node
print(f"Root Node Type: {root_node.type}")
print(f"Root Node Text (first 50 chars): {root_node.text.decode('utf8')[:50]}...")
# Traverse and print types of top-level children
print("\nTop-level children types:")
for child in root_node.children:
print(f"- {child.type}")