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

Improve nullability support for generated parsers #142

Merged
merged 4 commits into from
Jan 10, 2024

Conversation

lppedd
Copy link
Contributor

@lppedd lppedd commented Jan 9, 2024

Improves type nullability support in generated parsers, and solves a part of #84.

In this case we are checking for the optional property - injected by the ANTLR code generator - on the ContextTokenGetterDecl and ContextRuleGetterDecl template parts.
Note that the generated methods now start with get* instead of find*.

The missing part is for TokenDecl. We would have to use the lateinit modifier, but I'm still unsure if it's appropriate.

@lppedd
Copy link
Contributor Author

lppedd commented Jan 9, 2024

@ftomassetti let me know what you think about this.

@lppedd lppedd marked this pull request as ready for review January 10, 2024 10:52
@lppedd
Copy link
Contributor Author

lppedd commented Jan 10, 2024

The missing part is for TokenDecl. We would have to use the lateinit modifier, but I'm still unsure if it's appropriate.

This can't be done at all, as it's then impossible to check if a lateinit variable has been initialized or not.

}
is ErrorNode -> return t.toString()
is TerminalNode -> return t.symbol.text!!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we write something along the lines of:

t.symbol.text ?: throw IllegalStateException("Token symbol text should not be null")

Wouldn't it be null for EOF?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's better, I'll commit the change.


if (symbol != null) {
return symbol.text!!
when (t) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we factor out the return?

Copy link
Contributor Author

@lppedd lppedd Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot as that's a non-local return (when inside of an if). Will be supported starting from Kotlin 2.0.

Copy link
Contributor Author

@lppedd lppedd Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, wrong answer. We cannot, as it requires an else branch to return something. We don't want that, we want it to continue over the rest of the method, as it was doing with if-elseif.

@ftomassetti
Copy link
Member

It looks good, but it is difficult to assess if the code work. I assume tests guarantee that to some extent. I just added minor comments

@lppedd
Copy link
Contributor Author

lppedd commented Jan 10, 2024

@ftomassetti haven't changed much so everything still work as before.

The "issue" here is the change in generated method names. This is a breaking change basically.

@ftomassetti
Copy link
Member

Thank you for your answers. Merging

@ftomassetti ftomassetti merged commit 6b5c3d2 into Strumenta:master Jan 10, 2024
6 checks passed
@lppedd lppedd deleted the refactor/nullability branch January 10, 2024 14:18
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

Successfully merging this pull request may close these issues.

2 participants