Translator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 21
dl 0
loc 36
rs 10
c 1
b 0
f 1
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 18 5
A __construct() 0 5 1
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