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