1*37748cd8SNickeau<?php 2*37748cd8SNickeau 3*37748cd8SNickeaudeclare(strict_types=1); 4*37748cd8SNickeau 5*37748cd8SNickeaunamespace Antlr\Antlr4\Runtime; 6*37748cd8SNickeau 7*37748cd8SNickeauuse Antlr\Antlr4\Runtime\Utils\Pair; 8*37748cd8SNickeau 9*37748cd8SNickeau/** 10*37748cd8SNickeau * This default implementation of {@see TokenFactory} creates 11*37748cd8SNickeau * {@see CommonToken} objects. 12*37748cd8SNickeau */ 13*37748cd8SNickeaufinal class CommonTokenFactory implements TokenFactory 14*37748cd8SNickeau{ 15*37748cd8SNickeau /** 16*37748cd8SNickeau * Indicates whether {@see CommonToken::setText()} should be called after 17*37748cd8SNickeau * constructing tokens to explicitly set the text. This is useful for cases 18*37748cd8SNickeau * where the input stream might not be able to provide arbitrary substrings 19*37748cd8SNickeau * of text from the input after the lexer creates a token (e.g. the 20*37748cd8SNickeau * implementation of {@see CharStream::getText()} in 21*37748cd8SNickeau * {@see UnbufferedCharStream} throws an 22*37748cd8SNickeau * {@see UnsupportedOperationException}). Explicitly setting the token text 23*37748cd8SNickeau * allows {@see Token::getText()} to be called at any time regardless of the 24*37748cd8SNickeau * input stream implementation. 25*37748cd8SNickeau * 26*37748cd8SNickeau * The default value is `false` to avoid the performance and memory 27*37748cd8SNickeau * overhead of copying text for every token unless explicitly requested. 28*37748cd8SNickeau * 29*37748cd8SNickeau * @var bool 30*37748cd8SNickeau */ 31*37748cd8SNickeau protected $copyText; 32*37748cd8SNickeau 33*37748cd8SNickeau /** 34*37748cd8SNickeau * Constructs a {@see CommonTokenFactory} with the specified value for 35*37748cd8SNickeau * {@see CommonTokenFactory::copyText()}. 36*37748cd8SNickeau * 37*37748cd8SNickeau * When `copyText` is `false`, the {@see CommonTokenFactory::DEFAULT} 38*37748cd8SNickeau * instance should be used instead of constructing a new instance. 39*37748cd8SNickeau * 40*37748cd8SNickeau * @param bool $copyText The value for {@see CommonTokenFactory::copyText()}. 41*37748cd8SNickeau */ 42*37748cd8SNickeau public function __construct(bool $copyText = false) 43*37748cd8SNickeau { 44*37748cd8SNickeau $this->copyText = $copyText; 45*37748cd8SNickeau } 46*37748cd8SNickeau 47*37748cd8SNickeau /** 48*37748cd8SNickeau * The default {@see CommonTokenFactory} instance. 49*37748cd8SNickeau * 50*37748cd8SNickeau * This token factory does not explicitly copy token text when constructing 51*37748cd8SNickeau * tokens. 52*37748cd8SNickeau */ 53*37748cd8SNickeau public static function default() : self 54*37748cd8SNickeau { 55*37748cd8SNickeau static $default; 56*37748cd8SNickeau 57*37748cd8SNickeau return $default = $default ?? new CommonTokenFactory(); 58*37748cd8SNickeau } 59*37748cd8SNickeau 60*37748cd8SNickeau public function createEx( 61*37748cd8SNickeau Pair $source, 62*37748cd8SNickeau int $type, 63*37748cd8SNickeau ?string $text, 64*37748cd8SNickeau int $channel, 65*37748cd8SNickeau int $start, 66*37748cd8SNickeau int $stop, 67*37748cd8SNickeau int $line, 68*37748cd8SNickeau int $column 69*37748cd8SNickeau ) : Token { 70*37748cd8SNickeau $token = new CommonToken($type, $source, $channel, $start, $stop); 71*37748cd8SNickeau 72*37748cd8SNickeau $token->setLine($line); 73*37748cd8SNickeau $token->setCharPositionInLine($column); 74*37748cd8SNickeau 75*37748cd8SNickeau if ($text !== null) { 76*37748cd8SNickeau $token->setText($text); 77*37748cd8SNickeau } elseif ($this->copyText && $source->b !== null) { 78*37748cd8SNickeau if (!$source->b instanceof CharStream) { 79*37748cd8SNickeau throw new \RuntimeException('Unexpected stream type.'); 80*37748cd8SNickeau } 81*37748cd8SNickeau 82*37748cd8SNickeau $token->setText($source->b->getText($start, $stop)); 83*37748cd8SNickeau } 84*37748cd8SNickeau 85*37748cd8SNickeau return $token; 86*37748cd8SNickeau } 87*37748cd8SNickeau 88*37748cd8SNickeau public function create(int $type, string $text) : Token 89*37748cd8SNickeau { 90*37748cd8SNickeau $token = new CommonToken($type); 91*37748cd8SNickeau 92*37748cd8SNickeau $token->setText($text); 93*37748cd8SNickeau 94*37748cd8SNickeau return $token; 95*37748cd8SNickeau } 96*37748cd8SNickeau} 97