tree-sitter-kotlin
tree-sitter-kotlin provides a Kotlin grammar for the Tree-sitter parsing library. Tree-sitter is an incremental parsing system designed for text editors, enabling fast and robust syntax tree generation and updates. This Python package provides pre-compiled binaries of the Kotlin grammar, allowing developers to parse Kotlin code and build Abstract Syntax Trees (ASTs) in Python applications. The library maintains an active development status, with recent releases indicating a cadence of a few updates per year.
Common errors
-
AttributeError: type object 'Language' has no attribute 'build_library'
cause Attempting to use the `build_library` method on `tree_sitter.Language`, which was removed in recent versions of the `tree_sitter` Python binding.fixRemove calls to `Language.build_library()`. For official grammars like Kotlin, install them directly via pip (e.g., `pip install tree-sitter-kotlin`). For custom grammars, manage compilation externally. -
ModuleNotFoundError: No module named 'tree_sitter_kotlin'
cause The `tree-sitter-kotlin` package has not been installed or is not accessible in the current Python environment.fixInstall the package using pip: `pip install tree-sitter-kotlin`. -
ValueError: Invalid language name or path for Language('/tmp/my_grammar.so', 'kotlin')cause The path to the compiled grammar (`.so` or `.dll` file) is incorrect, or the language name provided does not match the compiled grammar's identifier. For pip-installed grammars, direct file paths are usually not used.fixEnsure that the grammar is loaded correctly via the package's `language()` function, e.g., `KOTLIN_LANGUAGE = Language(tree_sitter_kotlin.language())`, rather than attempting to load a `.so` file directly if using the pip-installed package.
Warnings
- breaking The `Language.build_library()` static method for compiling grammars was removed from the `tree_sitter` Python binding around versions 0.21.x/0.22. Direct compilation of local grammar sources is no longer supported via this API.
- gotcha Tree-sitter can fail to parse large files (e.g., >32KB) with a generic 'Invalid argument' error if the internal buffer size is not adequately configured.
- gotcha Grammar objects are loaded via a function call, not directly as attributes. For `tree-sitter-kotlin`, you must call `tskt.language()` to get the underlying C grammar object.
Install
-
pip install tree-sitter-kotlin
Imports
- language
from tree_sitter_kotlin import KOTLIN_LANGUAGE
import tree_sitter_kotlin as tskt from tree_sitter import Language KOTLIN_LANGUAGE = Language(tskt.language())
Quickstart
import tree_sitter_kotlin as tskt
from tree_sitter import Language, Parser
# Load the Kotlin grammar
KOTLIN_LANGUAGE = Language(tskt.language())
# Create a parser and set the language
parser = Parser()
parser.set_language(KOTLIN_LANGUAGE)
# Sample Kotlin code to parse
kotlin_code = b'''
fun main() {
val greeting = "Hello, Tree-sitter!"
println(greeting)
}
'''
# Parse the code
tree = parser.parse(kotlin_code)
# Get the root node of the AST
root_node = tree.root_node
print(f"Root node type: {root_node.type}")
print(f"Root node children count: {len(root_node.children)}")
# Example: Find a function declaration
function_node = root_node.children[0] # Assuming 'fun main()' is the first child
if function_node.type == 'function_declaration':
print(f"Found function: {function_node.text.decode('utf8').split('(')[0]}")