Strumenta / antlr-kotlin

Support for Kotlin as a target for ANTLR 4
Apache License 2.0
227 stars 47 forks source link

wrong non-null assumption on unrollRecursionContexts() #60

Closed atsushieno closed 3 years ago

atsushieno commented 3 years ago

I have some syntax that causes run-time crash, saying:

java.lang.NullPointerException: null cannot be cast to non-null type org.antlr.v4.kotlinruntime.ParserRuleContext

image

(stacktrack omitted as they mostly mention my code.)

My syntax defines left-recursion like A : B | A B which I assume is the cause of the problem.

Here is the mentioned suspect in the trace:

https://github.com/Strumenta/antlr-kotlin/blob/0951069063d9bbe249eebb981925217a12f55f14/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/Parser.kt#L722

It assumes that context!!.readParent() returns non-null parent, but it actually returns null for my syntax, because the left recursion occurs at top level. Here is the screenshot on diagnosing that state:

image

Confirmed with 0951069 on Ubuntu 20.04, multiplatform (jvm at runtime)

I'm creating a one liner PR that fixes this issue that makes it not crash.

frett commented 3 years ago

I'm running into this error as well, any chance the PR can get reviewed and merged?

ftomassetti commented 3 years ago

The PR has been merged. Sorry for the delay