use of org.antlr.v4.runtime.atn.SetTransition 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();
}
use of org.antlr.v4.runtime.atn.SetTransition in project antlr4 by tunnelvisionlabs.
the class ATNDeserializer method optimizeSets.
private static int optimizeSets(ATN atn, boolean preserveOrder) {
if (preserveOrder) {
// this optimization currently doesn't preserve edge order.
return 0;
}
int removedPaths = 0;
List<DecisionState> decisions = atn.decisionToState;
for (DecisionState decision : decisions) {
IntervalSet setTransitions = new IntervalSet();
for (int i = 0; i < decision.getNumberOfOptimizedTransitions(); i++) {
Transition epsTransition = decision.getOptimizedTransition(i);
if (!(epsTransition instanceof EpsilonTransition)) {
continue;
}
if (epsTransition.target.getNumberOfOptimizedTransitions() != 1) {
continue;
}
Transition transition = epsTransition.target.getOptimizedTransition(0);
if (!(transition.target instanceof BlockEndState)) {
continue;
}
if (transition instanceof NotSetTransition) {
// TODO: not yet implemented
continue;
}
if (transition instanceof AtomTransition || transition instanceof RangeTransition || transition instanceof SetTransition) {
setTransitions.add(i);
}
}
if (setTransitions.size() <= 1) {
continue;
}
List<Transition> optimizedTransitions = new ArrayList<Transition>();
for (int i = 0; i < decision.getNumberOfOptimizedTransitions(); i++) {
if (!setTransitions.contains(i)) {
optimizedTransitions.add(decision.getOptimizedTransition(i));
}
}
ATNState blockEndState = decision.getOptimizedTransition(setTransitions.getMinElement()).target.getOptimizedTransition(0).target;
IntervalSet matchSet = new IntervalSet();
for (int i = 0; i < setTransitions.getIntervals().size(); i++) {
Interval interval = setTransitions.getIntervals().get(i);
for (int j = interval.a; j <= interval.b; j++) {
Transition matchTransition = decision.getOptimizedTransition(j).target.getOptimizedTransition(0);
if (matchTransition instanceof NotSetTransition) {
throw new UnsupportedOperationException("Not yet implemented.");
} else {
matchSet.addAll(matchTransition.label());
}
}
}
Transition newTransition;
if (matchSet.getIntervals().size() == 1) {
if (matchSet.size() == 1) {
newTransition = new AtomTransition(blockEndState, matchSet.getMinElement());
} else {
Interval matchInterval = matchSet.getIntervals().get(0);
newTransition = new RangeTransition(blockEndState, matchInterval.a, matchInterval.b);
}
} else {
newTransition = new SetTransition(blockEndState, matchSet);
}
ATNState setOptimizedState = new BasicState();
setOptimizedState.setRuleIndex(decision.ruleIndex);
atn.addState(setOptimizedState);
setOptimizedState.addTransition(newTransition);
optimizedTransitions.add(new EpsilonTransition(setOptimizedState));
removedPaths += decision.getNumberOfOptimizedTransitions() - optimizedTransitions.size();
if (decision.isOptimized()) {
while (decision.getNumberOfOptimizedTransitions() > 0) {
decision.removeOptimizedTransition(decision.getNumberOfOptimizedTransitions() - 1);
}
}
for (Transition transition : optimizedTransitions) {
decision.addOptimizedTransition(transition);
}
}
if (ParserATNSimulator.debug) {
System.out.println("ATN runtime optimizer removed " + removedPaths + " paths by collapsing sets.");
}
return removedPaths;
}
use of org.antlr.v4.runtime.atn.SetTransition in project antlr4 by tunnelvisionlabs.
the class ATNSerializer method serialize.
/**
* Serialize state descriptors, edge descriptors, and decision→state map
* into list of ints:
*
* grammar-type, (ANTLRParser.LEXER, ...)
* max token type,
* num states,
* state-0-type ruleIndex, state-1-type ruleIndex, ... state-i-type ruleIndex optional-arg ...
* num rules,
* rule-1-start-state rule-1-args, rule-2-start-state rule-2-args, ...
* (args are token type,actionIndex in lexer else 0,0)
* num modes,
* mode-0-start-state, mode-1-start-state, ... (parser has 0 modes)
* num unicode-bmp-sets
* bmp-set-0-interval-count intervals, bmp-set-1-interval-count intervals, ...
* num unicode-smp-sets
* smp-set-0-interval-count intervals, smp-set-1-interval-count intervals, ...
* num total edges,
* src, trg, edge-type, edge arg1, optional edge arg2 (present always), ...
* num decisions,
* decision-0-start-state, decision-1-start-state, ...
*
* Convenient to pack into unsigned shorts to make as Java string.
*/
public IntegerList serialize() {
IntegerList data = new IntegerList();
data.add(ATNDeserializer.SERIALIZED_VERSION);
serializeUUID(data, ATNDeserializer.SERIALIZED_UUID);
// convert grammar type to ATN const to avoid dependence on ANTLRParser
data.add(atn.grammarType.ordinal());
data.add(atn.maxTokenType);
int nedges = 0;
// Note that we use a LinkedHashMap as a set to
// maintain insertion order while deduplicating
// entries with the same key.
Map<IntervalSet, Boolean> sets = new LinkedHashMap<IntervalSet, Boolean>();
// dump states, count edges and collect sets while doing so
IntegerList nonGreedyStates = new IntegerList();
IntegerList sllStates = new IntegerList();
IntegerList precedenceStates = new IntegerList();
data.add(atn.states.size());
for (ATNState s : atn.states) {
if (s == null) {
// might be optimized away
data.add(ATNState.INVALID_TYPE);
continue;
}
int stateType = s.getStateType();
if (s instanceof DecisionState) {
DecisionState decisionState = (DecisionState) s;
if (decisionState.nonGreedy) {
nonGreedyStates.add(s.stateNumber);
}
if (decisionState.sll) {
sllStates.add(s.stateNumber);
}
}
if (s instanceof RuleStartState && ((RuleStartState) s).isPrecedenceRule) {
precedenceStates.add(s.stateNumber);
}
data.add(stateType);
if (s.ruleIndex == -1) {
data.add(Character.MAX_VALUE);
} else {
data.add(s.ruleIndex);
}
if (s.getStateType() == ATNState.LOOP_END) {
data.add(((LoopEndState) s).loopBackState.stateNumber);
} else if (s instanceof BlockStartState) {
data.add(((BlockStartState) s).endState.stateNumber);
}
if (s.getStateType() != ATNState.RULE_STOP) {
// the deserializer can trivially derive these edges, so there's no need to serialize them
nedges += s.getNumberOfTransitions();
}
for (int i = 0; i < s.getNumberOfTransitions(); i++) {
Transition t = s.transition(i);
int edgeType = Transition.serializationTypes.get(t.getClass());
if (edgeType == Transition.SET || edgeType == Transition.NOT_SET) {
SetTransition st = (SetTransition) t;
sets.put(st.set, true);
}
}
}
// non-greedy states
data.add(nonGreedyStates.size());
for (int i = 0; i < nonGreedyStates.size(); i++) {
data.add(nonGreedyStates.get(i));
}
// SLL decisions
data.add(sllStates.size());
for (int i = 0; i < sllStates.size(); i++) {
data.add(sllStates.get(i));
}
// precedence states
data.add(precedenceStates.size());
for (int i = 0; i < precedenceStates.size(); i++) {
data.add(precedenceStates.get(i));
}
int nrules = atn.ruleToStartState.length;
data.add(nrules);
for (int r = 0; r < nrules; r++) {
ATNState ruleStartState = atn.ruleToStartState[r];
data.add(ruleStartState.stateNumber);
boolean leftFactored = ruleNames.get(ruleStartState.ruleIndex).indexOf(ATNSimulator.RULE_VARIANT_DELIMITER) >= 0;
data.add(leftFactored ? 1 : 0);
if (atn.grammarType == ATNType.LEXER) {
if (atn.ruleToTokenType[r] == Token.EOF) {
data.add(Character.MAX_VALUE);
} else {
data.add(atn.ruleToTokenType[r]);
}
}
}
int nmodes = atn.modeToStartState.size();
data.add(nmodes);
if (nmodes > 0) {
for (ATNState modeStartState : atn.modeToStartState) {
data.add(modeStartState.stateNumber);
}
}
List<IntervalSet> bmpSets = new ArrayList<IntervalSet>();
List<IntervalSet> smpSets = new ArrayList<IntervalSet>();
for (IntervalSet set : sets.keySet()) {
if (set.getMaxElement() <= Character.MAX_VALUE) {
bmpSets.add(set);
} else {
smpSets.add(set);
}
}
serializeSets(data, bmpSets, new CodePointSerializer() {
@Override
public void serializeCodePoint(IntegerList data, int cp) {
data.add(cp);
}
});
serializeSets(data, smpSets, new CodePointSerializer() {
@Override
public void serializeCodePoint(IntegerList data, int cp) {
serializeInt(data, cp);
}
});
Map<IntervalSet, Integer> setIndices = new HashMap<IntervalSet, Integer>();
int setIndex = 0;
for (IntervalSet bmpSet : bmpSets) {
setIndices.put(bmpSet, setIndex++);
}
for (IntervalSet smpSet : smpSets) {
setIndices.put(smpSet, setIndex++);
}
data.add(nedges);
for (ATNState s : atn.states) {
if (s == null) {
// might be optimized away
continue;
}
if (s.getStateType() == ATNState.RULE_STOP) {
continue;
}
for (int i = 0; i < s.getNumberOfTransitions(); i++) {
Transition t = s.transition(i);
if (atn.states.get(t.target.stateNumber) == null) {
throw new IllegalStateException("Cannot serialize a transition to a removed state.");
}
int src = s.stateNumber;
int trg = t.target.stateNumber;
int edgeType = Transition.serializationTypes.get(t.getClass());
int arg1 = 0;
int arg2 = 0;
int arg3 = 0;
switch(edgeType) {
case Transition.RULE:
trg = ((RuleTransition) t).followState.stateNumber;
arg1 = ((RuleTransition) t).target.stateNumber;
arg2 = ((RuleTransition) t).ruleIndex;
arg3 = ((RuleTransition) t).precedence;
break;
case Transition.PRECEDENCE:
PrecedencePredicateTransition ppt = (PrecedencePredicateTransition) t;
arg1 = ppt.precedence;
break;
case Transition.PREDICATE:
PredicateTransition pt = (PredicateTransition) t;
arg1 = pt.ruleIndex;
arg2 = pt.predIndex;
arg3 = pt.isCtxDependent ? 1 : 0;
break;
case Transition.RANGE:
arg1 = ((RangeTransition) t).from;
arg2 = ((RangeTransition) t).to;
if (arg1 == Token.EOF) {
arg1 = 0;
arg3 = 1;
}
break;
case Transition.ATOM:
arg1 = ((AtomTransition) t).label;
if (arg1 == Token.EOF) {
arg1 = 0;
arg3 = 1;
}
break;
case Transition.ACTION:
ActionTransition at = (ActionTransition) t;
arg1 = at.ruleIndex;
arg2 = at.actionIndex;
if (arg2 == -1) {
arg2 = 0xFFFF;
}
arg3 = at.isCtxDependent ? 1 : 0;
break;
case Transition.SET:
arg1 = setIndices.get(((SetTransition) t).set);
break;
case Transition.NOT_SET:
arg1 = setIndices.get(((SetTransition) t).set);
break;
case Transition.WILDCARD:
break;
}
data.add(src);
data.add(trg);
data.add(edgeType);
data.add(arg1);
data.add(arg2);
data.add(arg3);
}
}
int ndecisions = atn.decisionToState.size();
data.add(ndecisions);
for (DecisionState decStartState : atn.decisionToState) {
data.add(decStartState.stateNumber);
}
//
if (atn.grammarType == ATNType.LEXER) {
data.add(atn.lexerActions.length);
for (LexerAction action : atn.lexerActions) {
data.add(action.getActionType().ordinal());
switch(action.getActionType()) {
case CHANNEL:
int channel = ((LexerChannelAction) action).getChannel();
data.add(channel != -1 ? channel : 0xFFFF);
data.add(0);
break;
case CUSTOM:
int ruleIndex = ((LexerCustomAction) action).getRuleIndex();
int actionIndex = ((LexerCustomAction) action).getActionIndex();
data.add(ruleIndex != -1 ? ruleIndex : 0xFFFF);
data.add(actionIndex != -1 ? actionIndex : 0xFFFF);
break;
case MODE:
int mode = ((LexerModeAction) action).getMode();
data.add(mode != -1 ? mode : 0xFFFF);
data.add(0);
break;
case MORE:
data.add(0);
data.add(0);
break;
case POP_MODE:
data.add(0);
data.add(0);
break;
case PUSH_MODE:
mode = ((LexerPushModeAction) action).getMode();
data.add(mode != -1 ? mode : 0xFFFF);
data.add(0);
break;
case SKIP:
data.add(0);
data.add(0);
break;
case TYPE:
int type = ((LexerTypeAction) action).getType();
data.add(type != -1 ? type : 0xFFFF);
data.add(0);
break;
default:
String message = String.format(Locale.getDefault(), "The specified lexer action type %s is not valid.", action.getActionType());
throw new IllegalArgumentException(message);
}
}
}
// don't adjust the first value since that's the version number
for (int i = 1; i < data.size(); i++) {
if (data.get(i) < Character.MIN_VALUE || data.get(i) > Character.MAX_VALUE) {
throw new UnsupportedOperationException("Serialized ATN data element " + data.get(i) + " element " + i + " out of range " + (int) Character.MIN_VALUE + ".." + (int) Character.MAX_VALUE);
}
int value = (data.get(i) + 2) & 0xFFFF;
data.set(i, value);
}
return data;
}
use of org.antlr.v4.runtime.atn.SetTransition in project antlr4 by tunnelvisionlabs.
the class ATNPrinter method asString.
public String asString() {
if (start == null)
return null;
marked = new HashSet<ATNState>();
work = new ArrayList<ATNState>();
work.add(start);
StringBuilder buf = new StringBuilder();
ATNState s;
while (!work.isEmpty()) {
s = work.remove(0);
if (marked.contains(s))
continue;
int n = s.getNumberOfTransitions();
// System.out.println("visit "+s+"; edges="+n);
marked.add(s);
for (int i = 0; i < n; i++) {
Transition t = s.transition(i);
if (!(s instanceof RuleStopState)) {
// don't add follow states to work
if (t instanceof RuleTransition)
work.add(((RuleTransition) t).followState);
else
work.add(t.target);
}
buf.append(getStateString(s));
if (t instanceof EpsilonTransition) {
buf.append("->").append(getStateString(t.target)).append('\n');
} else if (t instanceof RuleTransition) {
buf.append("-").append(g.getRule(((RuleTransition) t).ruleIndex).name).append("->").append(getStateString(t.target)).append('\n');
} else if (t instanceof ActionTransition) {
ActionTransition a = (ActionTransition) t;
buf.append("-").append(a.toString()).append("->").append(getStateString(t.target)).append('\n');
} else if (t instanceof SetTransition) {
SetTransition st = (SetTransition) t;
boolean not = st instanceof NotSetTransition;
if (g.isLexer()) {
buf.append("-").append(not ? "~" : "").append(st.toString()).append("->").append(getStateString(t.target)).append('\n');
} else {
buf.append("-").append(not ? "~" : "").append(st.label().toString(g.getVocabulary())).append("->").append(getStateString(t.target)).append('\n');
}
} else if (t instanceof AtomTransition) {
AtomTransition a = (AtomTransition) t;
String label = g.getTokenDisplayName(a.label);
buf.append("-").append(label).append("->").append(getStateString(t.target)).append('\n');
} else {
buf.append("-").append(t.toString()).append("->").append(getStateString(t.target)).append('\n');
}
}
}
return buf.toString();
}
use of org.antlr.v4.runtime.atn.SetTransition in project antlr4 by antlr.
the class ATNPrinter method asString.
public String asString() {
if (start == null)
return null;
marked = new HashSet<ATNState>();
work = new ArrayList<ATNState>();
work.add(start);
StringBuilder buf = new StringBuilder();
ATNState s;
while (!work.isEmpty()) {
s = work.remove(0);
if (marked.contains(s))
continue;
int n = s.getNumberOfTransitions();
// System.out.println("visit "+s+"; edges="+n);
marked.add(s);
for (int i = 0; i < n; i++) {
Transition t = s.transition(i);
if (!(s instanceof RuleStopState)) {
// don't add follow states to work
if (t instanceof RuleTransition)
work.add(((RuleTransition) t).followState);
else
work.add(t.target);
}
buf.append(getStateString(s));
if (t instanceof EpsilonTransition) {
buf.append("->").append(getStateString(t.target)).append('\n');
} else if (t instanceof RuleTransition) {
buf.append("-").append(g.getRule(((RuleTransition) t).ruleIndex).name).append("->").append(getStateString(t.target)).append('\n');
} else if (t instanceof ActionTransition) {
ActionTransition a = (ActionTransition) t;
buf.append("-").append(a.toString()).append("->").append(getStateString(t.target)).append('\n');
} else if (t instanceof SetTransition) {
SetTransition st = (SetTransition) t;
boolean not = st instanceof NotSetTransition;
if (g.isLexer()) {
buf.append("-").append(not ? "~" : "").append(st.toString()).append("->").append(getStateString(t.target)).append('\n');
} else {
buf.append("-").append(not ? "~" : "").append(st.label().toString(g.getVocabulary())).append("->").append(getStateString(t.target)).append('\n');
}
} else if (t instanceof AtomTransition) {
AtomTransition a = (AtomTransition) t;
String label = g.getTokenDisplayName(a.label);
buf.append("-").append(label).append("->").append(getStateString(t.target)).append('\n');
} else {
buf.append("-").append(t.toString()).append("->").append(getStateString(t.target)).append('\n');
}
}
}
return buf.toString();
}
Aggregations