Repository navigation
Expand file tree
/
Copy pathModernizer.php
More file actions
381 lines (325 loc) · 11.4 KB
/
Copy pathModernizer.php
File metadata and controls
381 lines (325 loc) · 11.4 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
declare(strict_types=1);
/*
* This file is part of the PHPRegex package.
*
* (c) Younes ENNAJI <younes.ennaji.pro@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPRegex\Optimizer;
use PHPRegex\Parser\AbstractNodeVisitor;
use PHPRegex\Parser\Node\AlternationNode;
use PHPRegex\Parser\Node\AnchorNode;
use PHPRegex\Parser\Node\AssertionNode;
use PHPRegex\Parser\Node\BackrefNode;
use PHPRegex\Parser\Node\CalloutNode;
use PHPRegex\Parser\Node\CharClassNode;
use PHPRegex\Parser\Node\CharLiteralNode;
use PHPRegex\Parser\Node\CharTypeNode;
use PHPRegex\Parser\Node\ClassSetOperationNode;
use PHPRegex\Parser\Node\CommentNode;
use PHPRegex\Parser\Node\ConditionalNode;
use PHPRegex\Parser\Node\ControlCharNode;
use PHPRegex\Parser\Node\DefineNode;
use PHPRegex\Parser\Node\DotNode;
use PHPRegex\Parser\Node\ExtendedCharClassNode;
use PHPRegex\Parser\Node\GroupNode;
use PHPRegex\Parser\Node\GroupType;
use PHPRegex\Parser\Node\KeepNode;
use PHPRegex\Parser\Node\LimitMatchNode;
use PHPRegex\Parser\Node\LiteralNode;
use PHPRegex\Parser\Node\NodeInterface;
use PHPRegex\Parser\Node\PcreVerbNode;
use PHPRegex\Parser\Node\PosixClassNode;
use PHPRegex\Parser\Node\QuantifierNode;
use PHPRegex\Parser\Node\RangeNode;
use PHPRegex\Parser\Node\RegexNode;
use PHPRegex\Parser\Node\ScriptRunNode;
use PHPRegex\Parser\Node\SequenceNode;
use PHPRegex\Parser\Node\SubroutineNode;
use PHPRegex\Parser\Node\UnicodePropNode;
use PHPRegex\Parser\Node\VersionConditionNode;
/**
* Modernizes legacy or messy regular expressions into clean, concise PCRE2-compliant patterns.
*
* @extends AbstractNodeVisitor<NodeInterface>
*/
final class Modernizer extends AbstractNodeVisitor
{
/**
* The code points "\s" matches without Unicode properties.
*/
private const SPACES = [9, 10, 11, 12, 13, 32];
/**
* Whether "\s" and "\d" read Unicode properties: "/u", or "(*UCP)".
*/
private bool $unicodeProperties = false;
public function __construct(
/**
* The LC_CTYPE locale the pattern runs under, when it is known:
* "\s" stands for "[\t\n\x0B\f\r ]" under "C" and "POSIX" only,
* and null, the locale unknown, writes no "\s".
*/
private readonly ?string $locale = null,
) {}
#[\Override]
public function visitRegex(RegexNode $node)
{
$this->unicodeProperties = UnicodeProperties::on($node);
return new RegexNode(
$node->pattern->accept($this),
$node->flags,
$node->delimiter,
$node->getStartPosition(),
$node->getEndPosition(),
);
}
#[\Override]
public function visitCharClass(CharClassNode $node)
{
$parts = $node->expression instanceof AlternationNode
? $node->expression->alternatives
: [$node->expression];
// "\d" matches the ten ASCII digits under every locale, as C's
// isdigit() does; with Unicode properties it matches every decimal
// digit, as U+0663.
if (!$node->isNegated && !$this->unicodeProperties && 1 === \count($parts) && $parts[0] instanceof RangeNode) {
$range = $parts[0];
if ($range->start instanceof LiteralNode && $range->end instanceof LiteralNode
&& '0' === $range->start->value && '9' === $range->end->value) {
return new CharTypeNode('d', $node->getStartPosition(), $node->getEndPosition());
}
}
// "\s" matches HT, LF, VT, FF, CR and the space, and nothing else, on
// every PCRE2 release under the C locale. Without Unicode properties
// PHP builds its table from LC_CTYPE once a program calls
// setlocale(), where most locales give it the no-break space, byte
// 0xA0; with them, under "/u" or "(*UCP)", it matches U+00A0 too.
if (!$node->isNegated && !$this->unicodeProperties && \in_array($this->locale, ['C', 'POSIX'], true) && self::SPACES === $this->codePoints($parts)) {
return new CharTypeNode('s', $node->getStartPosition(), $node->getEndPosition());
}
// For other cases, keep as is but modernize parts
$modernizedParts = array_map(fn (NodeInterface $part): NodeInterface => $part->accept($this), $parts);
$expression = 1 === \count($modernizedParts)
? $modernizedParts[0]
: new AlternationNode($modernizedParts, $node->getStartPosition(), $node->getEndPosition());
return new CharClassNode($expression, $node->isNegated, $node->getStartPosition(), $node->getEndPosition());
}
#[\Override]
public function visitLiteral(LiteralNode $node): NodeInterface
{
// Literal values are already decoded; the printer handles escaping.
return $node;
}
#[\Override]
public function visitGroup(GroupNode $node): NodeInterface
{
$child = $node->child->accept($this);
// Alternations need their group to keep surrounding sequence items
// (including anchors) bound to every alternative when printed.
if (GroupType::NonCapturing === $node->type && null === $node->name && null === $node->flags && !$child instanceof AlternationNode) {
return $child;
}
return new GroupNode(
$child,
$node->type,
$node->name,
$node->flags,
$node->getStartPosition(),
$node->getEndPosition(),
false,
$node->scannedGroups,
);
}
#[\Override]
public function visitBackref(BackrefNode $node): NodeInterface
{
// "\1" becomes "\g{1}": the braces end the number, so no digit after
// it can turn it into another reference or an octal escape.
$digits = ltrim($node->ref, '\\');
if (\strlen($node->ref) - \strlen($digits) <= 1 && '' !== $digits && \strlen($digits) === strspn($digits, '0123456789')) {
return new BackrefNode('\g{'.$digits.'}', $node->getStartPosition(), $node->getEndPosition());
}
return $node;
}
// For other nodes, just recurse or return as is
#[\Override]
public function visitAlternation(AlternationNode $node): NodeInterface
{
$alternatives = array_map(fn (NodeInterface $alt): NodeInterface => $alt->accept($this), $node->alternatives);
return new AlternationNode($alternatives, $node->getStartPosition(), $node->getEndPosition());
}
#[\Override]
public function visitSequence(SequenceNode $node): NodeInterface
{
$children = array_map(fn (NodeInterface $n): NodeInterface => $n->accept($this), $node->children);
return new SequenceNode($children, $node->getStartPosition(), $node->getEndPosition());
}
#[\Override]
public function visitQuantifier(QuantifierNode $node): NodeInterface
{
return new QuantifierNode(
$node->node->accept($this),
$node->quantifier,
$node->type,
$node->getStartPosition(),
$node->getEndPosition(),
);
}
#[\Override]
public function visitAnchor(AnchorNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitAssertion(AssertionNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitDot(DotNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitCharType(CharTypeNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitRange(RangeNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitUnicodeProp(UnicodePropNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitCharLiteral(CharLiteralNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitPosixClass(PosixClassNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitComment(CommentNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitConditional(ConditionalNode $node)
{
// The reference of a condition is no back reference: "(?(\g{1})…)"
// is refused, so it keeps the spelling it was written with.
$condition = $node->condition instanceof BackrefNode ? $node->condition : $node->condition->accept($this);
return new ConditionalNode(
$condition,
$node->yes->accept($this),
$node->no->accept($this),
$node->getStartPosition(),
$node->getEndPosition(),
);
}
#[\Override]
public function visitSubroutine(SubroutineNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitPcreVerb(PcreVerbNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitDefine(DefineNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitLimitMatch(LimitMatchNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitCallout(CalloutNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitScriptRun(ScriptRunNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitVersionCondition(VersionConditionNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitKeep(KeepNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitControlChar(ControlCharNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitExtendedCharClass(ExtendedCharClassNode $node): NodeInterface
{
return $node;
}
#[\Override]
public function visitClassSetOperation(ClassSetOperationNode $node): NodeInterface
{
return $node;
}
/**
* The code points a class's members match, sorted, each once; null when
* a member is not a single character or a range of them.
*
* @param array<NodeInterface> $parts
*
* @return list<int>|null
*/
private function codePoints(array $parts): ?array
{
$codePoints = [];
foreach ($parts as $part) {
if ($part instanceof RangeNode) {
$start = $this->codePoint($part->start);
$end = $this->codePoint($part->end);
// A member above 255 is no member of "\s", which is read without
// Unicode properties here.
if (null === $start || null === $end || $start > $end || $end > 0xFF) {
return null;
}
array_push($codePoints, ...range($start, $end));
continue;
}
$codePoint = $this->codePoint($part);
if (null === $codePoint) {
return null;
}
$codePoints[] = $codePoint;
}
$codePoints = array_values(array_unique($codePoints));
sort($codePoints);
return $codePoints;
}
private function codePoint(NodeInterface $node): ?int
{
if ($node instanceof CharLiteralNode) {
return $node->codePoint;
}
return $node instanceof LiteralNode && 1 === \strlen($node->value) ? \ord($node->value) : null;
}
}