cleanup, simplify array ctx equals

This commit is contained in:
Terence Parr 2012-07-22 12:17:34 -07:00
parent 43424cd5df
commit 8695210903
2 changed files with 12 additions and 13 deletions

View File

@ -160,15 +160,8 @@ public class ArrayPredictionContext extends PredictionContext {
} }
ArrayPredictionContext a = (ArrayPredictionContext)o; ArrayPredictionContext a = (ArrayPredictionContext)o;
if ( invokingStates.length != a.invokingStates.length ) { return Arrays.equals(invokingStates, a.invokingStates) &&
return false; Arrays.equals(parents, a.parents);
}
for (int i=0; i< invokingStates.length; i++) {
if ( invokingStates[i]!=a.invokingStates[i] ) return false;
if ( !parents[i].equals(a.parents[i]) ) return false;
}
return true;
} }
@Override @Override

View File

@ -204,23 +204,29 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
boolean rootIsWildcard) boolean rootIsWildcard)
{ {
if ( a.equals(b) ) return a; // share same graph if both same if ( a.equals(b) ) return a; // share same graph if both same
if ( a instanceof SingletonPredictionContext && b instanceof SingletonPredictionContext) { if ( a instanceof SingletonPredictionContext && b instanceof SingletonPredictionContext) {
return mergeSingletons((SingletonPredictionContext)a, return mergeSingletons((SingletonPredictionContext)a,
(SingletonPredictionContext)b, rootIsWildcard); (SingletonPredictionContext)b,
rootIsWildcard);
} }
// at least one of a or b is array; convert one so both are arrays
// unless one is $ and rootIsWildcard, which means we return $ as * wildcard // At least one of a or b is array
// If one is $ and rootIsWildcard, return $ as * wildcard
if ( rootIsWildcard ) { if ( rootIsWildcard ) {
if ( a instanceof EmptyPredictionContext ) return a; if ( a instanceof EmptyPredictionContext ) return a;
if ( b instanceof EmptyPredictionContext ) return b; if ( b instanceof EmptyPredictionContext ) return b;
} }
// convert singleton so both are arrays to normalize
if ( a instanceof SingletonPredictionContext ) { if ( a instanceof SingletonPredictionContext ) {
a = new ArrayPredictionContext((SingletonPredictionContext)a); a = new ArrayPredictionContext((SingletonPredictionContext)a);
} }
if ( b instanceof SingletonPredictionContext) { if ( b instanceof SingletonPredictionContext) {
b = new ArrayPredictionContext((SingletonPredictionContext)b); b = new ArrayPredictionContext((SingletonPredictionContext)b);
} }
return mergeArrays((ArrayPredictionContext)a, (ArrayPredictionContext)b, rootIsWildcard); return mergeArrays((ArrayPredictionContext)a, (ArrayPredictionContext)b,
rootIsWildcard);
} }
// http://www.antlr.org/wiki/download/attachments/32014352/singleton-merge.png // http://www.antlr.org/wiki/download/attachments/32014352/singleton-merge.png