kotlinx / ast

Generic AST parsing library for kotlin multiplatform
Apache License 2.0
317 stars 22 forks source link

Nullable types are not parsed correctly #13

Closed xgouchet closed 4 years ago

xgouchet commented 4 years ago

I'm parsing the following Kotlin code kotlinx.ast and the KotlinGrammarAntlrKotlinParser. But weirdly, I have some nullable types which are not marked as nullable.

package foo.bar
class Spam {
    fun doSomething(i: Int, s: String?, l : List<String>) {}
} 

Here's what I get when printing the AST nodes

PackageHeader(foo.bar)
importList
KlassDeclaration(object Spam)
PackageHeader(foo.bar)
importList
KlassDeclaration(class Spam)
  classBody
    KlassDeclaration(fun doSomething)
      KlassDeclaration(parameter i Int)
      KlassDeclaration(parameter s String)
      KlassDeclaration(parameter l List<String>)

I'm using version c35b50fa44 from JitPack :

implementation("com.github.kotlinx.ast:grammar-kotlin-parser-antlr-kotlin-jvm:c35b50fa44")
xgouchet commented 4 years ago

If I print the raw AST for the s: String? parameter, I get this, so the raw AST does know that the type is nullable :

functionValueParameter
  parameter
    simpleIdentifier
      Identifier >>>s<<< (DEFAULT_TOKEN_CHANNEL)
    COLON >>>:<<< (DEFAULT_TOKEN_CHANNEL)
    Inside_WS >>> <<< (HIDDEN)
    type
      nullableType
        typeReference
          userType
            simpleUserType
              simpleIdentifier
                Identifier >>>String<<< (DEFAULT_TOKEN_CHANNEL)
        quest
          QUEST_NO_WS >>>?<<< (DEFAULT_TOKEN_CHANNEL)
drieks commented 4 years ago

Hi @xgouchet,

sorry for the late answer, I was very busy.. I will have a look, it should be easy to fix.

drieks commented 4 years ago

Hi @xgouchet,

I added your example as an unit test here https://github.com/kotlinx/ast/commit/bc5260ab4a22ddc421175cb06effa3587f3fb280 and fixed the missing nullable check here https://github.com/kotlinx/ast/commit/c6e164e3c0eb5747db58f8458a164cec96db0c74.

Please try the latest version and let me know if you have still issues.

xgouchet commented 4 years ago

Looks all good to me :) Thanks a lot