Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated properties for non-nullable children should not be declared nullable #84

Open
hakanai opened this issue Oct 18, 2022 · 6 comments

Comments

@hakanai
Copy link

hakanai commented Oct 18, 2022

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.

@lppedd
Copy link
Contributor

lppedd commented Dec 4, 2023

Did you ever find a way to get this one solved?

@lppedd
Copy link
Contributor

lppedd commented Dec 6, 2023

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.

@lppedd
Copy link
Contributor

lppedd commented Dec 6, 2023

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.

@ftomassetti
Copy link
Member

Agreed, let's close this one

@lppedd
Copy link
Contributor

lppedd commented Jan 9, 2024

@ftomassetti can we reopen? Working in this.

@ftomassetti
Copy link
Member

Sure, reopening

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants