forked from jasder/antlr
Use HashMap instead of LinkedHashMap for DFA.states
This commit is contained in:
parent
7ebcc41b7b
commit
845bf53a51
|
@ -38,8 +38,10 @@ import org.antlr.v4.runtime.misc.NotNull;
|
||||||
import org.antlr.v4.runtime.misc.Nullable;
|
import org.antlr.v4.runtime.misc.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -49,7 +51,7 @@ public class DFA {
|
||||||
* ({@link Set} only allows you to see if it's there).
|
* ({@link Set} only allows you to see if it's there).
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public final Map<DFAState, DFAState> states = new LinkedHashMap<DFAState, DFAState>();
|
public final Map<DFAState, DFAState> states = new HashMap<DFAState, DFAState>();
|
||||||
@Nullable
|
@Nullable
|
||||||
public DFAState s0;
|
public DFAState s0;
|
||||||
|
|
||||||
|
@ -73,6 +75,22 @@ public class DFA {
|
||||||
this.decision = decision;
|
this.decision = decision;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a list of all states in this DFA, ordered by state number.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public List<DFAState> getStates() {
|
||||||
|
List<DFAState> result = new ArrayList<DFAState>(states.keySet());
|
||||||
|
Collections.sort(result, new Comparator<DFAState>() {
|
||||||
|
@Override
|
||||||
|
public int compare(DFAState o1, DFAState o2) {
|
||||||
|
return o1.stateNumber - o2.stateNumber;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public List<Set<ATNState>> getATNStatesAlongPath(ParserATNSimulator atn,
|
public List<Set<ATNState>> getATNStatesAlongPath(ParserATNSimulator atn,
|
||||||
List<DFAState> dfaStates,
|
List<DFAState> dfaStates,
|
||||||
TokenStream input, int start, int stop)
|
TokenStream input, int start, int stop)
|
||||||
|
|
|
@ -34,7 +34,7 @@ import org.antlr.v4.runtime.misc.NotNull;
|
||||||
import org.antlr.v4.runtime.misc.Nullable;
|
import org.antlr.v4.runtime.misc.Nullable;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.List;
|
||||||
|
|
||||||
/** A DFA walker that knows how to dump them to serialized strings. */
|
/** A DFA walker that knows how to dump them to serialized strings. */
|
||||||
public class DFASerializer {
|
public class DFASerializer {
|
||||||
|
@ -52,21 +52,20 @@ public class DFASerializer {
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if ( dfa.s0==null ) return null;
|
if ( dfa.s0==null ) return null;
|
||||||
StringBuilder buf = new StringBuilder();
|
StringBuilder buf = new StringBuilder();
|
||||||
Map<DFAState,DFAState> states = dfa.states;
|
List<DFAState> states = dfa.getStates();
|
||||||
if ( states!=null ) {
|
for (DFAState s : states) {
|
||||||
for (DFAState s : states.values()) {
|
int n = 0;
|
||||||
int n = 0;
|
if ( s.edges!=null ) n = s.edges.length;
|
||||||
if ( s.edges!=null ) n = s.edges.length;
|
for (int i=0; i<n; i++) {
|
||||||
for (int i=0; i<n; i++) {
|
DFAState t = s.edges[i];
|
||||||
DFAState t = s.edges[i];
|
if ( t!=null && t.stateNumber != Integer.MAX_VALUE ) {
|
||||||
if ( t!=null && t.stateNumber != Integer.MAX_VALUE ) {
|
buf.append(getStateString(s));
|
||||||
buf.append(getStateString(s));
|
String label = getEdgeLabel(i);
|
||||||
String label = getEdgeLabel(i);
|
buf.append("-").append(label).append("->").append(getStateString(t)).append('\n');
|
||||||
buf.append("-").append(label).append("->").append(getStateString(t)).append('\n');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String output = buf.toString();
|
String output = buf.toString();
|
||||||
//return Utils.sortLinesInString(output);
|
//return Utils.sortLinesInString(output);
|
||||||
return output;
|
return output;
|
||||||
|
|
Loading…
Reference in New Issue