|
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\PP2\Resolvers; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Compiler\Exception\UnknownPragmaException; |
|
13
|
|
|
use Railt\Compiler\Grammar\PP2\Delegate\PragmaDefinitionDelegate; |
|
14
|
|
|
use Railt\Compiler\Grammar\PP2\ResolverInterface; |
|
15
|
|
|
use Railt\Compiler\Reader\BasePragmas; |
|
16
|
|
|
use Railt\Io\Exception\ExternalFileException; |
|
17
|
|
|
use Railt\Io\Readable; |
|
18
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class PragmasResolver |
|
22
|
|
|
*/ |
|
23
|
|
|
class PragmasResolver extends BasePragmas implements ResolverInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @param Readable $readable |
|
27
|
|
|
* @param RuleInterface|PragmaDefinitionDelegate $rule |
|
28
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function resolve(Readable $readable, RuleInterface $rule): void |
|
31
|
|
|
{ |
|
32
|
|
|
[$name, $value] = [$rule->getPragmaName(), $rule->getPragmaValue()]; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
foreach ($this->getResolvers() as $group => $resolver) { |
|
35
|
|
|
if ($resolver->match($name)) { |
|
36
|
|
|
$resolved = $resolver->resolve($name); |
|
37
|
|
|
|
|
38
|
|
|
if (! $resolved) { |
|
39
|
|
|
throw $this->badPragma($name, $value)->throwsIn($readable, $rule->getOffset()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->set($group, $name, $value); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $name |
|
49
|
|
|
* @param string $value |
|
50
|
|
|
* @return ExternalFileException |
|
51
|
|
|
*/ |
|
52
|
|
|
private function badPragma(string $name, string $value): ExternalFileException |
|
53
|
|
|
{ |
|
54
|
|
|
$error = \sprintf('Unknown pragma name "%s" with value "%s"', $name, $value); |
|
55
|
|
|
|
|
56
|
|
|
return new UnknownPragmaException($error); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.