-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserBackup.php
More file actions
200 lines (167 loc) · 6.82 KB
/
Copy pathParserBackup.php
File metadata and controls
200 lines (167 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
declare(strict_types=1);
namespace Phplrt\Parser;
use Phplrt\Contracts\Lexer\Channel;
use Phplrt\Contracts\Lexer\LexerInterface;
use Phplrt\Contracts\Lexer\TokenInterface;
use Phplrt\Contracts\Parser\ParserInterface;
use Phplrt\Contracts\Source\Factory\SourceFactoryInterface;
use Phplrt\Contracts\Source\ReadableInterface;
use Phplrt\Parser\Buffer\ArrayBuffer;
use Phplrt\Parser\Grammar\Alternation;
use Phplrt\Parser\Grammar\Concatenation;
use Phplrt\Parser\Grammar\Lexeme;
use Phplrt\Parser\Grammar\Optional;
use Phplrt\Parser\Grammar\Repetition;
use Phplrt\Parser\Grammar\RuleInterface;
use Phplrt\Source\SourceFactory;
final readonly class ParserBackup implements ParserInterface
{
private SourceFactoryInterface $sources;
public function __construct(
private LexerInterface $lexer,
/** @var array<int, RuleInterface> */
private array $grammar,
private int $initial,
?SourceFactoryInterface $sources = null,
) {
$this->sources = $sources ?? SourceFactory::default();
}
public function parse(ReadableInterface $source): iterable
{
$readable = $this->sources->create($source);
$buffer = new ArrayBuffer($this->filter($this->lexer->lex($readable)));
$stack = new \SplStack();
if ($this->reduce($this->grammar[$this->initial], $buffer, $stack)) {
return $stack;
}
return [];
}
/**
* @param iterable<TokenInterface> $tokens
* @return \Traversable<TokenInterface>
*/
private function filter(iterable $tokens): \Traversable
{
foreach ($tokens as $token) {
if ($token->channel === Channel::DEFAULT) {
yield $token;
}
}
}
private function reduce(RuleInterface $rule, ArrayBuffer $buffer, \SplStack $resultStack): bool
{
$stack = [[$rule, 0, $buffer->key(), $resultStack->count()]];
$lastResult = null;
while (\count($stack) > 0) {
$index = \count($stack) - 1;
[$currentRule, $step, $rollbackBuffer, $rollbackStackCount] = $stack[$index];
if ($lastResult !== null) {
// Обработка результата дочернего правила
$result = $lastResult;
$lastResult = null;
if ($currentRule instanceof Concatenation) {
if (!$result) {
$buffer->seek($rollbackBuffer);
while ($resultStack->count() > $rollbackStackCount) {
$resultStack->pop();
}
$lastResult = false;
\array_pop($stack);
continue;
}
$nextStep = $step + 1;
if ($nextStep < \count($currentRule->ruleIds)) {
$stack[$index][1] = $nextStep;
$stack[] = [$this->grammar[$currentRule->ruleIds[$nextStep]], 0, $buffer->key(), $resultStack->count()];
continue;
}
$lastResult = true;
\array_pop($stack);
continue;
}
if ($currentRule instanceof Alternation) {
if ($result) {
$lastResult = true;
\array_pop($stack);
continue;
}
$nextStep = $step + 1;
if ($nextStep < \count($currentRule->ruleIds)) {
$stack[$index][1] = $nextStep;
$stack[] = [$this->grammar[$currentRule->ruleIds[$nextStep]], 0, $buffer->key(), $resultStack->count()];
continue;
}
$lastResult = false;
\array_pop($stack);
continue;
}
if ($currentRule instanceof Optional) {
$lastResult = true;
\array_pop($stack);
continue;
}
if ($currentRule instanceof Repetition) {
if (!$result) {
$buffer->seek($rollbackBuffer);
while ($resultStack->count() > $rollbackStackCount) {
$resultStack->pop();
}
$lastResult = $step >= $currentRule->gte;
\array_pop($stack);
continue;
}
$nextStep = $step + 1;
if ($nextStep < $currentRule->lte) {
$stack[$index][1] = $nextStep;
// Обновляем rollback points для новой итерации
$stack[$index][2] = $buffer->key();
$stack[$index][3] = $resultStack->count();
$stack[] = [$this->grammar[$currentRule->ruleId], 0, $buffer->key(), $resultStack->count()];
continue;
}
$lastResult = true;
\array_pop($stack);
continue;
}
}
// Вход в правило (первый раз)
if ($currentRule instanceof Lexeme) {
$token = $buffer->current();
if ($token->id === $currentRule->tokenId) {
$resultStack->push($token);
$buffer->next();
$lastResult = true;
} else {
$lastResult = false;
}
\array_pop($stack);
continue;
}
if ($currentRule instanceof Concatenation) {
$stack[] = [$this->grammar[$currentRule->ruleIds[0]], 0, $buffer->key(), $resultStack->count()];
continue;
}
if ($currentRule instanceof Alternation) {
$stack[] = [$this->grammar[$currentRule->ruleIds[0]], 0, $buffer->key(), $resultStack->count()];
continue;
}
if ($currentRule instanceof Optional) {
$stack[] = [$this->grammar[$currentRule->ruleId], 0, $buffer->key(), $resultStack->count()];
continue;
}
if ($currentRule instanceof Repetition) {
if ($currentRule->lte > 0) {
$stack[] = [$this->grammar[$currentRule->ruleId], 0, $buffer->key(), $resultStack->count()];
} else {
$lastResult = $currentRule->gte === 0;
\array_pop($stack);
}
continue;
}
$lastResult = false;
\array_pop($stack);
}
return $lastResult;
}
}