Debian Codegraph @

vendor/mck89/peast/doc/tree-traversing.md

raw
1 Tree Traversing
2 ==========
3 **_From version 1.1_**
4
5 To traverse an AST generated by Peast you can use the **Traverser** class:
6 ```php
7 //Generate the AST
8 $ast = Peast\Peast::latest($source, $options)->parse();
9 //Set up the Traverser
10 $traverser = new Peast\Traverser;
11 $traverser->addFunction(function ($node) {
12 //Do something with the current node
13 });
14 //Start traversing
15 $traverser->traverse($ast);
16 ```
17
18 You can add one or more functions to the Traverser using the `addFunction` method. These functions receive the current traversed node while the Traverser runs on the tree.
19
20 The `traverse` method runs the traversing starting from the given node.
21
22 Options
23 -------------
24
25 **_From version 1.12_**
26 The Traverser class constructor takes an optional associative array of options.
27 Available options are:
28 * "skipStartingNode": by default the traversing begins with the starting node passed to the `traverse` method. If this option is set to true the starting node will be ignored.
29 * "passParentNode": by default the functions added to the Traverser receive the traversed node as the only argument. if this option is set to true the node's parent node will be passed as second argument to all the functions. Note that the parent node is calculated during traversing, so for the starting node it will always be null.
30
31 Tree manipulation
32 -------------
33 Functions added to the Traverser instance can alter the tree during the traversing by modifying, replacing or removing the node they receive and they can also control the behaviour of the Traverser to make it stop or skip the node's children.
34
35 Traverser class provides some constants to perform these operations:
36 * `Traverser::REMOVE_NODE`: removes the node
37 * `Traverser::DONT_TRAVERSE_CHILD_NODES`: skips the node's children
38 * `Traverser::STOP_TRAVERSING`: stops the traversing
39
40 The action that will be executed depends on the value returned by the functions.
41
42 If you want to remove a node the function must return the `Traverser::REMOVE_NODE` constant:
43 ```php
44 $traverser->addFunction(function ($node) {
45 //Remove all literal nodes
46 if ($node->getType() === "Literal") {
47 return Peast\Traverser::REMOVE_NODE;
48 }
49 });
50 ```
51
52 If you want to control the traversing you can return `Traverser::DONT_TRAVERSE_CHILD_NODES` or `Traverser::STOP_TRAVERSING` constants:
53 ```php
54 $traverser->addFunction(function ($node) {
55 //Skip nodes inside array expressions
56 if ($node->getType() === "ArrayExpression") {
57 return Peast\Traverser::DONT_TRAVERSE_CHILD_NODES;
58 }
59 //Stop the traversing when an identifier named "stop" is found
60 elseif ($node->getType() === "Identifier" && $node->getName() === "stop") {
61 return Peast\Traverser::STOP_TRAVERSING;
62 }
63 });
64 ```
65
66 You can also return a combination of these constants:
67 ```php
68 $traverser->addFunction(function ($node) {
69 //Remove the string "test" and then stop the traversing
70 if ($node->getType() === "Literal" && $node->getValue() === "test") {
71 return Peast\Traverser::REMOVE_NODE | Peast\Traverser::STOP_TRAVERSING;
72 }
73 });
74 ```
75
76 To replace a node you must return the replacement node:
77 ```php
78 $traverser->addFunction(function ($node) {
79 //Replace the number 2 with 1
80 if ($node->getType() === "Literal" && $node->getValue() === 2) {
81 $literal = new \Peast\Syntax\Node\NumericLiteral();
82 return $literal->setValue(1);
83 }
84 });
85 ```
86
87 You can also control the traversing and replace a node in the same function:
88 ```php
89 $traverser->addFunction(function ($node) {
90 //Replace an array expression with an object expression without traversing its children
91 if ($node->getType() === "ArrayExpression") {
92 $obj = new \Peast\Syntax\Node\ObjectExpression();
93 return array($obj, Peast\Traverser::DONT_TRAVERSE_CHILD_NODES);
94 }
95 });
96 ```
97
98 If the function returns any other value or nothing, no action will be executed and you can modify nodes without altering the tree structure:
99 ```php
100 use Peast\Syntax\Node\StringLiteral;
101 $traverser->addFunction(function ($node) {
102 //Make all the strings uppercase
103 if ($node->getType() === "Literal" && $node instanceof StringLiteral) {
104 $node->setValue(strtoupper($node->getValue()));
105 }
106 });
107 ```
108
109 Shortcut method
110 -------------
111
112 Every node implements a `traverse` method as a shortcut to initialize a traverser on itself:
113 ```php
114 $traversingFn = function ($node) { /* ... */ };
115 //Traverse $ast node
116 $ast->traverse($traversingFn, $options);
117 //Equivalent to
118 $traverser = new Peast\Traverser;
119 $traverser->addFunction($traversingFn)->traverse($ast);
120 ```