Kotlin grammar for tree-sitter
Project description
Kotlin Grammar for Tree-sitter
Kotlin language grammar for Tree-sitter. You can try it out directly on the web.
The grammar is based on the official language grammar.
Project Structure
| File | Description |
|---|---|
| grammar.js | The Tree-sitter grammar |
| grammar-reference.js | A direct translation of the Kotlin language grammar that is, however, ambiguous to Tree-sitter |
| src | The generated parser |
Setup
npm install
Python Package
Install the Python package from PyPI:
pip install ts-kotlin
The package includes the highlight queries bundled with it. You can access the highlights.scm file using the command-line entry point:
ts-kotlin-highlights
This will print the contents of the queries/highlights.scm file to stdout. The queries directory is automatically included when installing the package via pip.
Usage
from tree_sitter import Parser, Language
from tree_sitter_kotlin import language
# Create parser
lang = Language(language())
parser = Parser(lang)
# Parse Kotlin code
code = b"""
fun greet(name: String): String {
return "Hello, $name!"
}
"""
tree = parser.parse(code)
print(tree.root_node.sexp()) # Print syntax tree
Development
Compilation
To (re-)compile the grammar, run:
npm run generate
Note that the grammar is written completely in JavaScript (grammar.js), the other source files are generated by tree-sitter.
Testing
To run the unit tests, run:
npm run test
It is also helpful to run the parser on a real Kotlin project's source files.
./node_modules/.bin/tree-sitter parse "/path/to/some/project/**/*.kt" --quiet --stat
WebAssembly
Compilation
First make sure to have Emscripten installed. If you use Homebrew, you can brew install emscripten. Then run:
npm run build-wasm
Playground
After compiling the grammar to WebAssembly, you can invoke
npm run playground
to launch an interactive editing environment that displays the parsed syntax tree on-the-fly in the browser. You can also view a deployed version of this playground on the web.
Documentation
More documentation on how to create Tree-sitter grammars can be found here.
See also
- Kotlin Language Server for code completion, diagnostics and more
- Kotlin Debug Adapter for JVM debugging support
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ts_kotlin-0.0.1.tar.gz.
File metadata
- Download URL: ts_kotlin-0.0.1.tar.gz
- Upload date:
- Size: 1.7 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0da21fef22b4aa86cc3e12830c3411db668392c29cfd99f7c784565de42ac51b
|
|
| MD5 |
6c9ac51cc035eb40602e6522b8f6daf0
|
|
| BLAKE2b-256 |
77b2004af5a72634f149057827204aaa805f059c327a1737791d29c450ef4240
|
File details
Details for the file ts_kotlin-0.0.1-cp38-abi3-win_amd64.whl.
File metadata
- Download URL: ts_kotlin-0.0.1-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 439.5 kB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c54c9af526bd53b89cff2ec93217b9ece3166b8044e9af19b8f80800733c6e5d
|
|
| MD5 |
0d6428602df3e897309e65c3946f99c7
|
|
| BLAKE2b-256 |
da59577473a5e05e05516fee2d8b744e47b149de4a9ce7aaeb950577fa56a8bc
|
File details
Details for the file ts_kotlin-0.0.1-cp38-abi3-win32.whl.
File metadata
- Download URL: ts_kotlin-0.0.1-cp38-abi3-win32.whl
- Upload date:
- Size: 440.1 kB
- Tags: CPython 3.8+, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9edb8d8f1e1be1278657a187f85bf62e54abb2b72b89743b8641fe8d761f3cb
|
|
| MD5 |
b38065c61cc2bcace659cb9c3282dffe
|
|
| BLAKE2b-256 |
48579495ffa8d6b4357593d66dd8824f0f9e9f466066bf593d83f72dddc5904e
|
File details
Details for the file ts_kotlin-0.0.1-cp38-abi3-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: ts_kotlin-0.0.1-cp38-abi3-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 458.8 kB
- Tags: CPython 3.8+, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77e5a1013346dc39043a0f2831165e0845fd8e3206a164d71ab1cc8d0b804bcb
|
|
| MD5 |
0b309e7b72e4ebf7c9b30c71d4058bf5
|
|
| BLAKE2b-256 |
81d39f048bb9751d24c4cc0517faa57bc333adf90cd2b0f2c3a844516940e08f
|
File details
Details for the file ts_kotlin-0.0.1-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: ts_kotlin-0.0.1-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 459.6 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca65cd919d636eeb1a48453145f7e9c04ed72e63d1d6df5bb96038ba20fb251f
|
|
| MD5 |
9dcc4ec3572b1e09008fc7ba8321262a
|
|
| BLAKE2b-256 |
c1f196b6dc45e354b24f85acc9d1121486025b6c0799733608a7cdd32fc5289a
|
File details
Details for the file ts_kotlin-0.0.1-cp38-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: ts_kotlin-0.0.1-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 478.0 kB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1349d1b2209f458e9ea11c79fad3b6cb5444fe05ad9ae97ebcb1ff92149b0ab
|
|
| MD5 |
171aafe65c8a90cc975f973767979eb9
|
|
| BLAKE2b-256 |
8ca40ee66f515dae99f4f6808b07f060c886cd6923dbeffc0bf0e8801f247674
|