1<?php
2
3declare(strict_types=1);
4
5namespace Antlr\Antlr4\Runtime\Atn;
6
7use Antlr\Antlr4\Runtime\Dfa\DFAState;
8
9/**
10 * When we hit an accept state in either the DFA or the ATN, we
11 * have to notify the character stream to start buffering characters
12 * via {@see IntStream::mark()} and record the current state. The current sim state
13 * includes the current index into the input, the current line,
14 * and current character position in that line. Note that the Lexer is
15 * tracking the starting line and characterization of the token. These
16 * variables track the "state" of the simulator when it hits an accept state.
17 *
18 * We track these variables separately for the DFA and ATN simulation
19 * because the DFA simulation often has to fail over to the ATN
20 * simulation. If the ATN simulation fails, we need the DFA to fall
21 * back to its previously accepted state, if any. If the ATN succeeds,
22 * then the ATN does the accept and the DFA simulator that invoked it
23 * can simply return the predicted token type.
24 */
25final class SimState
26{
27    /** @var int */
28    private $index = -1;
29
30    /** @var int */
31    private $line = 0;
32
33    /** @var int */
34    private $charPos = -1;
35
36    /** @var DFAState|null */
37    private $dfaState;
38
39    public function reset() : void
40    {
41        $this->index = -1;
42        $this->line = 0;
43        $this->charPos = -1;
44        $this->dfaState = null;
45    }
46
47    public function getIndex() : int
48    {
49        return $this->index;
50    }
51
52    public function setIndex(int $index) : void
53    {
54        $this->index = $index;
55    }
56
57    public function getLine() : int
58    {
59        return $this->line;
60    }
61
62    public function setLine(int $line) : void
63    {
64        $this->line = $line;
65    }
66
67    public function getCharPos() : int
68    {
69        return $this->charPos;
70    }
71
72    public function setCharPos(int $charPos) : void
73    {
74        $this->charPos = $charPos;
75    }
76
77    public function getDfaState() : ?DFAState
78    {
79        return $this->dfaState;
80    }
81
82    public function setDfaState(?DFAState $dfaState) : void
83    {
84        $this->dfaState = $dfaState;
85    }
86}
87