spoon icon indicating copy to clipboard operation
spoon copied to clipboard

feature(spoon-kotlin): use the Spoon metamodel to analyze and transform Kotlin code

Open monperrus opened this issue 6 years ago • 15 comments

Hi all,

We would like to propose a new item in the Spoon roadmap: use the Spoon metamodel to analyze and transform Kotlin code.

This would be useful because there is no major Kotlin analysis and transformation tool, with an API as powerful as Spoon's one (@martinezmatias would need this for his research). The Spoon community would reach a new target, in a super active and growing community.

This would be doable, because the concepts of Kotlin are heavily based on Java, and there is likely little information to add in the metamodel to support Kotlin. We would need to:

  • Write KotlinTreeBuilder, based on a Kotlin parser / compiler. We could likely reuse the official one from @JetBrains.
  • Write KotlinPrettyPrinter.

And last but not least, this would be a lot of fun!

What do you think?

--Martin (@monperrus) and Nicolas (@petitpre)

monperrus avatar Sep 26 '18 07:09 monperrus

Hi @monperrus @petitpre I will be amazing. Right now, we are parsing Kotlin ASTs using Detekt which if I am not wrong, also uses the official parser from @JetBrains. We have some results of parsing Kotlin code. As far I know -not expert on Detekt-, the API of Detekt to retrieve AST is not "clear", i.e., one has to write a rule, which then is matched on the AST from the app under analysis.

martinezmatias avatar Sep 26 '18 08:09 martinezmatias

Kotlin is the fastest growing language on Github: https://octoverse.github.com/projects#repositories

monperrus avatar Oct 19 '18 16:10 monperrus

found by @martinezmatias

Simple Kotlin Source AST and Syntax Parsing, Editing, and Writing https://github.com/cretz/kastree

monperrus avatar Feb 21 '19 14:02 monperrus

In addition to detekt and kastree mentioned in earlier comments, here are two more potential options. In contrast to detekt and kastree, these are not using the PSI module of the official compiler for parsing

kotlinx.ast: Generic AST parsing library for kotlin multiplatform https://github.com/kotlinx/ast

Kotlin grammar tools: Tokenization and parsing Kotlin code using the ANTLR Kotlin grammar https://github.com/Kotlin/grammar-tools

jlundhol avatar Feb 14 '20 10:02 jlundhol

Hi @jlundhol Another tool:

Better-parse: A nice parser combinator library for Kotlin: https://github.com/h0tk3y/better-parse

martinezmatias avatar Feb 20 '20 08:02 martinezmatias

FYI: Jesper Lundholm's thesis.

KtSpoon : Modelling Kotlin by extending Spoon’s Java Metamodel (2021): http://urn.kb.se/resolve?urn=urn:nbn:se:kth:diva-304429

monperrus avatar Nov 08 '21 11:11 monperrus

@jlundhol Can you comment on your parser's support for error recovery? Does it need to perform symbol resolution or it possible to define a source code transformation on an incomplete code snippet? Thank you!

breandan avatar Nov 08 '21 21:11 breandan

Hi @breandan , it depends heavily on the Kotlin IR module which will generate IR error nodes for errors such as unresolved references. I didn't implement any handling of error nodes, though. Hence, it won't build a model of code containing errors.

jlundhol avatar Nov 09 '21 20:11 jlundhol

Thank you! From what I have seen, Spoon is able to handle whole-classes, but methods must be enclosed. While some parsers have better support for error recovery, it is unclear what should occur when transforming a partial code snippet, so it makes sense to only consider fully-formed classes. Otherwise I quite like your fluent API design and look forward to using it. Out of the ones I have tried, it seems to be the most reasonable.

breandan avatar Nov 13 '21 20:11 breandan

The code now lives on https://github.com/SpoonLabs/KtSpoon/.

Thanks @jlundhol!

monperrus avatar Jan 10 '22 09:01 monperrus

@monperrus Is that a private repository or is the link incorrect? I am unable to access it.

algomaster99 avatar Jan 10 '22 09:01 algomaster99

made public, thanks @algomaster99

monperrus avatar Jan 10 '22 10:01 monperrus

FTR: https://github.com/fbsamples/kotlin_ast_tools

monperrus avatar Oct 26 '22 07:10 monperrus

FYI: kotlin-spec contains an official ANTLR grammar.

rex-structorum avatar Apr 18 '23 20:04 rex-structorum