{"id":9369,"library":"tree-sitter-kotlin","title":"tree-sitter-kotlin","description":"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.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter-grammars/tree-sitter-kotlin","tags":["tree-sitter","kotlin","parsing","grammar","AST","compiler"],"install":[{"cmd":"pip install tree-sitter-kotlin","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Required for the Python bindings to interact with Tree-sitter grammars.","package":"tree_sitter","optional":false}],"imports":[{"note":"The grammar object is exposed via a `language()` method within the `tree_sitter_kotlin` module, which should then be passed to `tree_sitter.Language`.","wrong":"from tree_sitter_kotlin import KOTLIN_LANGUAGE","symbol":"language","correct":"import tree_sitter_kotlin as tskt\nfrom tree_sitter import Language\nKOTLIN_LANGUAGE = Language(tskt.language())"}],"quickstart":{"code":"import tree_sitter_kotlin as tskt\nfrom tree_sitter import Language, Parser\n\n# Load the Kotlin grammar\nKOTLIN_LANGUAGE = Language(tskt.language())\n\n# Create a parser and set the language\nparser = Parser()\nparser.set_language(KOTLIN_LANGUAGE)\n\n# Sample Kotlin code to parse\nkotlin_code = b'''\nfun main() {\n    val greeting = \"Hello, Tree-sitter!\"\n    println(greeting)\n}\n'''\n\n# Parse the code\ntree = parser.parse(kotlin_code)\n\n# Get the root node of the AST\nroot_node = tree.root_node\n\nprint(f\"Root node type: {root_node.type}\")\nprint(f\"Root node children count: {len(root_node.children)}\")\n# Example: Find a function declaration\nfunction_node = root_node.children[0] # Assuming 'fun main()' is the first child\nif function_node.type == 'function_declaration':\n    print(f\"Found function: {function_node.text.decode('utf8').split('(')[0]}\")","lang":"python","description":"Demonstrates how to load the `tree-sitter-kotlin` grammar and use `tree_sitter.Parser` to parse a basic Kotlin code snippet, then access the root node of the generated Abstract Syntax Tree."},"warnings":[{"fix":"Instead of `Language.build_library()`, install pre-compiled language packages (e.g., `pip install tree-sitter-kotlin`). For custom grammars, external compilation tools or older `tree_sitter` versions (pre-0.22.0) are required.","message":"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.","severity":"breaking","affected_versions":"tree_sitter >= 0.22.0"},{"fix":"When creating a `Parser`, consider setting a larger `bufferSize` in the `parse()` method's options if dealing with very large source files, or splitting files into smaller chunks before parsing. Consult `tree_sitter` documentation for `parse()` method options.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure you call `tree_sitter_kotlin.language()` (e.g., `Language(tskt.language())`) and do not treat `language` as a direct attribute or constant without the `()`.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Remove 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.","cause":"Attempting to use the `build_library` method on `tree_sitter.Language`, which was removed in recent versions of the `tree_sitter` Python binding.","error":"AttributeError: type object 'Language' has no attribute 'build_library'"},{"fix":"Install the package using pip: `pip install tree-sitter-kotlin`.","cause":"The `tree-sitter-kotlin` package has not been installed or is not accessible in the current Python environment.","error":"ModuleNotFoundError: No module named 'tree_sitter_kotlin'"},{"fix":"Ensure 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.","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.","error":"ValueError: Invalid language name or path for Language('/tmp/my_grammar.so', 'kotlin')"}]}