1<?php
2
3declare(strict_types=1);
4
5namespace Antlr\Antlr4\Runtime\Atn\Actions;
6
7use Antlr\Antlr4\Runtime\Comparison\Hasher;
8use Antlr\Antlr4\Runtime\Lexer;
9
10final class LexerModeAction implements LexerAction
11{
12    /** @var int */
13    private $mode;
14
15    /**
16     * Constructs a new `mode` action with the specified mode value.
17     *
18     * @param int $mode The mode value to pass to {@see Lexer::mode()}.
19     */
20    public function __construct(int $mode)
21    {
22        $this->mode = $mode;
23    }
24
25    /**
26     * Get the lexer mode this action should transition the lexer to.
27     *
28     * @return int The lexer mode for this `mode` command.
29     */
30    public function getMode() : int
31    {
32        return $this->mode;
33    }
34
35    /**
36     * {@inheritdoc}
37     *
38     * @return int This method returns {@see LexerActionType::MODE}.
39     */
40    public function getActionType() : int
41    {
42        return LexerActionType::MODE;
43    }
44
45    /**
46     * {@inheritdoc}
47     *
48     * @return bool This method returns `false`.
49     */
50    public function isPositionDependent() : bool
51    {
52        return false;
53    }
54
55    /**
56     * {@inheritdoc}
57     *
58     * This action is implemented by calling {@see Lexer::mode()} with the
59     * value provided by {@see LexerModeAction::getMode()}.
60     */
61    public function execute(Lexer $lexer) : void
62    {
63        $lexer->mode($this->mode);
64    }
65
66    public function hashCode() : int
67    {
68        return Hasher::hash($this->getActionType(), $this->mode);
69    }
70
71    public function equals(object $other) : bool
72    {
73        if ($this === $other) {
74            return true;
75        }
76
77        if (!$other instanceof self) {
78            return false;
79        }
80
81        return $this->mode === $other->mode;
82    }
83
84    public function __toString() : string
85    {
86        return \sprintf('mode(%d)', $this->mode);
87    }
88}
89