1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Compiler\Grammar; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class LookaheadIterator |
14
|
|
|
*/ |
15
|
|
|
class LookaheadIterator extends \IteratorIterator |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Current key. |
19
|
|
|
* |
20
|
|
|
* @var mixed |
21
|
|
|
*/ |
22
|
|
|
protected $key = 0; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Current value. |
26
|
|
|
* |
27
|
|
|
* @var mixed |
28
|
|
|
*/ |
29
|
|
|
protected $current; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Whether the current element is valid or not. |
33
|
|
|
* |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $valid = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* LookaheadIterator constructor. |
40
|
|
|
* @param iterable $iterator |
41
|
|
|
*/ |
42
|
|
|
public function __construct(iterable $iterator) |
43
|
|
|
{ |
44
|
|
|
parent::__construct($this->toIterator($iterator)); |
45
|
|
|
$this->rewind(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param iterable $iterable |
50
|
|
|
* @return \Traversable |
51
|
|
|
*/ |
52
|
|
|
private function toIterator(iterable $iterable): \Traversable |
53
|
|
|
{ |
54
|
|
|
return $iterable instanceof \Traversable ? $iterable : new \ArrayIterator($iterable); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return the current element. |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
public function current() |
63
|
|
|
{ |
64
|
|
|
return $this->current; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return the key of the current element. |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function key() |
73
|
|
|
{ |
74
|
|
|
return $this->key; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Rewind the iterator to the first element. |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function rewind(): void |
83
|
|
|
{ |
84
|
|
|
$this->getInnerIterator()->rewind(); |
85
|
|
|
|
86
|
|
|
$this->next(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Move forward to next element. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function next(): void |
95
|
|
|
{ |
96
|
|
|
$innerIterator = $this->getInnerIterator(); |
97
|
|
|
$this->valid = $innerIterator->valid(); |
98
|
|
|
|
99
|
|
|
if ($this->valid === false) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->key = $innerIterator->key(); |
104
|
|
|
$this->current = $innerIterator->current(); |
105
|
|
|
|
106
|
|
|
$innerIterator->next(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Check if current position is valid. |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function valid(): bool |
115
|
|
|
{ |
116
|
|
|
return $this->valid; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Check whether there is a next element. |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function hasNext(): bool |
125
|
|
|
{ |
126
|
|
|
return $this->getInnerIterator()->valid(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get next value. |
131
|
|
|
* |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public function getNext() |
135
|
|
|
{ |
136
|
|
|
return $this->getInnerIterator()->current(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get next key. |
141
|
|
|
* |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
public function getNextKey() |
145
|
|
|
{ |
146
|
|
|
return $this->getInnerIterator()->key(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|