|
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
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
14
|
|
|
* file that was distributed with this source code. |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace League\CommonMark\Parser; |
|
18
|
|
|
|
|
19
|
|
|
use League\CommonMark\Delimiter\DelimiterStack; |
|
20
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
|
21
|
|
|
use League\CommonMark\Reference\ReferenceMapInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class InlineParserContext |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var AbstractBlock |
|
27
|
|
|
* |
|
28
|
|
|
* @psalm-readonly |
|
29
|
|
|
*/ |
|
30
|
|
|
private $container; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ReferenceMapInterface |
|
34
|
|
|
* |
|
35
|
|
|
* @psalm-readonly |
|
36
|
|
|
*/ |
|
37
|
|
|
private $referenceMap; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Cursor |
|
41
|
|
|
* |
|
42
|
|
|
* @psalm-readonly |
|
43
|
|
|
*/ |
|
44
|
|
|
private $cursor; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var DelimiterStack |
|
48
|
|
|
* |
|
49
|
|
|
* @psalm-readonly |
|
50
|
|
|
*/ |
|
51
|
|
|
private $delimiterStack; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string[] |
|
55
|
|
|
* @psalm-var non-empty-array<string> |
|
56
|
|
|
* |
|
57
|
|
|
* @psalm-readonly-allow-private-mutation |
|
58
|
|
|
*/ |
|
59
|
|
|
private $matches; |
|
60
|
|
|
|
|
61
|
2601 |
|
public function __construct(Cursor $contents, AbstractBlock $container, ReferenceMapInterface $referenceMap) |
|
62
|
|
|
{ |
|
63
|
2601 |
|
$this->referenceMap = $referenceMap; |
|
64
|
2601 |
|
$this->container = $container; |
|
65
|
2601 |
|
$this->cursor = $contents; |
|
66
|
2601 |
|
$this->delimiterStack = new DelimiterStack(); |
|
67
|
2601 |
|
} |
|
68
|
|
|
|
|
69
|
1992 |
|
public function getContainer(): AbstractBlock |
|
70
|
|
|
{ |
|
71
|
1992 |
|
return $this->container; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
486 |
|
public function getReferenceMap(): ReferenceMapInterface |
|
75
|
|
|
{ |
|
76
|
486 |
|
return $this->referenceMap; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2004 |
|
public function getCursor(): Cursor |
|
80
|
|
|
{ |
|
81
|
2004 |
|
return $this->cursor; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2592 |
|
public function getDelimiterStack(): DelimiterStack |
|
85
|
|
|
{ |
|
86
|
2592 |
|
return $this->delimiterStack; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return string The full text that matched the InlineParserMatch definition |
|
91
|
|
|
*/ |
|
92
|
996 |
|
public function getFullMatch(): string |
|
93
|
|
|
{ |
|
94
|
996 |
|
return $this->matches[0]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return int The length of the full match (in characters, not bytes) |
|
99
|
|
|
*/ |
|
100
|
660 |
|
public function getFullMatchLength(): int |
|
101
|
|
|
{ |
|
102
|
660 |
|
return \mb_strlen($this->matches[0]); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return string[] Similar to preg_match(), index 0 will contain the full match, and any other array elements will be captured sub-matches |
|
107
|
|
|
* |
|
108
|
|
|
* @psalm-return non-empty-array<string> |
|
109
|
|
|
*/ |
|
110
|
267 |
|
public function getMatches(): array |
|
111
|
|
|
{ |
|
112
|
267 |
|
return $this->matches; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string[] |
|
117
|
|
|
*/ |
|
118
|
123 |
|
public function getSubMatches(): array |
|
119
|
|
|
{ |
|
120
|
123 |
|
return \array_slice($this->matches, 1); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string[] $matches |
|
125
|
|
|
* |
|
126
|
|
|
* @psalm-param non-empty-array<string> $matches |
|
127
|
|
|
*/ |
|
128
|
2004 |
|
public function withMatches(array $matches): InlineParserContext |
|
129
|
|
|
{ |
|
130
|
2004 |
|
$ctx = clone $this; |
|
131
|
|
|
|
|
132
|
2004 |
|
$ctx->matches = $matches; |
|
133
|
|
|
|
|
134
|
2004 |
|
return $ctx; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|