|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Remorhaz\UniLex\Parser; |
|
6
|
|
|
|
|
7
|
|
|
use Remorhaz\UniLex\Exception; |
|
8
|
|
|
use Remorhaz\UniLex\Stack\StackableSymbolInterface; |
|
9
|
|
|
|
|
10
|
|
|
class Production implements StackableSymbolInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private $header; |
|
13
|
|
|
|
|
14
|
|
|
private $index; |
|
15
|
|
|
|
|
16
|
|
|
private $symbolList; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Symbol $header, int $index, Symbol ...$symbolList) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->header = $header; |
|
21
|
|
|
$this->index = $index; |
|
22
|
|
|
$this->symbolList = $symbolList; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function __toString() |
|
26
|
|
|
{ |
|
27
|
|
|
return "{$this->header->getSymbolId()}:{$this->index}"; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return array|AttributeListShortcut |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getHeaderShortcut(): AttributeListShortcut |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->header->getShortcut(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return array[]|SymbolListShortcut |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getSymbolListShortcut(): SymbolListShortcut |
|
42
|
|
|
{ |
|
43
|
|
|
return new SymbolListShortcut($this); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getHeader(): Symbol |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->header; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getIndex(): int |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->index; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return Symbol[] |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getSymbolList(): array |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->symbolList; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param int $index |
|
66
|
|
|
* @return Symbol |
|
67
|
|
|
* @throws Exception |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getSymbol(int $index): Symbol |
|
70
|
|
|
{ |
|
71
|
|
|
if (!$this->symbolExists($index)) { |
|
72
|
|
|
throw new Exception("Symbol at index {$index} is undefined in production {$this}"); |
|
73
|
|
|
} |
|
74
|
|
|
return $this->symbolList[$index]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function isEpsilon(): bool |
|
78
|
|
|
{ |
|
79
|
|
|
return empty($this->getSymbolList()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function symbolExists(int $index): bool |
|
83
|
|
|
{ |
|
84
|
|
|
return isset($this->symbolList[$index]); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|