antlr-kotlin
antlr-kotlin copied to clipboard
Generated properties for non-nullable children should not be declared nullable
For a rule like this:
number: nested=(INTEGER | FLOAT | PI | INFINITY);
There is no way with that grammar that the nested token could be null, but the property you get after compiling the grammar is:
public final var nested: Token?
So now I have to put !!
all over my code to assert something the grammar is already supposed to be doing for me.
The more insidious side of this issue is, I can't just put !!
on all of them, because some of them really are nullable. And the nullability may change from time to time as the grammar evolves. So it would be nice if the generated code matched the actual grammar.
Did you ever find a way to get this one solved?
The real answer is this is not possible, as the engine that looks for template parts does not differentiate between nullable or non-nullable tokens.
You should open an issue upstream on the ANTLR4 repository, as other languages like Swift are affected.
@ftomassetti imo even this one can be closed as we can't do anything.
To expand and add more context.
If we have a parser rule such as:
myRule
: TagOpen tagName=Body? ... # Body
The Swift target will generate:
public class BodyContext: MyRuleContext {
public var tagName: Token!
Note the !
. I suppose that trying to access it without checking for nil will throw.
And the same concept doesn't exist in Kotlin, I'd have to use non-null delegates, but still, not entirely correct.
Agreed, let's close this one
@ftomassetti can we reopen? Working in this.
Sure, reopening