Skip to content

Commit c3b490f

Browse files
committed
Walk the parser's own tree
The translation layer between the parser's AST and the shape walkers is gone: the walkers read the nodes the parser built, the alternations come without their separator tokens and the empty branches without their patch-up passes, and the quantifier bounds come from the parser's own reading instead of a re-parse of the token text. IgnoredRegexValidator reads the same tree, its branch text sliced from the source between node positions so escapes come back as they were written. A padded hex escape still types 'ax{ 41 }' for PHP 7.4 and the letter A on 8.x, the inline (?i:...) groups still drop the constants their caseless body cannot promise, and every resolver fixture says the same thing it said through the translation: 1873 of 1873.
1 parent 53cc9c4 commit c3b490f

8 files changed

Lines changed: 445 additions & 949 deletions

File tree

‎src/Command/IgnoredRegexValidator.php‎

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
use Nette\Utils\Strings;
66
use PHPRegex\Parser\Exception\RegexException;
7+
use PHPRegex\Parser\Node\AlternationNode;
8+
use PHPRegex\Parser\Node\AnchorNode;
9+
use PHPRegex\Parser\Node\NodeInterface;
10+
use PHPRegex\Parser\Node\SequenceNode;
711
use PHPStan\PhpDoc\TypeStringResolver;
812
use PHPStan\PhpDocParser\Parser\ParserException;
913
use PHPStan\ShouldNotHappenException;
1014
use PHPStan\Type\ObjectType;
11-
use PHPStan\Type\Regex\PHPRegexTreeNodeBuilder;
12-
use PHPStan\Type\Regex\RegexTreeNode;
15+
use PHPStan\Type\Regex\PHPRegexReader;
1316
use PHPStan\Type\VerbosityLevel;
1417
use function count;
1518
use function strrpos;
@@ -20,7 +23,7 @@ final class IgnoredRegexValidator
2023

2124
public function __construct(
2225
private TypeStringResolver $typeStringResolver,
23-
private PHPRegexTreeNodeBuilder $treeNodeBuilder,
26+
private PHPRegexReader $reader,
2427
)
2528
{
2629
}
@@ -31,7 +34,7 @@ public function validate(string $regex): IgnoredRegexValidatorResult
3134
$regex = $this->removeDelimiters($regex);
3235

3336
try {
34-
$ast = $this->treeNodeBuilder->parse($regex, '', $delimiter);
37+
$root = $this->reader->read($regex, '', $delimiter);
3538
} catch (RegexException) {
3639
return new IgnoredRegexValidatorResult(ignoredTypes: [], anchorsInTheMiddle: false, allErrorsIgnored: false);
3740
}
@@ -44,29 +47,24 @@ public function validate(string $regex): IgnoredRegexValidatorResult
4447
}
4548

4649
return new IgnoredRegexValidatorResult(
47-
ignoredTypes: $this->getIgnoredTypes($ast),
48-
anchorsInTheMiddle: $this->hasAnchorsInTheMiddle($ast),
50+
ignoredTypes: $this->getIgnoredTypes($root->pattern, $root->source ?? $regex),
51+
anchorsInTheMiddle: $this->hasAnchorsInTheMiddle($root->pattern),
4952
allErrorsIgnored: false,
5053
);
5154
}
5255

