ReferenceParser.php
9.0 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
* - (c) John MacFarlane
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Reference;
use League\CommonMark\Parser\Cursor;
use League\CommonMark\Util\LinkParserHelper;
final class ReferenceParser
{
// Looking for the start of a definition, i.e. `[`
private const START_DEFINITION = 0;
// Looking for and parsing the label, i.e. `[foo]` within `[foo]`
private const LABEL = 1;
// Parsing the destination, i.e. `/url` in `[foo]: /url`
private const DESTINATION = 2;
// Looking for the start of a title, i.e. the first `"` in `[foo]: /url "title"`
private const START_TITLE = 3;
// Parsing the content of the title, i.e. `title` in `[foo]: /url "title"`
private const TITLE = 4;
// End state, no matter what kind of lines we add, they won't be references
private const PARAGRAPH = 5;
/** @psalm-readonly-allow-private-mutation */
private string $paragraph = '';
/**
* @var array<int, ReferenceInterface>
*
* @psalm-readonly-allow-private-mutation
*/
private array $references = [];
/** @psalm-readonly-allow-private-mutation */
private int $state = self::START_DEFINITION;
/** @psalm-readonly-allow-private-mutation */
private ?string $label = null;
/** @psalm-readonly-allow-private-mutation */
private ?string $destination = null;
/**
* @var string string
*
* @psalm-readonly-allow-private-mutation
*/
private string $title = '';
/** @psalm-readonly-allow-private-mutation */
private ?string $titleDelimiter = null;
/** @psalm-readonly-allow-private-mutation */
private bool $referenceValid = false;
public function getParagraphContent(): string
{
return $this->paragraph;
}
/**
* @return ReferenceInterface[]
*/
public function getReferences(): iterable
{
$this->finishReference();
return $this->references;
}
public function hasReferences(): bool
{
return $this->references !== [];
}
public function parse(string $line): void
{
if ($this->paragraph !== '') {
$this->paragraph .= "\n";
}
$this->paragraph .= $line;
$cursor = new Cursor($line);
while (! $cursor->isAtEnd()) {
$result = false;
switch ($this->state) {
case self::PARAGRAPH:
// We're in a paragraph now. Link reference definitions can only appear at the beginning, so once
// we're in a paragraph, there's no going back.
return;
case self::START_DEFINITION:
$result = $this->parseStartDefinition($cursor);
break;
case self::LABEL:
$result = $this->parseLabel($cursor);
break;
case self::DESTINATION:
$result = $this->parseDestination($cursor);
break;
case self::START_TITLE:
$result = $this->parseStartTitle($cursor);
break;
case self::TITLE:
$result = $this->parseTitle($cursor);
break;
default:
// this should never happen
break;
}
if (! $result) {
$this->state = self::PARAGRAPH;
return;
}
}
}
private function parseStartDefinition(Cursor $cursor): bool
{
$cursor->advanceToNextNonSpaceOrTab();
if ($cursor->isAtEnd() || $cursor->getCurrentCharacter() !== '[') {
return false;
}
$this->state = self::LABEL;
$this->label = '';
$cursor->advance();
if ($cursor->isAtEnd()) {
$this->label .= "\n";
}
return true;
}
private function parseLabel(Cursor $cursor): bool
{
$cursor->advanceToNextNonSpaceOrTab();
$partialLabel = LinkParserHelper::parsePartialLinkLabel($cursor);
if ($partialLabel === null) {
return false;
}
\assert($this->label !== null);
$this->label .= $partialLabel;
if ($cursor->isAtEnd()) {
// label might continue on next line
$this->label .= "\n";
return true;
}
if ($cursor->getCurrentCharacter() !== ']') {
return false;
}
$cursor->advance();
// end of label
if ($cursor->getCurrentCharacter() !== ':') {
return false;
}
$cursor->advance();
// spec: A link label can have at most 999 characters inside the square brackets
if (\mb_strlen($this->label, 'UTF-8') > 999) {
return false;
}
// spec: A link label must contain at least one non-whitespace character
if (\trim($this->label) === '') {
return false;
}
$cursor->advanceToNextNonSpaceOrTab();
$this->state = self::DESTINATION;
return true;
}
private function parseDestination(Cursor $cursor): bool
{
$cursor->advanceToNextNonSpaceOrTab();
$destination = LinkParserHelper::parseLinkDestination($cursor);
if ($destination === null) {
return false;
}
$this->destination = $destination;
$advanced = $cursor->advanceToNextNonSpaceOrTab();
if ($cursor->isAtEnd()) {
// Destination was at end of line, so this is a valid reference for sure (and maybe a title).
// If not at end of line, wait for title to be valid first.
$this->referenceValid = true;
$this->paragraph = '';
} elseif ($advanced === 0) {
// spec: The title must be separated from the link destination by whitespace
return false;
}
$this->state = self::START_TITLE;
return true;
}
private function parseStartTitle(Cursor $cursor): bool
{
$cursor->advanceToNextNonSpaceOrTab();
if ($cursor->isAtEnd()) {
$this->state = self::START_DEFINITION;
return true;
}
$this->titleDelimiter = null;
switch ($c = $cursor->getCurrentCharacter()) {
case '"':
case "'":
$this->titleDelimiter = $c;
break;
case '(':
$this->titleDelimiter = ')';
break;
default:
// no title delimter found
break;
}
if ($this->titleDelimiter !== null) {
$this->state = self::TITLE;
$cursor->advance();
if ($cursor->isAtEnd()) {
$this->title .= "\n";
}
} else {
$this->finishReference();
// There might be another reference instead, try that for the same character.
$this->state = self::START_DEFINITION;
}
return true;
}
private function parseTitle(Cursor $cursor): bool
{
\assert($this->titleDelimiter !== null);
$title = LinkParserHelper::parsePartialLinkTitle($cursor, $this->titleDelimiter);
if ($title === null) {
// Invalid title, stop
return false;
}
// Did we find the end delimiter?
$endDelimiterFound = false;
if (\substr($title, -1) === $this->titleDelimiter) {
$endDelimiterFound = true;
// Chop it off
$title = \substr($title, 0, -1);
}
$this->title .= $title;
if (! $endDelimiterFound && $cursor->isAtEnd()) {
// Title still going, continue on next line
$this->title .= "\n";
return true;
}
// We either hit the end delimiter or some extra whitespace
$cursor->advanceToNextNonSpaceOrTab();
if (! $cursor->isAtEnd()) {
// spec: No further non-whitespace characters may occur on the line.
return false;
}
$this->referenceValid = true;
$this->finishReference();
$this->paragraph = '';
// See if there's another definition
$this->state = self::START_DEFINITION;
return true;
}
private function finishReference(): void
{
if (! $this->referenceValid) {
return;
}
/** @psalm-suppress PossiblyNullArgument -- these can't possibly be null if we're in this state */
$this->references[] = new Reference($this->label, $this->destination, $this->title);
$this->label = null;
$this->referenceValid = false;
$this->destination = null;
$this->title = '';
$this->titleDelimiter = null;
}
}