Skip to content
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

Document some nodes related to pattern matching #2914

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

herwinw
Copy link
Member

@herwinw herwinw commented Jun 28, 2024

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
Copy link
Member Author

herwinw commented Jun 28, 2024

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.

config.yml Outdated Show resolved Hide resolved
config.yml Outdated Show resolved Hide resolved
config.yml Outdated Show resolved Hide resolved
@herwinw herwinw force-pushed the document_pattern_match_field branch from 9b6df7a to 4281c68 Compare June 29, 2024 15:18
@herwinw herwinw force-pushed the document_pattern_match_field branch from 4281c68 to cc7fdf5 Compare July 2, 2024 17:18
Copy link
Collaborator

@kddnewton kddnewton left a comment

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
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
Copy link
Member Author

herwinw commented Jul 16, 2024

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
Collaborator

No problem take your time!

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