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 Railt\Io\Readable; |
15
|
|
|
use Railt\Parser\Ast\NodeInterface; |
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\AST\ProvidesOpcode; |
21
|
|
|
use Railt\SDL\Frontend\IR\Opcode; |
22
|
|
|
use Railt\SDL\Frontend\IR\Opcode\OpenOpcode; |
23
|
|
|
use Railt\SDL\Frontend\IR\OpcodeHeap; |
24
|
|
|
use Railt\SDL\Frontend\IR\OpcodeInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Frontend |
28
|
|
|
*/ |
29
|
|
|
class Frontend implements LoggerAwareInterface |
30
|
|
|
{ |
31
|
|
|
use LoggerAwareTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Parser |
35
|
|
|
*/ |
36
|
|
|
private $parser; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var OpcodeHeap |
40
|
|
|
*/ |
41
|
|
|
private $heap; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Context |
45
|
|
|
*/ |
46
|
|
|
private $context; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Frontend constructor. |
50
|
|
|
*/ |
51
|
|
|
public function __construct() |
52
|
|
|
{ |
53
|
|
|
$this->parser = new Parser(); |
54
|
|
|
$this->context = new Context(); |
55
|
|
|
$this->heap = new OpcodeHeap($this->context); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Readable $file |
60
|
|
|
* @return iterable|OpcodeInterface[] |
61
|
|
|
* @throws SyntaxException |
62
|
|
|
*/ |
63
|
|
|
public function load(Readable $file): iterable |
64
|
|
|
{ |
65
|
|
|
$this->context->create(); |
66
|
|
|
|
67
|
|
|
$this->heap->add(new OpenOpcode($file), $file); |
68
|
|
|
|
69
|
|
|
$iterator = $this->bypass($this->parse($file)); |
70
|
|
|
|
71
|
|
|
while ($iterator->valid()) { |
72
|
|
|
[$ast, $opcode] = [$iterator->key(), $iterator->current()]; |
|
|
|
|
73
|
|
|
|
74
|
|
|
$iterator->send($this->heap->add($opcode, $file, $ast->getOffset())); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
foreach ($this->heap as $opcode) { |
78
|
|
|
yield $opcode; |
79
|
|
|
|
80
|
|
|
if ($this->logger) { |
81
|
|
|
$this->logger->debug($opcode); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param NodeInterface $node |
88
|
|
|
* @return iterable|Opcode[]|\Generator |
89
|
|
|
*/ |
90
|
|
|
private function bypass(NodeInterface $node): \Generator |
91
|
|
|
{ |
92
|
|
|
foreach ($node as $child) { |
|
|
|
|
93
|
|
|
$current = $this->context->current(); |
94
|
|
|
|
95
|
|
|
if ($child instanceof ProvidesOpcode) { |
96
|
|
|
yield from $this->extract($child); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($child instanceof RuleInterface) { |
100
|
|
|
yield from $this->bypass($child); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($current !== $this->context->current()) { |
104
|
|
|
$this->context->close(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param ProvidesOpcode $provider |
111
|
|
|
* @return \Generator|OpcodeInterface[] |
112
|
|
|
*/ |
113
|
|
|
private function extract(ProvidesOpcode $provider): \Generator |
114
|
|
|
{ |
115
|
|
|
/** @var \Generator $iterator */ |
116
|
|
|
$iterator = $provider->getOpcodes($this->context); |
117
|
|
|
|
118
|
|
|
while ($iterator->valid()) { |
119
|
|
|
$key = $iterator->key(); |
120
|
|
|
|
121
|
|
|
$iterator->send(yield $key instanceof RuleInterface ? $key : $provider => $iterator->current()); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param Readable $file |
127
|
|
|
* @return RuleInterface |
128
|
|
|
* @throws SyntaxException |
129
|
|
|
*/ |
130
|
|
|
private function parse(Readable $file): RuleInterface |
131
|
|
|
{ |
132
|
|
|
try { |
133
|
|
|
return $this->parser->parse($file); |
134
|
|
|
} catch (UnexpectedTokenException | UnrecognizedTokenException $e) { |
135
|
|
|
$error = new SyntaxException($e->getMessage(), $e->getCode()); |
136
|
|
|
$error->throwsIn($file, $e->getLine(), $e->getColumn()); |
137
|
|
|
|
138
|
|
|
throw $error; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.