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