public static Antlr4.Runtime.Atn.PredictionContext GetCachedContext([NotNull] Antlr4.Runtime.Atn.PredictionContext context, [NotNull] ConcurrentDictionary <Antlr4.Runtime.Atn.PredictionContext, Antlr4.Runtime.Atn.PredictionContext> contextCache, [NotNull] PredictionContext.IdentityHashMap visited)
        {
            if (context.IsEmpty)
            {
                return(context);
            }
            Antlr4.Runtime.Atn.PredictionContext existing;
            if (visited.TryGetValue(context, out existing))
            {
                return(existing);
            }
            if (contextCache.TryGetValue(context, out existing))
            {
                visited[context] = existing;
                return(existing);
            }
            bool changed = false;

            Antlr4.Runtime.Atn.PredictionContext[] parents = new Antlr4.Runtime.Atn.PredictionContext[context.Size];
            for (int i = 0; i < parents.Length; i++)
            {
                Antlr4.Runtime.Atn.PredictionContext parent = GetCachedContext(context.GetParent(i), contextCache, visited);
                if (changed || parent != context.GetParent(i))
                {
                    if (!changed)
                    {
                        parents = new Antlr4.Runtime.Atn.PredictionContext[context.Size];
                        for (int j = 0; j < context.Size; j++)
                        {
                            parents[j] = context.GetParent(j);
                        }
                        changed = true;
                    }
                    parents[i] = parent;
                }
            }
            if (!changed)
            {
                existing         = contextCache.GetOrAdd(context, context);
                visited[context] = existing;
                return(context);
            }
            // We know parents.length>0 because context.isEmpty() is checked at the beginning of the method.
            Antlr4.Runtime.Atn.PredictionContext updated;
            if (parents.Length == 1)
            {
                updated = new SingletonPredictionContext(parents[0], context.GetReturnState(0));
            }
            else
            {
                ArrayPredictionContext arrayPredictionContext = (ArrayPredictionContext)context;
                updated = new ArrayPredictionContext(parents, arrayPredictionContext.returnStates, context.cachedHashCode);
            }
            existing         = contextCache.GetOrAdd(updated, updated);
            visited[updated] = existing;
            visited[context] = existing;
            return(updated);
        }
        protected internal static int CalculateHashCode(Antlr4.Runtime.Atn.PredictionContext parent, int returnState)
        {
            int hash = MurmurHash.Initialize(InitialHash);

            hash = MurmurHash.Update(hash, parent);
            hash = MurmurHash.Update(hash, returnState);
            hash = MurmurHash.Finish(hash, 2);
            return(hash);
        }
        public virtual string[] ToStrings(IRecognizer recognizer, Antlr4.Runtime.Atn.PredictionContext stop, int currentState)
        {
            List <string> result = new List <string>();

            for (int perm = 0; ; perm++)
            {
                int  offset = 0;
                bool last   = true;
                Antlr4.Runtime.Atn.PredictionContext p = this;
                int           stateNumber = currentState;
                StringBuilder localBuffer = new StringBuilder();
                localBuffer.Append("[");
                while (!p.IsEmpty && p != stop)
                {
                    int index = 0;
                    if (p.Size > 0)
                    {
                        int bits = 1;
                        while ((1 << bits) < p.Size)
                        {
                            bits++;
                        }
                        int mask = (1 << bits) - 1;
                        index = (perm >> offset) & mask;
                        last &= index >= p.Size - 1;
                        if (index >= p.Size)
                        {
                            goto outer_continue;
                        }
                        offset += bits;
                    }
                    if (recognizer != null)
                    {
                        if (localBuffer.Length > 1)
                        {
                            // first char is '[', if more than that this isn't the first rule
                            localBuffer.Append(' ');
                        }
                        ATN      atn      = recognizer.Atn;
                        ATNState s        = atn.states[stateNumber];
                        string   ruleName = recognizer.RuleNames[s.ruleIndex];
                        localBuffer.Append(ruleName);
                    }
                    else
                    {
                        if (p.GetReturnState(index) != EmptyFullStateKey)
                        {
                            if (!p.IsEmpty)
                            {
                                if (localBuffer.Length > 1)
                                {
                                    // first char is '[', if more than that this isn't the first rule
                                    localBuffer.Append(' ');
                                }
                                localBuffer.Append(p.GetReturnState(index));
                            }
                        }
                    }
                    stateNumber = p.GetReturnState(index);
                    p           = p.GetParent(index);
                }
                localBuffer.Append("]");
                result.Add(localBuffer.ToString());
                if (last)
                {
                    break;
                }
                outer_continue :;
            }

            return(result.ToArray());
        }
 public abstract Antlr4.Runtime.Atn.PredictionContext AppendContext(Antlr4.Runtime.Atn.PredictionContext suffix, PredictionContextCache contextCache);
 public static bool IsEmptyLocal(Antlr4.Runtime.Atn.PredictionContext context)
 {
     return(context == EmptyLocal);
 }
        /*package*/
        internal static Antlr4.Runtime.Atn.PredictionContext Join([NotNull] Antlr4.Runtime.Atn.PredictionContext context0, [NotNull] Antlr4.Runtime.Atn.PredictionContext context1, [NotNull] PredictionContextCache contextCache)
        {
            if (context0 == context1)
            {
                return(context0);
            }
            if (context0.IsEmpty)
            {
                return(IsEmptyLocal(context0) ? context0 : AddEmptyContext(context1));
            }
            else
            {
                if (context1.IsEmpty)
                {
                    return(IsEmptyLocal(context1) ? context1 : AddEmptyContext(context0));
                }
            }
            int context0size = context0.Size;
            int context1size = context1.Size;

            if (context0size == 1 && context1size == 1 && context0.GetReturnState(0) == context1.GetReturnState(0))
            {
                Antlr4.Runtime.Atn.PredictionContext merged = contextCache.Join(context0.GetParent(0), context1.GetParent(0));
                if (merged == context0.GetParent(0))
                {
                    return(context0);
                }
                else
                {
                    if (merged == context1.GetParent(0))
                    {
                        return(context1);
                    }
                    else
                    {
                        return(merged.GetChild(context0.GetReturnState(0)));
                    }
                }
            }
            int count = 0;

            Antlr4.Runtime.Atn.PredictionContext[] parentsList = new Antlr4.Runtime.Atn.PredictionContext[context0size + context1size];
            int[] returnStatesList = new int[parentsList.Length];
            int   leftIndex        = 0;
            int   rightIndex       = 0;
            bool  canReturnLeft    = true;
            bool  canReturnRight   = true;

            while (leftIndex < context0size && rightIndex < context1size)
            {
                if (context0.GetReturnState(leftIndex) == context1.GetReturnState(rightIndex))
                {
                    parentsList[count]      = contextCache.Join(context0.GetParent(leftIndex), context1.GetParent(rightIndex));
                    returnStatesList[count] = context0.GetReturnState(leftIndex);
                    canReturnLeft           = canReturnLeft && parentsList[count] == context0.GetParent(leftIndex);
                    canReturnRight          = canReturnRight && parentsList[count] == context1.GetParent(rightIndex);
                    leftIndex++;
                    rightIndex++;
                }
                else
                {
                    if (context0.GetReturnState(leftIndex) < context1.GetReturnState(rightIndex))
                    {
                        parentsList[count]      = context0.GetParent(leftIndex);
                        returnStatesList[count] = context0.GetReturnState(leftIndex);
                        canReturnRight          = false;
                        leftIndex++;
                    }
                    else
                    {
                        System.Diagnostics.Debug.Assert(context1.GetReturnState(rightIndex) < context0.GetReturnState(leftIndex));
                        parentsList[count]      = context1.GetParent(rightIndex);
                        returnStatesList[count] = context1.GetReturnState(rightIndex);
                        canReturnLeft           = false;
                        rightIndex++;
                    }
                }
                count++;
            }
            while (leftIndex < context0size)
            {
                parentsList[count]      = context0.GetParent(leftIndex);
                returnStatesList[count] = context0.GetReturnState(leftIndex);
                leftIndex++;
                canReturnRight = false;
                count++;
            }
            while (rightIndex < context1size)
            {
                parentsList[count]      = context1.GetParent(rightIndex);
                returnStatesList[count] = context1.GetReturnState(rightIndex);
                rightIndex++;
                canReturnLeft = false;
                count++;
            }
            if (canReturnLeft)
            {
                return(context0);
            }
            else
            {
                if (canReturnRight)
                {
                    return(context1);
                }
            }
            if (count < parentsList.Length)
            {
                parentsList      = Arrays.CopyOf(parentsList, count);
                returnStatesList = Arrays.CopyOf(returnStatesList, count);
            }
            if (parentsList.Length == 0)
            {
                // if one of them was EMPTY_LOCAL, it would be empty and handled at the beginning of the method
                return(EmptyFull);
            }
            else
            {
                if (parentsList.Length == 1)
                {
                    return(new SingletonPredictionContext(parentsList[0], returnStatesList[0]));
                }
                else
                {
                    return(new ArrayPredictionContext(parentsList, returnStatesList));
                }
            }
        }
 public static Antlr4.Runtime.Atn.PredictionContext Join(Antlr4.Runtime.Atn.PredictionContext context0, Antlr4.Runtime.Atn.PredictionContext context1)
 {
     return(Join(context0, context1, PredictionContextCache.Uncached));
 }
 private static Antlr4.Runtime.Atn.PredictionContext RemoveEmptyContext(Antlr4.Runtime.Atn.PredictionContext context)
 {
     return(context.RemoveEmptyContext());
 }