Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
105 changes: 105 additions & 0 deletions src/Type/Regex/RegexAstWalkResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Regex;

/** @immutable */
final class RegexAstWalkResult
{

/**
* @param array<int, RegexCapturingGroup> $capturingGroups
* @param list<string> $markVerbs
*/
public function __construct(
private int $alternationId,
private int $captureGroupId,
private array $capturingGroups,
private array $markVerbs,
)
{
}

public static function createEmpty(): self
{
return new self(
-1,
// use different start-index for groups to make it easier to distinguish groupids from other ids
100,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you moved the code only, but why 100?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the initial implementation there we so many arrays involved, that I used a different start-index to trigger php warnings in case I mixed up the keys between different arrays ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a comment into the code

[],
[],
);
}

public function nextAlternationId(): self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name it better, it does not return int

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it returns a "copy" of the current object, which contains the next alternation id.

I think the name fits.

{
return new self(
$this->alternationId + 1,
$this->captureGroupId,
$this->capturingGroups,
$this->markVerbs,
);
}

public function nextCaptureGroupId(): self
{
return new self(
$this->alternationId,
$this->captureGroupId + 1,
$this->capturingGroups,
$this->markVerbs,
);
}

public function addCapturingGroup(RegexCapturingGroup $group): self
{
$capturingGroups = $this->capturingGroups;
$capturingGroups[$group->getId()] = $group;

return new self(
$this->alternationId,
$this->captureGroupId,
$capturingGroups,
$this->markVerbs,
);
}

public function markVerb(string $markVerb): self
{
$verbs = $this->markVerbs;
$verbs[] = $markVerb;

return new self(
$this->alternationId,
$this->captureGroupId,
$this->capturingGroups,
$verbs,
);
}

public function getAlternationId(): int
{
return $this->alternationId;
}

public function getCaptureGroupId(): int
{
return $this->captureGroupId;
}

/**
* @return array<int, RegexCapturingGroup>
*/
public function getCapturingGroups(): array
{
return $this->capturingGroups;
}

/**
* @return list<string>
*/
public function getMarkVerbs(): array
{
return $this->markVerbs;
}

}
50 changes: 19 additions & 31 deletions src/Type/Regex/RegexGroupParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,51 +73,39 @@ public function parseGroups(string $regex): ?array
$captureOnlyNamed = str_contains($modifiers, 'n');
}

$capturingGroups = [];
$alternationId = -1;
$captureGroupId = 100;
$markVerbs = [];
$this->walkRegexAst(
$astWalkResult = $this->walkRegexAst(
$ast,
null,
$alternationId,
0,
false,
null,
$captureGroupId,
$capturingGroups,
$markVerbs,
$captureOnlyNamed,
false,
$modifiers,
RegexAstWalkResult::createEmpty(),
);

return [$capturingGroups, $markVerbs];
return [$astWalkResult->getCapturingGroups(), $astWalkResult->getMarkVerbs()];
}

/**
* @param array<int, RegexCapturingGroup> $capturingGroups
* @param list<string> $markVerbs
*/
private function walkRegexAst(
TreeNode $ast,
?RegexAlternation $alternation,
int &$alternationId,
int $combinationIndex,
bool $inOptionalQuantification,
RegexCapturingGroup|RegexNonCapturingGroup|null $parentGroup,
int &$captureGroupId,
array &$capturingGroups,
array &$markVerbs,
bool $captureOnlyNamed,
bool $repeatedMoreThanOnce,
string $patternModifiers,
): void
RegexAstWalkResult $astWalkResult,
): RegexAstWalkResult
{
$group = null;
if ($ast->getId() === '#capturing') {
$astWalkResult = $astWalkResult->nextCaptureGroupId();

$group = new RegexCapturingGroup(
$captureGroupId++,
$astWalkResult->getCaptureGroupId(),
null,
$alternation,
$inOptionalQuantification,
Expand All @@ -130,9 +118,11 @@ private function walkRegexAst(
);
$parentGroup = $group;
} elseif ($ast->getId() === '#namedcapturing') {
$astWalkResult = $astWalkResult->nextCaptureGroupId();

$name = $ast->getChild(0)->getValueValue();
$group = new RegexCapturingGroup(
$captureGroupId++,
$astWalkResult->getCaptureGroupId(),
$name,
$alternation,
$inOptionalQuantification,
Expand Down Expand Up @@ -176,40 +166,36 @@ private function walkRegexAst(
}

if ($ast->getId() === '#alternation') {
$alternationId++;
$alternation = new RegexAlternation($alternationId, count($ast->getChildren()));
$astWalkResult = $astWalkResult->nextAlternationId();
$alternation = new RegexAlternation($astWalkResult->getAlternationId(), count($ast->getChildren()));
}

if ($ast->getId() === '#mark') {
$markVerbs[] = $ast->getChild(0)->getValueValue();
return;
return $astWalkResult->markVerb($ast->getChild(0)->getValueValue());
}

if (
$group instanceof RegexCapturingGroup &&
(!$captureOnlyNamed || $group->isNamed())
) {
$capturingGroups[$group->getId()] = $group;
$astWalkResult = $astWalkResult->addCapturingGroup($group);

if ($alternation !== null) {
$alternation->pushGroup($combinationIndex, $group);
}
}

foreach ($ast->getChildren() as $child) {
$this->walkRegexAst(
$astWalkResult = $this->walkRegexAst(
$child,
$alternation,
$alternationId,
$combinationIndex,
$inOptionalQuantification,
$parentGroup,
$captureGroupId,
$capturingGroups,
$markVerbs,
$captureOnlyNamed,
$repeatedMoreThanOnce,
$patternModifiers,
$astWalkResult,
);

if ($ast->getId() !== '#alternation') {
Expand All @@ -218,6 +204,8 @@ private function walkRegexAst(

$combinationIndex++;
}

return $astWalkResult;
}

private function allowConstantTypes(
Expand Down
Loading