Table::addProduction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\UniLex\Parser\LL1\Lookup;
6
7
use Remorhaz\UniLex\Exception;
8
9
class Table implements TableInterface
10
{
11
    private $map = [];
12
13
    /**
14
     * @param int $symbolId
15
     * @param int $tokenId
16
     * @param int $productionIndex
17
     * @throws Exception
18
     */
19
    public function addProduction(int $symbolId, int $tokenId, int $productionIndex): void
20
    {
21
        if ($this->hasProduction($symbolId, $tokenId)) {
22
            throw new Exception("Production for [{$symbolId}:{$tokenId}] is already defined");
23
        }
24
        $this->map[$symbolId][$tokenId] = $productionIndex;
25
    }
26
27
    /**
28
     * @param int $symbolId
29
     * @param int $tokenId
30
     * @return int
31
     * @throws Exception
32
     */
33
    public function getProductionIndex(int $symbolId, int $tokenId): int
34
    {
35
        if (!$this->hasProduction($symbolId, $tokenId)) {
36
            throw new Exception("Production for [{$symbolId}:{$tokenId}] is not defined");
37
        }
38
        return $this->map[$symbolId][$tokenId];
39
    }
40
41
    public function getExpectedTokenList(int $symbolId): array
42
    {
43
        return isset($this->map[$symbolId]) ? array_keys($this->map[$symbolId]) : [];
44
    }
45
46
    public function hasProduction(int $symbolId, int $tokenId): bool
47
    {
48
        return isset($this->map[$symbolId][$tokenId]);
49
    }
50
51
    public function exportMap(): array
52
    {
53
        return $this->map;
54
    }
55
56
    public function importMap(array $map): void
57
    {
58
        $this->map = $map;
59
    }
60
}
61