kotlinx / ast

Generic AST parsing library for kotlin multiplatform
Apache License 2.0
325 stars 23 forks source link

Function with generic parameter with optional breaks the parsing #2

Closed martinflorek closed 5 years ago

martinflorek commented 5 years ago

Example below will crash when calling summary() i.e. KotlinAstParser.parseKotlinFile(source).summary() throws java.lang.UnsupportedOperationException: Empty collection can't be reduced..

Parsing the code below, with the String? in a generic, leads to a new structure and summary() does not work anymore.

package com.example.test

class TestClass {
    fun function(record: Map<String?, String>) {
    }
}
drieks commented 5 years ago

Thank you @martinflorek, it should be easy to fix. I will have a look at the issue this evening.

drieks commented 5 years ago

Hi @martinflorek, I added support for nullable types in Version ac934f1076.

KlassIdentifer contains now a Boolean flag nullable (added in commit https://github.com/kotlinx/ast/commit/6055b752f13166ea33bf33fe8e5a568e5867fdad)

Structure after call to summary:

PackageHeader(com.example.test)
importList
KlassDeclaration(class TestClass)
  classBody
    KlassDeclaration(fun function)
      KlassDeclaration(parameter record Map<String?, String>)
drieks commented 5 years ago

Hi @martinflorek,

please try version 7fbbd71235. I added a unit test that parses the source of kotlinx.ast and arrow. I fixed some small errors, all files are now parsed without AstFailure. There are two bugs left, I'm already working on it: #3 #4

I hope you can now use kotlinx.ast to parse your code. If you have any suggestions please let me know :smiley:

martinflorek commented 5 years ago

Works perfectly now. Everything I have thrown at the parser looks OK. Just the speed is very slow. The Kastree parses my sources in a second or so and AST takes maybe 10-15 seconds. Hopefully the #3 will improve this.

Thanks for this great parser!

drieks commented 5 years ago

Ok, that's good that it's working fine now :smiley:

I know that it is slow, but currently there are no speed optimizations at all. I will look into this. What are you doing with the parsed data? Maybe running the AST transformation using Kotlin native can also speed up things. (Currently not supported but planned) I'm using kotlinpoet to create generated sources based on the parsed data.

Fits the Layout of the Klass data classes your needs?

martinflorek commented 5 years ago

Klass is OK for my use case, which is extracting annotations. It is even easier to use than Kastree.

drieks commented 5 years ago

I'm closing this issue because you wrote that it is working fine now.

Works perfectly now. Everything I have thrown at the parser looks OK. Just the speed is very slow. The Kastree parses my sources in a second or so and AST takes maybe 10-15 seconds. Hopefully the #3 will improve this.

Thanks for this great parser!