1<?php
2
3declare(strict_types=1);
4
5namespace Antlr\Antlr4\Runtime\Tree;
6
7use Antlr\Antlr4\Runtime\Interval;
8use Antlr\Antlr4\Runtime\RuleContext;
9use Antlr\Antlr4\Runtime\Token;
10
11class TerminalNodeImpl implements TerminalNode
12{
13    /** @var Token */
14    public $symbol;
15
16    /** @var ParseTree|null */
17    public $parent;
18
19    public function __construct(Token $symbol)
20    {
21        $this->symbol = $symbol;
22    }
23
24    public function getChild(int $i, ?string $type = null) : ?Tree
25    {
26        return null;
27    }
28
29    public function getSymbol() : Token
30    {
31        return $this->symbol;
32    }
33
34    /**
35     * @return ParseTree|null
36     */
37    public function getParent() : ?Tree
38    {
39        return $this->parent;
40    }
41
42    public function setParent(RuleContext $parent) : void
43    {
44        $this->parent = $parent;
45    }
46
47    public function getPayload() : Token
48    {
49        return $this->symbol;
50    }
51
52    public function getSourceInterval() : Interval
53    {
54        if ($this->symbol === null) {
55            return new Interval(-1, -2);
56        }
57
58        $tokenIndex = $this->symbol->getTokenIndex();
59
60        return new Interval($tokenIndex, $tokenIndex);
61    }
62
63    public function getChildCount() : int
64    {
65        return 0;
66    }
67
68    public function accept(ParseTreeVisitor $visitor)
69    {
70        return $visitor->visitTerminal($this);
71    }
72
73    public function getText() : ?string
74    {
75        return $this->symbol->getText();
76    }
77
78    /**
79     * @param array<string>|null $ruleNames
80     */
81    public function toStringTree(?array $ruleNames = null) : string
82    {
83        return (string) $this;
84    }
85
86    public function __toString() : string
87    {
88        if ($this->symbol->getType() === Token::EOF) {
89            return '<EOF>';
90        }
91
92        return $this->symbol->getText() ?? '';
93    }
94}
95