Skip to content

Commit c2551b4

Browse files
committed
Changes for antlr#36
1 parent 947247d commit c2551b4

File tree

6 files changed

+60
-15
lines changed

6 files changed

+60
-15
lines changed

src/Atn/ATNConfig.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ public function equals(object $other): bool
128128
}
129129

130130
return $other instanceof self
131+
&& $this->state->stateNumber === $other->state->stateNumber
131132
&& $this->alt === $other->alt
132133
&& $this->isPrecedenceFilterSuppressed() === $other->isPrecedenceFilterSuppressed()
133134
&& $this->semanticContext->equals($other->semanticContext)
134-
&& Equality::equals($this->state, $other->state)
135135
&& Equality::equals($this->context, $other->context);
136136
}
137137

src/Atn/ATNSimulator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Antlr\Antlr4\Runtime\Atn;
66

77
use Antlr\Antlr4\Runtime\Dfa\DFAState;
8+
use Antlr\Antlr4\Runtime\PredictionContexts\IdentityHashMap;
89
use Antlr\Antlr4\Runtime\PredictionContexts\PredictionContext;
910
use Antlr\Antlr4\Runtime\PredictionContexts\PredictionContextCache;
1011

@@ -92,7 +93,7 @@ public function getSharedContextCache(): PredictionContextCache
9293

9394
public function getCachedContext(PredictionContext $context): PredictionContext
9495
{
95-
$visited = [];
96+
$visited = new IdentityHashMap();
9697

9798
return PredictionContext::getCachedPredictionContext(
9899
$context,

src/Atn/LexerATNConfig.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ public function equals(object $other): bool
6363
return false;
6464
}
6565

66-
if (!parent::equals($other)) {
66+
if ($this->passedThroughNonGreedyDecision !== $other->passedThroughNonGreedyDecision) {
6767
return false;
6868
}
6969

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

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

7777
private static function checkNonGreedyDecision(LexerATNConfig $source, ATNState $target): bool

src/Atn/States/ATNState.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function __toString(): string
152152

153153
public function hashCode(): int
154154
{
155-
return $this->getStateType();
155+
return $this->stateNumber;
156156
}
157157

158158
abstract public function getStateType(): int;
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Antlr\Antlr4\Runtime\PredictionContexts;
6+
7+
use Antlr\Antlr4\Runtime\Comparison\Equivalence;
8+
use Antlr\Antlr4\Runtime\Comparison\Hashable;
9+
use Antlr\Antlr4\Runtime\Utils\Map;
10+
11+
/**
12+
* @extends Map<PredictionContext,PredictionContext>
13+
*/
14+
class IdentityHashMap extends Map
15+
{
16+
public function __construct()
17+
{
18+
parent::__construct(new class implements Equivalence {
19+
public function equivalent(Hashable $left, Hashable $right): bool
20+
{
21+
if (! $left instanceof PredictionContext) {
22+
return false;
23+
}
24+
25+
if (! $right instanceof PredictionContext) {
26+
return false;
27+
}
28+
29+
return $left === $right;
30+
}
31+
32+
public function hash(Hashable $value): int
33+
{
34+
if (! $value instanceof PredictionContext) {
35+
return 0;
36+
}
37+
38+
return $value->hashCode();
39+
}
40+
41+
public function equals(object $other): bool
42+
{
43+
return $other instanceof self;
44+
}
45+
});
46+
}
47+
}

src/PredictionContexts/PredictionContext.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -605,19 +605,16 @@ protected static function combineCommonParents(array &$parents): void
605605
}
606606
}
607607

608-
/**
609-
* @param array<PredictionContext|null> $visited
610-
*/
611608
public static function getCachedPredictionContext(
612609
PredictionContext $context,
613610
PredictionContextCache $contextCache,
614-
array &$visited,
611+
IdentityHashMap &$visited,
615612
): self {
616613
if ($context->isEmpty()) {
617614
return $context;
618615
}
619616

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

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

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

631628
return $existing;
632629
}
@@ -660,7 +657,7 @@ public static function getCachedPredictionContext(
660657
if (!$changed) {
661658
$contextCache->add($context);
662659

663-
$visited[\spl_object_id($context)] = $context;
660+
$visited->put($context, $context);
664661

665662
return $context;
666663
}
@@ -680,8 +677,8 @@ public static function getCachedPredictionContext(
680677
}
681678

682679
$contextCache->add($updated);
683-
$visited[\spl_object_id($updated)] = $updated;
684-
$visited[\spl_object_id($context)] = $updated;
680+
$visited->put($updated, $updated);
681+
$visited->put($context, $updated);
685682

686683
return $updated;
687684
}

0 commit comments

Comments
 (0)