From 90516272fed11d4925d2b8e807c6500e2d83e43d Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Sun, 18 Mar 2012 15:39:08 -0700 Subject: [PATCH] snapshot --- .../antlr/v4/runtime/atn/ATNConfigSet.java | 95 ++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNConfigSet.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNConfigSet.java index 9e961241f..4fe1154aa 100755 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNConfigSet.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNConfigSet.java @@ -30,15 +30,19 @@ package org.antlr.v4.runtime.atn; import org.antlr.v4.runtime.misc.IntervalSet; -import org.antlr.v4.runtime.misc.OrderedHashSet; +import org.antlr.v4.runtime.misc.Triple; +import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; import java.util.Set; /** Specialized OrderedHashSet that can track info about the set. * Might be able to optimize later w/o affecting code that uses this set. */ -public class ATNConfigSet extends OrderedHashSet { +public class ATNConfigSet implements Set { // TODO: these fields make me pretty uncomfortable but nice to pack up info together, saves recomputation // TODO: can we track conflicts as they are added to save scanning configs later? public int uniqueAlt; @@ -46,6 +50,9 @@ public class ATNConfigSet extends OrderedHashSet { public boolean hasSemanticContext; public boolean dipsIntoOuterContext; + Map, PredictionContext> m = + new HashMap, PredictionContext>(); + public ATNConfigSet() { } public ATNConfigSet(ATNConfigSet old) { @@ -56,6 +63,23 @@ public class ATNConfigSet extends OrderedHashSet { this.dipsIntoOuterContext = old.dipsIntoOuterContext; } + /** Adding a new config means merging contexts with existing configs for + * (s, i, pi, _) + * We use (s,i,pi) as key + */ + @Override + public boolean add(ATNConfig value) { + Triple key = + new Triple( + value.state,value.alt,value.semanticContext + ); + PredictionContext existing = m.get(key); + if ( existing==null ) return false; + PredictionContext merged = PredictionContext.merge(existing, value.context, true); + m.put(key, merged); + return true; + } + public Set getStates() { Set states = new HashSet(); for (ATNConfig c : this.elements) { @@ -74,4 +98,71 @@ public class ATNConfigSet extends OrderedHashSet { if ( dipsIntoOuterContext ) buf.append(",dipsIntoOuterContext"); return buf.toString(); } + + private static long getKey(ATNConfig e) { + long key = ((long) e.state.stateNumber << 32) + (e.alt << 3); + //key |= e.reachesIntoOuterContext != 0 ? 1 : 0; + //key |= e.resolveWithPredicate ? 1 << 1 : 0; + //key |= e.traversedPredicate ? 1 << 2 : 0; + return key; + } + + @Override + public boolean addAll(Collection c) { + return false; + } + + @Override + public int size() { + return 0; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public boolean contains(Object o) { + return false; + } + + @Override + public Iterator iterator() { + return null; + } + + @Override + public Object[] toArray() { + return new Object[0]; + } + + @Override + public T[] toArray(T[] a) { + return null; + } + + @Override + public boolean remove(Object o) { + return false; + } + + @Override + public boolean containsAll(Collection c) { + return false; + } + + @Override + public boolean retainAll(Collection c) { + return false; + } + + @Override + public boolean removeAll(Collection c) { + return false; + } + + @Override + public void clear() { + } }