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