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
235 changes: 235 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -903,16 +903,64 @@ nodes:
fields:
- name: constant
type: node?
kind:
- ConstantPathNode
- ConstantReadNode
comment: |
Represents the optional constant preceding the Array

foo in Bar[]
^^^

foo in Bar[1, 2, 3]
^^^

foo in Bar::Baz[1, 2, 3]
^^^^^^^^
- name: requireds
type: node[]
comment: |
This represents all the entries in case there is no wildcard, or the entries up to the wildcard in case there is a single wildcard.

foo in 1, 2, 3
^^^^^^^

foo in 1, 2, *
^^^^
- name: rest
type: node?
comment: |
This represents the wildcard, which can be unnamed.

foo in 1, 2, *
^

foo in 1, 2, *a, 3
^^
- name: posts
type: node[]
comment: |
This represents all the entries after the wildcard.

foo in *, 1, 2, 3
^^^^^^^

foo in 1, 2, *, 3, 4
^^^^
- name: opening_loc
type: location?
comment: |
The location of the opening brace.

foo = [1, 2]
^
- name: closing_loc
type: location?
comment: |
The location of the closing brace.

foo = [1, 2]
^
comment: |
Represents an array pattern in pattern matching.

Expand Down Expand Up @@ -1833,16 +1881,64 @@ nodes:
fields:
- name: constant
type: node?
kind:
- ConstantPathNode
- ConstantReadNode
comment: |
Represents the optional constant preceding the pattern

foo in Foo(*bar, baz, *qux)
^^^
- name: left
type: node
comment: |
Represents the first wildcard node in the pattern.

foo in *bar, baz, *qux
^^^^

foo in Foo(*bar, baz, *qux)
^^^^
- name: requireds
type: node[]
comment: |
Represents the nodes in between the wildcards.

foo in *bar, baz, *qux
^^^

foo in Foo(*bar, baz, 1, *qux)
^^^^^^
- name: right
type: node
comment: |
Represents the second wildcard node in the pattern.

foo in *bar, baz, *qux
^^^^

foo in Foo(*bar, baz, *qux)
^^^^
- name: opening_loc
type: location?
comment: |
The location of the openingbrace.

foo in [*bar, baz, *qux]
^

foo in Foo(*bar, baz, *qux)
^
- name: closing_loc
type: location?
comment: |
The location of the closing brace.

foo in [*bar, baz, *qux]
^

foo in Foo(*bar, baz, *qux)
^
comment: |
Represents a find pattern in pattern matching.

Expand All @@ -1854,6 +1950,9 @@ nodes:

foo in Foo(*bar, baz, *qux)
^^^^^^^^^^^^^^^^^^^^

foo => *bar, baz, *qux
^^^^^^^^^^^^^^^
- name: FlipFlopNode
fields:
- name: flags
Expand Down Expand Up @@ -2112,18 +2211,61 @@ nodes:
fields:
- name: constant
type: node?
kind:
- ConstantPathNode
- ConstantReadNode
comment: |
Represents the optional constant preceding the Hash.

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

foo => Bar::Baz[a: 1, b: 2]
^^^^^^^^
- name: elements
type: node[]
kind: AssocNode
comment: |
Represents the explicit named hash keys and values.

foo => { a: 1, b:, ** }
^^^^^^^^
- name: rest
type: node?
kind:
- AssocSplatNode
- NoKeywordsParameterNode
comment: |
Represents the rest of the Hash keys and values. This can be named, unnamed, or explicitly forbidden via `**nil`, this last one results in a `NoKeywordsParameterNode`.

foo => { a: 1, b:, **c }
^^^

foo => { a: 1, b:, ** }
^^

foo => { a: 1, b:, **nil }
^^^^^
- name: opening_loc
type: location?
comment: |
The location of the opening brace.

foo => { a: 1 }
^

foo => Bar[a: 1]
^
- name: closing_loc
type: location?
comment: |
The location of the closing brace.

foo => { a: 1 }
^

foo => Bar[a: 1]
^
comment: |
Represents a hash pattern in pattern matching.

Expand All @@ -2132,6 +2274,12 @@ nodes:

foo => { a: 1, b: 2, **c }
^^^^^^^^^^^^^^^^^^^

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

foo in { a: 1, b: 2 }
^^^^^^^^^^^^^^
- name: IfNode
fields:
- name: if_keyword_loc
Expand Down Expand Up @@ -2768,6 +2916,9 @@ nodes:

foo, bar = baz
^^^ ^^^

foo => baz
^^^
- name: LocalVariableWriteNode
fields:
- name: name
Expand Down Expand Up @@ -2856,10 +3007,64 @@ nodes:
fields:
- name: value
type: node
comment: |
Represents the left-hand side of the operator, and can be any kind of Ruby expression.

foo => bar
^^^
- 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 => *, 1, *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[**]

To use any variable that needs run time evaluation, pinning is required. This results in a `PinnedVariableNode`

foo => ^a
^^

Similar, any expression can be used with pinning. This results in a `PinnedExpressionNode`.

foo => ^(a + 1)

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

foo => CONST
- name: operator_loc
type: location
comment: |
The location of the operator.

foo => bar
^^
comment: |
Represents the use of the `=>` operator.

Expand Down Expand Up @@ -3273,12 +3478,32 @@ nodes:
fields:
- name: expression
type: node
comment: |
The expression used in the pinned expression

foo in ^(bar)
^^^
- name: operator_loc
type: location
comment: |
The location of the `^` operator

foo in ^(bar)
^
- name: lparen_loc
type: location
comment: |
The location of the opening parenthesis.

foo in ^(bar)
^
- name: rparen_loc
type: location
comment: |
The location of the closing parenthesis.

foo in ^(bar)
^
comment: |
Represents the use of the `^` operator for pinning an expression in a pattern matching expression.

Expand All @@ -3288,8 +3513,18 @@ nodes:
fields:
- name: variable
type: node
comment: |
The variable used in the pinned expression

foo in ^bar
^^^
- name: operator_loc
type: location
comment: |
The location of the `^` operator

foo in ^bar
^
comment: |
Represents the use of the `^` operator for pinning a variable in a pattern matching expression.

Expand Down
Loading