Пример #1
0
        /*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));
                }
            }
        }