Skip to content

Repository files navigation

tree-sitter-languages-kmp

Kotlin Multiplatform bindings to 24 Tree-sitter grammars, built on the published KTreeSitter core (io.github.tree-sitter:ktreesitter). Grammar bindings are generated by an in-house Gradle plugin ported from the upstream kotlin-tree-sitter plugin.

Modules

Module Grammar
languages/agda tree-sitter-agda v1.3.3
languages/bash tree-sitter-bash v0.25.1
languages/c tree-sitter-c v0.24.1
languages/c-sharp tree-sitter-c-sharp v0.23.1
languages/cpp tree-sitter-cpp v0.23.4
languages/css tree-sitter-css v0.25.0
languages/embedded-template tree-sitter-embedded-template v0.25.0
languages/go tree-sitter-go v0.25.0
languages/haskell tree-sitter-haskell v0.23.1
languages/html tree-sitter-html v0.23.2
languages/java tree-sitter-java v0.23.5
languages/javascript tree-sitter-javascript v0.25.0
languages/json tree-sitter-json v0.24.8
languages/julia tree-sitter-julia v0.25.0
languages/ocaml tree-sitter-ocaml v0.23.2 (grammars/ocaml)
languages/php tree-sitter-php v0.24.2 (php/src)
languages/python tree-sitter-python v0.25.0
languages/regex tree-sitter-regex v0.25.0
languages/ruby tree-sitter-ruby v0.23.1
languages/rust tree-sitter-rust v0.24.0
languages/scala tree-sitter-scala v0.24.0
languages/tsx tree-sitter-typescript v0.23.2 (tsx/src)
languages/typescript tree-sitter-typescript v0.23.2 (typescript/src)
languages/verilog tree-sitter-verilog v1.0.3

Each grammar repository is pinned as a git submodule under languages/<name>/tree-sitter-<name> at the same version tree-sitter-ng uses. tsx and typescript share the tree-sitter-typescript repository; ocaml builds the grammars/ocaml grammar.

Supported platforms

Every language module targets:

  • JVM — native grammar compiled to a JNI shared library (CMake), loaded from jar resources
  • Android — native grammar compiled with the NDK toolchain (arm64-v8a, armeabi-v7a, x86_64)
  • Kotlin/NativelinuxX64, linuxArm64, mingwX64, macosX64, macosArm64, iosArm64, iosSimulatorArm64

Layout

  • gradle/libs.versions.toml — version catalog (Kotlin 2.3.20, AGP 9.3.1, KTreeSitter; build-logic dependencies are declared here too)
  • build-logic/ — in-house tree-sitter-grammar plugin (grammar file generation, JNI/CMake scaffolding, cinterop wiring) plus the tree-sitter-language convention plugin driving every languages/* module

Publishing

Languages are published to Maven Local with com.vanniktech.maven.publish 0.37.0. Each platform is its own artifact (no merged metadata module):

./gradlew publishToMavenLocal                      # all modules, all platforms
./gradlew :languages:java:publishToMavenLocal      # single language

Coordinates: cn.enaium.treesitter:treesitter-languages-<lang>-kmp-<platform>:<version>, with -jvm, -android, -linuxx64, -linuxarm64, -mingwx64, -macosx64, -macosarm64, -iosarm64, -iossimulatorarm64 suffixes. Each language module is versioned by its grammar repository tag (e.g. treesitter-languages-java-kmp-jvm:0.23.5, treesitter-languages-bash-kmp-jvm:0.25.1, treesitter-languages-agda-kmp-jvm:1.3.3), read from the pinned submodule tag at build time.

Usage

// build.gradle.kts
repositories { mavenLocal(); mavenCentral() }

dependencies {
    implementation("io.github.tree-sitter:ktreesitter:0.25.1")
    implementation("cn.enaium.treesitter:treesitter-languages-java-kmp-jvm:0.25.1")
}
import io.github.treesitter.ktreesitter.Language
import io.github.treesitter.ktreesitter.Parser
import cn.enaium.treesitter.languages.java.TreeSitterJava

val language = Language(TreeSitterJava.language())
val parser = Parser(language)
val tree = parser.parse("class A { int x; }")
println(tree.rootNode.type) // program

Building

./gradlew :languages:java:jvmTest             # JVM (builds JNI lib via CMake)
./gradlew :languages:java:macosArm64Test      # Kotlin/Native (cinterop)
./gradlew :languages:java:compileAndroidMain  # Android
./gradlew :example:run                        # Compose desktop playground

Requires JDK 17+ (JDK 21 recommended), the Android SDK with NDK (see gradle.properties), and CMake for JNI builds. Gradle 9.7 / AGP 9.3.1 / Kotlin 2.3.20.

Notes

  • The tree-sitter-grammar plugin in build-logic/ is a port of the upstream io.github.tree-sitter.ktreesitter-plugin; the published Gradle plugin is not used.
  • AGP 9's com.android.kotlin.multiplatform.library plugin has no externalNativeBuild DSL, so the Android JNI library is compiled directly with the NDK toolchain into src/androidMain/jniLibs (arm64-v8a, armeabi-v7a, x86_64).
  • The published KTreeSitter 0.25.1 native binding passes the Kotlin string's character count as the byte length to ts_parser_parse_string_encoding; non-ASCII sources can be truncated on Kotlin/Native targets. Samples are ASCII to avoid tripping this upstream issue (the JVM binding uses the correct UTF length).

Example playground

example/ is a Compose Multiplatform port of the tree-sitter playground:

  • language dropdown (all 24 grammars)
  • query input with options (highlight matches / show only matches / case sensitive)
  • source editor with query-match highlighting and node-selection highlighting
  • collapsible syntax tree panel showing every parsed node with byte ranges and text

Targets: desktop (JVM), Android, iOS, macOS. Run with ./gradlew :example:run (desktop).

Note: Compose Multiplatform 1.12 publishes artifacts only for macosArm64 (native desktop), not linuxX64/mingwX64, so the example targets macosArm64/iOS plus Android/JVM.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages