You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+22
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,7 @@ It is built with only standard library dependencies. It additionally ships with
29
29
-[pretty_print(q)](#pretty_printq)
30
30
-[to_json(*opts)](#to_jsonopts)
31
31
-[format(q)](#formatq)
32
+
-[construct_keys](#construct_keys)
32
33
-[Visitor](#visitor)
33
34
-[visit_method](#visit_method)
34
35
-[Language server](#language-server)
@@ -295,6 +296,27 @@ formatter.output.join
295
296
# => "1 + 1"
296
297
```
297
298
299
+
### construct_keys
300
+
301
+
Every node responds to `construct_keys`, which will return a string that contains a Ruby pattern-matching expression that could be used to match against the current node. It's meant to be used in tooling and through the CLI mostly.
302
+
303
+
```ruby
304
+
program =SyntaxTree.parse("1 + 1")
305
+
puts program.construct_keys
306
+
307
+
# SyntaxTree::Program[
308
+
# statements: SyntaxTree::Statements[
309
+
# body: [
310
+
# SyntaxTree::Binary[
311
+
# left: SyntaxTree::Int[value: "1"],
312
+
# operator: :+,
313
+
# right: SyntaxTree::Int[value: "1"]
314
+
# ]
315
+
# ]
316
+
# ]
317
+
# ]
318
+
```
319
+
298
320
## Visitor
299
321
300
322
If you want to operate over a set of nodes in the tree but don't want to walk the tree manually, the `Visitor` class makes it easy. `SyntaxTree::Visitor` is an implementation of the double dispatch visitor pattern. It works by the user defining visit methods that process nodes in the tree, which then call back to other visit methods to continue the descent. This is easier shown in code.
0 commit comments