-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathTraverser.php
215 lines (198 loc) · 6.67 KB
/
Traverser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* This file is part of the Peast package
*
* (c) Marco Marchiò <[email protected]>
*
* For the full copyright and license information refer to the LICENSE file
* distributed with this source code
*/
namespace Peast;
/**
* Nodes traverser class
*
* @author Marco Marchiò <[email protected]>
*/
class Traverser
{
/**
* If a function return this value, the current node will be removed
*/
const REMOVE_NODE = 1;
/**
* If a function return this value, the current node's children won't be
* traversed
*/
const DONT_TRAVERSE_CHILD_NODES = 2;
/**
* If a function return this value, the traverser will stop running
*/
const STOP_TRAVERSING = 4;
/**
* Array of functions to call on each node
*
* @var array
*/
protected $functions = array();
/**
* Pass parent node flag
*
* @var bool
*/
protected $passParentNode = false;
/**
* Skip starting node flag
*
* @var bool
*/
protected $skipStartingNode = false;
/**
* Class constructor. Available options are:
* - skipStartingNode: if true the starting node will be skipped
* - passParentNode: if true the parent node of each node will be
* passed as second argument when the functions are called. Note
* that the parent node is calculated during traversing, so for
* the starting node it will always be null.
*
* @param array $options Options array
*/
public function __construct($options = array())
{
if (isset($options["passParentNode"])) {
$this->passParentNode = (bool) $options["passParentNode"];
}
if (isset($options["skipStartingNode"])) {
$this->skipStartingNode = (bool) $options["skipStartingNode"];
}
}
/**
* Adds a function that will be called for each node in the tree. The
* function will receive the current node as argument. The action that will
* be executed on the node by the traverser depends on the returned value
* of the function:
* - a node: it will replace the node with the returned one
* - a numeric value that is a combination of the constants defined in this
* class: it will execute the function related to each constant
* - an array where the first element is a node and the second element is a
* numeric value that is a combination of the constants defined in this
* class: it will replace the node with the returned one and it will
* execute the function related to each constant (REMOVE_NODE will be
* ignored since it does not make any sense in this case)
* - other: nothing
*
* @param callable $fn Function to add
*
* @return $this
*/
public function addFunction(callable $fn)
{
$this->functions[] = $fn;
return $this;
}
/**
* Starts the traversing
*
* @param Syntax\Node\Node $node Starting node
*
* @return Syntax\Node\Node
*/
public function traverse(Syntax\Node\Node $node)
{
if ($this->skipStartingNode) {
$this->traverseChildren($node);
} else {
$this->execFunctions($node);
}
return $node;
}
/**
* Executes all functions on the given node and, if required, starts
* traversing its children. The returned value is an array where the first
* value is the node or null if it has been removed and the second value is
* a boolean indicating if the traverser must continue the traversing or not
*
* @param Syntax\Node\Node $node Node
* @param Syntax\Node\Node|null $parent Parent node
*
* @return array
*/
protected function execFunctions($node, $parent = null)
{
$traverseChildren = true;
$continueTraversing = true;
foreach ($this->functions as $fn) {
$ret = $this->passParentNode ? $fn($node, $parent) : $fn($node);
if ($ret) {
if (is_array($ret) && $ret[0] instanceof Syntax\Node\Node) {
$node = $ret[0];
if (isset($ret[1]) && is_numeric($ret[1])) {
if ($ret[1] & self::DONT_TRAVERSE_CHILD_NODES) {
$traverseChildren = false;
}
if ($ret[1] & self::STOP_TRAVERSING) {
$continueTraversing = false;
}
}
} elseif ($ret instanceof Syntax\Node\Node) {
$node = $ret;
} elseif (is_numeric($ret)) {
if ($ret & self::DONT_TRAVERSE_CHILD_NODES) {
$traverseChildren = false;
}
if ($ret & self::STOP_TRAVERSING) {
$continueTraversing = false;
}
if ($ret & self::REMOVE_NODE) {
$node = null;
$traverseChildren = false;
break;
}
}
}
}
if ($traverseChildren && $continueTraversing) {
$continueTraversing = $this->traverseChildren($node);
}
return array($node, $continueTraversing);
}
/**
* Traverses node children. It returns a boolean indicating if the
* traversing must continue or not
*
* @param Syntax\Node\Node $node Node
*
* @return bool
*/
protected function traverseChildren(Syntax\Node\Node $node)
{
$continue = true;
foreach (Syntax\Utils::getNodeProperties($node, true) as $prop) {
$getter = $prop["getter"];
$setter = $prop["setter"];
$child = $node->$getter();
if (!$child) {
continue;
} elseif (is_array($child)) {
$newChildren = array();
foreach ($child as $c) {
if (!$c || !$continue) {
$newChildren[] = $c;
} else {
list($c, $continue) = $this->execFunctions($c, $node);
if ($c) {
$newChildren[] = $c;
}
}
}
$node->$setter($newChildren);
} else {
list($child, $continue) = $this->execFunctions($child, $node);
$node->$setter($child);
}
if (!$continue) {
break;
}
}
return $continue;
}
}