Angry-Potato/prettier-plugin-kotlin

Alternative parser suggestion

Opened this issue · 3 comments

First of all I love that you started building the plugin. I wanted to do it myself but never as far as you. I just played around with it and tried some ideas. One of those ideas was to generate a javascript parser with ANTLR and the official grammer files. That approach went nowhere as the generated parser is painfully slow. After stumbling upon your repo, I revisited the idea and generated a java parser which I compiled to a native binary using GraalVM (7 MB).

At the moment my kotlin-parser is not more than a POC but I am curious if that would be something that could help your project.

@marschwar Thanks for being interested in this plugin! The implementation I have ended up with is probably the laziest possible 🤔.
The parser currently being used by this plugin is not fast, my main gripe with it is that it doesn't build a granular enough AST, especially when parsing strings, it becomes impossible to tell if the string was interpolated or not - this is why it serializes and deserializes back into Kotlin rather than being written 100% in Js. Would your parser handle this?

It is certainly possible to build the parser this way. I did not plan to build a general purpose parser but rather one that is tailored to the needs of a prettier plugin. Since the parser is being generated, the actual work would be the visitor to transfer the AST into a data model that can be processed in js. I would be more than happy to put some time into it if you think it is worth it.

With regards to serialization/deserializion: Have you seen https://github.com/eishay/jvm-serializers/wiki#cross-lang-binary-serializers ?

I looked into it a bit more and realized the AST in "my" parser does not seem to contain the comments. If I understand prettier correctly the source code is parsed and the parser result is printed in a unified form. I guess one would have to mess with the gramer files to retain comments and blank lines.