Skip to content

Regex parsing fixes for newline characters and marker verbs #3268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions resources/RegexGrammar.pp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
// @license New BSD License
//


// Skip.
%skip nl \n

// Character classes.
%token negative_class_ \[\^ -> class
%token class_ \[ -> class
Expand All @@ -58,7 +54,7 @@
%token class:character \\([aefnrtb]|c[\x00-\x7f])
%token class:dynamic_character \\([0-7]{3}|x[0-9a-zA-Z]{2}|x{[0-9a-zA-Z]+})
%token class:character_type \\([CdDhHNRsSvVwWX]|[pP]{[^}]+})
%token class:literal \\.|.
%token class:literal \\.|.|\n

// Internal options.
// See https://www.regular-expressions.info/refmodifiers.html
Expand All @@ -82,6 +78,11 @@
%token co:_comment \) -> default
%token co:comment .*?(?=(?<!\\)\))

// Marker verbs
%token marker_ \(\*: -> mark
%token mark:name [^)]+
%token mark:_marker \) -> default

// Capturing group.
%token named_capturing_ \(\?P?< -> nc
%token nc:_named_capturing > -> default
Expand Down Expand Up @@ -122,7 +123,7 @@
%token character_type \\([CdDhHNRsSvVwWX]|[pP]{[^}]+})
%token anchor \\([bBAZzG])|\^|\$
%token match_point_reset \\K
%token literal \\.|.
%token literal \\.|.|\n


// Rules.
Expand Down Expand Up @@ -190,7 +191,8 @@
| literal()

#capturing:
::comment_:: <comment>? ::_comment:: #comment
::marker_:: <name> ::_marker:: #mark
| ::comment_:: <comment>? ::_comment:: #comment
| (
::named_capturing_:: <capturing_name> ::_named_capturing:: #namedcapturing
| ::non_capturing_:: #noncapturing
Expand Down
27 changes: 24 additions & 3 deletions src/Type/Php/RegexArrayShapeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
// regex could not be parsed by Hoa/Regex
return null;
}
[$groupList, $groupCombinations] = $parseResult;
[$groupList, $groupCombinations, $hasMark] = $parseResult;

$trailingOptionals = 0;
foreach (array_reverse($groupList) as $captureGroup) {
Expand All @@ -152,6 +152,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
$wasMatched,
$trailingOptionals,
$flags ?? 0,
$hasMark,
);

if (!$this->containsUnmatchedAsNull($flags ?? 0)) {
Expand Down Expand Up @@ -189,6 +190,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
$wasMatched,
$trailingOptionals,
$flags ?? 0,
$hasMark,
);

$combiTypes[] = $combiType;
Expand All @@ -211,6 +213,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
$wasMatched,
$trailingOptionals,
$flags ?? 0,
$hasMark,
);
}

Expand Down Expand Up @@ -272,6 +275,7 @@ private function buildArrayType(
TrinaryLogic $wasMatched,
int $trailingOptionals,
int $flags,
bool $hasMark,
): Type
{
$builder = ConstantArrayTypeBuilder::createEmpty();
Expand Down Expand Up @@ -325,6 +329,14 @@ private function buildArrayType(
$i++;
}

if ($hasMark) {
$builder->setOffsetValueType(
$this->getKeyType('MARK'),
new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]),
true,
);
}

return $builder->getArray();
}

Expand Down Expand Up @@ -372,7 +384,7 @@ private function getValueType(Type $baseType, int $flags): Type
}

/**
* @return array{array<int, RegexCapturingGroup>, array<int, array<int, int[]>>}|null
* @return array{array<int, RegexCapturingGroup>, array<int, array<int, int[]>>, bool}|null
*/
private function parseGroups(string $regex): ?array
{
Expand All @@ -398,6 +410,7 @@ private function parseGroups(string $regex): ?array
$groupCombinations = [];
$alternationId = -1;
$captureGroupId = 100;
$hasMark = false;
$this->walkRegexAst(
$ast,
false,
Expand All @@ -408,9 +421,10 @@ private function parseGroups(string $regex): ?array
$captureGroupId,
$capturingGroups,
$groupCombinations,
$hasMark,
);

return [$capturingGroups, $groupCombinations];
return [$capturingGroups, $groupCombinations, $hasMark];
}

/**
Expand All @@ -427,6 +441,7 @@ private function walkRegexAst(
int &$captureGroupId,
array &$capturingGroups,
array &$groupCombinations,
bool &$hasMark,
): void
{
$group = null;
Expand Down Expand Up @@ -483,6 +498,11 @@ private function walkRegexAst(
$inAlternation = true;
}

if ($ast->getId() === '#mark') {
$hasMark = true;
return;
}

if ($group instanceof RegexCapturingGroup) {
$capturingGroups[$group->getId()] = $group;

Expand All @@ -506,6 +526,7 @@ private function walkRegexAst(
$captureGroupId,
$capturingGroups,
$groupCombinations,
$hasMark,
);

if ($ast->getId() !== '#alternation') {
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/nsrt/preg_match_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ function bug11323(string $s): void {
if (preg_match('{([^1-4])}', $s, $matches)) {
assertType('array{string, non-empty-string}', $matches);
}
if (preg_match("{([\r\n]+)(\n)([\n])}", $s, $matches)) {
assertType('array{string, non-empty-string, non-empty-string, non-empty-string}', $matches);
}
if (preg_match('/foo(*:first)|bar(*:second)([x])/', $s, $matches)) {
assertType('array{0: string, 1?: non-empty-string, MARK?: non-empty-string}', $matches);
}
}

function (string $s): void {
Expand Down
Loading