1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
5
|
|
|
* @link https://github.com/nicoSWD |
6
|
|
|
* @author Nicolas Oelgart <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace nicoSWD\Rules; |
11
|
|
|
|
12
|
|
|
use Iterator; |
13
|
|
|
use nicoSWD\Rules\Tokens\{ |
14
|
|
|
BaseToken, |
15
|
|
|
TokenFactory, |
16
|
|
|
TokenFunction, |
17
|
|
|
TokenOpeningArray, |
18
|
|
|
TokenRegex, |
19
|
|
|
TokenString, |
20
|
|
|
TokenVariable |
21
|
|
|
}; |
22
|
|
|
use nicoSWD\Rules\AST\Nodes\{ |
23
|
|
|
NodeArray, |
24
|
|
|
NodeFunction, |
25
|
|
|
NodeString, |
26
|
|
|
NodeVariable |
27
|
|
|
}; |
28
|
|
|
|
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 |
63
|
|
|
{ |
64
|
224 |
|
return $this->stack->valid(); |
65
|
|
|
} |
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 |
109
|
|
|
{ |
110
|
94 |
|
if (!array_key_exists($name, $this->variables)) { |
111
|
4 |
|
$token = $this->stack->current(); |
112
|
|
|
|
113
|
4 |
|
throw new Exceptions\ParserException(sprintf( |
114
|
4 |
|
'Undefined variable "%s" at position %d on line %d', |
115
|
4 |
|
$name, |
116
|
4 |
|
$token->getPosition(), |
117
|
4 |
|
$token->getLine() |
118
|
|
|
)); |
119
|
|
|
} |
120
|
|
|
|
121
|
90 |
|
return TokenFactory::createFromPHPType($this->variables[$name]); |
122
|
|
|
} |
123
|
|
|
|
124
|
206 |
|
public function getStack() : Stack |
125
|
|
|
{ |
126
|
206 |
|
return $this->stack; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|