Skip to content

Commit

Permalink
Changes for antlr#36
Browse files Browse the repository at this point in the history
  • Loading branch information
kaby76 committed Dec 30, 2022
1 parent 947247d commit c2551b4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Atn/ATNConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ public function equals(object $other): bool
}

return $other instanceof self
&& $this->state->stateNumber === $other->state->stateNumber
&& $this->alt === $other->alt
&& $this->isPrecedenceFilterSuppressed() === $other->isPrecedenceFilterSuppressed()
&& $this->semanticContext->equals($other->semanticContext)
&& Equality::equals($this->state, $other->state)
&& Equality::equals($this->context, $other->context);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Atn/ATNSimulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Antlr\Antlr4\Runtime\Atn;

use Antlr\Antlr4\Runtime\Dfa\DFAState;
use Antlr\Antlr4\Runtime\PredictionContexts\IdentityHashMap;
use Antlr\Antlr4\Runtime\PredictionContexts\PredictionContext;
use Antlr\Antlr4\Runtime\PredictionContexts\PredictionContextCache;

Expand Down Expand Up @@ -92,7 +93,7 @@ public function getSharedContextCache(): PredictionContextCache

public function getCachedContext(PredictionContext $context): PredictionContext
{
$visited = [];
$visited = new IdentityHashMap();

return PredictionContext::getCachedPredictionContext(
$context,
Expand Down
6 changes: 3 additions & 3 deletions src/Atn/LexerATNConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public function equals(object $other): bool
return false;
}

if (!parent::equals($other)) {
if ($this->passedThroughNonGreedyDecision !== $other->passedThroughNonGreedyDecision) {
return false;
}

if ($this->passedThroughNonGreedyDecision !== $other->passedThroughNonGreedyDecision) {
if (!Equality::equals($this->lexerActionExecutor, $other->lexerActionExecutor)) {
return false;
}

return Equality::equals($this->lexerActionExecutor, $other->lexerActionExecutor);
return parent::equals($other);
}

private static function checkNonGreedyDecision(LexerATNConfig $source, ATNState $target): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Atn/States/ATNState.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function __toString(): string

public function hashCode(): int
{
return $this->getStateType();
return $this->stateNumber;
}

abstract public function getStateType(): int;
Expand Down
47 changes: 47 additions & 0 deletions src/PredictionContexts/IdentityHashMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Antlr\Antlr4\Runtime\PredictionContexts;

use Antlr\Antlr4\Runtime\Comparison\Equivalence;
use Antlr\Antlr4\Runtime\Comparison\Hashable;
use Antlr\Antlr4\Runtime\Utils\Map;

/**
* @extends Map<PredictionContext,PredictionContext>
*/
class IdentityHashMap extends Map
{
public function __construct()
{
parent::__construct(new class implements Equivalence {
public function equivalent(Hashable $left, Hashable $right): bool
{
if (! $left instanceof PredictionContext) {
return false;
}

if (! $right instanceof PredictionContext) {
return false;
}

return $left === $right;
}

public function hash(Hashable $value): int
{
if (! $value instanceof PredictionContext) {
return 0;
}

return $value->hashCode();
}

public function equals(object $other): bool
{
return $other instanceof self;
}
});
}
}
15 changes: 6 additions & 9 deletions src/PredictionContexts/PredictionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,19 +605,16 @@ protected static function combineCommonParents(array &$parents): void
}
}

/**
* @param array<PredictionContext|null> $visited
*/
public static function getCachedPredictionContext(
PredictionContext $context,
PredictionContextCache $contextCache,
array &$visited,
IdentityHashMap &$visited,
): self {
if ($context->isEmpty()) {
return $context;
}

$existing = $visited[\spl_object_id($context)] ?? null;
$existing = $visited->get($context);

if ($existing !== null) {
return $existing;
Expand All @@ -626,7 +623,7 @@ public static function getCachedPredictionContext(
$existing = $contextCache->get($context);

if ($existing !== null) {
$visited[\spl_object_id($context)] = $existing;
$visited->put($context, $existing);

return $existing;
}
Expand Down Expand Up @@ -660,7 +657,7 @@ public static function getCachedPredictionContext(
if (!$changed) {
$contextCache->add($context);

$visited[\spl_object_id($context)] = $context;
$visited->put($context, $context);

return $context;
}
Expand All @@ -680,8 +677,8 @@ public static function getCachedPredictionContext(
}

$contextCache->add($updated);
$visited[\spl_object_id($updated)] = $updated;
$visited[\spl_object_id($context)] = $updated;
$visited->put($updated, $updated);
$visited->put($context, $updated);

return $updated;
}
Expand Down

0 comments on commit c2551b4

Please sign in to comment.