use of org.antlr.analysis.NFAState in project antlrworks by antlr.
the class VisualDrawing method threadProcessRule.
private void threadProcessRule() throws Exception {
if (threadRule == null)
return;
String error = null;
ErrorListener.getThreadInstance().setPrintToConsole(false);
if (syntaxDiagramTab.getEngineGrammar().hasGrammar()) {
NFAState startState = null;
try {
startState = syntaxDiagramTab.getEngineGrammar().getRuleStartState(threadRule.name);
} catch (Exception e) {
// ignore
}
if (startState == null) {
error = "Cannot display rule \"" + threadRule + "\" because start state not found";
}
} else {
error = "Cannot display rule \"" + threadRule + "\" because grammar could not be generated";
}
if (error != null) {
syntaxDiagramTab.setPlaceholder(error);
//visual.getConsole().println(error, Console.LEVEL_ERROR);
return;
}
// Try to get the optimized graph from cache first. If the grammar didn't change (i.e. user
// only moving cursor in the text zone), the speed-up can be important.
createGraphsForRule(threadRule);
threadLastProcessedRule = threadRule;
refresh();
}
use of org.antlr.analysis.NFAState in project antlrworks by antlr.
the class FAFactory method buildRecursiveState.
public FAState buildRecursiveState(NFAState state, Set<NFAState> currentPath) {
if (processedStates.get(state) != null) {
FAState js = processedStates.get(state);
if (currentPath.contains(state)) {
// Set this temporary flag to indicate to the parent method that
// the transition to be created has to be flagged as "loop".
js.loop = true;
}
return js;
}
FAState js = new FAState(state);
processedStates.put(state, js);
currentPath.add(state);
if (state.isAcceptState()) {
// Stop as soon as we reach an accepted state
return js;
}
for (int t = 0; t < state.getNumberOfTransitions(); t++) {
FAState parentState = js;
Transition transition = state.transition(t);
NFAState target = (NFAState) transition.target;
if (targetStateIsInAnotherRule(transition)) {
target = targetStateOfTransition(transition);
parentState = createRuleReferenceState(parentState, transition, null);
}
if (transition.isEpsilon()) {
buildRecursiveSkipState(parentState, target, new HashSet<NFAState>(currentPath), new ArrayList<Integer>());
} else {
FAState targetState = buildRecursiveState(target, new HashSet<NFAState>(currentPath));
if (targetState.loop) {
// Handle "loop" transition by creating a "normal" transition and assigning a flag
// to this transition so when drawing it, we can draw the arrow at the right place
// (in the reverse direction)
targetState.addTransition(new FATransition(transition.label.toString(g), parentState), true);
targetState.loop = false;
} else
parentState.addTransition(new FATransition(transition.label.toString(g), targetState));
}
}
return js;
}
use of org.antlr.analysis.NFAState in project antlrworks by antlr.
the class FAAnalysis method toString.
public String toString() {
StringBuilder s = new StringBuilder();
Iterator iterator = stateIncomingTransitionCount.keySet().iterator();
while (iterator.hasNext()) {
NFAState key = (NFAState) iterator.next();
Integer count = (Integer) stateIncomingTransitionCount.get(key);
s.append(key.stateNumber + " = " + count.intValue() + "\n");
}
return s.toString();
}
use of org.antlr.analysis.NFAState in project antlrworks by antlr.
the class FAFactory method endStateOfAlternative.
public NFAState endStateOfAlternative(NFAState alt) {
int endOfBlockStateNumber = alt.endOfBlockStateNumber;
NFAState state = alt;
while (state.stateNumber != endOfBlockStateNumber) {
state = (NFAState) state.transition(0).target;
}
return state;
}
use of org.antlr.analysis.NFAState in project antlrworks by antlr.
the class FAFactory method buildRecursiveSkipState.
/** This method is used to skip redundant state following epsilon transition.
*
*/
public void buildRecursiveSkipState(FAState parentState, NFAState state, Set<NFAState> currentPath, List<Integer> skippedStates) {
if (canBeSkipped(state)) {
// If the state can be skipped, apply recursively the same method for each transition(s)
// providing the parent state.
// Record each skipped state. They will be added later to the transition that replace them
Integer skippedState = state.stateNumber;
skippedStates.add(skippedState);
skippedStatesMap.put(skippedState, parentState);
for (int t = 0; t < state.getNumberOfTransitions(); t++) {
Transition transition = state.transition(t);
if (targetStateIsInAnotherRule(transition)) {
NFAState target = targetStateOfTransition(transition);
FAState ruleRefState = createRuleReferenceState(parentState, transition, skippedStates);
buildRecursiveSkipState(ruleRefState, target, currentPath, new ArrayList<Integer>(skippedStates));
} else
buildRecursiveSkipState(parentState, (NFAState) transition.target, currentPath, new ArrayList<Integer>(skippedStates));
}
} else {
// The state cannot be skipped. Build the remaining of the NFA...
FAState targetState = buildRecursiveState(state, currentPath);
// (this is the simplification ;-))
if (targetState.loop) {
// See comment above (in the previous method)
targetState.addTransition(new FATransition(parentState, skippedStates), true);
targetState.loop = false;
} else
parentState.addTransition(new FATransition(targetState, skippedStates));
}
}
Aggregations