Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
monperrus opened this issue Sep 26, 2018 · 15 comments
Labels
mastersthesis Potential topic for a bachelor/master's thesis at KTH roadmap

Comments

@monperrus
Copy link
Collaborator

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)

@martinezmatias
Copy link

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.

@monperrus
Copy link
Collaborator Author

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

@monperrus
Copy link
Collaborator Author

found by @martinezmatias

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

@monperrus monperrus added the mastersthesis Potential topic for a bachelor/master's thesis at KTH label Dec 20, 2019
@jlundhol
Copy link

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

@martinezmatias
Copy link

Hi @jlundhol
Another tool:

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

@monperrus
Copy link
Collaborator Author

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

@breandan
Copy link

breandan commented Nov 8, 2021

@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!

@jlundhol
Copy link

jlundhol commented Nov 9, 2021

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.

@breandan
Copy link

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.

@monperrus
Copy link
Collaborator Author

monperrus commented Jan 10, 2022

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

Thanks @jlundhol!

@algomaster99
Copy link
Contributor

algomaster99 commented Jan 10, 2022

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

@monperrus
Copy link
Collaborator Author

made public, thanks @algomaster99

@monperrus
Copy link
Collaborator Author

@monperrus
Copy link
Collaborator Author

@rex-structorum
Copy link

FYI: kotlin-spec contains an official ANTLR grammar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mastersthesis Potential topic for a bachelor/master's thesis at KTH roadmap
Projects
None yet
Development

No branches or pull requests

6 participants