1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
7
|
|
|
* @link https://github.com/nicoSWD |
8
|
|
|
* @author Nicolas Oelgart <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
namespace nicoSWD\Rules; |
11
|
|
|
|
12
|
|
|
use Closure; |
13
|
|
|
use Iterator; |
14
|
|
|
use nicoSWD\Rules\Core\CallableUserFunction; |
15
|
|
|
use nicoSWD\Rules\Tokens; |
16
|
|
|
use nicoSWD\Rules\AST\Nodes; |
17
|
|
|
|
18
|
|
|
class TokenStream implements Iterator |
19
|
|
|
{ |
20
|
|
|
/** @var Stack */ |
21
|
|
|
protected $stack; |
22
|
|
|
|
23
|
|
|
/** @var AST */ |
24
|
|
|
private $ast; |
25
|
|
|
|
26
|
220 |
|
public function create(Stack $stack, AST $ast) |
27
|
|
|
{ |
28
|
220 |
|
$stream = new self(); |
29
|
220 |
|
$stream->stack = $stack; |
30
|
220 |
|
$stream->ast = $ast; |
31
|
|
|
|
32
|
220 |
|
return $stream; |
33
|
|
|
} |
34
|
|
|
|
35
|
210 |
|
public function next() |
36
|
|
|
{ |
37
|
210 |
|
$this->stack->next(); |
38
|
210 |
|
} |
39
|
|
|
|
40
|
220 |
|
public function valid(): bool |
41
|
|
|
{ |
42
|
220 |
|
return $this->stack->valid(); |
43
|
|
|
} |
44
|
|
|
|
45
|
218 |
|
public function current() |
46
|
|
|
{ |
47
|
218 |
|
$current = $this->stack->current(); |
48
|
|
|
|
49
|
218 |
|
switch (get_class($current)) { |
50
|
|
|
default: |
51
|
208 |
|
return $current; |
52
|
218 |
|
case Tokens\TokenString::class: |
53
|
218 |
|
case Tokens\TokenEncapsedString::class: |
54
|
216 |
|
case Tokens\TokenRegex::class: |
55
|
168 |
|
$current = new Nodes\NodeString($this); |
56
|
168 |
|
break; |
57
|
214 |
|
case Tokens\TokenOpeningArray::class: |
58
|
52 |
|
$current = new Nodes\NodeArray($this); |
59
|
52 |
|
break; |
60
|
214 |
|
case Tokens\TokenVariable::class: |
61
|
90 |
|
$current = new Nodes\NodeVariable($this); |
62
|
90 |
|
break; |
63
|
212 |
|
case Tokens\TokenFunction::class: |
64
|
26 |
|
$current = new Nodes\NodeFunction($this); |
65
|
26 |
|
break; |
66
|
|
|
} |
67
|
|
|
|
68
|
202 |
|
return $current->getNode(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function key(): int |
72
|
|
|
{ |
73
|
|
|
return $this->stack->key(); |
74
|
|
|
} |
75
|
|
|
|
76
|
220 |
|
public function rewind() |
77
|
|
|
{ |
78
|
220 |
|
$this->stack->rewind(); |
79
|
220 |
|
} |
80
|
|
|
|
81
|
90 |
|
public function getVariable(string $name): Tokens\BaseToken |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
90 |
|
return $this->ast->getVariable($name); |
85
|
4 |
|
} catch (Exceptions\UndefinedVariableException $e) { |
86
|
4 |
|
throw Exceptions\ParserException::undefinedVariable($name, $this->stack->current()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
26 |
|
public function getFunction(string $functionName): Closure |
91
|
|
|
{ |
92
|
26 |
|
return $this->ast->getFunction($functionName); |
93
|
|
|
} |
94
|
|
|
|
95
|
108 |
|
public function getMethod(string $methodName, Tokens\BaseToken $token): CallableUserFunction |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
108 |
|
return $this->ast->getMethod($methodName, $token); |
99
|
4 |
|
} catch (Exceptions\UndefinedMethodException $e) { |
100
|
4 |
|
throw Exceptions\ParserException::undefinedMethod($methodName, $this->stack->current()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
202 |
|
public function getStack(): Stack |
105
|
|
|
{ |
106
|
202 |
|
return $this->stack; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|