|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\Compiler\Reader; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Compiler\Exception\GrammarException; |
|
13
|
|
|
use Railt\Io\Readable; |
|
14
|
|
|
use Railt\Parser\Ast\Delegate; |
|
15
|
|
|
use Railt\Parser\Ast\Rule; |
|
16
|
|
|
use Railt\Parser\Rule\Production; |
|
17
|
|
|
use Railt\Parser\Rule\Symbol; |
|
18
|
|
|
use Railt\Parser\Rule\Terminal; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class BaseRules |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class BaseRules implements ProvideRules |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var array|Symbol[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $rules = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $mappings = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $delegates = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array|Readable[] |
|
42
|
|
|
*/ |
|
43
|
|
|
private $files = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param Symbol $symbol |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function add(Symbol $symbol): void |
|
49
|
|
|
{ |
|
50
|
|
|
$this->rules[$symbol->getId()] = $symbol; |
|
51
|
|
|
|
|
52
|
|
|
$providesName = $symbol instanceof Terminal || |
|
53
|
|
|
($symbol instanceof Production && $symbol->getName()); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
if ($providesName) { |
|
56
|
|
|
$this->mappings[$symbol->getName()] = $symbol->getId(); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $rule |
|
62
|
|
|
* @param string $delegate |
|
63
|
|
|
* @throws GrammarException |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function addDelegate(string $rule, string $delegate): void |
|
66
|
|
|
{ |
|
67
|
|
|
if (! \class_exists($delegate)) { |
|
68
|
|
|
$error = 'Could not found delegate class "%s"'; |
|
69
|
|
|
throw new GrammarException(\sprintf($error, $delegate)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (! \is_subclass_of($delegate, Delegate::class)) { |
|
|
|
|
|
|
73
|
|
|
$error = 'Delegate should be an instance of %s, but %s given'; |
|
74
|
|
|
throw new GrammarException(\sprintf($error, Delegate::class, $delegate)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->delegates[$rule] = $delegate; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $rule |
|
82
|
|
|
* @param Readable $grammar |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function addFile(string $rule, Readable $grammar): void |
|
85
|
|
|
{ |
|
86
|
|
|
$this->files[$rule] = $grammar; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function all(): array |
|
93
|
|
|
{ |
|
94
|
|
|
return dd($this->rules); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $rule |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
public function has(string $rule): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return \array_key_exists($rule, $this->rules); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return iterable |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getDelegates(): iterable |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->delegates; |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $rule |
|
116
|
|
|
* @return Readable |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getFile(string $rule): Readable |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->files[$rule]; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: