merge static method

Implementation

static PredictionContext merge(
  PredictionContext a,
  PredictionContext b,
  bool rootIsWildcard,
  Map<Pair<PredictionContext, PredictionContext>, PredictionContext>?
      mergeCache,
) {
  // share same graph if both same
  if (a == b || a == b) return a;

  if (a is SingletonPredictionContext && b is SingletonPredictionContext) {
    return mergeSingletons(a, b, rootIsWildcard, mergeCache);
  }

  // At least one of a or b is array
  // If one is $ and rootIsWildcard, return $ as * wildcard
  if (rootIsWildcard) {
    if (a is EmptyPredictionContext) return a;
    if (b is EmptyPredictionContext) return b;
  }

  // convert singleton so both are arrays to normalize
  if (a is SingletonPredictionContext) {
    a = ArrayPredictionContext.of(a);
  }
  if (b is SingletonPredictionContext) {
    b = ArrayPredictionContext.of(b);
  }
  return mergeArrays(
    a as ArrayPredictionContext,
    b as ArrayPredictionContext,
    rootIsWildcard,
    mergeCache,
  );
}