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 * Executes a custom lexer action by calling {@see Recognizer::action()} with the 12*37748cd8SNickeau * rule and action indexes assigned to the custom action. The implementation of 13*37748cd8SNickeau * a custom action is added to the generated code for the lexer in an override 14*37748cd8SNickeau * of {@see Recognizer::action()} when the grammar is compiled. 15*37748cd8SNickeau * 16*37748cd8SNickeau * This class may represent embedded actions created with the `{...}` 17*37748cd8SNickeau * syntax in ANTLR 4, as well as actions created for lexer commands where the 18*37748cd8SNickeau * command argument could not be evaluated when the grammar was compiled. 19*37748cd8SNickeau * 20*37748cd8SNickeau * @author Sam Harwell 21*37748cd8SNickeau */ 22*37748cd8SNickeaufinal class LexerCustomAction implements LexerAction 23*37748cd8SNickeau{ 24*37748cd8SNickeau /** @var int */ 25*37748cd8SNickeau private $ruleIndex; 26*37748cd8SNickeau 27*37748cd8SNickeau /** @var int */ 28*37748cd8SNickeau private $actionIndex; 29*37748cd8SNickeau 30*37748cd8SNickeau /** 31*37748cd8SNickeau * Constructs a custom lexer action with the specified rule and action 32*37748cd8SNickeau * indexes. 33*37748cd8SNickeau * 34*37748cd8SNickeau * @param int $ruleIndex The rule index to use for calls to 35*37748cd8SNickeau * {@see Recognizer::action()}. 36*37748cd8SNickeau * @param int $actionIndex The action index to use for calls to 37*37748cd8SNickeau * {@see Recognizer::action()}. 38*37748cd8SNickeau */ 39*37748cd8SNickeau public function __construct(int $ruleIndex, int $actionIndex) 40*37748cd8SNickeau { 41*37748cd8SNickeau $this->ruleIndex = $ruleIndex; 42*37748cd8SNickeau $this->actionIndex = $actionIndex; 43*37748cd8SNickeau } 44*37748cd8SNickeau 45*37748cd8SNickeau /** 46*37748cd8SNickeau * Gets the rule index to use for calls to {@see Recognizer::action()}. 47*37748cd8SNickeau * 48*37748cd8SNickeau * @return int The rule index for the custom action. 49*37748cd8SNickeau */ 50*37748cd8SNickeau public function getRuleIndex() : int 51*37748cd8SNickeau { 52*37748cd8SNickeau return $this->ruleIndex; 53*37748cd8SNickeau } 54*37748cd8SNickeau 55*37748cd8SNickeau /** 56*37748cd8SNickeau * Gets the action index to use for calls to {@see Recognizer::action()}. 57*37748cd8SNickeau * 58*37748cd8SNickeau * @return int The action index for the custom action. 59*37748cd8SNickeau */ 60*37748cd8SNickeau public function getActionIndex() : int 61*37748cd8SNickeau { 62*37748cd8SNickeau return $this->actionIndex; 63*37748cd8SNickeau } 64*37748cd8SNickeau 65*37748cd8SNickeau /** 66*37748cd8SNickeau * {@inheritdoc} 67*37748cd8SNickeau * 68*37748cd8SNickeau * @return int This method returns {@see LexerActionType::CUSTOM()}. 69*37748cd8SNickeau */ 70*37748cd8SNickeau public function getActionType() : int 71*37748cd8SNickeau { 72*37748cd8SNickeau return LexerActionType::CUSTOM; 73*37748cd8SNickeau } 74*37748cd8SNickeau 75*37748cd8SNickeau /** 76*37748cd8SNickeau * Gets whether the lexer action is position-dependent. Position-dependent 77*37748cd8SNickeau * actions may have different semantics depending on the {@see CharStream} 78*37748cd8SNickeau * index at the time the action is executed. 79*37748cd8SNickeau * 80*37748cd8SNickeau * Custom actions are position-dependent since they may represent a 81*37748cd8SNickeau * user-defined embedded action which makes calls to methods like 82*37748cd8SNickeau * {@see Lexer::getText()}. 83*37748cd8SNickeau * 84*37748cd8SNickeau * @return bool This method returns `true`. 85*37748cd8SNickeau */ 86*37748cd8SNickeau public function isPositionDependent() : bool 87*37748cd8SNickeau { 88*37748cd8SNickeau return true; 89*37748cd8SNickeau } 90*37748cd8SNickeau 91*37748cd8SNickeau /** 92*37748cd8SNickeau * {@inheritdoc} 93*37748cd8SNickeau * 94*37748cd8SNickeau * Custom actions are implemented by calling {@see Lexer::action()} with the 95*37748cd8SNickeau * appropriate rule and action indexes. 96*37748cd8SNickeau */ 97*37748cd8SNickeau public function execute(Lexer $lexer) : void 98*37748cd8SNickeau { 99*37748cd8SNickeau $lexer->action(null, $this->ruleIndex, $this->actionIndex); 100*37748cd8SNickeau } 101*37748cd8SNickeau 102*37748cd8SNickeau public function hashCode() : int 103*37748cd8SNickeau { 104*37748cd8SNickeau return Hasher::hash($this->getActionType(), $this->ruleIndex, $this->actionIndex); 105*37748cd8SNickeau } 106*37748cd8SNickeau 107*37748cd8SNickeau public function equals(object $other) : bool 108*37748cd8SNickeau { 109*37748cd8SNickeau if ($this === $other) { 110*37748cd8SNickeau return true; 111*37748cd8SNickeau } 112*37748cd8SNickeau 113*37748cd8SNickeau if (!$other instanceof self) { 114*37748cd8SNickeau return false; 115*37748cd8SNickeau } 116*37748cd8SNickeau 117*37748cd8SNickeau return $this->ruleIndex === $other->ruleIndex 118*37748cd8SNickeau && $this->actionIndex === $other->actionIndex; 119*37748cd8SNickeau } 120*37748cd8SNickeau} 121