add counter for stack graph nodes.

This commit is contained in:
Terence Parr 2013-04-20 10:13:02 -07:00
parent 7a7f4a7851
commit 43e73c8da9
1 changed files with 21 additions and 0 deletions

View File

@ -30,6 +30,7 @@
package org.antlr.v4.runtime.atn;
import org.antlr.misc.MutableInteger;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.misc.DoubleKeyMap;
@ -654,6 +655,26 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
}
}
public static int countAllContextNodes(PredictionContext context) {
MutableInteger count = new MutableInteger(0);
Map<PredictionContext, PredictionContext> visited =
new IdentityHashMap<PredictionContext, PredictionContext>();
countAllContextNodes_(context, count, visited);
return count.value;
}
public static void countAllContextNodes_(PredictionContext context,
MutableInteger count,
Map<PredictionContext, PredictionContext> visited)
{
if ( context==null || visited.containsKey(context) ) return;
visited.put(context, context);
count.value++;
for (int i = 0; i < context.size(); i++) {
countAllContextNodes_(context.getParent(i), count, visited);
}
}
public String toString(@Nullable Recognizer<?,?> recog) {
return toString();
// return toString(recog, ParserRuleContext.EMPTY);