-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParserIterator.php
249 lines (213 loc) · 5 KB
/
ParserIterator.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
class ParserIterator implements Iterator
{
/**
* A counter for ::key()
*
* @var integer
*/
private $counter;
/**
* A stack of the key in the children array that points to the tip
*
* @var array
*/
private $key_stack;
/**
* A list of tag names to use with 'inclusive' and 'exclusive' iterations
*
* @var array
*/
private $list;
/**
* A stack of the parent tags to the current tag
*
* @var array
*/
private $parent_stack;
/**
* The parser object
*
* @var object
*/
private $parser;
/**
* The type of iteration to perform:
*
* - 'all' tags
* - 'block' level tags
* - 'inclusive' list of tags, defined by $this->list
* - 'exclusive' list of tags, defined by $this->list
*
* @var string
*/
private $type;
/**
* The reference to the current point in the parser tree
*
* @var stdClass
*/
private $tip;
/**
* Creates an iterator that does a depth-first traversal of the parse tree
*
* @param object $tree The stdClass object representing the root of the tree to parse, this is not included in the iteration
* @param string $type If 'all' tags should be iterated, 'block' level tags, direct 'child' tags, an 'inclusive' list of tags names or an 'exclusive' list of tag names
* @param array $list The list of tags to include or exclude based on the $type
* @return ParserIterator
*/
public function __construct($tree, $type='all', $list=array())
{
$this->list = (array) $list;
$this->tree = $tree instanceof WikiParser ? $tree->getTree() : $tree;
$this->type = $type;
}
/**
* Returns the current tag in the iteration of the parse tree
*
* @return stdClass The current tag object
*/
public function current()
{
return $this->tip;
}
/**
* Returns the depth in the tree of the current tag
*
* @return integer The depth of the current tag
*/
public function depth()
{
return count($this->parent_stack);
}
/**
* Returns the number of the current tag in the context of the current iteration
*
* @return integer The tag number
*/
public function key()
{
return $this->counter;
}
/**
* Moves to the next tag in the parse tree
*
* @return stdClass The next tag in the parse tree
*/
public function next()
{
$this->counter++;
$found_child = FALSE;
if (!empty($this->tip->children) && ($this->type != 'child' || !$this->parent_stack)) {
$key = 0;
if ($this->type == 'block') {
while ($key < count($this->tip->children)) {
if (WikiParser::$tags[$this->tip->children[$key]->name]['type'] == 'block') {
$found_child = TRUE;
break;
}
$key++;
}
} else {
$found_child = TRUE;
}
if ($found_child) {
$this->key_stack[] = $key;
$this->parent_stack[] = $this->tip;
$this->tip = $this->tip->children[$key];
}
}
if (!$found_child) {
if ($parent = $this->parent()) {
array_pop($this->parent_stack);
}
while (($key = array_pop($this->key_stack)) !== FALSE) {
$key += 1;
if (isset($parent->children[$key])) {
$found = FALSE;
if ($this->type == 'block') {
while ($key < count($parent->children)) {
if (WikiParser::$tags[$parent->children[$key]->name]['type'] == 'block') {
$found = TRUE;
break;
}
$key++;
}
} else {
$found = TRUE;
}
if ($found) {
$this->key_stack[] = $key;
$this->parent_stack[] = $parent;
$this->tip = $parent->children[$key];
break;
}
}
if (!$parent = $this->parent()) {
break;
} else {
array_pop($this->parent_stack);
}
}
if (!$parent) {
$this->tip = NULL;
}
}
while ($this->type == 'inclusive' && $this->current() && !in_array($this->current()->name, $this->list)) {
$this->next();
}
while ($this->type == 'exclusive' && $this->current() && in_array($this->current()->name, $this->list)) {
$this->next();
}
return $this->tip;
}
/**
* Gets the parent tag of the current tag - does not alter iteration
*
* @return stdClass The parent tag of the current tag
*/
public function parent()
{
if (!$this->parent_stack) {
return FALSE;
}
return $this->parent_stack[count($this->parent_stack)-1];
}
/**
* Gets the array of parent tags of the current tag - does not alter iteration
*
* @return array The parent tags of the current tag
*/
public function parents()
{
if (!$this->parent_stack) {
return array();
}
return $this->parent_stack;
}
/**
* Resets the iterator to the root of the parse tree
*
* @return void
*/
public function rewind()
{
$this->counter = -1;
$this->key_stack = array();
$this->parent_stack = array();
$this->tip = $this->tree;
$this->next();
}
/**
* Indicates if the iterator can be moved forward by ::next()
*
* @return boolean If the iterator has another tag
*/
public function valid()
{
if ($this->tip) {
return TRUE;
}
return FALSE;
}
}