Skip to content

Commit

Permalink
Document named captures
Browse files Browse the repository at this point in the history
  • Loading branch information
Earlopain authored and marcandre committed Oct 16, 2024
1 parent 7414167 commit cbd766e
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/modules/ROOT/pages/node_pattern.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Ruby code with a method call with two integer literals as arguments, `foo(1, 2)`
To match just those method calls where the first argument is a literal `1`, use `(send nil? :foo (int 1) int)`.
Any child that is a node can be a target for nested matching.

[#any-single-node]
== `_` for any single node

`_` will check if there's something present in the specific position, no matter the
Expand All @@ -135,6 +136,22 @@ value:
* `(int _ _)` will not match because `int` types have just one child that
contains the value.

You can specify a name to make a more descriptive reference:

----
(send nil? _method_name)
----

You can also reference them later in the pattern to match against the value that was previously captured:

----
(pair
(_ _key)
(_ _key))
----

`{ a: :a }` will match, while `{ a: :b }` won't.

== `+...+` for several subsequent nodes

Where `_` matches any single node, `+...+` matches any number of nodes.
Expand Down Expand Up @@ -314,6 +331,17 @@ The following pattern will have two captures, both arrays:
(send nil? $int+ (send $...))
----

When capturing <<any-single-node, any single node>>, you can reference the value you previously captured.

The following pattern will have one capture:

----
(pair
(_ $_key)
(_ _key))
----


== `^` for parent

One may use the `^` character to check against a parent.
Expand Down Expand Up @@ -347,6 +375,7 @@ we can write:
This would match both of these methods `foo` and `bar`, even though
these `return` for `foo` and `bar` are not at the same level.

[source,ruby]
----
def foo # (def :foo
return 42 # (args)
Expand Down

0 comments on commit cbd766e

Please sign in to comment.