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