5356
/**
5457
* @return array<string, string>
5558
*/
56-
private function getIgnoredTypes(RegexTreeNode $ast): array
59+
private function getIgnoredTypes(NodeInterface $ast, string $source): array
5760
{
58-
$alternation = $ast->getChild(0);
59-
if ($alternation === null) {
60-
return [];
61-
}
62-
63-
if ($alternation->getId() !== '#alternation') {
61+
if (!$ast instanceof AlternationNode) {
6462
return [];
6563
}
6664

6765
$types = [];
68-
foreach ($alternation->getChildren() as $child) {
69-
$text = $this->getText($child);
66+
foreach ($ast->alternatives as $alternative) {
67+
$text = $this->getText($alternative, $source);
7068
if ($text === null) {
7169
continue;
7270
}
@@ -109,16 +107,17 @@ private function removeDelimiters(string $regex): string
109107
return substr($regex, 1, $endDelimiterPosition - 1);
110108
}
111109

112-
private function getText(RegexTreeNode $treeNode): ?string
110+
/**
111+
* The text of a branch as it was written, escapes included: the tree
112+
* decodes them, so each leaf is sliced from the source between its
113+
* positions, whatever the leaf spells.
114+
*/
115+
private function getText(NodeInterface $node, string $source): ?string
113116
{
114-
if ($treeNode->getId() === 'token') {
115-
return $treeNode->getValueValue();
116-
}
117-
118-
if ($treeNode->getId() === '#concatenation') {
117+
if ($node instanceof SequenceNode) {
119118
$fullText = '';
120-
foreach ($treeNode->getChildren() as $child) {
121-
$text = $this->getText($child);
119+
foreach ($node->children as $child) {
120+
$text = $this->getText($child, $source);
122121
if ($text === null) {
123122
continue;
124123
}
@@ -133,22 +132,26 @@ private function getText(RegexTreeNode $treeNode): ?string
133132
return $fullText;
134133
}
135134

136-
return null;
135+
if ($node->getChildren() !== []) {
136+
return null;
137+
}
138+
139+
return substr($source, $node->getStartPosition(), $node->getEndPosition() - $node->getStartPosition());
137140
}
138141

139-
private function hasAnchorsInTheMiddle(RegexTreeNode $ast): bool
142+
private function hasAnchorsInTheMiddle(NodeInterface $ast): bool
140143
{
141-
if ($ast->getId() === 'token') {
142-
$valueArray = $ast->getValue();
143-
144-
return $valueArray['token'] === 'anchor' && $valueArray['value'] === '$';
144+
if ($ast instanceof AnchorNode) {
145+
return $ast->value === '$';
145146
}
146-
$childrenCount = count($ast->getChildren());
147-
foreach ($ast->getChildren() as $i => $child) {
147+
148+
$children = $ast instanceof SequenceNode ? $ast->children : $ast->getChildren();
149+
$childrenCount = count($children);
150+
foreach ($children as $i => $child) {
148151
$has = $this->hasAnchorsInTheMiddle($child);
149152
if (
150153
$has
151-
&& ($ast->getId() !== '#concatenation' || $i !== $childrenCount - 1)
154+
&& (!$ast instanceof SequenceNode || $i !== $childrenCount - 1)
152155
) {
153156
return true;
154157
}

‎src/DependencyInjection/ValidateIgnoredErrorsExtension.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PHPStan\Analyser\NameScope;
1111
use PHPStan\Command\IgnoredRegexValidator;
1212
use PHPStan\File\FileExcluder;
13-
use PHPStan\Type\Regex\PHPRegexTreeNodeBuilder;
13+
use PHPStan\Type\Regex\PHPRegexReader;
1414
use PHPStan\Php\ComposerPhpVersionFactory;
1515
use PHPStan\Php\ConfiguredPhpVersionRangeHelper;
1616
use PHPStan\Php\PcreVersion;
@@ -129,7 +129,7 @@ public function resolveTypeAlias(string $aliasName, NameScope $nameScope): ?Type
129129
// ignoreErrors patterns are matched by the running PHPStan
130130
// itself, so their read is judged for the running PHP and
131131
// its engine
132-
new PHPRegexTreeNodeBuilder(
132+
new PHPRegexReader(
133133
new PhpVersion(PHP_VERSION_ID),
134134
PcreVersion::fromRuntime(),
135135
),

‎src/Type/Regex/PHPRegexReader.php‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Regex;
4+
5+
use PHPRegex\Parser\Exception\RegexException;
6+
use PHPRegex\Parser\Node\RegexNode;
7+
use PHPRegex\Parser\PcreTarget;
8+
use PHPRegex\Parser\RegexParser;
9+
use PHPStan\DependencyInjection\AutowiredService;
10+
use PHPStan\Php\PcreVersion;
11+
use PHPStan\Php\PhpVersion;
12+
13+
/**
14+
* Reads a pattern body into the PHPRegex AST, judged for the analysed
15+
* project: the analysed PHP and its bundled engine, narrowed below it when
16+
* the config or composer.json names an older one — a pin never raises the
17+
* judged engine above what that PHP can run. A padded \x{ 41 } escape is
18+
* the letter A on the engines PHP 8.x ships with and the letter x then
19+
* text on PHP 7.4's, and the trees follow that verdict.
20+
*/
21+
#[AutowiredService]
22+
final class PHPRegexReader
23+
{
24+
25+
private ?RegexParser $parser = null;
26+
27+
public function __construct(
28+
private PhpVersion $phpVersion,
29+
private PcreVersion $pcreVersion,
30+
)
31+
{
32+
}
33+
34+
/**
35+
* @param string $rawRegex the pattern body, delimiters and modifiers stripped
36+
* @param string $modifiers the pattern modifiers as RegexGroupParser read them
37+
* @param string $delimiter the opening delimiter of the pattern as written
38+
*
39+
* @throws RegexException when the pattern body cannot be read
40+
*/
41+
public function read(string $rawRegex, string $modifiers, string $delimiter): RegexNode
42+
{
43+
$this->parser ??= RegexParser::create($this->targetOptions());
44+
45+
return $this->parser->parsePattern($rawRegex, $modifiers, $delimiter);
46+
}
47+
48+
/**
49+
* @return array<string, int|string>
50+
*/
51+
private function targetOptions(): array
52+
{
53+
$phpVersionId = $this->phpVersion->getVersionId();
54+
$bundled = PcreTarget::bundledWith($phpVersionId);
55+
56+
$options = ['php_version' => $phpVersionId];
57+
if (!$this->pcreVersion->atLeast($bundled->pcreVersion)) {
58+
$options['pcre_version'] = $this->pcreVersion->getVersionString();
59+
}
60+
61+
return $options;
62+
}
63+
64+
}

0 commit comments

Comments
 (0)