Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/endoqa/kotlin-tree-sitter
Kotlin tree-sitter
https://github.com/endoqa/kotlin-tree-sitter
kotlin tree-sitter
Last synced: 4 months ago
JSON representation
Kotlin tree-sitter
- Host: GitHub
- URL: https://github.com/endoqa/kotlin-tree-sitter
- Owner: Endoqa
- Created: 2024-05-26T23:58:14.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-09-03T02:57:09.000Z (5 months ago)
- Last Synced: 2024-10-09T23:22:48.140Z (4 months ago)
- Topics: kotlin, tree-sitter
- Language: Kotlin
- Homepage:
- Size: 67.4 KB
- Stars: 10
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Kotlin Tree Sitter SDK
Kotlin sdk for [tree-sitter](https://tree-sitter.github.io/tree-sitter/)
## Install
### Prepare `tree-sitter` binaries
Prepare binaries with tree-sitter and required languages.
[Optional prebuilt binaries](https://github.com/Endoqa/tree-sitter-prebuilt)
### Add maven repository
```
https://maven.endoqa.io
```### Add dependencies
#### C-style api
```
io.endoqa:tree-sitter-c:0.0.2
```#### Object-style api:
```
io.endoqa:tree-sitter:0.0.3
```## Before Start
### Load native libraries
```kotlin
System.load("/absolute/path/to/tree-sitter.*")
System.load("/absolute/path/to/tree-sitter-.*")
```Pick api style:
- [Kotlin](#get-started)
- [C-style](#get-started-c-style)## Get Started
```kotlin
val parser = Parser()
val clang = Language.getLanguage("c")
parser.setLanguage(clang)val tree = parser.parse(sourceCode)
val rootNode = tree.rootNode
rootNode.namedChildren.forEach { node ->
println(node.symbol)
}
```## Get Started (C-Style)
All api are identical to c api
[Using Parser - Get Started](https://tree-sitter.github.io/tree-sitter/using-parsers)
### Obtain a language
```kotlin
val languageMethod = Linker.nativeLinker().downcallHandle(
RuntimeHelper.symbolLookup.find("tree_sitter_").get(),
FunctionDescriptor.of(ValueLayout.ADDRESS)
)val language: Pointer = languageMethod.invokeExact() as MemorySegment
```### Use parser
```kotlin
val parser = ts_parser_new()
ts_parser_set_language(parser, language)
val source: MemorySegment = TODO("allocate source")
ts_parser_parse_string(
parser,
MemorySegment.NULL,
source,
sourceLen
)
val rootNode = ts_tree_root_node(tree)
ts_tree_delete(tree)
ts_parser_delete(parser)
```