1<?php
2
3declare(strict_types=1);
4
5namespace Antlr\Antlr4\Runtime\Atn\SemanticContexts;
6
7use Antlr\Antlr4\Runtime\Comparison\Hasher;
8use Antlr\Antlr4\Runtime\Recognizer;
9use Antlr\Antlr4\Runtime\RuleContext;
10
11final class Predicate extends SemanticContext
12{
13    /** @var int */
14    public $ruleIndex;
15
16    /** @var int */
17    public $predIndex;
18
19    /** @var bool */
20    public $isCtxDependent;
21
22    public function __construct(int $ruleIndex = -1, int $predIndex = -1, bool $isCtxDependent = false)
23    {
24        $this->ruleIndex = $ruleIndex;
25        $this->predIndex = $predIndex;
26        $this->isCtxDependent = $isCtxDependent;
27    }
28
29    public function eval(Recognizer $parser, RuleContext $parserCallStack) : bool
30    {
31        $localctx = $this->isCtxDependent ? $parserCallStack : null;
32
33        return $parser->sempred($localctx, $this->ruleIndex, $this->predIndex);
34    }
35
36    public function hashCode() : int
37    {
38        return Hasher::hash($this->ruleIndex, $this->predIndex, $this->isCtxDependent);
39    }
40
41    public function equals(object $other) : bool
42    {
43        if ($this === $other) {
44            return true;
45        }
46
47        if (!$other instanceof self) {
48            return false;
49        }
50
51        return $this->ruleIndex === $other->ruleIndex
52            && $this->predIndex === $other->predIndex
53            && $this->isCtxDependent === $other->isCtxDependent;
54    }
55
56    public function __toString() : string
57    {
58        return \sprintf('{%d:%d}?', $this->ruleIndex, $this->predIndex);
59    }
60}
61