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 `type` lexer action by calling {@see Lexer::setType()} 12 * with the assigned type. 13 * 14 * @author Sam Harwell 15 */ 16final class LexerTypeAction implements LexerAction 17{ 18 /** @var int */ 19 private $type; 20 21 /** 22 * Constructs a new `type` action with the specified token type value. 23 * 24 * @param int $type The type to assign to the token using {@see Lexer::setType()}. 25 */ 26 public function __construct(int $type) 27 { 28 $this->type = $type; 29 } 30 31 /** 32 * Gets the type to assign to a token created by the lexer. 33 * 34 * @return int The type to assign to a token created by the lexer. 35 */ 36 public function getType() : int 37 { 38 return $this->type; 39 } 40 41 /** 42 * {@inheritdoc} 43 * 44 * @return int This method returns {@see LexerActionType::TYPE}. 45 */ 46 public function getActionType() : int 47 { 48 return LexerActionType::TYPE; 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::setType()} with the 65 * value provided by {@see LexerTypeAction::getType()}. 66 */ 67 public function execute(Lexer $lexer) : void 68 { 69 $lexer->setType($this->type); 70 } 71 72 public function hashCode() : int 73 { 74 return Hasher::hash($this->getActionType(), $this->type); 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->type === $other->type; 88 } 89 90 public function __toString() : string 91 { 92 return \sprintf('type(%d)', $this->type); 93 } 94} 95