kotlinx/ast

Nullable types are not parsed correctly

xgouchet opened this issue · 4 comments

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")

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)

Hi @xgouchet,

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

Hi @xgouchet,

I added your example as an unit test here bc5260a and fixed the missing nullable check here c6e164e.

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

Looks all good to me :) Thanks a lot