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

Fix for #12 -- Allowed memory size of 134217728 bytes exhausted. #34

Open
wants to merge 18 commits 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require-dev": {
"ergebnis/composer-normalize": "^2.15",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^1.4",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-strict-rules": "^1.1",
"slevomat/coding-standard": "^7.0",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions 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 Expand Up @@ -180,7 +180,7 @@ public function __toString(): string
$this->semanticContext->equals(SemanticContext::none())
? ''
: ',' . $this->semanticContext,
$this->reachesIntoOuterContext > 0 ? ',up=' . $this->reachesIntoOuterContext : '',
$this->reachesIntoOuterContext > 0 ? ',up=' . $this->getOuterContextDepth() : '',
);
}
}
52 changes: 19 additions & 33 deletions src/Atn/ATNConfigSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Antlr\Antlr4\Runtime\Atn;

use Antlr\Antlr4\Runtime\Atn\SemanticContexts\SemanticContext;
use Antlr\Antlr4\Runtime\Atn\States\ATNState;
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;
Expand Down Expand Up @@ -34,7 +34,8 @@ class ATNConfigSet implements Hashable
* All configs but hashed by (s, i, _, pi) not including context. Wiped out
* when we go readonly as this set becomes a DFA state.
*/
public ?Set $configLookup = null;
// ConfigHashSet : Dictionary<ATNConfig, ATNConfig>
public ?ConfigHashSet $configLookup = null;

/**
* Track the elements as they are added to the set; supports get(i).
Expand Down Expand Up @@ -82,32 +83,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 ConfigHashSet();

$this->fullCtx = $fullCtx;
}
Expand All @@ -133,14 +109,14 @@ public function add(ATNConfig $config, ?DoubleKeyMap $mergeCache = null): bool
$this->hasSemanticContext = true;
}

if ($config->reachesIntoOuterContext > 0) {
if ($config->getOuterContextDepth() > 0) {
$this->dipsIntoOuterContext = true;
}

/** @var ATNConfig $existing */
$existing = $this->configLookup->getOrAdd($config);

if ($existing->equals($config)) {
if ($existing === $config) {
$this->cachedHashCode = null;

$this->configs[] = $config; // track order here
Expand All @@ -149,12 +125,14 @@ public function add(ATNConfig $config, ?DoubleKeyMap $mergeCache = null): bool
}

// A previous (s,i,pi,_), merge with it and save result
/** @var bool $rootIsWildcard */
$rootIsWildcard = !$this->fullCtx;

if ($existing->context === null || $config->context === null) {
throw new \LogicException('Unexpected null context.');
}

/** @var PredictionContext $merged */
$merged = PredictionContext::merge($existing->context, $config->context, $rootIsWildcard, $mergeCache);

// No need to check for existing->context, config->context in cache
Expand Down Expand Up @@ -186,8 +164,12 @@ public function elements(): array
return $this->configs;
}

/**
* @return Set<ATNState>
*/
public function getStates(): Set
{
/** @var Set<ATNState> $states */
$states = new Set();
foreach ($this->configs as $config) {
$states->add($config->state);
Expand Down Expand Up @@ -305,6 +287,10 @@ public function contains(object $item): bool
throw new \InvalidArgumentException('This method is not implemented for readonly sets.');
}

if (!($item instanceof ATNConfig)) {
return false;
}

return $this->configLookup->contains($item);
}

Expand All @@ -325,8 +311,8 @@ public function clear(): void
}

$this->configs = [];
$this->cachedHashCode = -1;
$this->configLookup = new Set();
$this->cachedHashCode = null;
$this->configLookup = new ConfigHashSet();
}

public function isReadOnly(): bool
Expand Down Expand Up @@ -358,7 +344,7 @@ public function __toString(): string
return \sprintf(
'[%s]%s%s%s%s',
\implode(', ', $this->configs),
$this->hasSemanticContext ? ',hasSemanticContext=' . $this->hasSemanticContext : '',
$this->hasSemanticContext ? ',hasSemanticContext=true' : '',
$this->uniqueAlt !== ATN::INVALID_ALT_NUMBER ? ',uniqueAlt=' . $this->uniqueAlt : '',
$this->conflictingAlts !== null ? ',conflictingAlts=' . $this->conflictingAlts : '',
$this->dipsIntoOuterContext ? ',dipsIntoOuterContext' : '',
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
81 changes: 81 additions & 0 deletions src/Atn/ConfigHashSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Antlr\Antlr4\Runtime\Atn;

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

/**
* The reason that we need this is because we don't want the hash map to use
* the standard hash code and equals. We need all configurations with the same
* {@code (s,i,_,semctx)} to be equal. Unfortunately, this key effectively doubles
* the number of objects associated with ATNConfigs. The other solution is to
* use a hash table that lets us specify the equals/hashcode operation.
*
* @extends Map<ATNConfig, ATNConfig>
*/
final class ConfigHashSet extends Map
{
public function __construct(?Equivalence $comparer = null)
{
if ($comparer === null) {
parent::__construct(new class implements Equivalence {
public function equivalent(Hashable $left, Hashable $right): bool
{
if (! $left instanceof ATNConfig) {
return false;
}

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

if ($left === $right) {
return true;
}

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

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

return Hasher::hash(
$value->state->stateNumber,
$value->alt,
$value->semanticContext,
);
}

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

public function getOrAdd(ATNConfig $config): ATNConfig
{
/** @var ?ATNConfig $existing */
$existing = null;
if ($this->tryGetValue($config, $existing)) {
return $existing;
} else {
$this->put($config, $config);

return $config;
}
}
}
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
34 changes: 32 additions & 2 deletions src/Atn/OrderedATNConfigSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,44 @@

namespace Antlr\Antlr4\Runtime\Atn;

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

final class OrderedATNConfigSet extends ATNConfigSet
{
public function __construct()
{
parent::__construct();

$this->configLookup = new Set();
$this->configLookup = new ConfigHashSet(new class implements Equivalence {
public function equivalent(Hashable $left, Hashable $right): bool
{
if ($left === $right) {
return true;
}

/** @phpstan-ignore-next-line */
if ($left === null) {
return false;
}

/** @phpstan-ignore-next-line */
if ($right === null) {
return false;
}

return $left->equals($right);
}

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

public function equals(object $other): bool
{
return $other instanceof self;
}
});
}
}
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
Loading