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\SDL; |
11
|
|
|
|
12
|
|
|
use Psr\Log\LoggerAwareInterface; |
13
|
|
|
use Psr\Log\LoggerAwareTrait; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Railt\Io\Readable; |
16
|
|
|
use Railt\Parser\Ast\RuleInterface; |
17
|
|
|
use Railt\Parser\Exception\UnexpectedTokenException; |
18
|
|
|
use Railt\Parser\Exception\UnrecognizedTokenException; |
19
|
|
|
use Railt\SDL\Exception\SyntaxException; |
20
|
|
|
use Railt\SDL\Frontend\Builder; |
21
|
|
|
use Railt\SDL\Frontend\Context\ContextInterface; |
22
|
|
|
use Railt\SDL\Frontend\Context\GlobalContext; |
23
|
|
|
use Railt\SDL\Frontend\Parser; |
24
|
|
|
use Railt\SDL\IR\DefinitionValueObject; |
25
|
|
|
use Railt\SDL\IR\SymbolTable; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Frontend |
29
|
|
|
*/ |
30
|
|
|
class Frontend implements LoggerAwareInterface |
31
|
|
|
{ |
32
|
|
|
use LoggerAwareTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Builder |
36
|
|
|
*/ |
37
|
|
|
private $builder; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Frontend constructor. |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
$this->builder = new Builder(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Readable $file |
49
|
|
|
* @return iterable |
50
|
|
|
* @throws SyntaxException |
51
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
52
|
|
|
*/ |
53
|
|
|
public function load(Readable $file) |
54
|
|
|
{ |
55
|
|
|
return $this->buildFile($file); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Readable $readable |
60
|
|
|
* @return iterable |
61
|
|
|
* @throws SyntaxException |
62
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
63
|
|
|
*/ |
64
|
|
|
public function buildFile(Readable $readable) |
65
|
|
|
{ |
66
|
|
|
$process = $this->builder->buildFile($readable); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var ContextInterface $context |
70
|
|
|
* @var mixed $result |
71
|
|
|
*/ |
72
|
|
|
foreach ($process as [$context, $result]) { |
73
|
|
|
if ($this->filter($context, $result)) { |
|
|
|
|
74
|
|
|
yield $result; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param ContextInterface $ctx |
81
|
|
|
* @param mixed $result |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
private function filter(ContextInterface $ctx, $result): bool |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
return $result !== null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param LoggerInterface $logger |
91
|
|
|
* @return Frontend |
92
|
|
|
*/ |
93
|
|
|
public function setLogger(LoggerInterface $logger): self |
94
|
|
|
{ |
95
|
|
|
$this->logger = $logger; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.