Skip to content

Document some nodes related to pattern matching#2914

Merged
kddnewton merged 7 commits into
ruby:mainfrom
herwinw:document_pattern_match_field
Sep 12, 2025
Merged

Document some nodes related to pattern matching#2914
kddnewton merged 7 commits into
ruby:mainfrom
herwinw:document_pattern_match_field

Conversation

@herwinw

@herwinw herwinw commented Jun 28, 2024

Copy link
Copy Markdown
Member

Part of #2123

This adds documentation for ArrayPatternNode, FindPatternNode, HashPatternNode, LocalVariableTargetNode (a small update) and MatchRequiredNode.

@herwinw
herwinw marked this pull request as draft June 28, 2024 12:21
@herwinw
herwinw force-pushed the document_pattern_match_field branch from ca609d2 to 94a709c Compare June 28, 2024 12:30
@herwinw

herwinw commented Jun 28, 2024

Copy link
Copy Markdown
Member Author

MatchRequiredNode has some additional information on what types of nodes it will generate:

- name: pattern
  type: node
  comment: |
    Represents the right-hand side of the operator. The type of the node depends on the expression.

    Anything that looks like a local variable name (including `_`) will result in a `LocalVariableTargetNode`.

        foo => a # This is equivalent to writing `a = foo`
               ^

    Using an explicit `Array` or combining expressions with `,` will result in a `ArrayPatternNode`. This can be preceded by a constant.

        foo => [a]
               ^^^

        foo => a, b
               ^^^^

        foo => Bar[a, b]
               ^^^^^^^^^

    If the array pattern contains at least two wildcard matches, a `FindPatternNode` is created instead.

        foo => *, *a
               ^^^^^

    Using an explicit `Hash` or a constant with square brackets and hash keys in the square brackets will result in a `HashPatternNode`.

        foo => { a: 1, b: }

        foo => Bar[a: 1, b:]

        foo => Bar[**]

    Anything else will result in the regular node for that expression, for example a `ConstantReadNode`.

        foo => CONST

But this information applies to other locations as well. It is recursively applied to all possible fields (e.g. something that looks like a variable name inside an ArrayPatternNode will result in a LocalVariableTargetNode. And this applies to all other pattern matching related parse nodes as well (like foo in [a]). There probably is a better way to document this than to copy-paste it everywhere or referring back to other nodes for details.
Marking this one as draft until this issue has been resolved/answered.

Comment thread config.yml Outdated
Comment thread config.yml Outdated
Comment thread config.yml Outdated
@herwinw
herwinw force-pushed the document_pattern_match_field branch 2 times, most recently from 4281c68 to cc7fdf5 Compare July 2, 2024 17:18

@kddnewton kddnewton left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This looks great, @herwinw is this good to go?

@kddnewton

Copy link
Copy Markdown
Collaborator

Oh sorry I didn't see your comment. Typically what I have done in those cases is put the documentation into the parsing_rules.md file and linked to it. Could you do the same?

@herwinw

herwinw commented Jul 16, 2024

Copy link
Copy Markdown
Member Author

I will have a look at this to finish this one up, but that's going to be this weekend (or maybe later), I'm a bit short on time lately.

@kddnewton

Copy link
Copy Markdown
Collaborator

No problem take your time!

@kddnewton
kddnewton force-pushed the document_pattern_match_field branch from cc7fdf5 to 12a85aa Compare September 12, 2025 17:08
@kddnewton
kddnewton force-pushed the document_pattern_match_field branch from 12a85aa to 0d94291 Compare September 12, 2025 17:10
@kddnewton
kddnewton marked this pull request as ready for review September 12, 2025 17:11
@kddnewton
kddnewton merged commit 8c286ee into ruby:main Sep 12, 2025
60 checks passed
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