Completed
Push — master ( b9bb4c...c9202e )
by Nico
01:58
created

TokenStream::getStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 234
    public function __construct(ArrayIterator $stack, AST $ast)
26
    {
27 234
        $this->stack = $stack;
28 234
        $this->ast = $ast;
29 234
    }
30
31 208
    public function next()
32
    {
33 208
        $this->stack->next();
34 208
    }
35
36 220
    public function valid(): bool
37
    {
38 220
        return $this->stack->valid();
39
    }
40
41 218
    public function current(): BaseToken
42
    {
43 218
        return $this->getCurrentToken()->createNode($this);
44
    }
45
46 2
    public function key(): int
47
    {
48 2
        return $this->stack->key();
49
    }
50
51 220
    public function rewind()
52
    {
53 220
        $this->stack->rewind();
54 220
    }
55
56 204
    public function getStack(): ArrayIterator
57
    {
58 204
        return $this->stack;
59
    }
60
61 224
    private function getCurrentToken(): BaseToken
62
    {
63 224
        return $this->stack->current();
64
    }
65
66 94
    public function getVariable(string $variableName): BaseToken
67
    {
68
        try {
69 94
            return $this->ast->getVariable($variableName);
70 6
        } catch (Exception\UndefinedVariableException $e) {
71 6
            throw ParserException::undefinedVariable($variableName, $this->getCurrentToken());
72
        }
73
    }
74
75 30
    public function getFunction(string $functionName): Closure
76
    {
77
        try {
78 30
            return $this->ast->getFunction($functionName);
79 6
        } catch (Exception\UndefinedFunctionException $e) {
80 6
            throw ParserException::undefinedFunction($functionName, $this->getCurrentToken());
81
        }
82
    }
83
84 112
    public function getMethod(string $methodName, BaseToken $token): CallableUserFunction
85
    {
86
        try {
87 112
            return $this->ast->getMethod($methodName, $token);
88 6
        } catch (Exception\UndefinedMethodException $e) {
89 6
            throw ParserException::undefinedMethod($methodName, $this->getCurrentToken());
90
        }
91
    }
92
}
93