Kotlin / kotlin-spec

Kotlin Language Specification:
https://kotlinlang.org/spec
Apache License 2.0
387 stars 80 forks source link

ANTLR parser fails when a newline exists inside an expression interpolated in a string #40

Closed ondrej closed 3 years ago

ondrej commented 4 years ago

Hi team,

I noticed a problem when parsing files using the official Kotlin ANTLR grammar from https://kotlinlang.org/docs/reference/grammar.html.

This parses OK:

val numbers = mapOf("1" to 1, "2" to 2, "3" to 3)
println("output: ${numbers.entries.joinToString { "${it.key}: ${it.value}" }}")

This fails (but compiles without issue in e.g. Android Studio):

val numbers = mapOf("1" to 1, "2" to 2, "3" to 3)
println("output: ${
    numbers.entries.joinToString { "${it.key}: ${it.value}" }}")

This issue is possibly related to https://youtrack.jetbrains.com/issue/KT-26077. When adding parentheses around the expression, ANTRL does parse the statement just fine. Assuming this is the same issue, and since KT-26077 hasn't been touched in over a year, is there anything I might be able to do in the short term to work around this?

Many thanks!

drieks commented 3 years ago

Hi,

@arkadiuszpalka found the same problem in https://github.com/kotlinx/ast/issues/31. The fix is to add NL* inside of lineStringExpression:

https://github.com/kotlinx/ast/commit/e574d67170c34b451680b49241429855593eda47

belyaev-mikhail commented 3 years ago

Just for the record: KT-26077 is not planned to be fixed in ANTLR grammar at all because it comes from basic inconsistency of Kotlin lexical structure with traditional lexer/parser dichotomy. This issue, though, is not related and seems to be fixed by #77.

lbtrace commented 1 year ago

@drieks I have a question for you, can you tell me? Is it only ANTLR grammar file kotlinParser.g4 bug? kotlinc compiler always process NL* inside of lineStringExpression, right?

drieks commented 1 year ago

Hi @lbtrace, yes, it's related to the antlr grammar here: https://kotlinlang.org/docs/reference/grammar.html. The Kotlin Compile is not using this grammar and is therefore not affected.