Skip to content

Commit bb86a02

Browse files
committed
Treat regex assertions as zero-width and groups in negative assertions as never set
Amp-Thread-ID: https://ampcode.com/threads/T-01a177b5-960f-71d2-a3e2-fa7649f39d2c
1 parent 85e691f commit bb86a02

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎src/Type/Regex/RegexGroupParser.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ final class RegexGroupParser
4949
// to avoid combinatorial explosion from nested optional/bounded quantifications
5050
private const LITERALS_LIMIT = 100;
5151

52+
private const NEGATIVE_ASSERTIONS = ['#negativelookahead', '#negativelookbehind'];
53+
54+
private const ASSERTIONS = ['#lookahead', '#lookbehind', '#negativelookahead', '#negativelookbehind'];
55+
5256
private static ?RegexParser $parser = null;
5357

5458
/** @var array<string, ?RegexNode> */
@@ -268,6 +272,15 @@ private function walkRegexAst(
268272
true,
269273
);
270274
$parentGroup = $group;
275+
} elseif (in_array($ast->getId(), self::NEGATIVE_ASSERTIONS, true)) {
276+
// capturing groups in a negative assertion are never set when the whole pattern matches
277+
$group = new RegexNonCapturingGroup(
278+
$alternation,
279+
true,
280+
$parentGroup,
281+
false,
282+
);
283+
$parentGroup = $group;
271284
}
272285

273286
$inOptionalQuantification = false;
@@ -478,6 +491,11 @@ private function walkGroupAst(
478491
RegexGroupWalkResult $walkResult,
479492
): RegexGroupWalkResult
480493
{
494+
if (in_array($ast->getId(), self::ASSERTIONS, true)) {
495+
// assertions are zero-width, they do not add anything to the matched string
496+
return $walkResult;
497+
}
498+
481499
$children = $ast->getChildren();
482500
$quantifiedLiterals = null;
483501

@@ -741,6 +759,10 @@ private function repeatLiterals(array $literals, int $min, int $max): ?array
741759

742760
private function isMaybeEmptyNode(RegexNode $node, string $patternModifiers, bool &$isNonFalsy): bool
743761
{
762+
if (in_array($node->getId(), self::ASSERTIONS, true)) {
763+
return true;
764+
}
765+
744766
if ($node->getId() === '#quantification') {
745767
[$min] = $this->getQuantificationRange($node);
746768

‎tests/PHPStan/Analyser/nsrt/preg_match_shapes.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,3 +1088,32 @@ function testExtendedSyntaxEscapedHash(string $string): void {
10881088
assertType('array{non-falsy-string, numeric-string, non-empty-string}', $matches);
10891089
}
10901090
}
1091+
1092+
function testAssertionsAreZeroWidth(string $s): void {
1093+
// from php-src ext/pcre/tests/bug66121.phpt
1094+
if (preg_match('/(?<!k)/u', $s, $matches)) {
1095+
assertType('array{string}', $matches);
1096+
}
1097+
1098+
// from PCRE2 testdata/testinput1
1099+
if (preg_match('/(?=C)/', $s, $matches)) {
1100+
assertType('array{string}', $matches);
1101+
}
1102+
1103+
// from PCRE2 testdata/testinput1
1104+
if (preg_match('/(\.\d\d((?=0)|\d(?=\d)))/', $s, $matches)) {
1105+
assertType('array{non-falsy-string, non-falsy-string, string}', $matches);
1106+
}
1107+
1108+
// from PCRE2 testdata/testinput1
1109+
if (preg_match('/$(?<=^(a))/', $s, $matches)) {
1110+
assertType("array{string, 'a'}", $matches);
1111+
}
1112+
}
1113+
1114+
function testGroupsInNegativeAssertionsAreNeverSet(string $s): void {
1115+
// from PCRE2 testdata/testinput1
1116+
if (preg_match('/(?<!(c|d))[ab]/', $s, $matches)) {
1117+
assertType("array{0: non-falsy-string, 1?: 'c'|'d'}", $matches);
1118+
}
1119+
}

0 commit comments

Comments
 (0)