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\Frontend; |
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\NodeInterface; |
17
|
|
|
use Railt\Parser\Ast\RuleInterface; |
18
|
|
|
use Railt\Parser\Exception\UnexpectedTokenException; |
19
|
|
|
use Railt\Parser\Exception\UnrecognizedTokenException; |
20
|
|
|
use Railt\SDL\Exception\SyntaxException; |
21
|
|
|
use Railt\SDL\Frontend\AST\ProvidesOpcode; |
22
|
|
|
use Railt\SDL\Frontend\IR\Collection; |
23
|
|
|
use Railt\SDL\Frontend\IR\Deferred; |
24
|
|
|
use Railt\SDL\Frontend\IR\Opcode\OpenOpcode; |
25
|
|
|
use Railt\SDL\Frontend\IR\OpcodeInterface; |
26
|
|
|
use Railt\SDL\Frontend\IR\Prototype; |
27
|
|
|
use Railt\SDL\Frontend\IR\UnmountedOpcodeInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class Frontend |
31
|
|
|
*/ |
32
|
|
|
class Frontend implements LoggerAwareInterface |
33
|
|
|
{ |
34
|
|
|
use LoggerAwareTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Parser |
38
|
|
|
*/ |
39
|
|
|
private $parser; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Frontend constructor. |
43
|
|
|
*/ |
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
$this->parser = new Parser(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Readable $file |
51
|
|
|
* @return iterable|OpcodeInterface[] |
52
|
|
|
* @throws SyntaxException |
53
|
|
|
*/ |
54
|
|
|
public function load(Readable $file): iterable |
55
|
|
|
{ |
56
|
|
|
$context = new Context(); |
57
|
|
|
|
58
|
|
|
return $this->collect($file, $context); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Readable $file |
63
|
|
|
* @param Context $context |
64
|
|
|
* @return Collection|OpcodeInterface[] |
65
|
|
|
* @throws SyntaxException |
66
|
|
|
*/ |
67
|
|
|
private function collect(Readable $file, Context $context): Collection |
68
|
|
|
{ |
69
|
|
|
// Create a container in which all the opcodes are stored. |
70
|
|
|
$collection = new Collection($context); |
71
|
|
|
|
72
|
|
|
$collection->add(new OpenOpcode($file), $file); |
73
|
|
|
|
74
|
|
|
// We start bypassing and add each element to the collection. |
75
|
|
|
$iterator = $this->bypass($this->parse($file), $context); |
76
|
|
|
|
77
|
|
|
while ($iterator->valid()) { |
78
|
|
|
[$ast, $result] = [$iterator->key(), $iterator->current()]; |
|
|
|
|
79
|
|
|
|
80
|
|
|
// If an unmounted opcode (prototype) is returned, |
81
|
|
|
// then attach it and return it back. |
82
|
|
|
if ($result instanceof OpcodeInterface) { |
83
|
|
|
$result = $collection->add($result, $file, $ast->getOffset()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// If the result is callable/invocable, then we create |
87
|
|
|
// a pending (deferred) execution element. |
88
|
|
|
if (\is_callable($result)) { |
89
|
|
|
$result = new Deferred($ast, $result); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$iterator->send($result); |
93
|
|
|
|
94
|
|
|
if ($this->logger) { |
95
|
|
|
$this->log($result); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $collection; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param mixed $value |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
private function log($value): void |
107
|
|
|
{ |
108
|
|
|
if ($value instanceof OpcodeInterface) { |
109
|
|
|
$this->logger->debug((string)$value); |
110
|
|
|
} else { |
111
|
|
|
$this->logger->info(\gettype($value)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* A method for recursively traversing all rules of an Abstract |
117
|
|
|
* Syntax Tree to obtain all the opcodes that the tree provides. |
118
|
|
|
* |
119
|
|
|
* @param NodeInterface|RuleInterface $node |
120
|
|
|
* @param Context $context |
121
|
|
|
* @return iterable|OpcodeInterface[]|\Generator |
122
|
|
|
*/ |
123
|
|
|
private function bypass(NodeInterface $node, Context $context): \Generator |
124
|
|
|
{ |
125
|
|
|
foreach ($node->getChildren() as $child) { |
|
|
|
|
126
|
|
|
/** @var Deferred[] $deferred */ |
127
|
|
|
$deferred = []; |
128
|
|
|
|
129
|
|
|
$current = $context->current(); |
130
|
|
|
|
131
|
|
|
// Is AST Rule provides opcodes list? |
132
|
|
|
if ($child instanceof ProvidesOpcode) { |
133
|
|
|
$iterator = $this->extract($child, $child->getOpcodes($context)); |
|
|
|
|
134
|
|
|
|
135
|
|
|
yield from \iterator_reverse_each($iterator, function($response) use (&$deferred) { |
|
|
|
|
136
|
|
|
// In the event that the parent sends a deferred callback |
137
|
|
|
// back, we memorize the reference to him in order |
138
|
|
|
// to fulfill in the future. |
139
|
|
|
if ($response instanceof Deferred) { |
140
|
|
|
$deferred[] = $response; |
141
|
|
|
} |
142
|
|
|
}); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Is the AST provides other children? |
146
|
|
|
if ($child instanceof RuleInterface) { |
147
|
|
|
yield from $this->bypass($child, $context); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Execute pending elements before closing the context. |
151
|
|
|
foreach ($deferred as $callable) { |
152
|
|
|
yield from $this->extract($child, $callable->resolve()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// Is the context was changed at runtime - close |
156
|
|
|
// it and restore the previous one. |
157
|
|
|
if ($current !== $context->current()) { |
158
|
|
|
$context->close(); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Method for unpacking the list of opcodes from the rule. |
165
|
|
|
* |
166
|
|
|
* @param RuleInterface $key |
167
|
|
|
* @param \Generator $iterator |
168
|
|
|
* @return \Generator|OpcodeInterface[] |
169
|
|
|
*/ |
170
|
|
|
private function extract(RuleInterface $key, \Generator $iterator): \Generator |
171
|
|
|
{ |
172
|
|
|
while ($iterator->valid()) { |
173
|
|
|
// Take an AST node |
174
|
|
|
$node = $this->extractKey($iterator, $key); |
175
|
|
|
|
176
|
|
|
// We return the prototype and get the opcode: |
177
|
|
|
// ie with reference to the file, line, column, offset, etc., |
178
|
|
|
// including the identifier of the opcode inside the collection. |
179
|
|
|
$opcode = yield $node => $iterator->current(); |
180
|
|
|
|
181
|
|
|
// Transfer control back to the AST. |
182
|
|
|
$iterator->send($opcode); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Make sure that the key is a valid AST rule that contains |
188
|
|
|
* a link to the position inside the file. |
189
|
|
|
* |
190
|
|
|
* @param \Generator $current |
191
|
|
|
* @param RuleInterface $parent |
192
|
|
|
* @return RuleInterface |
193
|
|
|
*/ |
194
|
|
|
private function extractKey(\Generator $current, RuleInterface $parent): RuleInterface |
195
|
|
|
{ |
196
|
|
|
$key = $current->key(); |
197
|
|
|
|
198
|
|
|
return $key instanceof RuleInterface ? $key : $parent; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Parse the file using top-down parser and |
203
|
|
|
* return the Abstract Syntax Tree. |
204
|
|
|
* |
205
|
|
|
* @param Readable $file |
206
|
|
|
* @return RuleInterface |
207
|
|
|
* @throws SyntaxException |
208
|
|
|
*/ |
209
|
|
|
private function parse(Readable $file): RuleInterface |
210
|
|
|
{ |
211
|
|
|
try { |
212
|
|
|
return $this->parser->parse($file); |
213
|
|
|
} catch (UnexpectedTokenException | UnrecognizedTokenException $e) { |
214
|
|
|
$error = new SyntaxException($e->getMessage(), $e->getCode()); |
215
|
|
|
$error->throwsIn($file, $e->getLine(), $e->getColumn()); |
216
|
|
|
|
217
|
|
|
throw $error; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param LoggerInterface $logger |
223
|
|
|
* @return Frontend |
224
|
|
|
*/ |
225
|
|
|
public function setLogger(LoggerInterface $logger): self |
226
|
|
|
{ |
227
|
|
|
$this->logger = $logger; |
228
|
|
|
|
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
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.