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

allow type params before navigation suffix #79

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

brandonspark
Copy link
Contributor

In my pursuits running the Kotlin parser on many open-source repos, I've noticed that a very common we cannot parse is expressions of the form Foo<*>::bar. It seems that this is an example of suffixing an expression with type parameters, and then doing a navigation suffix.

Unfortunately, it seems that naively writing the grammar with type parameters after arbitrary expressions is a surefire way to break comparison expressions. I decided to follow the same form as call_suffix, and instead allow type parameters in only a particular suffix, that being navigation suffixes.

Now, it works. Our parse rate went up like a percent as a result.

Test plan: Automated tests.

@fwcd fwcd added the grammar Related to the grammar label Mar 22, 2023
@RenFraser
Copy link

This is awesome @brandonspark. I sincerely hope that you keep contributing! I thought that this repo was losing traction but it seems I was wrong! Thank you @fwcd and @brandonspark! :)

@fwcd
Copy link
Owner

fwcd commented Mar 22, 2023

Thanks for investigating! Please rebase it onto main (as always), so we can run CI.

@@ -673,6 +674,8 @@ module.exports = grammar({
indexing_suffix: $ => seq("[", sep1($._expression, ","), "]"),

navigation_suffix: $ => seq(
// this introduces ambiguities with 'less than' for comparisons
optional($.type_arguments),
Copy link
Owner

Choose a reason for hiding this comment

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

Hm, I'm not quite sure how I feel about this workaround. While I understand that it's hard to parse, it seems to deviate quite a bit from the reference grammar and including the type arguments in the navigation suffix node (both of which are exposed), downstream consumers might have to rewrite the syntax tree quite a bit to get accurate results.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose it just comes down to how much you weigh being able to parse this versus the compromises we've already made with the syntax tree. For instance, this call_suffix workaround (which basically does the same thing, by embedding the optional type arguments in a different node of the tree) was worthy enough of being included, so I see this as adding just another case.

I can keep thinking about it, but this seems like a rather pervasive parsing issue, from the things I've seen, and I'm not sure if we can make something truly faithful to the original syntax specification. At the end of the day, the Kotlin grammar is a reference for specification, but implementation may face some difficulties emulating it.

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

Successfully merging this pull request may close these issues.

3 participants