1 | <?php |
||
29 | final class AST implements Iterator |
||
30 | { |
||
31 | /** |
||
32 | * @var Parser |
||
33 | */ |
||
34 | public $parser; |
||
35 | |||
36 | /** |
||
37 | * @var Stack |
||
38 | */ |
||
39 | protected $stack; |
||
40 | |||
41 | /** |
||
42 | * @var mixed[] |
||
43 | */ |
||
44 | protected $variables = []; |
||
45 | |||
46 | /** |
||
47 | * @param Stack $stack |
||
48 | * @param Parser $parser |
||
49 | */ |
||
50 | 224 | public function __construct(Stack $stack, Parser $parser) |
|
51 | { |
||
52 | 224 | $this->stack = $stack; |
|
53 | 224 | $this->variables = $parser->variables; |
|
54 | 224 | $this->parser = $parser; |
|
55 | 224 | } |
|
56 | |||
57 | 214 | public function next() |
|
58 | { |
||
59 | 214 | $this->stack->next(); |
|
60 | 214 | } |
|
61 | |||
62 | 224 | public function valid() : bool |
|
66 | |||
67 | 222 | public function current() |
|
68 | { |
||
69 | 222 | $current = $this->stack->current(); |
|
70 | |||
71 | switch (true) { |
||
72 | default: |
||
73 | 212 | return $current; |
|
74 | 222 | case $current instanceof TokenString: |
|
75 | 220 | case $current instanceof TokenRegex: |
|
76 | 168 | $current = new NodeString($this); |
|
77 | 168 | break; |
|
78 | 218 | case $current instanceof TokenOpeningArray: |
|
79 | 52 | $current = new NodeArray($this); |
|
80 | 52 | break; |
|
81 | 218 | case $current instanceof TokenVariable: |
|
82 | 94 | $current = new NodeVariable($this); |
|
83 | 94 | break; |
|
84 | 216 | case $current instanceof TokenFunction: |
|
85 | 28 | $current = new NodeFunction($this); |
|
86 | 28 | break; |
|
87 | } |
||
88 | |||
89 | 206 | return $current->getNode(); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @codeCoverageIgnore |
||
94 | */ |
||
95 | public function key() : int |
||
96 | { |
||
97 | return $this->stack->key(); |
||
98 | } |
||
99 | |||
100 | 224 | public function rewind() |
|
101 | { |
||
102 | 224 | $this->stack->rewind(); |
|
103 | 224 | } |
|
104 | |||
105 | /** |
||
106 | * @throws Exceptions\ParserException |
||
107 | */ |
||
108 | 94 | public function getVariable(string $name) : BaseToken |
|
123 | |||
124 | 206 | public function getStack() : Stack |
|
125 | { |
||
126 | 206 | return $this->stack; |
|
127 | } |
||
128 | } |
||
129 |