The tree-sitter parser for Kotlin is quite unreliable.
In many cases, it generates ERROR nodes (more details in the issue below)
bonede/tree-sitter-ng#108
and it also parses functional interfaces as functions.
I would strongly recommend to use kotlin-compiler-embeddable
https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-compiler-embeddable/versions?repo=central
for generating the AST.
It provides a org.jetbrains.kotlin.psi.KtVisitor
Parsing is easy as follows:
String filePath = ""; // the path of a kt file
String fileContent = "" // the string content of a kt file
KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForProduction(
Disposer.newDisposable(),
new CompilerConfiguration(),
EnvironmentConfigFiles.JVM_CONFIG_FILES
);
PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(environment.getProject());
PsiFile psiFile = factory.createFileFromText(filePath, KotlinLanguage.INSTANCE, fileContent);
KtFile ktFile = (KtFile)psiFile;
The tree-sitter parser for Kotlin is quite unreliable.
In many cases, it generates
ERRORnodes (more details in the issue below)bonede/tree-sitter-ng#108
and it also parses
functional interfacesasfunctions.I would strongly recommend to use
kotlin-compiler-embeddablehttps://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-compiler-embeddable/versions?repo=central
for generating the AST.
It provides a
org.jetbrains.kotlin.psi.KtVisitorParsing is easy as follows: