Skip to content

Conversation

@bkramer-relyance
Copy link
Contributor

@bkramer-relyance bkramer-relyance commented Sep 13, 2022

This PR continues the work of supporting Java records, letting us:

  1. define a record that’s nested in an interface, not just a class.
  2. inherit from an interface
  3. define a compact constructor

See https://docs.oracle.com/en/java/javase/18/language/records.html.

Caveat: to reduce the complexity of the grammar/parser, I defined compact constructors to be accepted at any place one might find a normal constructor, making the tree-sitter parser more permissive than the spec. I'm going by the assumption that the philosophy of tree-sitter is to understand the correct code, not to enforce it. (I could have made the compact_constructor_declaration rule by allowing methods to have no arguments, but felt that was taking it too far.) I'm open to some guidance on this topic.

Because it was convenient for me (fewer versions of generated code to keep track of), I am also adding in this PR support for the optional final modifier after instanceof. I can't find the spec for this, but here's some communication concerning it: checkstyle/checkstyle#9897

All of these issues were discovered in the Elasticsearch repo.

Checklist:

  • All tests pass in CI.
  • There are sufficient tests for the new fix/feature.
  • Grammar rules have not been renamed unless absolutely necessary.
  • The conflicts section hasn't grown too much.
  • The parser size hasn't grown too much (check the value of STATE_COUNT in src/parser.c).

Question: while I was working out the grammar updates for records, I noticed that there is the inclusion of record as a "reserved identifier," along with just a couple of others. This seems pretty arbitrary and I'm wondering if they are to deal with cases where the Java language permits these keywords to be repurposed as identifiers, like with lambda arguments. I've seen this in one of the corpus tests, as well as for module in one of the Github repos we use to test. I also see a current parse failure in the Ghidra repo where sealed appears as a variable name undergoing assignment. So what's the general story here? Are we just opportunistically adding reserved identifiers to deal with these discoveries, and if so, isn't the naming backward (we are not "reserving keywords" but rather "un-reserving them.")?

@bkramer-relyance bkramer-relyance changed the title Java parser: fix issues with records, support instanceof final fix issues with records, support instanceof final Sep 13, 2022
@bkramer-relyance
Copy link
Contributor Author

bkramer-relyance commented Sep 13, 2022

@aryx or @maxbrunsfeld

Requesting CI testing and review. Thank you.

grammar.js Outdated
'open',
'module',
'record'
'record',
Copy link
Contributor Author

@bkramer-relyance bkramer-relyance Sep 13, 2022

Choose a reason for hiding this comment

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

Accidentally left in this trailing ,. I'll remove this when I make an update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@aryx
Copy link
Contributor

aryx commented Sep 18, 2022

@bmahe is this related to your recent PR?

Copy link
Contributor

@aryx aryx left a comment

Choose a reason for hiding this comment

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

LGTM.
@bmahe ?

@aryx
Copy link
Contributor

aryx commented Sep 18, 2022

Could you rebase to solve the conflicts?
I'll merge after that.

@bkramer-relyance bkramer-relyance force-pushed the bkramer/records_and_instanceof_final branch from 2d8084f to 9fb5d40 Compare September 18, 2022 22:29
@bkramer-relyance bkramer-relyance changed the title fix issues with records, support instanceof final Fix issues with records and support instanceof final Sep 18, 2022
@bkramer-relyance bkramer-relyance force-pushed the bkramer/records_and_instanceof_final branch from 9fb5d40 to ee2d345 Compare September 18, 2022 22:32
@bkramer-relyance
Copy link
Contributor Author

@aryx I rebased my branch on master and force pushed. This could use another round of testing before merging. Thank you.

@aryx aryx merged commit 09d650d into tree-sitter:master Sep 19, 2022
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