nicoSWD /
php-rule-parser
| 1 | <?php declare(strict_types=1); |
||
| 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 | namespace nicoSWD\Rule\TokenStream; |
||
| 9 | |||
| 10 | use ArrayIterator; |
||
| 11 | use Closure; |
||
| 12 | use nicoSWD\Rule\Parser\Exception\ParserException; |
||
| 13 | use nicoSWD\Rule\Grammar\CallableUserFunctionInterface; |
||
| 14 | use nicoSWD\Rule\TokenStream\Token\BaseToken; |
||
| 15 | |||
| 16 | class TokenStream extends ArrayIterator |
||
| 17 | { |
||
| 18 | public function __construct( |
||
| 19 | private ArrayIterator $stack, |
||
| 20 | private AST $ast |
||
| 21 | ) { |
||
| 22 | } |
||
| 23 | 296 | ||
| 24 | public function next(): void |
||
| 25 | 296 | { |
|
| 26 | 296 | $this->stack->next(); |
|
| 27 | 296 | } |
|
| 28 | |||
| 29 | 236 | public function valid(): bool |
|
| 30 | { |
||
| 31 | 236 | return $this->stack->valid(); |
|
| 32 | 236 | } |
|
| 33 | |||
| 34 | 282 | /** @throws ParserException */ |
|
| 35 | public function current(): BaseToken |
||
| 36 | 282 | { |
|
| 37 | return $this->getCurrentToken()->createNode($this); |
||
| 38 | } |
||
| 39 | |||
| 40 | 280 | public function key(): int |
|
| 41 | { |
||
| 42 | 280 | return $this->stack->key(); |
|
| 43 | } |
||
| 44 | |||
| 45 | 2 | public function rewind(): void |
|
| 46 | { |
||
| 47 | 2 | $this->stack->rewind(); |
|
| 48 | } |
||
| 49 | |||
| 50 | 282 | /** @return ArrayIterator<BaseToken> */ |
|
| 51 | public function getStack(): ArrayIterator |
||
| 52 | 282 | { |
|
| 53 | 282 | return $this->stack; |
|
| 54 | } |
||
| 55 | |||
| 56 | 266 | private function getCurrentToken(): BaseToken |
|
| 57 | { |
||
| 58 | 266 | return $this->stack->current(); |
|
| 59 | } |
||
| 60 | |||
| 61 | 286 | /** @throws ParserException */ |
|
| 62 | public function getVariable(string $variableName): BaseToken |
||
| 63 | 286 | { |
|
| 64 | try { |
||
| 65 | return $this->ast->getVariable($variableName); |
||
| 66 | } catch (Exception\UndefinedVariableException) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 67 | 144 | throw ParserException::undefinedVariable($variableName, $this->getCurrentToken()); |
|
| 68 | } |
||
| 69 | } |
||
| 70 | 144 | ||
| 71 | 6 | /** @throws ParserException */ |
|
| 72 | 6 | public function getFunction(string $functionName): Closure |
|
| 73 | { |
||
| 74 | try { |
||
| 75 | return $this->ast->getFunction($functionName); |
||
| 76 | } catch (Exception\UndefinedFunctionException) { |
||
| 77 | 30 | throw ParserException::undefinedFunction($functionName, $this->getCurrentToken()); |
|
| 78 | } |
||
| 79 | } |
||
| 80 | 30 | ||
| 81 | 6 | /** @throws ParserException */ |
|
| 82 | 6 | public function getMethod(string $methodName, BaseToken $token): CallableUserFunctionInterface |
|
| 83 | { |
||
| 84 | try { |
||
| 85 | return $this->ast->getMethod($methodName, $token); |
||
| 86 | } catch (Exception\UndefinedMethodException) { |
||
| 87 | 170 | throw ParserException::undefinedMethod($methodName, $this->getCurrentToken()); |
|
| 88 | } catch (Exception\ForbiddenMethodException) { |
||
| 89 | throw ParserException::forbiddenMethod($methodName, $this->getCurrentToken()); |
||
| 90 | 170 | } |
|
| 91 | 40 | } |
|
| 92 | } |
||
| 93 |