1<?php
2
3declare(strict_types=1);
4
5namespace Antlr\Antlr4\Runtime\Error\Exceptions;
6
7use Antlr\Antlr4\Runtime\Atn\ATNConfigSet;
8use Antlr\Antlr4\Runtime\CharStream;
9use Antlr\Antlr4\Runtime\Lexer;
10use Antlr\Antlr4\Runtime\Utils\StringUtils;
11
12class LexerNoViableAltException extends RecognitionException
13{
14    /**
15     * Matching attempted at what input index?
16     *
17     * @var int
18     */
19    private $startIndex;
20
21    /**
22     * Which configurations did we try at $input->index() that couldn't match $input->LA(1)?
23     *
24     * @var ATNConfigSet
25     */
26    private $deadEndConfigs;
27
28    public function __construct(Lexer $lexer, CharStream $input, int $startIndex, ATNConfigSet $deadEndConfigs)
29    {
30        parent::__construct($lexer, $input, null);
31
32        $this->startIndex = $startIndex;
33        $this->deadEndConfigs = $deadEndConfigs;
34    }
35
36    public function getDeadEndConfigs() : ATNConfigSet
37    {
38        return $this->deadEndConfigs;
39    }
40
41    public function __toString() : string
42    {
43        $symbol = '';
44        $input = $this->getInputStream();
45
46        if (!$input instanceof CharStream) {
47            throw new \RuntimeException('Unexpected stream type.');
48        }
49
50        if ($input !== null && $this->startIndex >= 0 && $this->startIndex < $input->getLength()) {
51            $symbol = $input->getText($this->startIndex, $this->startIndex);
52            $symbol = StringUtils::escapeWhitespace($symbol, false);
53        }
54
55        return \sprintf('%s(\'%s\')', self::class, $symbol);
56    }
57}
58