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\Rule\TokenStream; |
11
|
|
|
|
12
|
|
|
use ArrayIterator; |
13
|
|
|
use Closure; |
14
|
|
|
use nicoSWD\Rule\Parser\Exception\ParserException; |
15
|
|
|
use nicoSWD\Rule\Grammar\CallableUserFunction; |
16
|
|
|
use nicoSWD\Rule\TokenStream\Token\BaseToken; |
17
|
|
|
|
18
|
|
|
class TokenStream extends ArrayIterator |
19
|
|
|
{ |
20
|
|
|
/** @var ArrayIterator */ |
21
|
|
|
private $stack; |
22
|
|
|
/** @var AST */ |
23
|
|
|
private $ast; |
24
|
|
|
|
25
|
218 |
|
public function __construct(ArrayIterator $stack, AST $ast) |
26
|
|
|
{ |
27
|
218 |
|
$this->stack = $stack; |
28
|
218 |
|
$this->ast = $ast; |
29
|
218 |
|
} |
30
|
|
|
|
31
|
206 |
|
public function next() |
32
|
|
|
{ |
33
|
206 |
|
$this->stack->next(); |
34
|
206 |
|
} |
35
|
|
|
|
36
|
218 |
|
public function valid(): bool |
37
|
|
|
{ |
38
|
218 |
|
return $this->stack->valid(); |
39
|
|
|
} |
40
|
|
|
|
41
|
216 |
|
public function current(): BaseToken |
42
|
|
|
{ |
43
|
216 |
|
return $this->getCurrentToken()->createNode($this); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function key(): int |
47
|
|
|
{ |
48
|
|
|
return $this->stack->key(); |
49
|
|
|
} |
50
|
|
|
|
51
|
218 |
|
public function rewind() |
52
|
|
|
{ |
53
|
218 |
|
$this->stack->rewind(); |
54
|
218 |
|
} |
55
|
|
|
|
56
|
200 |
|
public function getStack(): ArrayIterator |
57
|
|
|
{ |
58
|
200 |
|
return $this->stack; |
59
|
|
|
} |
60
|
|
|
|
61
|
216 |
|
private function getCurrentToken(): BaseToken |
62
|
|
|
{ |
63
|
216 |
|
return $this->stack->current(); |
64
|
|
|
} |
65
|
|
|
|
66
|
90 |
|
public function getVariable(string $variableName): BaseToken |
67
|
|
|
{ |
68
|
|
|
try { |
69
|
90 |
|
return $this->ast->getVariable($variableName); |
70
|
4 |
|
} catch (Exception\UndefinedVariableException $e) { |
71
|
4 |
|
throw ParserException::undefinedVariable($variableName, $this->getCurrentToken()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
26 |
|
public function getFunction(string $functionName): Closure |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
26 |
|
return $this->ast->getFunction($functionName); |
79
|
4 |
|
} catch (Exception\UndefinedFunctionException $e) { |
80
|
4 |
|
throw ParserException::undefinedFunction($functionName, $this->getCurrentToken()); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
108 |
|
public function getMethod(string $methodName, BaseToken $token): CallableUserFunction |
85
|
|
|
{ |
86
|
|
|
try { |
87
|
108 |
|
return $this->ast->getMethod($methodName, $token); |
88
|
4 |
|
} catch (Exception\UndefinedMethodException $e) { |
89
|
4 |
|
throw ParserException::undefinedMethod($methodName, $this->getCurrentToken()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|