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 PrecedencePredicate extends SemanticContext
12{
13    /** @var int */
14    public $precedence;
15
16    public function __construct(int $precedence = 0)
17    {
18        $this->precedence = $precedence;
19    }
20
21    public function eval(Recognizer $parser, RuleContext $parserCallStack) : bool
22    {
23        return $parser->precpred($parserCallStack, $this->precedence);
24    }
25
26    public function evalPrecedence(Recognizer $parser, RuleContext $parserCallStack) : ?SemanticContext
27    {
28        if ($parser->precpred($parserCallStack, $this->precedence)) {
29            return SemanticContext::none();
30        }
31
32        return null;
33    }
34
35    public function hashCode() : int
36    {
37        return Hasher::hash(31, $this->precedence);
38    }
39
40    public function equals(object $other) : bool
41    {
42        if ($this === $other) {
43            return true;
44        }
45
46        if (!$other instanceof self) {
47            return false;
48        }
49
50        return $this->precedence === $other->precedence;
51    }
52
53    public function __toString() : string
54    {
55        return \sprintf('{%d>=prec}?', $this->precedence);
56    }
57}
58