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
10/**
11 * Implements the `pushMode` lexer action by calling {@see Lexer::pushMode()}
12 * with the assigned mode.
13 *
14 * @author Sam Harwell
15 */
16final class LexerPushModeAction implements LexerAction
17{
18    /** @var int */
19    private $mode;
20
21    public function __construct(int $mode)
22    {
23        $this->mode = $mode;
24    }
25
26    /**
27     * Get the lexer mode this action should transition the lexer to.
28     *
29     * @return int The lexer mode for this `pushMode` command.
30     */
31    public function getMode() : int
32    {
33        return $this->mode;
34    }
35
36    /**
37     * {@inheritdoc}
38     *
39     * @return int This method returns {@see LexerActionType::PUSH_MODE}.
40     */
41    public function getActionType() : int
42    {
43        return LexerActionType::PUSH_MODE;
44    }
45
46    /**
47     * {@inheritdoc}
48     *
49     * @return bool This method returns `false`.
50     */
51    public function isPositionDependent() : bool
52    {
53        return false;
54    }
55
56    /**
57     * {@inheritdoc}
58     *
59     * This action is implemented by calling {@see Lexer::pushMode()} with the
60     * value provided by {@see LexerPushModeAction::getMode()}.
61     */
62    public function execute(Lexer $lexer) : void
63    {
64        $lexer->pushMode($this->mode);
65    }
66
67    public function hashCode() : int
68    {
69        return Hasher::hash($this->getActionType(), $this->mode);
70    }
71
72    public function equals(object $other) : bool
73    {
74        if ($this === $other) {
75            return true;
76        }
77
78        if (!$other instanceof self) {
79            return false;
80        }
81
82        return $this->mode === $other->mode;
83    }
84
85    public function __toString() : string
86    {
87        return \sprintf('pushMode(%d)', $this->mode);
88    }
89}
90