Skip to content

Commit 8c286ee

Browse files
authored
Merge pull request #2914 from herwinw/document_pattern_match_field
Document some nodes related to pattern matching
2 parents fcf06b0 + 0d94291 commit 8c286ee

File tree

1 file changed

+195
-3
lines changed

1 file changed

+195
-3
lines changed

config.yml

Lines changed: 195 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -992,8 +992,19 @@ nodes:
992992
- name: constant
993993
type: node?
994994
kind:
995-
- ConstantReadNode
996995
- ConstantPathNode
996+
- ConstantReadNode
997+
comment: |
998+
Represents the optional constant preceding the Array
999+
1000+
foo in Bar[]
1001+
^^^
1002+
1003+
foo in Bar[1, 2, 3]
1004+
^^^
1005+
1006+
foo in Bar::Baz[1, 2, 3]
1007+
^^^^^^^^
9971008
- name: requireds
9981009
type: node[]
9991010
kind: pattern expression
@@ -2409,23 +2420,68 @@ nodes:
24092420
- name: constant
24102421
type: node?
24112422
kind:
2412-
- ConstantReadNode
24132423
- ConstantPathNode
2424+
- ConstantReadNode
2425+
comment: |
2426+
Represents the optional constant preceding the pattern
2427+
2428+
foo in Foo(*bar, baz, *qux)
2429+
^^^
24142430
- name: left
24152431
type: node
24162432
kind: SplatNode
2433+
comment: |
2434+
Represents the first wildcard node in the pattern.
2435+
2436+
foo in *bar, baz, *qux
2437+
^^^^
2438+
2439+
foo in Foo(*bar, baz, *qux)
2440+
^^^^
24172441
- name: requireds
24182442
type: node[]
24192443
kind: pattern expression
2444+
comment: |
2445+
Represents the nodes in between the wildcards.
2446+
2447+
foo in *bar, baz, *qux
2448+
^^^
2449+
2450+
foo in Foo(*bar, baz, 1, *qux)
2451+
^^^^^^
24202452
- name: right
24212453
type: node
24222454
kind:
24232455
- SplatNode
24242456
- on error: MissingNode
2457+
comment: |
2458+
Represents the second wildcard node in the pattern.
2459+
2460+
foo in *bar, baz, *qux
2461+
^^^^
2462+
2463+
foo in Foo(*bar, baz, *qux)
2464+
^^^^
24252465
- name: opening_loc
24262466
type: location?
2467+
comment: |
2468+
The location of the opening brace.
2469+
2470+
foo in [*bar, baz, *qux]
2471+
^
2472+
2473+
foo in Foo(*bar, baz, *qux)
2474+
^
24272475
- name: closing_loc
24282476
type: location?
2477+
comment: |
2478+
The location of the closing brace.
2479+
2480+
foo in [*bar, baz, *qux]
2481+
^
2482+
2483+
foo in Foo(*bar, baz, *qux)
2484+
^
24292485
comment: |
24302486
Represents a find pattern in pattern matching.
24312487
@@ -2437,6 +2493,9 @@ nodes:
24372493
24382494
foo in Foo(*bar, baz, *qux)
24392495
^^^^^^^^^^^^^^^^^^^^
2496+
2497+
foo => *bar, baz, *qux
2498+
^^^^^^^^^^^^^^^
24402499
- name: FlipFlopNode
24412500
flags: RangeFlags
24422501
fields:
@@ -2714,20 +2773,60 @@ nodes:
27142773
- name: constant
27152774
type: node?
27162775
kind:
2717-
- ConstantReadNode
27182776
- ConstantPathNode
2777+
- ConstantReadNode
2778+
comment: |
2779+
Represents the optional constant preceding the Hash.
2780+
2781+
foo => Bar[a: 1, b: 2]
2782+
^^^
2783+
2784+
foo => Bar::Baz[a: 1, b: 2]
2785+
^^^^^^^^
27192786
- name: elements
27202787
type: node[]
27212788
kind: AssocNode
2789+
comment: |
2790+
Represents the explicit named hash keys and values.
2791+
2792+
foo => { a: 1, b:, ** }
2793+
^^^^^^^^
27222794
- name: rest
27232795
type: node?
27242796
kind:
27252797
- AssocSplatNode
27262798
- NoKeywordsParameterNode
2799+
comment: |
2800+
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`.
2801+
2802+
foo => { a: 1, b:, **c }
2803+
^^^
2804+
2805+
foo => { a: 1, b:, ** }
2806+
^^
2807+
2808+
foo => { a: 1, b:, **nil }
2809+
^^^^^
27272810
- name: opening_loc
27282811
type: location?
2812+
comment: |
2813+
The location of the opening brace.
2814+
2815+
foo => { a: 1 }
2816+
^
2817+
2818+
foo => Bar[a: 1]
2819+
^
27292820
- name: closing_loc
27302821
type: location?
2822+
comment: |
2823+
The location of the closing brace.
2824+
2825+
foo => { a: 1 }
2826+
^
2827+
2828+
foo => Bar[a: 1]
2829+
^
27312830
comment: |
27322831
Represents a hash pattern in pattern matching.
27332832
@@ -2736,6 +2835,12 @@ nodes:
27362835
27372836
foo => { a: 1, b: 2, **c }
27382837
^^^^^^^^^^^^^^^^^^^
2838+
2839+
foo => Bar[a: 1, b: 2]
2840+
^^^^^^^^^^^^^^^
2841+
2842+
foo in { a: 1, b: 2 }
2843+
^^^^^^^^^^^^^^
27392844
- name: IfNode
27402845
fields:
27412846
- name: if_keyword_loc
@@ -3388,6 +3493,9 @@ nodes:
33883493
33893494
foo, bar = baz
33903495
^^^ ^^^
3496+
3497+
foo => baz
3498+
^^^
33913499
- name: LocalVariableWriteNode
33923500
fields:
33933501
- name: name
@@ -3478,11 +3586,65 @@ nodes:
34783586
- name: value
34793587
type: node
34803588
kind: non-void expression
3589+
comment: |
3590+
Represents the left-hand side of the operator.
3591+
3592+
foo => bar
3593+
^^^
34813594
- name: pattern
34823595
type: node
34833596
kind: pattern expression
3597+
comment: |
3598+
Represents the right-hand side of the operator. The type of the node depends on the expression.
3599+
3600+
Anything that looks like a local variable name (including `_`) will result in a `LocalVariableTargetNode`.
3601+
3602+
foo => a # This is equivalent to writing `a = foo`
3603+
^
3604+
3605+
Using an explicit `Array` or combining expressions with `,` will result in a `ArrayPatternNode`. This can be preceded by a constant.
3606+
3607+
foo => [a]
3608+
^^^
3609+
3610+
foo => a, b
3611+
^^^^
3612+
3613+
foo => Bar[a, b]
3614+
^^^^^^^^^
3615+
3616+
If the array pattern contains at least two wildcard matches, a `FindPatternNode` is created instead.
3617+
3618+
foo => *, 1, *a
3619+
^^^^^
3620+
3621+
Using an explicit `Hash` or a constant with square brackets and hash keys in the square brackets will result in a `HashPatternNode`.
3622+
3623+
foo => { a: 1, b: }
3624+
3625+
foo => Bar[a: 1, b:]
3626+
3627+
foo => Bar[**]
3628+
3629+
To use any variable that needs run time evaluation, pinning is required. This results in a `PinnedVariableNode`
3630+
3631+
foo => ^a
3632+
^^
3633+
3634+
Similar, any expression can be used with pinning. This results in a `PinnedExpressionNode`.
3635+
3636+
foo => ^(a + 1)
3637+
3638+
Anything else will result in the regular node for that expression, for example a `ConstantReadNode`.
3639+
3640+
foo => CONST
34843641
- name: operator_loc
34853642
type: location
3643+
comment: |
3644+
The location of the operator.
3645+
3646+
foo => bar
3647+
^^
34863648
comment: |
34873649
Represents the use of the `=>` operator.
34883650
@@ -3912,12 +4074,32 @@ nodes:
39124074
- name: expression
39134075
type: node
39144076
kind: non-void expression
4077+
comment: |
4078+
The expression used in the pinned expression
4079+
4080+
foo in ^(bar)
4081+
^^^
39154082
- name: operator_loc
39164083
type: location
4084+
comment: |
4085+
The location of the `^` operator
4086+
4087+
foo in ^(bar)
4088+
^
39174089
- name: lparen_loc
39184090
type: location
4091+
comment: |
4092+
The location of the opening parenthesis.
4093+
4094+
foo in ^(bar)
4095+
^
39194096
- name: rparen_loc
39204097
type: location
4098+
comment: |
4099+
The location of the closing parenthesis.
4100+
4101+
foo in ^(bar)
4102+
^
39214103
comment: |
39224104
Represents the use of the `^` operator for pinning an expression in a pattern matching expression.
39234105
@@ -3936,8 +4118,18 @@ nodes:
39364118
- NumberedReferenceReadNode # foo in ^$1
39374119
- ItLocalVariableReadNode # proc { 1 in ^it }
39384120
- on error: MissingNode # foo in ^Bar
4121+
comment: |
4122+
The variable used in the pinned expression
4123+
4124+
foo in ^bar
4125+
^^^
39394126
- name: operator_loc
39404127
type: location
4128+
comment: |
4129+
The location of the `^` operator
4130+
4131+
foo in ^bar
4132+
^
39414133
comment: |
39424134
Represents the use of the `^` operator for pinning a variable in a pattern matching expression.
39434135

0 commit comments

Comments
 (0)