use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.
the class BaseTest method checkRuleDFA.
void checkRuleDFA(Grammar g, DecisionState blk, String expecting) throws Exception {
DFA dfa = createDFA(g, blk);
String result = null;
if (dfa != null)
result = dfa.toString();
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.
the class BaseTest method getTypesFromString.
// public void minimizeDFA(DFA dfa) {
// DFAMinimizer dmin = new DFAMinimizer(dfa);
// dfa.minimized = dmin.minimize();
// }
IntegerList getTypesFromString(Grammar g, String expecting) {
IntegerList expectingTokenTypes = new IntegerList();
if (expecting != null && !expecting.trim().isEmpty()) {
for (String tname : expecting.replace(" ", "").split(",")) {
int ttype = g.getTokenType(tname);
expectingTokenTypes.add(ttype);
}
}
return expectingTokenTypes;
}
use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.
the class LexerATNFactory method stringLiteral.
/**
* For a lexer, a string is a sequence of char to match. That is,
* "fog" is treated as 'f' 'o' 'g' not as a single transition in
* the DFA. Machine== o-'f'->o-'o'->o-'g'->o and has n+1 states
* for n characters.
*/
@Override
public Handle stringLiteral(TerminalAST stringLiteralAST) {
String chars = stringLiteralAST.getText();
ATNState left = newState(stringLiteralAST);
ATNState right;
String s = CharSupport.getStringFromGrammarStringLiteral(chars);
if (s == null) {
// the lexer will already have given an error
return new Handle(left, left);
}
int n = s.length();
ATNState prev = left;
right = null;
for (int i = 0; i < n; ) {
right = newState(stringLiteralAST);
int codePoint = s.codePointAt(i);
prev.addTransition(CodePointTransitions.createWithCodePoint(right, codePoint));
prev = right;
i += Character.charCount(codePoint);
}
stringLiteralAST.atnState = left;
return new Handle(left, right);
}
use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.
the class DOTGenerator method getDOT.
public String getDOT(DFA dfa, boolean isLexer) {
if (dfa.s0.get() == null)
return null;
ST dot = stlib.getInstanceOf("dfa");
dot.add("name", "DFA" + dfa.decision);
dot.add("startState", dfa.s0.get().stateNumber);
// dot.add("useBox", Tool.internalOption_ShowATNConfigsInDFA);
dot.add("rankdir", rankdir);
// define stop states first; seems to be a bug in DOT where doublecircle
for (DFAState d : dfa.states.keySet()) {
if (!d.isAcceptState())
continue;
ST st = stlib.getInstanceOf("stopstate");
st.add("name", "s" + d.stateNumber);
st.add("label", getStateLabel(d));
dot.add("states", st);
}
for (DFAState d : dfa.states.keySet()) {
if (d.isAcceptState())
continue;
if (d.stateNumber == Integer.MAX_VALUE)
continue;
ST st = stlib.getInstanceOf("state");
st.add("name", "s" + d.stateNumber);
st.add("label", getStateLabel(d));
dot.add("states", st);
}
for (DFAState d : dfa.states.keySet()) {
Map<Integer, DFAState> edges = d.getEdgeMap();
for (Map.Entry<Integer, DFAState> entry : edges.entrySet()) {
DFAState target = entry.getValue();
if (target == null)
continue;
if (target.stateNumber == Integer.MAX_VALUE)
continue;
int ttype = entry.getKey();
String label = String.valueOf(ttype);
if (isLexer)
label = "'" + getEdgeLabel(new StringBuilder().appendCodePoint(entry.getKey()).toString()) + "'";
else if (grammar != null)
label = grammar.getTokenDisplayName(ttype);
ST st = stlib.getInstanceOf("edge");
st.add("label", label);
st.add("src", "s" + d.stateNumber);
st.add("target", "s" + target.stateNumber);
st.add("arrowhead", arrowhead);
dot.add("edges", st);
}
}
String output = dot.render();
return Utils.sortLinesInString(output);
}
use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.
the class ParserATNSimulator method getStartState.
protected SimulatorState getStartState(@NotNull DFA dfa, @NotNull TokenStream input, @NotNull ParserRuleContext outerContext, boolean useContext) {
if (!useContext) {
if (dfa.isPrecedenceDfa()) {
// the start state for a precedence DFA depends on the current
// parser precedence, and is provided by a DFA method.
DFAState state = dfa.getPrecedenceStartState(parser.getPrecedence(), false);
if (state == null) {
return null;
}
return new SimulatorState(outerContext, state, false, outerContext);
} else {
if (dfa.s0.get() == null) {
return null;
}
return new SimulatorState(outerContext, dfa.s0.get(), false, outerContext);
}
}
if (!enable_global_context_dfa) {
return null;
}
ParserRuleContext remainingContext = outerContext;
assert outerContext != null;
DFAState s0;
if (dfa.isPrecedenceDfa()) {
s0 = dfa.getPrecedenceStartState(parser.getPrecedence(), true);
} else {
s0 = dfa.s0full.get();
}
while (remainingContext != null && s0 != null && s0.isContextSensitive()) {
remainingContext = skipTailCalls(remainingContext);
s0 = s0.getContextTarget(getReturnState(remainingContext));
if (remainingContext.isEmpty()) {
assert s0 == null || !s0.isContextSensitive();
} else {
remainingContext = remainingContext.getParent();
}
}
if (s0 == null) {
return null;
}
return new SimulatorState(outerContext, s0, useContext, remainingContext);
}
Aggregations