use of org.antlr.analysis.NFAState in project antlrworks by antlr.
the class TokensDFA method getDOTString.
@Override
public String getDOTString() throws Exception {
ANTLRGrammarEngine eg = window.getGrammarEngine().getANTLRGrammarEngine();
eg.analyze();
Grammar g = eg.getLexerGrammar();
if (g == null) {
throw new Exception("Cannot show tokens DFA because there is no lexer grammar");
}
Rule r = g.getRule(Grammar.ARTIFICIAL_TOKENS_RULENAME);
NFAState s = (NFAState) r.startState.transition(0).target;
DFA dfa = g.getLookaheadDFA(s.getDecisionNumber());
DOTGenerator dg = new DOTGenerator(g);
dg.setArrowheadType("none");
// Left-to-right
dg.setRankdir("LR");
return dg.getDOT(dfa.startState);
}
use of org.antlr.analysis.NFAState in project antlrworks by antlr.
the class ANTLRGrammarEngineImpl method computeRuleError.
private void computeRuleError(GrammarError error, GrammarNonDeterminismMessage message) {
List nonDetAlts = message.probe.getNonDeterministicAltsForState(message.problemState);
Set disabledAlts = message.probe.getDisabledAlternatives(message.problemState);
int firstAlt = 0;
for (Object nonDetAlt : nonDetAlts) {
Integer displayAltI = (Integer) nonDetAlt;
NFAState nfaStart = message.probe.dfa.getNFADecisionStartState();
int tracePathAlt = nfaStart.translateDisplayAltToWalkAlt(displayAltI);
if (firstAlt == 0)
firstAlt = tracePathAlt;
List path = message.probe.getNFAPathStatesForAlt(firstAlt, tracePathAlt, error.getLabels());
error.addPath(path, disabledAlts.contains(displayAltI));
error.addStates(path);
// Find all rules enclosing each state (because a path can extend over multiple rules)
for (Object aPath : path) {
NFAState state = (NFAState) aPath;
error.addRule(state.enclosingRule.name);
}
}
}
use of org.antlr.analysis.NFAState in project antlrworks by antlr.
the class DecisionDFAEngine method getDecisionDFAItems.
public List<DecisionDFAItem> getDecisionDFAItems() {
List<DecisionDFAItem> items = new ArrayList<DecisionDFAItem>();
for (int lineIndex : decisionDFA.keySet()) {
for (int columnIndex : decisionDFA.get(lineIndex)) {
DFA dfa = getDFAAtPosition(lineIndex, columnIndex);
if (dfa == null) {
System.err.println("DFA is null for line " + lineIndex + " and column " + columnIndex);
continue;
}
Grammar g = discoveredLexerGrammar;
if (g != null) {
Rule r = g.getRule(Grammar.ARTIFICIAL_TOKENS_RULENAME);
NFAState s = (NFAState) r.startState.transition(0).target;
if (s == null) {
System.err.println("NFAState s is null for rule " + r.name);
continue;
}
// Ignore tokens DFA
if (dfa.getDecisionNumber() == s.getDecisionNumber())
continue;
}
Color c = new Color(0, 128, 64);
String title = "DFA decision " + dfa.getDecisionNumber();
String info = "";
if (usesSemPreds.contains(dfa.getDecisionNumber())) {
info += "uses semantic predicate";
c = new Color(255, 220, 0);
} else if (usesSynPreds.contains(dfa.getDecisionNumber())) {
info += "uses syntactic predicate";
c = new Color(255, 220, 0);
}
if (dfa.isCyclic()) {
if (info.length() > 0)
info += ", ";
info += "cyclic";
}
if (info.length() > 0)
info += ", ";
if (dfa.getNumberOfStates() != 0) {
info += dfa.getNumberOfStates() + " states";
} else {
info += "<=" + dfa.getMaxStateNumber() + " states";
}
Point p = window.textEditor.getLineTextPositionsAtLineIndex(lineIndex - 1);
if (p != null) {
DecisionDFAItem item = new DecisionDFAItem(window);
item.setAttributes(null, p.x + columnIndex - 1, p.x + columnIndex, lineIndex - 1, c, title + " (" + info + ")");
item.shape = ATEOverlayManager.SHAPE_RECT;
items.add(item);
}
}
}
return items;
}
use of org.antlr.analysis.NFAState in project antlrworks by antlr.
the class FAFactory method targetStateOfTransition.
public NFAState targetStateOfTransition(Transition transition) {
NFAState target;
if (transition instanceof RuleClosureTransition) {
RuleClosureTransition rct = (RuleClosureTransition) transition;
target = rct.followState;
} else {
target = (NFAState) transition.target;
}
return target;
}
use of org.antlr.analysis.NFAState in project antlrworks by antlr.
the class FAFactory method isAlternativeTransitionEndingAtSameState.
public boolean isAlternativeTransitionEndingAtSameState(NFAState state) {
NFAState endState = endStateOfAlternative((NFAState) state.transition(0).target);
for (int t = 1; t < state.getNumberOfTransitions(); t++) {
Transition transition = state.transition(t);
NFAState newEndState = endStateOfAlternative((NFAState) transition.target);
if (!endState.equals(newEndState))
return false;
}
return true;
}
Aggregations