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\Grammar; |
11
|
|
|
|
12
|
|
|
use Railt\Compiler\Grammar\Delegate\IncludeDelegate; |
13
|
|
|
use Railt\Compiler\Grammar\Delegate\RuleDelegate; |
14
|
|
|
use Railt\Compiler\Grammar\Delegate\TokenDelegate; |
15
|
|
|
use Railt\Io\Readable; |
16
|
|
|
use Railt\Lexer\Driver\NativeRegex; |
17
|
|
|
use Railt\Lexer\LexerInterface; |
18
|
|
|
use Railt\Parser\Driver\Llk; |
19
|
|
|
use Railt\Parser\Grammar; |
20
|
|
|
use Railt\Parser\GrammarInterface; |
21
|
|
|
use Railt\Parser\ParserInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Reader |
25
|
|
|
*/ |
26
|
|
|
class Reader |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Readable |
30
|
|
|
*/ |
31
|
|
|
private $file; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ParserInterface |
35
|
|
|
*/ |
36
|
|
|
private $pp; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var LexerInterface |
40
|
|
|
*/ |
41
|
|
|
private $lexer; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var GrammarInterface |
45
|
|
|
*/ |
46
|
|
|
private $grammar; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Analyzer |
50
|
|
|
*/ |
51
|
|
|
private $analyzer; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Reader constructor. |
55
|
|
|
* @param Readable $file |
56
|
|
|
*/ |
57
|
|
|
public function __construct(Readable $file) |
58
|
|
|
{ |
59
|
|
|
$this->file = $file; |
60
|
|
|
$this->pp = new Parser(); |
61
|
|
|
$this->lexer = new NativeRegex(); |
62
|
|
|
$this->grammar = new Grammar(); |
63
|
|
|
$this->analyzer = new Analyzer(); |
64
|
|
|
|
65
|
|
|
$this->boot(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
private function boot(): void |
72
|
|
|
{ |
73
|
|
|
$this->pp->env(LexerInterface::class, $this->lexer); |
74
|
|
|
$this->pp->env(GrammarInterface::class, $this->grammar); |
75
|
|
|
$this->pp->env(self::class, $this); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return ParserInterface |
80
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
81
|
|
|
* @throws \Railt\Io\Exception\NotReadableException |
82
|
|
|
*/ |
83
|
|
|
public function getParser(): ParserInterface |
84
|
|
|
{ |
85
|
|
|
$this->addGrammar($this->file); |
86
|
|
|
|
87
|
|
|
foreach ($this->analyzer->analyze() as $rule) { |
88
|
|
|
$this->grammar->addRule($rule); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return new Llk($this->lexer, $this->grammar); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Readable $file |
96
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
97
|
|
|
* @throws \Railt\Io\Exception\NotReadableException |
98
|
|
|
*/ |
99
|
|
|
private function addGrammar(Readable $file): void |
100
|
|
|
{ |
101
|
|
|
$ast = $this->pp->parse($file); |
102
|
|
|
|
103
|
|
|
foreach ($ast->getChildren() as $child) { |
104
|
|
|
switch (true) { |
105
|
|
|
case $child instanceof IncludeDelegate: |
106
|
|
|
$this->addGrammar($child->getPathname($file)); |
107
|
|
|
break; |
108
|
|
|
|
109
|
|
|
case $child instanceof TokenDelegate: |
110
|
|
|
$this->lexer->add($child->getTokenName(), $child->getTokenPattern()); |
|
|
|
|
111
|
|
|
if (! $child->isKept()) { |
112
|
|
|
$this->lexer->skip($child->getTokenName()); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
break; |
115
|
|
|
|
116
|
|
|
case $child instanceof RuleDelegate: |
117
|
|
|
$this->analyzer->addRuleDelegate($child); |
118
|
|
|
if ($child->getDelegate()) { |
|
|
|
|
119
|
|
|
$this->grammar->addDelegate($child->getRuleName(), $child->getDelegate()); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
break; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: