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