Translator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\UniLex\AST;
6
7
use Remorhaz\UniLex\Exception;
8
use Remorhaz\UniLex\Stack\SymbolStack;
9
10
class Translator
11
{
12
    private $tree;
13
14
    private $listener;
15
16
    private $stack;
17
18
    public function __construct(Tree $tree, TranslatorListenerInterface $listener)
19
    {
20
        $this->tree = $tree;
21
        $this->listener = $listener;
22
        $this->stack = new SymbolStack();
23
    }
24
25
    /**
26
     * @throws Exception
27
     */
28
    public function run(): void
29
    {
30
        $this->stack->reset();
31
        $rootNode = $this->tree->getRootNode();
32
        $this->listener->onStart($rootNode);
33
        $this->stack->push($rootNode);
34
        while (!$this->stack->isEmpty()) {
35
            $symbol = $this->stack->pop();
36
            if ($symbol instanceof EopSymbol) {
37
                $this->listener->onFinishProduction($symbol->getNode());
38
            } elseif ($symbol instanceof Node) {
39
                $this->stack->push(new EopSymbol($symbol));
40
                $this->listener->onBeginProduction($symbol, $this->stack);
41
            } elseif ($symbol instanceof Symbol) {
42
                $this->listener->onSymbol($symbol, $this->stack);
43
            }
44
        }
45
        $this->listener->onFinish();
46
    }
47
}
48