|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Remorhaz\UniLex\Grammar\ContextFree; |
|
6
|
|
|
|
|
7
|
|
|
use Remorhaz\UniLex\Exception; |
|
8
|
|
|
|
|
9
|
|
|
abstract class GrammarLoader |
|
10
|
|
|
{ |
|
11
|
|
|
public const TOKEN_MAP_KEY = 'tokens'; |
|
12
|
|
|
|
|
13
|
|
|
public const PRODUCTION_MAP_KEY = 'productions'; |
|
14
|
|
|
|
|
15
|
|
|
public const ROOT_SYMBOL_KEY = 'root_symbol'; |
|
16
|
|
|
|
|
17
|
|
|
public const START_SYMBOL_KEY = 'start_symbol'; |
|
18
|
|
|
|
|
19
|
|
|
public const EOI_SYMBOL_KEY = 'eoi_symbol'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param array $config |
|
23
|
|
|
* @return Grammar |
|
24
|
|
|
* @throws Exception |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function loadConfig($config): Grammar |
|
27
|
|
|
{ |
|
28
|
|
|
$tokenMap = self::getConfigValue($config, self::TOKEN_MAP_KEY); |
|
29
|
|
|
$productionMap = self::getConfigValue($config, self::PRODUCTION_MAP_KEY); |
|
30
|
|
|
$rootSymbol = self::getConfigValue($config, self::ROOT_SYMBOL_KEY); |
|
31
|
|
|
$startSymbol = self::getConfigValue($config, self::START_SYMBOL_KEY); |
|
32
|
|
|
$eoiSymbol = self::getConfigValue($config, self::EOI_SYMBOL_KEY); |
|
33
|
|
|
$grammar = new Grammar($rootSymbol, $startSymbol, $eoiSymbol); |
|
34
|
|
|
self::loadFromMaps($grammar, $tokenMap, $productionMap); |
|
35
|
|
|
return $grammar; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $fileName |
|
40
|
|
|
* @return Grammar |
|
41
|
|
|
* @throws Exception |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function loadFile(string $fileName): Grammar |
|
44
|
|
|
{ |
|
45
|
|
|
/** @noinspection PhpIncludeInspection */ |
|
46
|
|
|
$config = @include $fileName; |
|
47
|
|
|
if (false === $config) { |
|
48
|
|
|
throw new Exception("Config file {$fileName} not found"); |
|
49
|
|
|
} |
|
50
|
|
|
return self::loadConfig($config); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param mixed $config |
|
55
|
|
|
* @param string $key |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
* @throws Exception |
|
58
|
|
|
*/ |
|
59
|
|
|
private static function getConfigValue($config, string $key) |
|
60
|
|
|
{ |
|
61
|
|
|
if (!is_array($config)) { |
|
62
|
|
|
throw new Exception("Config should be an array"); |
|
63
|
|
|
} |
|
64
|
|
|
if (!isset($config[$key])) { |
|
65
|
|
|
throw new Exception("Key '{$key}' not found in config"); |
|
66
|
|
|
} |
|
67
|
|
|
return $config[$key]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private static function loadFromMaps(Grammar $grammar, array $tokenMap, array $productionMap): void |
|
71
|
|
|
{ |
|
72
|
|
|
foreach ($tokenMap as $symbolId => $tokenId) { |
|
73
|
|
|
$grammar->addToken($symbolId, $tokenId); |
|
74
|
|
|
} |
|
75
|
|
|
foreach ($productionMap as $symbolId => $productionList) { |
|
76
|
|
|
foreach ($productionList as $production) { |
|
77
|
|
|
$grammar->addProduction($symbolId, ...$production); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|