Skip to content

Add tests for trailing comma parsing #14742

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

Merged
merged 1 commit into from
Mar 24, 2022

Conversation

adampauls
Copy link
Contributor

@adampauls adampauls commented Mar 22, 2022

Fixes scala/bug#11900 in dotty. Forward port of scala/scala#8780.

@odersky put up an alternative fix in #14757. This PR just adds tests (some of which are from scala/scala#8780).

@@ -0,0 +1,79 @@

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test pulled over from scala/scala#8780.

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

I am afraid that confirms my doubt. This pretty much murders the grammar. It seems out of proportion to accept such a drastic complication for such a minor change like this.

In retrospect I think it was a mistake that I even accepted (grudgingly) to add trailing commas in the language. My suspicion was that the cost would be higher than the benefit and that has born out as far as I can see.

@som-snytt
Copy link
Contributor

Trailing commas improves the lives of millions of each day. The lexer compromise may have been most appropriate.

@adampauls
Copy link
Contributor Author

adampauls commented Mar 22, 2022

I'm afraid I don't follow. The current implementation of trailing commas murders the grammar even more. Significant indentation murders the grammar. Yet trailing commas aren't even documented at all in the grammar because they are hidden in the lexer. Significant indentation is documented with illegal EBNF. Unless I'm mistaken, if you held either the original trailing commas SIP or significant indentation to the same standard you are holding this change, they would never have happened.

Would it be alright if I just added a few lines of prose and a special symbol like <<< >>> for comma separated lists?

@adampauls adampauls force-pushed the no_weird_trailing_commas_simple branch from 243398d to 3b428f5 Compare March 22, 2022 15:07
@adampauls
Copy link
Contributor Author

adampauls commented Mar 22, 2022

I updated the grammar to be more readable and a only sort of abuse notation. I could go even further and define e.g.

<)> ::= [ ',' nl ] ')'

, which I think would be about what you would have to do to document the current behavior anyways.

@adampauls adampauls requested a review from odersky March 22, 2022 18:11
@som-snytt
Copy link
Contributor

som-snytt commented Mar 22, 2022

Speaking of a murder of grammars (like crows), I just wondered about trailing commas in optional braces, as I just had a need to write one.

enum X {
  case A,
       B,
       C,
}
enum Y:
  case A,
       B,
       C,
end Y

Have we joked, "comma chameleon" on this thread yet? like the '80s hit song.

@adampauls adampauls force-pushed the no_weird_trailing_commas_simple branch from 3b428f5 to e188590 Compare March 23, 2022 02:42
@adampauls
Copy link
Contributor Author

I went ahead and made some specific notation for delimiters in a comma-separated environment.

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

Whatever is done here is way too complex/expensive for the very small gains it provides. I am convinced this is a dead end.

There IS a way to improve the Scanner to be more discriminating when it accepts commas, since it now actually knows whether commas can be expected. It's a bit fishy since so far this info was only passed for error recover purposes. But i think it's better than trying to change the grammar and the parser. In a sense trailing commas is a hack (yes, that's the right term!) for usability. If we can confine that hack in the Scanner that's better than polluting everything else with it.

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

I think it would be good to add the tests to #14757 or make a separate PR with them once #14757 is merged.

@adampauls
Copy link
Contributor Author

adampauls commented Mar 23, 2022

Ah, I did not realize you viewed trailing commas in general as a hack -- that explains your reasoning here a bit better. I will just point out the core change is actually 2 fewer lines of code than the current implementation, but I can understand why you might want to hide a hack. I will add the tests once #14757 is merged.

@adampauls adampauls force-pushed the no_weird_trailing_commas_simple branch from e188590 to 284ac78 Compare March 24, 2022 05:57
@adampauls adampauls changed the title Trailing commas check in Parser Add tests for trailing comma parsing Mar 24, 2022
@adampauls adampauls requested a review from odersky March 24, 2022 06:40
Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

Thanks for the tests. Glad everything works as expected.

@odersky odersky merged commit 0e97414 into scala:main Mar 24, 2022
@som-snytt
Copy link
Contributor

som-snytt commented Mar 24, 2022

Thanks, this was educational. I backported the alternative coordination between parser and scanner on trailing comma to Scala 2. The only thing that regressed (edit: so far) was a quasiquote syntax error message that had progressed two years ago.

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

Successfully merging this pull request may close these issues.

Trailing commas in function bodies and constructors
4 participants