use of org.antlr.v4.runtime.atn.StarLoopEntryState in project antlr4 by antlr.
the class ParserInterpreter method visitState.
protected void visitState(ATNState p) {
// System.out.println("visitState "+p.stateNumber);
int predictedAlt = 1;
if (p instanceof DecisionState) {
predictedAlt = visitDecisionState((DecisionState) p);
}
Transition transition = p.transition(predictedAlt - 1);
switch(transition.getSerializationType()) {
case Transition.EPSILON:
if (p.getStateType() == ATNState.STAR_LOOP_ENTRY && ((StarLoopEntryState) p).isPrecedenceDecision && !(transition.target instanceof LoopEndState)) {
// We are at the start of a left recursive rule's (...)* loop
// and we're not taking the exit branch of loop.
InterpreterRuleContext localctx = createInterpreterRuleContext(_parentContextStack.peek().a, _parentContextStack.peek().b, _ctx.getRuleIndex());
pushNewRecursionContext(localctx, atn.ruleToStartState[p.ruleIndex].stateNumber, _ctx.getRuleIndex());
}
break;
case Transition.ATOM:
match(((AtomTransition) transition).label);
break;
case Transition.RANGE:
case Transition.SET:
case Transition.NOT_SET:
if (!transition.matches(_input.LA(1), Token.MIN_USER_TOKEN_TYPE, 65535)) {
recoverInline();
}
matchWildcard();
break;
case Transition.WILDCARD:
matchWildcard();
break;
case Transition.RULE:
RuleStartState ruleStartState = (RuleStartState) transition.target;
int ruleIndex = ruleStartState.ruleIndex;
InterpreterRuleContext newctx = createInterpreterRuleContext(_ctx, p.stateNumber, ruleIndex);
if (ruleStartState.isLeftRecursiveRule) {
enterRecursionRule(newctx, ruleStartState.stateNumber, ruleIndex, ((RuleTransition) transition).precedence);
} else {
enterRule(newctx, transition.target.stateNumber, ruleIndex);
}
break;
case Transition.PREDICATE:
PredicateTransition predicateTransition = (PredicateTransition) transition;
if (!sempred(_ctx, predicateTransition.ruleIndex, predicateTransition.predIndex)) {
throw new FailedPredicateException(this);
}
break;
case Transition.ACTION:
ActionTransition actionTransition = (ActionTransition) transition;
action(_ctx, actionTransition.ruleIndex, actionTransition.actionIndex);
break;
case Transition.PRECEDENCE:
if (!precpred(_ctx, ((PrecedencePredicateTransition) transition).precedence)) {
throw new FailedPredicateException(this, String.format("precpred(_ctx, %d)", ((PrecedencePredicateTransition) transition).precedence));
}
break;
default:
throw new UnsupportedOperationException("Unrecognized ATN transition type.");
}
setState(transition.target.stateNumber);
}
use of org.antlr.v4.runtime.atn.StarLoopEntryState in project antlr4 by antlr.
the class ParserATNFactory method star.
/**
* From {@code (blk)*} build {@code ( blk+ )?} with *two* decisions, one for
* entry and one for choosing alts of {@code blk}.
*
* <pre>
* |-------------|
* v |
* o--[o-blk-o]->o o
* | ^
* -----------------|
* </pre>
*
* Note that the optional bypass must jump outside the loop as
* {@code (A|B)*} is not the same thing as {@code (A|B|)+}.
*/
@Override
public Handle star(GrammarAST starAST, Handle elem) {
StarBlockStartState blkStart = (StarBlockStartState) elem.left;
BlockEndState blkEnd = (BlockEndState) elem.right;
preventEpsilonClosureBlocks.add(new Triple<Rule, ATNState, ATNState>(currentRule, blkStart, blkEnd));
StarLoopEntryState entry = newState(StarLoopEntryState.class, starAST);
entry.nonGreedy = !((QuantifierAST) starAST).isGreedy();
atn.defineDecisionState(entry);
LoopEndState end = newState(LoopEndState.class, starAST);
StarLoopbackState loop = newState(StarLoopbackState.class, starAST);
entry.loopBackState = loop;
end.loopBackState = loop;
BlockAST blkAST = (BlockAST) starAST.getChild(0);
if (((QuantifierAST) starAST).isGreedy()) {
if (expectNonGreedy(blkAST)) {
g.tool.errMgr.grammarError(ErrorType.EXPECTED_NON_GREEDY_WILDCARD_BLOCK, g.fileName, starAST.getToken(), starAST.getToken().getText());
}
// loop enter edge (alt 1)
epsilon(entry, blkStart);
// bypass loop edge (alt 2)
epsilon(entry, end);
} else {
// if not greedy, priority to exit branch; make it first
// bypass loop edge (alt 1)
epsilon(entry, end);
// loop enter edge (alt 2)
epsilon(entry, blkStart);
}
// block end hits loop back
epsilon(blkEnd, loop);
// loop back to entry/exit decision
epsilon(loop, entry);
// decision is to enter/exit; blk is its own decision
starAST.atnState = entry;
return new Handle(entry, end);
}
use of org.antlr.v4.runtime.atn.StarLoopEntryState in project antlr4 by antlr.
the class DOTGenerator method getDOT.
/**
* Return a String containing a DOT description that, when displayed,
* will show the incoming state machine visually. All nodes reachable
* from startState will be included.
*/
public String getDOT(ATNState startState, String[] ruleNames, boolean isLexer) {
if (startState == null)
return null;
// The output DOT graph for visualization
Set<ATNState> markedStates = new HashSet<ATNState>();
ST dot = stlib.getInstanceOf("atn");
dot.add("startState", startState.stateNumber);
dot.add("rankdir", rankdir);
List<ATNState> work = new LinkedList<ATNState>();
work.add(startState);
while (!work.isEmpty()) {
ATNState s = work.get(0);
if (markedStates.contains(s)) {
work.remove(0);
continue;
}
markedStates.add(s);
// don't go past end of rule node to the follow states
if (s instanceof RuleStopState)
continue;
// special case: if decision point, then line up the alt start states
// unless it's an end of block
// if ( s instanceof BlockStartState ) {
// ST rankST = stlib.getInstanceOf("decision-rank");
// DecisionState alt = (DecisionState)s;
// for (int i=0; i<alt.getNumberOfTransitions(); i++) {
// ATNState target = alt.transition(i).target;
// if ( target!=null ) {
// rankST.add("states", target.stateNumber);
// }
// }
// dot.add("decisionRanks", rankST);
// }
// make a DOT edge for each transition
ST edgeST;
for (int i = 0; i < s.getNumberOfTransitions(); i++) {
Transition edge = s.transition(i);
if (edge instanceof RuleTransition) {
RuleTransition rr = ((RuleTransition) edge);
// don't jump to other rules, but display edge to follow node
edgeST = stlib.getInstanceOf("edge");
String label = "<" + ruleNames[rr.ruleIndex];
if (((RuleStartState) rr.target).isLeftRecursiveRule) {
label += "[" + rr.precedence + "]";
}
label += ">";
edgeST.add("label", label);
edgeST.add("src", "s" + s.stateNumber);
edgeST.add("target", "s" + rr.followState.stateNumber);
edgeST.add("arrowhead", arrowhead);
dot.add("edges", edgeST);
work.add(rr.followState);
continue;
}
if (edge instanceof ActionTransition) {
edgeST = stlib.getInstanceOf("action-edge");
edgeST.add("label", getEdgeLabel(edge.toString()));
} else if (edge instanceof AbstractPredicateTransition) {
edgeST = stlib.getInstanceOf("edge");
edgeST.add("label", getEdgeLabel(edge.toString()));
} else if (edge.isEpsilon()) {
edgeST = stlib.getInstanceOf("epsilon-edge");
edgeST.add("label", getEdgeLabel(edge.toString()));
boolean loopback = false;
if (edge.target instanceof PlusBlockStartState) {
loopback = s.equals(((PlusBlockStartState) edge.target).loopBackState);
} else if (edge.target instanceof StarLoopEntryState) {
loopback = s.equals(((StarLoopEntryState) edge.target).loopBackState);
}
edgeST.add("loopback", loopback);
} else if (edge instanceof AtomTransition) {
edgeST = stlib.getInstanceOf("edge");
AtomTransition atom = (AtomTransition) edge;
String label = String.valueOf(atom.label);
if (isLexer)
label = "'" + getEdgeLabel(new StringBuilder().appendCodePoint(atom.label).toString()) + "'";
else if (grammar != null)
label = grammar.getTokenDisplayName(atom.label);
edgeST.add("label", getEdgeLabel(label));
} else if (edge instanceof SetTransition) {
edgeST = stlib.getInstanceOf("edge");
SetTransition set = (SetTransition) edge;
String label = set.label().toString();
if (isLexer)
label = set.label().toString(true);
else if (grammar != null)
label = set.label().toString(grammar.getVocabulary());
if (edge instanceof NotSetTransition)
label = "~" + label;
edgeST.add("label", getEdgeLabel(label));
} else if (edge instanceof RangeTransition) {
edgeST = stlib.getInstanceOf("edge");
RangeTransition range = (RangeTransition) edge;
String label = range.label().toString();
if (isLexer)
label = range.toString();
else if (grammar != null)
label = range.label().toString(grammar.getVocabulary());
edgeST.add("label", getEdgeLabel(label));
} else {
edgeST = stlib.getInstanceOf("edge");
edgeST.add("label", getEdgeLabel(edge.toString()));
}
edgeST.add("src", "s" + s.stateNumber);
edgeST.add("target", "s" + edge.target.stateNumber);
edgeST.add("arrowhead", arrowhead);
if (s.getNumberOfTransitions() > 1) {
edgeST.add("transitionIndex", i);
} else {
edgeST.add("transitionIndex", false);
}
dot.add("edges", edgeST);
work.add(edge.target);
}
}
// }
for (ATNState s : markedStates) {
if (!(s instanceof RuleStopState))
continue;
ST st = stlib.getInstanceOf("stopstate");
st.add("name", "s" + s.stateNumber);
st.add("label", getStateLabel(s));
dot.add("states", st);
}
for (ATNState s : markedStates) {
if (s instanceof RuleStopState)
continue;
ST st = stlib.getInstanceOf("state");
st.add("name", "s" + s.stateNumber);
st.add("label", getStateLabel(s));
st.add("transitions", s.getTransitions());
dot.add("states", st);
}
return dot.render();
}
use of org.antlr.v4.runtime.atn.StarLoopEntryState in project antlr4 by tunnelvisionlabs.
the class GrammarParserInterpreter method findOuterMostDecisionStates.
/**
* identify the ATN states where we need to set the outer alt number.
* For regular rules, that's the block at the target to rule start state.
* For left-recursive rules, we track the primary block, which looks just
* like a regular rule's outer block, and the star loop block (always
* there even if 1 alt).
*/
public BitSet findOuterMostDecisionStates() {
BitSet track = new BitSet(atn.states.size());
int numberOfDecisions = atn.getNumberOfDecisions();
for (int i = 0; i < numberOfDecisions; i++) {
DecisionState decisionState = atn.getDecisionState(i);
RuleStartState startState = atn.ruleToStartState[decisionState.ruleIndex];
// Look for StarLoopEntryState that is in any left recursive rule
if (decisionState instanceof StarLoopEntryState) {
StarLoopEntryState loopEntry = (StarLoopEntryState) decisionState;
if (loopEntry.precedenceRuleDecision) {
// Recursive alts always result in a (...)* in the transformed
// left recursive rule and that always has a BasicBlockStartState
// even if just 1 recursive alt exists.
ATNState blockStart = loopEntry.transition(0).target;
// track the StarBlockStartState associated with the recursive alternatives
track.set(blockStart.stateNumber);
}
} else if (startState.transition(0).target == decisionState) {
// always track outermost block for any rule if it exists
track.set(decisionState.stateNumber);
}
}
return track;
}
use of org.antlr.v4.runtime.atn.StarLoopEntryState in project antlr4 by tunnelvisionlabs.
the class DOTGenerator method getDOT.
/**
* Return a String containing a DOT description that, when displayed,
* will show the incoming state machine visually. All nodes reachable
* from startState will be included.
*/
public String getDOT(ATNState startState, String[] ruleNames, boolean isLexer) {
if (startState == null)
return null;
// The output DOT graph for visualization
Set<ATNState> markedStates = new HashSet<ATNState>();
ST dot = stlib.getInstanceOf("atn");
dot.add("startState", startState.stateNumber);
dot.add("rankdir", rankdir);
List<ATNState> work = new LinkedList<ATNState>();
work.add(startState);
while (!work.isEmpty()) {
ATNState s = work.get(0);
if (markedStates.contains(s)) {
work.remove(0);
continue;
}
markedStates.add(s);
// don't go past end of rule node to the follow states
if (s instanceof RuleStopState)
continue;
// special case: if decision point, then line up the alt start states
// unless it's an end of block
// if ( s instanceof BlockStartState ) {
// ST rankST = stlib.getInstanceOf("decision-rank");
// DecisionState alt = (DecisionState)s;
// for (int i=0; i<alt.getNumberOfTransitions(); i++) {
// ATNState target = alt.transition(i).target;
// if ( target!=null ) {
// rankST.add("states", target.stateNumber);
// }
// }
// dot.add("decisionRanks", rankST);
// }
// make a DOT edge for each transition
ST edgeST;
for (int i = 0; i < s.getNumberOfTransitions(); i++) {
Transition edge = s.transition(i);
if (edge instanceof RuleTransition) {
RuleTransition rr = ((RuleTransition) edge);
// don't jump to other rules, but display edge to follow node
edgeST = stlib.getInstanceOf("edge");
String label = "<" + ruleNames[rr.ruleIndex];
if (((RuleStartState) rr.target).isPrecedenceRule) {
label += "[" + rr.precedence + "]";
}
label += ">";
edgeST.add("label", label);
edgeST.add("src", "s" + s.stateNumber);
edgeST.add("target", "s" + rr.followState.stateNumber);
edgeST.add("arrowhead", arrowhead);
dot.add("edges", edgeST);
work.add(rr.followState);
continue;
}
if (edge instanceof ActionTransition) {
edgeST = stlib.getInstanceOf("action-edge");
edgeST.add("label", getEdgeLabel(edge.toString()));
} else if (edge instanceof AbstractPredicateTransition) {
edgeST = stlib.getInstanceOf("edge");
edgeST.add("label", getEdgeLabel(edge.toString()));
} else if (edge.isEpsilon()) {
edgeST = stlib.getInstanceOf("epsilon-edge");
edgeST.add("label", getEdgeLabel(edge.toString()));
boolean loopback = false;
if (edge.target instanceof PlusBlockStartState) {
loopback = s.equals(((PlusBlockStartState) edge.target).loopBackState);
} else if (edge.target instanceof StarLoopEntryState) {
loopback = s.equals(((StarLoopEntryState) edge.target).loopBackState);
}
edgeST.add("loopback", loopback);
} else if (edge instanceof AtomTransition) {
edgeST = stlib.getInstanceOf("edge");
AtomTransition atom = (AtomTransition) edge;
String label = String.valueOf(atom.label);
if (isLexer)
label = "'" + getEdgeLabel(new StringBuilder().appendCodePoint(atom.label).toString()) + "'";
else if (grammar != null)
label = grammar.getTokenDisplayName(atom.label);
edgeST.add("label", getEdgeLabel(label));
} else if (edge instanceof SetTransition) {
edgeST = stlib.getInstanceOf("edge");
SetTransition set = (SetTransition) edge;
String label = set.label().toString();
if (isLexer)
label = set.label().toString(true);
else if (grammar != null)
label = set.label().toString(grammar.getVocabulary());
if (edge instanceof NotSetTransition)
label = "~" + label;
edgeST.add("label", getEdgeLabel(label));
} else if (edge instanceof RangeTransition) {
edgeST = stlib.getInstanceOf("edge");
RangeTransition range = (RangeTransition) edge;
String label = range.label().toString();
if (isLexer)
label = range.toString();
else if (grammar != null)
label = range.label().toString(grammar.getVocabulary());
edgeST.add("label", getEdgeLabel(label));
} else {
edgeST = stlib.getInstanceOf("edge");
edgeST.add("label", getEdgeLabel(edge.toString()));
}
edgeST.add("src", "s" + s.stateNumber);
edgeST.add("target", "s" + edge.target.stateNumber);
edgeST.add("arrowhead", arrowhead);
if (s.getNumberOfTransitions() > 1) {
edgeST.add("transitionIndex", i);
} else {
edgeST.add("transitionIndex", false);
}
dot.add("edges", edgeST);
work.add(edge.target);
}
}
// }
for (ATNState s : markedStates) {
if (!(s instanceof RuleStopState))
continue;
ST st = stlib.getInstanceOf("stopstate");
st.add("name", "s" + s.stateNumber);
st.add("label", getStateLabel(s));
dot.add("states", st);
}
for (ATNState s : markedStates) {
if (s instanceof RuleStopState)
continue;
ST st = stlib.getInstanceOf("state");
st.add("name", "s" + s.stateNumber);
st.add("label", getStateLabel(s));
st.add("transitions", s.getTransitions());
dot.add("states", st);
}
return dot.render();
}
Aggregations