fix(grammar): allow a qualified type name in a record pattern - #231
Open
akd-devvine wants to merge 1 commit into
Open
fix(grammar): allow a qualified type name in a record pattern#231akd-devvine wants to merge 1 commit into
akd-devvine wants to merge 1 commit into
Conversation
`record_pattern` only accepted `identifier`, `_reserved_identifier` and
`generic_type`, so a record pattern that names its record through an
enclosing type or package fails to parse:
case Outer.Created(String id) -> ...
if (o instanceof Outer.Created(String id)) { }
Because the prefix syntax is identical to a method call, the parser
commits to `method_invocation` and emits an ERROR inside its
`argument_list` rather than a `record_pattern`. The unqualified form
parses correctly, which makes the failure easy to miss.
JLS 21 §14.30.1 defines a record pattern's type as a ReferenceType, which
admits a qualified name, so this is valid Java that the grammar rejects.
Adding `scoped_type_identifier` to the existing choice fixes it. No new
conflicts are introduced -- the `[$.argument_list, $.record_pattern_body]`
and `scoped_type_identifier` conflict entries already cover the ambiguity
-- and the full corpus passes with a new test covering both the
`instanceof` and `switch` forms.
Found while parsing a Java 21 sealed-interface hierarchy whose switch
arms name nested records through their enclosing type.
|
Hi @akd-devvine, Thanks for working on this. This repository isn't actively maintained, so we have been maintaining a fork which has already integrated many improvements, including the one you are proposing here. If you encounter any issue switching to our fork, I would be happy to know what needs improving on our side. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
record_patternonly acceptedidentifier,_reserved_identifierandgeneric_type, so a record pattern that names its record through an enclosing type or package fails to parse:Because the prefix syntax is identical to a method call, the parser commits to
method_invocationand emits an ERROR inside itsargument_listrather than arecord_pattern. The unqualified form parses correctly, which makes the failure easy to miss.JLS 21 §14.30.1 defines a record pattern's type as a ReferenceType, which admits a qualified name, so this is valid Java that the grammar rejects.
Adding
scoped_type_identifierto the existing choice fixes it. No new conflicts are introduced -- the[$.argument_list, $.record_pattern_body]andscoped_type_identifierconflict entries already cover the ambiguity -- and the full corpus passes with a new test covering both theinstanceofandswitchforms.Found while parsing a Java 21 sealed-interface hierarchy whose switch arms name nested records through their enclosing type.