Strumenta / antlr-kotlin

Support for Kotlin as a target for ANTLR 4
Apache License 2.0
222 stars 47 forks source link

Cleanup the runtime code #114

Closed lppedd closed 8 months ago

lppedd commented 9 months ago

Goals

Non goals

It is a non goal to change the behavior of the code. It should possibly stay aligned with the Java runtime, line by line.

Methodology

Each commit is mapped to a runtime Kotlin package.

Each file is cleaned-up in multiple passes:

TODO

lppedd commented 9 months ago

@ftomassetti I'm going to finish off the main package and then call it done.
There will be org.antlr.v4.kotlinruntime.atn still not cleaned up but I can do it in the future.

ftomassetti commented 9 months ago

I started taking a look at the code, but I am afraid I will not be able to do a proper review as there are too many changes. Also, I think this is code coming from the ANTLR Java runtime, so I assume that most of it will be correct. And I will rely on tests to catch blatant things

lppedd commented 9 months ago

Definitely don't review it in details, not worth it.
The code is a 1 to 1 mapping with the Java runtime (I'm doing it by reading each source file line by line), with some Kotlin-friendly code style changes.

lppedd commented 9 months ago

I'm almost done. Just missing a couple of big classes.

In the end I did also the atn package, as the type nullability is tied to the parser and lexer.

lppedd commented 9 months ago

Done!

Turns out there was code not on par with ANTLR 4.13.1.
I've also re-enabled functionalities which were commented out, such as ParserInterpreter, ATNSerializer, XPath* (although we still miss a multiplatform implementation for a couple of Java methods), and many others.

I've deleted two files, which were commented out completely:

We might be able to restore them at some point, but now it doesn't make sense.

lppedd commented 9 months ago

I'll do a couple tests (@ftomassetti you can run some tests on your grammars too if you want), update the branch with master's changes, and then mark it as ready to be merged.

Note that we now have a very strong type system, so a release with this code is not backward compatible.

ftomassetti commented 9 months ago

Note that we now have a very strong type system, so a release with this code is not backward compatible.

Makes sense, I think at this stage breaking changes have to be expected

frett commented 9 months ago

as someone using this in production multiplatform code I'm fine with the breaking changes since it's bringing it up to be more maintainable and Kotlin friendly :)

lppedd commented 9 months ago

Cool! Hopefully there won't be changes this big anymore.

We can merge this one and publish it as another RC, so you can also test it.
I'll try to integrate more grammar tests.

lppedd commented 9 months ago

The C++ grammar from the ANTLR4 grammars repository (which is enormous), compiles fine 🔝

The not-so-good news: the C++ generated parser (20000 LOC) crashes the Kotlin compiler lol

lppedd commented 9 months ago

Managed to get it to run by replacing the scoped { } inline function with if (true).

C++ tests run fine and pass. I'll open a YouTrack issue once this is merged, since this is valid for K2 too.

lppedd commented 9 months ago

I've inspected the outputted JS code and the good news is the K/JS compiler optimizes the if (true) check away.

I'll use that one in place of scoped for now, so that it works ok even if you've got an absurdly big grammar.

ftomassetti commented 9 months ago

Great work @lppedd , should we mark the PR as ready for review and merge it?

lppedd commented 9 months ago

Thank you! Yup, let's merge this one too, but first let's merge #115