Skip to content
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

Name an anonymous class to allow serialization #28

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
27 changes: 1 addition & 26 deletions src/Atn/ATNConfigSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,7 @@ public function __construct(bool $fullCtx = true)
* not including context. Wiped out when we go readonly as this se
* becomes a DFA state.
*/
$this->configLookup = new Set(new class implements Equivalence {
public function equivalent(Hashable $left, Hashable $right): bool
{
if ($left === $right) {
return true;
}

if (!$left instanceof ATNConfig || !$right instanceof ATNConfig) {
return false;
}

return $left->alt === $right->alt
&& $left->semanticContext->equals($right->semanticContext)
&& Equality::equals($left->state, $right->state);
}

public function hash(Hashable $value): int
{
return $value->hashCode();
}

public function equals(object $other): bool
{
return $other instanceof self;
}
});
$this->configLookup = new Set(new ATNEquivalence());

$this->fullCtx = $fullCtx;
}
Expand Down
45 changes: 45 additions & 0 deletions src/Atn/ATNEquivalence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Antlr\Antlr4\Runtime\Atn;

use Antlr\Antlr4\Runtime\Comparison\Equality;
use Antlr\Antlr4\Runtime\Comparison\Equivalence;
use Antlr\Antlr4\Runtime\Comparison\Hashable;
use Antlr\Antlr4\Runtime\Comparison\Hasher;
use Antlr\Antlr4\Runtime\PredictionContexts\PredictionContext;


/**
* An equivalence class for configurations, {@see ATNConfigSet}.
*/
class ATNEquivalence implements Equivalence
Copy link
Author

Choose a reason for hiding this comment

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

Should we make this final ?

{

public function equivalent(Hashable $left, Hashable $right): bool
{
if ($left === $right) {
return true;
}

if (!($left instanceof ATNConfig) || !($right instanceof ATNConfig)) {
return false;
}

return $left->alt === $right->alt
&& $left->semanticContext->equals($right->semanticContext)
&& Equality::equals($left->state, $right->state);
}

public function hash(Hashable $value): int
{
return $value->hashCode();
}

public function equals(object $other): bool
{
return $other instanceof self;
}

}