tree-sitter-java icon indicating copy to clipboard operation
tree-sitter-java copied to clipboard

Stricter declarations

Open jmsfltchr opened this issue 1 year ago • 1 comments

Description

Restrict top level declarations and the declarations permitted as statements.

In doing so we also provide improved support for unnamed classes at the top level, and we clearly define the only valid locations for import and package declarations as outlined in the Java Language Specification.

Motivation

My motivation for this PR is that I am using tree-sitter both for syntax highlighting and for code validation. A key issue that I encountered was that various types of declaration could occur very widely within the grammar because they are permitted within statements.

I believe these changes are actually very congruent with the rest of the grammar, because only in statement were all declarations allowed. In other cases such as _class_body_declaration, annotation_type_body, interface_body we specify the exact types of permitted inner declarations. We now extend this idea to disallow any declaration inside a statement, and only permit class_declaration inside a block.

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).

jmsfltchr avatar Jan 12 '24 13:01 jmsfltchr

Please let me know your feedback on this direction and then I'll get to fixing the tests :)

jmsfltchr avatar Jan 12 '24 13:01 jmsfltchr