[kotlin] Fix kotlin grammar for parsing multidollar interpolation#6497
Conversation
…g tests, now with failing Multiline Multi dollar issue in test
… used to match the number of tripple quote prefix dollars
|
No relevant source code has been changed, pmdtester skipped. (comment created at 2026-04-23 19:00:24+00:00 for 1328d18) |
|
Went down the number of dollars rabbit hole: now the grammar takes into consideration the number of dollars to match actual refs and expressions in the multi line templates. Added unit tests for regular and "split" dollar cases. |
adangel
left a comment
There was a problem hiding this comment.
Thanks for the PR and sorry for the late response.
I think, there are a couple of things, we need to fix, see my comments.
Apart from that:
- We probably should introduce a new kotlin language version 2.2.0. This would be just for information purposes. In Java, we actually fail to parse code, that as newer language constructs than the select version allows, but I think, this would go too far for Kotlin. So, regardless of the language version, the parser behaves the same. It is just to document, we support (partially) version 2.2.0 now.
- The language feature is officially called "Multidollar interpolation: improved handling of $ in string literals". And that's the only language feature, that will be added by this PR. Note: it was preview in 2.1.0 and stabilized in 2.2.0. To make this clear, we should document this also on https://github.com/pmd/pmd/blob/main/docs/pages/pmd/languages/kotlin.md . Something along the lines: "PMDs parser lacks behind ... the following features are supported... other features of 2.0.0, 2.1.0, 2.2.0, 2.3.0 are not supported. PRs are welcome."
- Unfortunately, there is currently no official antlr grammar anymore, see https://youtrack.jetbrains.com/issue/KT-84579/Outdated-ANTLR-grammar-in-Kotlin-specification - but the best overview about the language features I found, is this one: https://kotlinlang.org/docs/kotlin-language-features-and-proposals.html#stable . So, it seems we need to keep our grammar up to date on our own unfortunately.
|
@adangel thanks for your review! Will start working on the improvements. |
Thanks for asking jetbrains youtrack! Hopefully they are able to sync. Did you also have a look at the two alternative grammars here: https://github.com/antlr/grammars-v4/tree/master/kotlin? |
I didn't have a detailed look. But I guess, we should stick to the grammar we have - if we switch the entire grammar, there might be a risk that all custom rules need to be rewritten, if the parser outputs a different parse tree. What I would suggest is this:
In general, I wouldn't expect an updated, official upstream grammar from Jetbrains soon. AFAIK, they've rewritten their parser/compiler (see K2) and it looks like, there is no formal grammar used anywhere anymore. If it comes, we need to see, whether we are still able to use it, since we are now implementing our own grammar. |
…de. Prevent NullPointerException in getTextRegion()
…tead of just printing to standard err so test case fails when parsing fails.
…keep behaviour same. Use custom parser that throws ParserException for running tests and getting notified for parse issues.
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| BestPractice | 6 medium |
🟢 Metrics 11 complexity · 1 duplication
Metric Results Complexity 11 Duplication 1
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
|
Hi @adangel, I requested a new review of this PR via the button on top. Can you have another look please and I think the process is that you close the conversations that are ok? |
|
Hello @adangel, can you please have a look at the updates on your review comments? (Or should I maybe turn it to "resolved" myself?) |
adangel
left a comment
There was a problem hiding this comment.
Thanks, looks good. You can fix the minor things I mentioned, otherwise I'll do it myself.
I won't have time to merge it before next week though.
added refererence to kotlin feature and fix a typo Co-authored-by: Andreas Dangel <[email protected]>
…Test for future regression tests
|
@adangel please have another look at the requested changes above, there is a remaining question about the toString(): leave it as is in this PR or revert back to Object.toString()? |
I've looked at it and yes, seems we need it. I'll add it to AntlrTerminalPmdAdapter as well, as there we are missing the delegation to pmdNode - then we get also a nice reminder "!debug only!" for terminals.
Actually, some nodes are created by the parser, even if there are no tokens/terminals. E.g. we always get a "packageHeader" node, even if there is no |
460a6d5 to
1328d18
Compare
Fix kotlin grammar for parsing multidollar interpolation
Newer syntax in Kotlin fails to parse with messages being written to standard err.
As reported: the multi dollar syntax gives errors during parsing. Example:
Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)Background and discussion
$${and$$${in example are turned into expressions now, so it is up to the rules to check the number of$$$.toString()now: needed code update inpmd-core/BaseAntlrInnerNode.javato make that possibleKotlinParsingHelper.