Package org.antlr.v4.runtime.atn
Class LexerATNSimulator
- java.lang.Object
-
- org.antlr.v4.runtime.atn.ATNSimulator
-
- org.antlr.v4.runtime.atn.LexerATNSimulator
-
public class LexerATNSimulator extends ATNSimulator
"dup" of ParserInterpreter
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
LexerATNSimulator.SimState
When we hit an accept state in either the DFA or the ATN, we have to notify the character stream to start buffering characters viaIntStream.mark()
and record the current state.
-
Field Summary
Fields Modifier and Type Field Description protected int
charPositionInLine
The index of the character relative to the beginning of the line 0..n-1static boolean
debug
DFA[]
decisionToDFA
static boolean
dfa_debug
protected int
line
line number 1..n within the inputstatic int
MAX_DFA_EDGE
static int
MIN_DFA_EDGE
protected int
mode
protected LexerATNSimulator.SimState
prevAccept
Used during DFA/ATN exec to record the most recent accept configuration infoprotected Lexer
recog
protected int
startIndex
The current token's starting index into the character stream.-
Fields inherited from class org.antlr.v4.runtime.atn.ATNSimulator
atn, ERROR, sharedContextCache
-
-
Constructor Summary
Constructors Constructor Description LexerATNSimulator(ATN atn, DFA[] decisionToDFA, PredictionContextCache sharedContextCache)
LexerATNSimulator(Lexer recog, ATN atn, DFA[] decisionToDFA, PredictionContextCache sharedContextCache)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
accept(CharStream input, LexerActionExecutor lexerActionExecutor, int startIndex, int index, int line, int charPos)
protected DFAState
addDFAEdge(DFAState from, int t, ATNConfigSet q)
protected void
addDFAEdge(DFAState p, int t, DFAState q)
protected DFAState
addDFAState(ATNConfigSet configs)
Add a new DFA state if there isn't one with this set of configurations already.protected void
captureSimState(LexerATNSimulator.SimState settings, CharStream input, DFAState dfaState)
void
clearDFA()
Clear the DFA cache used by the current instance.protected boolean
closure(CharStream input, LexerATNConfig config, ATNConfigSet configs, boolean currentAltReachedAcceptState, boolean speculative, boolean treatEofAsEpsilon)
Since the alternatives within any lexer decision are ordered by preference, this method stops pursuing the closure as soon as an accept state is reached.protected ATNConfigSet
computeStartState(CharStream input, ATNState p)
protected DFAState
computeTargetState(CharStream input, DFAState s, int t)
Compute a target state for an edge in the DFA, and attempt to add the computed state and corresponding edge to the DFA.void
consume(CharStream input)
void
copyState(LexerATNSimulator simulator)
protected boolean
evaluatePredicate(CharStream input, int ruleIndex, int predIndex, boolean speculative)
Evaluate a predicate specified in the lexer.protected int
execATN(CharStream input, DFAState ds0)
protected int
failOrAccept(LexerATNSimulator.SimState prevAccept, CharStream input, ATNConfigSet reach, int t)
int
getCharPositionInLine()
DFA
getDFA(int mode)
protected LexerATNConfig
getEpsilonTarget(CharStream input, LexerATNConfig config, Transition t, ATNConfigSet configs, boolean speculative, boolean treatEofAsEpsilon)
protected DFAState
getExistingTargetState(DFAState s, int t)
Get an existing target state for an edge in the DFA.int
getLine()
protected void
getReachableConfigSet(CharStream input, ATNConfigSet closure, ATNConfigSet reach, int t)
Given a starting configuration set, figure out all ATN configurations we can reach upon inputt
.protected ATNState
getReachableTarget(Transition trans, int t)
String
getText(CharStream input)
Get the text matched so far for the current token.String
getTokenName(int t)
int
match(CharStream input, int mode)
protected int
matchATN(CharStream input)
void
reset()
void
setCharPositionInLine(int charPositionInLine)
void
setLine(int line)
-
Methods inherited from class org.antlr.v4.runtime.atn.ATNSimulator
getCachedContext, getSharedContextCache
-
-
-
-
Field Detail
-
debug
public static final boolean debug
- See Also:
- Constant Field Values
-
dfa_debug
public static final boolean dfa_debug
- See Also:
- Constant Field Values
-
MIN_DFA_EDGE
public static final int MIN_DFA_EDGE
- See Also:
- Constant Field Values
-
MAX_DFA_EDGE
public static final int MAX_DFA_EDGE
- See Also:
- Constant Field Values
-
recog
protected final Lexer recog
-
startIndex
protected int startIndex
The current token's starting index into the character stream. Shared across DFA to ATN simulation in case the ATN fails and the DFA did not have a previous accept state. In this case, we use the ATN-generated exception object.
-
line
protected int line
line number 1..n within the input
-
charPositionInLine
protected int charPositionInLine
The index of the character relative to the beginning of the line 0..n-1
-
decisionToDFA
public final DFA[] decisionToDFA
-
mode
protected int mode
-
prevAccept
protected final LexerATNSimulator.SimState prevAccept
Used during DFA/ATN exec to record the most recent accept configuration info
-
-
Constructor Detail
-
LexerATNSimulator
public LexerATNSimulator(ATN atn, DFA[] decisionToDFA, PredictionContextCache sharedContextCache)
-
LexerATNSimulator
public LexerATNSimulator(Lexer recog, ATN atn, DFA[] decisionToDFA, PredictionContextCache sharedContextCache)
-
-
Method Detail
-
copyState
public void copyState(LexerATNSimulator simulator)
-
match
public int match(CharStream input, int mode)
-
reset
public void reset()
- Specified by:
reset
in classATNSimulator
-
clearDFA
public void clearDFA()
Description copied from class:ATNSimulator
Clear the DFA cache used by the current instance. Since the DFA cache may be shared by multiple ATN simulators, this method may affect the performance (but not accuracy) of other parsers which are being used concurrently.- Overrides:
clearDFA
in classATNSimulator
-
matchATN
protected int matchATN(CharStream input)
-
execATN
protected int execATN(CharStream input, DFAState ds0)
-
getExistingTargetState
protected DFAState getExistingTargetState(DFAState s, int t)
Get an existing target state for an edge in the DFA. If the target state for the edge has not yet been computed or is otherwise not available, this method returnsnull
.- Parameters:
s
- The current DFA statet
- The next input symbol- Returns:
- The existing target DFA state for the given input symbol
t
, ornull
if the target state for this edge is not already cached
-
computeTargetState
protected DFAState computeTargetState(CharStream input, DFAState s, int t)
Compute a target state for an edge in the DFA, and attempt to add the computed state and corresponding edge to the DFA.- Parameters:
input
- The input streams
- The current DFA statet
- The next input symbol- Returns:
- The computed target DFA state for the given input symbol
t
. Ift
does not lead to a valid DFA state, this method returnsATNSimulator.ERROR
.
-
failOrAccept
protected int failOrAccept(LexerATNSimulator.SimState prevAccept, CharStream input, ATNConfigSet reach, int t)
-
getReachableConfigSet
protected void getReachableConfigSet(CharStream input, ATNConfigSet closure, ATNConfigSet reach, int t)
Given a starting configuration set, figure out all ATN configurations we can reach upon inputt
. Parameterreach
is a return parameter.
-
accept
protected void accept(CharStream input, LexerActionExecutor lexerActionExecutor, int startIndex, int index, int line, int charPos)
-
getReachableTarget
protected ATNState getReachableTarget(Transition trans, int t)
-
computeStartState
protected ATNConfigSet computeStartState(CharStream input, ATNState p)
-
closure
protected boolean closure(CharStream input, LexerATNConfig config, ATNConfigSet configs, boolean currentAltReachedAcceptState, boolean speculative, boolean treatEofAsEpsilon)
Since the alternatives within any lexer decision are ordered by preference, this method stops pursuing the closure as soon as an accept state is reached. After the first accept state is reached by depth-first search fromconfig
, all other (potentially reachable) states for this rule would have a lower priority.- Returns:
true
if an accept state is reached, otherwisefalse
.
-
getEpsilonTarget
protected LexerATNConfig getEpsilonTarget(CharStream input, LexerATNConfig config, Transition t, ATNConfigSet configs, boolean speculative, boolean treatEofAsEpsilon)
-
evaluatePredicate
protected boolean evaluatePredicate(CharStream input, int ruleIndex, int predIndex, boolean speculative)
Evaluate a predicate specified in the lexer.If
speculative
istrue
, this method was called beforeconsume(org.antlr.v4.runtime.CharStream)
for the matched character. This method should callconsume(org.antlr.v4.runtime.CharStream)
before evaluating the predicate to ensure position sensitive values, includingLexer.getText()
,Lexer.getLine()
, andLexer.getCharPositionInLine()
, properly reflect the current lexer state. This method should restoreinput
and the simulator to the original state before returning (i.e. undo the actions made by the call toconsume(org.antlr.v4.runtime.CharStream)
.- Parameters:
input
- The input stream.ruleIndex
- The rule containing the predicate.predIndex
- The index of the predicate within the rule.speculative
-true
if the current index ininput
is one character before the predicate's location.- Returns:
true
if the specified predicate evaluates totrue
.
-
captureSimState
protected void captureSimState(LexerATNSimulator.SimState settings, CharStream input, DFAState dfaState)
-
addDFAEdge
protected DFAState addDFAEdge(DFAState from, int t, ATNConfigSet q)
-
addDFAState
protected DFAState addDFAState(ATNConfigSet configs)
Add a new DFA state if there isn't one with this set of configurations already. This method also detects the first configuration containing an ATN rule stop state. Later, when traversing the DFA, we will know which rule to accept.
-
getDFA
public final DFA getDFA(int mode)
-
getText
public String getText(CharStream input)
Get the text matched so far for the current token.
-
getLine
public int getLine()
-
setLine
public void setLine(int line)
-
getCharPositionInLine
public int getCharPositionInLine()
-
setCharPositionInLine
public void setCharPositionInLine(int charPositionInLine)
-
consume
public void consume(CharStream input)
-
getTokenName
public String getTokenName(int t)
-
-