|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the league/commonmark package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
|
11
|
|
|
* - (c) John MacFarlane |
|
12
|
|
|
* |
|
13
|
|
|
* Additional emphasis processing code based on commonmark-java (https://github.com/atlassian/commonmark-java) |
|
14
|
|
|
* - (c) Atlassian Pty Ltd |
|
15
|
|
|
* |
|
16
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
17
|
|
|
* file that was distributed with this source code. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace League\CommonMark\Delimiter; |
|
21
|
|
|
|
|
22
|
|
|
use League\CommonMark\Delimiter\Processor\CacheableDelimiterProcessorInterface; |
|
23
|
|
|
use League\CommonMark\Delimiter\Processor\DelimiterProcessorCollection; |
|
24
|
|
|
use League\CommonMark\Node\Inline\AdjacentTextMerger; |
|
25
|
|
|
use League\CommonMark\Node\Node; |
|
26
|
|
|
|
|
27
|
|
|
final class DelimiterStack |
|
28
|
|
|
{ |
|
29
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
|
30
|
|
|
private ?DelimiterInterface $top = null; |
|
31
|
|
|
|
|
32
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
|
33
|
|
|
private ?Bracket $brackets = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @deprecated This property will be removed in 3.0 once all delimiters MUST have an index/position |
|
37
|
|
|
* |
|
38
|
|
|
* @var \SplObjectStorage<DelimiterInterface, int>|\WeakMap<DelimiterInterface, int> |
|
39
|
|
|
*/ |
|
40
|
|
|
private $missingIndexCache; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
private int $remainingDelimiters = 0; |
|
44
|
|
|
|
|
45
|
2116 |
|
public function __construct(int $maximumStackSize = PHP_INT_MAX) |
|
46
|
|
|
{ |
|
47
|
2116 |
|
$this->remainingDelimiters = $maximumStackSize; |
|
48
|
|
|
|
|
49
|
2116 |
|
if (\PHP_VERSION_ID >= 80000) { |
|
50
|
|
|
/** @psalm-suppress PropertyTypeCoercion */ |
|
51
|
2116 |
|
$this->missingIndexCache = new \WeakMap(); // @phpstan-ignore-line |
|
|
|
|
|
|
52
|
|
|
} else { |
|
53
|
|
|
$this->missingIndexCache = new \SplObjectStorage(); // @phpstan-ignore-line |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
578 |
|
public function push(DelimiterInterface $newDelimiter): void |
|
58
|
|
|
{ |
|
59
|
578 |
|
if ($this->remainingDelimiters-- <= 0) { |
|
60
|
16 |
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
576 |
|
$newDelimiter->setPrevious($this->top); |
|
64
|
|
|
|
|
65
|
576 |
|
if ($this->top !== null) { |
|
66
|
488 |
|
$this->top->setNext($newDelimiter); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
576 |
|
$this->top = $newDelimiter; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @internal |
|
74
|
|
|
*/ |
|
75
|
410 |
|
public function addBracket(Node $node, int $index, bool $image): void |
|
76
|
|
|
{ |
|
77
|
410 |
|
if ($this->brackets !== null) { |
|
78
|
42 |
|
$this->brackets->setHasNext(true); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
410 |
|
$this->brackets = new Bracket($node, $this->brackets, $index, $image); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @psalm-immutable |
|
86
|
|
|
*/ |
|
87
|
400 |
|
public function getLastBracket(): ?Bracket |
|
88
|
|
|
{ |
|
89
|
400 |
|
return $this->brackets; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2090 |
|
private function findEarliest(int $stackBottom): ?DelimiterInterface |
|
93
|
|
|
{ |
|
94
|
|
|
// Move back to first relevant delim. |
|
95
|
2090 |
|
$delimiter = $this->top; |
|
96
|
2090 |
|
$lastChecked = null; |
|
97
|
|
|
|
|
98
|
2090 |
|
while ($delimiter !== null && self::getIndex($delimiter) > $stackBottom) { |
|
|
|
|
|
|
99
|
560 |
|
$lastChecked = $delimiter; |
|
100
|
560 |
|
$delimiter = $delimiter->getPrevious(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2090 |
|
return $lastChecked; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @internal |
|
108
|
|
|
*/ |
|
109
|
410 |
|
public function removeBracket(): void |
|
110
|
|
|
{ |
|
111
|
410 |
|
if ($this->brackets === null) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
410 |
|
$this->brackets = $this->brackets->getPrevious(); |
|
116
|
|
|
|
|
117
|
410 |
|
if ($this->brackets !== null) { |
|
118
|
42 |
|
$this->brackets->setHasNext(false); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
576 |
|
public function removeDelimiter(DelimiterInterface $delimiter): void |
|
123
|
|
|
{ |
|
124
|
576 |
|
if ($delimiter->getPrevious() !== null) { |
|
125
|
|
|
/** @psalm-suppress PossiblyNullReference */ |
|
126
|
214 |
|
$delimiter->getPrevious()->setNext($delimiter->getNext()); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
576 |
|
if ($delimiter->getNext() === null) { |
|
130
|
|
|
// top of stack |
|
131
|
576 |
|
$this->top = $delimiter->getPrevious(); |
|
132
|
|
|
} else { |
|
133
|
|
|
/** @psalm-suppress PossiblyNullReference */ |
|
134
|
426 |
|
$delimiter->getNext()->setPrevious($delimiter->getPrevious()); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// Nullify all references from the removed delimiter to other delimiters. |
|
138
|
|
|
// All references to this particular delimiter in the linked list should be gone, |
|
139
|
|
|
// but it's possible we're still hanging on to other references to things that |
|
140
|
|
|
// have been (or soon will be) removed, which may interfere with efficient |
|
141
|
|
|
// garbage collection by the PHP runtime. |
|
142
|
|
|
// Explicitly releasing these references should help to avoid possible |
|
143
|
|
|
// segfaults like in https://bugs.php.net/bug.php?id=68606. |
|
144
|
576 |
|
$delimiter->setPrevious(null); |
|
145
|
576 |
|
$delimiter->setNext(null); |
|
146
|
|
|
|
|
147
|
|
|
// TODO: Remove the line below once PHP 7.4 support is dropped, as WeakMap won't hold onto the reference, making this unnecessary |
|
148
|
576 |
|
unset($this->missingIndexCache[$delimiter]); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
426 |
|
private function removeDelimiterAndNode(DelimiterInterface $delimiter): void |
|
152
|
|
|
{ |
|
153
|
426 |
|
$delimiter->getInlineNode()->detach(); |
|
154
|
426 |
|
$this->removeDelimiter($delimiter); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
428 |
|
private function removeDelimitersBetween(DelimiterInterface $opener, DelimiterInterface $closer): void |
|
158
|
|
|
{ |
|
159
|
428 |
|
$delimiter = $closer->getPrevious(); |
|
160
|
428 |
|
$openerPosition = self::getIndex($opener); |
|
|
|
|
|
|
161
|
428 |
|
while ($delimiter !== null && self::getIndex($delimiter) > $openerPosition) { |
|
|
|
|
|
|
162
|
34 |
|
$previous = $delimiter->getPrevious(); |
|
163
|
34 |
|
$this->removeDelimiter($delimiter); |
|
164
|
34 |
|
$delimiter = $previous; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param DelimiterInterface|int|null $stackBottom |
|
170
|
|
|
*/ |
|
171
|
2092 |
|
public function removeAll($stackBottom = null): void |
|
172
|
|
|
{ |
|
173
|
2092 |
|
$stackBottomPosition = \is_int($stackBottom) ? $stackBottom : self::getIndex($stackBottom); |
|
|
|
|
|
|
174
|
|
|
|
|
175
|
2092 |
|
while ($this->top && $this->getIndex($this->top) > $stackBottomPosition) { |
|
|
|
|
|
|
176
|
136 |
|
$this->removeDelimiter($this->top); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @deprecated This method is no longer used internally and will be removed in 3.0 |
|
182
|
|
|
*/ |
|
183
|
2 |
|
public function removeEarlierMatches(string $character): void |
|
184
|
|
|
{ |
|
185
|
2 |
|
$opener = $this->top; |
|
186
|
2 |
|
while ($opener !== null) { |
|
187
|
2 |
|
if ($opener->getChar() === $character) { |
|
188
|
2 |
|
$opener->setActive(false); |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
2 |
|
$opener = $opener->getPrevious(); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @internal |
|
197
|
|
|
*/ |
|
198
|
276 |
|
public function deactivateLinkOpeners(): void |
|
199
|
|
|
{ |
|
200
|
276 |
|
$opener = $this->brackets; |
|
201
|
276 |
|
while ($opener !== null && $opener->isActive()) { |
|
202
|
20 |
|
$opener->setActive(false); |
|
203
|
20 |
|
$opener = $opener->getPrevious(); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @deprecated This method is no longer used internally and will be removed in 3.0 |
|
209
|
|
|
* |
|
210
|
|
|
* @param string|string[] $characters |
|
211
|
|
|
*/ |
|
212
|
6 |
|
public function searchByCharacter($characters): ?DelimiterInterface |
|
213
|
|
|
{ |
|
214
|
6 |
|
if (! \is_array($characters)) { |
|
215
|
6 |
|
$characters = [$characters]; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
6 |
|
$opener = $this->top; |
|
219
|
6 |
|
while ($opener !== null) { |
|
220
|
4 |
|
if (\in_array($opener->getChar(), $characters, true)) { |
|
221
|
4 |
|
break; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
4 |
|
$opener = $opener->getPrevious(); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
6 |
|
return $opener; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param DelimiterInterface|int|null $stackBottom |
|
232
|
|
|
* |
|
233
|
|
|
* @todo change $stackBottom to an int in 3.0 |
|
234
|
|
|
*/ |
|
235
|
2088 |
|
public function processDelimiters($stackBottom, DelimiterProcessorCollection $processors): void |
|
236
|
|
|
{ |
|
237
|
|
|
/** @var array<string, int> $openersBottom */ |
|
238
|
2088 |
|
$openersBottom = []; |
|
239
|
|
|
|
|
240
|
2088 |
|
$stackBottomPosition = \is_int($stackBottom) ? $stackBottom : self::getIndex($stackBottom); |
|
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
// Find first closer above stackBottom |
|
243
|
2088 |
|
$closer = $this->findEarliest($stackBottomPosition); |
|
244
|
|
|
|
|
245
|
|
|
// Move forward, looking for closers, and handling each |
|
246
|
2088 |
|
while ($closer !== null) { |
|
247
|
558 |
|
$closingDelimiterChar = $closer->getChar(); |
|
248
|
|
|
|
|
249
|
558 |
|
$delimiterProcessor = $processors->getDelimiterProcessor($closingDelimiterChar); |
|
250
|
558 |
|
if (! $closer->canClose() || $delimiterProcessor === null) { |
|
251
|
494 |
|
$closer = $closer->getNext(); |
|
252
|
494 |
|
continue; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
504 |
|
if ($delimiterProcessor instanceof CacheableDelimiterProcessorInterface) { |
|
256
|
456 |
|
$openersBottomCacheKey = $delimiterProcessor->getCacheKey($closer); |
|
257
|
|
|
} else { |
|
258
|
48 |
|
$openersBottomCacheKey = $closingDelimiterChar; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
504 |
|
$openingDelimiterChar = $delimiterProcessor->getOpeningCharacter(); |
|
262
|
|
|
|
|
263
|
504 |
|
$useDelims = 0; |
|
264
|
504 |
|
$openerFound = false; |
|
265
|
504 |
|
$potentialOpenerFound = false; |
|
266
|
504 |
|
$opener = $closer->getPrevious(); |
|
267
|
504 |
|
while ($opener !== null && ($openerPosition = self::getIndex($opener)) > $stackBottomPosition && $openerPosition >= ($openersBottom[$openersBottomCacheKey] ?? 0)) { |
|
|
|
|
|
|
268
|
440 |
|
if ($opener->canOpen() && $opener->getChar() === $openingDelimiterChar) { |
|
269
|
438 |
|
$potentialOpenerFound = true; |
|
270
|
438 |
|
$useDelims = $delimiterProcessor->getDelimiterUse($opener, $closer); |
|
271
|
438 |
|
if ($useDelims > 0) { |
|
272
|
426 |
|
$openerFound = true; |
|
273
|
426 |
|
break; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
60 |
|
$opener = $opener->getPrevious(); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
504 |
|
if (! $openerFound) { |
|
281
|
|
|
// Set lower bound for future searches |
|
282
|
|
|
// TODO: Remove this conditional check in 3.0. It only exists to prevent behavioral BC breaks in 2.x. |
|
283
|
158 |
|
if ($potentialOpenerFound === false || $delimiterProcessor instanceof CacheableDelimiterProcessorInterface) { |
|
284
|
156 |
|
$openersBottom[$openersBottomCacheKey] = self::getIndex($closer); |
|
|
|
|
|
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
158 |
|
if (! $potentialOpenerFound && ! $closer->canOpen()) { |
|
288
|
|
|
// We can remove a closer that can't be an opener, |
|
289
|
|
|
// once we've seen there's no matching opener. |
|
290
|
92 |
|
$next = $closer->getNext(); |
|
291
|
92 |
|
$this->removeDelimiter($closer); |
|
292
|
92 |
|
$closer = $next; |
|
293
|
|
|
} else { |
|
294
|
70 |
|
$closer = $closer->getNext(); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
158 |
|
continue; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
\assert($opener !== null); |
|
301
|
|
|
|
|
302
|
426 |
|
$openerNode = $opener->getInlineNode(); |
|
303
|
426 |
|
$closerNode = $closer->getInlineNode(); |
|
304
|
|
|
|
|
305
|
|
|
// Remove number of used delimiters from stack and inline nodes. |
|
306
|
426 |
|
$opener->setLength($opener->getLength() - $useDelims); |
|
307
|
426 |
|
$closer->setLength($closer->getLength() - $useDelims); |
|
308
|
|
|
|
|
309
|
426 |
|
$openerNode->setLiteral(\substr($openerNode->getLiteral(), 0, -$useDelims)); |
|
310
|
426 |
|
$closerNode->setLiteral(\substr($closerNode->getLiteral(), 0, -$useDelims)); |
|
311
|
|
|
|
|
312
|
426 |
|
$this->removeDelimitersBetween($opener, $closer); |
|
313
|
|
|
// The delimiter processor can re-parent the nodes between opener and closer, |
|
314
|
|
|
// so make sure they're contiguous already. Exclusive because we want to keep opener/closer themselves. |
|
315
|
426 |
|
AdjacentTextMerger::mergeTextNodesBetweenExclusive($openerNode, $closerNode); |
|
316
|
426 |
|
$delimiterProcessor->process($openerNode, $closerNode, $useDelims); |
|
317
|
|
|
|
|
318
|
|
|
// No delimiter characters left to process, so we can remove delimiter and the now empty node. |
|
319
|
426 |
|
if ($opener->getLength() === 0) { |
|
320
|
408 |
|
$this->removeDelimiterAndNode($opener); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
// phpcs:disable SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed |
|
324
|
426 |
|
if ($closer->getLength() === 0) { |
|
325
|
410 |
|
$next = $closer->getNext(); |
|
326
|
410 |
|
$this->removeDelimiterAndNode($closer); |
|
327
|
410 |
|
$closer = $next; |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
// Remove all delimiters |
|
332
|
2088 |
|
$this->removeAll($stackBottomPosition); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* @internal |
|
337
|
|
|
*/ |
|
338
|
2116 |
|
public function __destruct() |
|
339
|
|
|
{ |
|
340
|
2116 |
|
while ($this->top) { |
|
341
|
16 |
|
$this->removeDelimiter($this->top); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
2116 |
|
while ($this->brackets) { |
|
345
|
28 |
|
$this->removeBracket(); |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* @deprecated This method will be dropped in 3.0 once all delimiters MUST have an index/position |
|
351
|
|
|
*/ |
|
352
|
2098 |
|
private function getIndex(?DelimiterInterface $delimiter): int |
|
353
|
|
|
{ |
|
354
|
2098 |
|
if ($delimiter === null) { |
|
355
|
2092 |
|
return -1; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
568 |
|
if (($index = $delimiter->getIndex()) !== null) { |
|
359
|
564 |
|
return $index; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
6 |
|
if (isset($this->missingIndexCache[$delimiter])) { |
|
|
|
|
|
|
363
|
4 |
|
return $this->missingIndexCache[$delimiter]; |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
6 |
|
$prev = $delimiter->getPrevious(); |
|
367
|
6 |
|
$next = $delimiter->getNext(); |
|
368
|
|
|
|
|
369
|
6 |
|
$i = 0; |
|
370
|
|
|
do { |
|
371
|
6 |
|
$i++; |
|
372
|
6 |
|
if ($prev === null) { |
|
373
|
6 |
|
break; |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
6 |
|
if ($prev->getIndex() !== null) { |
|
377
|
2 |
|
return $this->missingIndexCache[$delimiter] = $prev->getIndex() + $i; |
|
|
|
|
|
|
378
|
|
|
} |
|
379
|
6 |
|
} while ($prev = $prev->getPrevious()); |
|
380
|
|
|
|
|
381
|
6 |
|
$j = 0; |
|
382
|
|
|
do { |
|
383
|
6 |
|
$j++; |
|
384
|
6 |
|
if ($next === null) { |
|
385
|
4 |
|
break; |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
6 |
|
if ($next->getIndex() !== null) { |
|
389
|
2 |
|
return $this->missingIndexCache[$delimiter] = $next->getIndex() - $j; |
|
|
|
|
|
|
390
|
|
|
} |
|
391
|
4 |
|
} while ($next = $next->getNext()); |
|
392
|
|
|
|
|
393
|
|
|
// No index was defined on this delimiter, and none could be guesstimated based on the stack. |
|
394
|
4 |
|
return $this->missingIndexCache[$delimiter] = $this->getIndex($delimiter->getPrevious()) + 1; |
|
|
|
|
|
|
395
|
|
|
} |
|
396
|
|
|
} |
|
397
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..