Kotlin Grammar Notes/Issues


#1

I built a converter that converts the PSI-parsed AST to a more straightforward one. Here are some notes I took while converting parser results, numbered/categorized for easy response:

  1. Where the reference grammar is wrong:
    1. collectionLiteral's reference to element is broken link
    2. callSuffix's trailing annotated lambda is not properly marked as optional
    3. callSuffix says it takes typeArguments on the LHS which doesn’t include the * projection even though it’s valid
    4. functionType's parameter ref makes it seem like type is required which it’s not
    5. callableReference's LHS says it only supports userType which is not true, should be some kind of expression
    6. callableReference mentions an optional typeArguments at the end. Is there anywhere I can see an example of this?
    7. classModifier is missing inner
  2. Where the PSI parser corpus (taken from here) has something unexpected:
    1. What are function expressions? Specifically, what do the following mean:
      1. val a = fun b()
      2. val a = fun @[a] T.(a : foo) : bar
    2. Grammar doesn’t allow nameless function, but parser does and there is a corpus test enforcing it. Why make this a valid parse?
    3. Docs say “Any expression in Kotlin may be marked with a label”, but label1@ val x = 1 is a labeled statement (var decl) instead of an expression. Would that actually be considered two statements, a label def and a var/property decl, or should a var decl be considered an expression? Doesn’t appear properly in the grammar either way.
    4. Grammar doesn’t allow functions without names, but parser does and there is a corpus test enforcing it. Why make this a valid parse?
    5. Grammar doesn’t allow something like an interface decl, but there is a parser corpus test at recovery/InterfaceInExpressionPosition.kt that says it should parse validly. Why make this a valid parse?

That’s only the things I jotted down while developing, there are a bunch of other idiosyncracies I probably just worked around. Feel free to respond, ignore, make issues, etc.


#2

Thank you! I created https://youtrack.jetbrains.com/issue/KT-25552 to fix issues from the first part of your post.