GetOptimizedTransition() public method

public GetOptimizedTransition ( int i ) : Antlr4.Runtime.Atn.Transition
i int
return Antlr4.Runtime.Atn.Transition
Exemplo n.º 1
0
        /// <summary>
        /// Since the alternatives within any lexer decision are ordered by
        /// preference, this method stops pursuing the closure as soon as an accept
        /// state is reached.
        /// </summary>
        /// <remarks>
        /// Since the alternatives within any lexer decision are ordered by
        /// preference, this method stops pursuing the closure as soon as an accept
        /// state is reached. After the first accept state is reached by depth-first
        /// search from
        /// <code>config</code>
        /// , all other (potentially reachable) states for
        /// this rule would have a lower priority.
        /// </remarks>
        /// <returns>
        ///
        /// <code>true</code>
        /// if an accept state is reached, otherwise
        /// <code>false</code>
        /// .
        /// </returns>
        protected internal virtual bool Closure(ICharStream input, ATNConfig config, ATNConfigSet
                                                configs, bool speculative)
        {
            if (config.State is RuleStopState)
            {
                PredictionContext context = config.Context;
                if (context.IsEmpty)
                {
                    configs.AddItem(config);
                    return(true);
                }
                else
                {
                    if (context.HasEmpty)
                    {
                        configs.AddItem(config.Transform(config.State, PredictionContext.EmptyFull));
                        return(true);
                    }
                }
                for (int i = 0; i < context.Size; i++)
                {
                    int returnStateNumber = context.GetReturnState(i);
                    if (returnStateNumber == PredictionContext.EmptyFullStateKey)
                    {
                        continue;
                    }
                    PredictionContext newContext = context.GetParent(i);
                    // "pop" return state
                    ATNState  returnState = atn.states[returnStateNumber];
                    ATNConfig c           = ATNConfig.Create(returnState, config.Alt, newContext);
                    if (Closure(input, c, configs, speculative))
                    {
                        return(true);
                    }
                }
                return(false);
            }
            // optimization
            if (!config.State.OnlyHasEpsilonTransitions)
            {
                configs.AddItem(config);
            }
            ATNState p = config.State;

            for (int i_1 = 0; i_1 < p.NumberOfOptimizedTransitions; i_1++)
            {
                Transition t = p.GetOptimizedTransition(i_1);
                ATNConfig  c = GetEpsilonTarget(input, config, t, configs, speculative);
                if (c != null)
                {
                    if (Closure(input, c, configs, speculative))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Since the alternatives within any lexer decision are ordered by
        /// preference, this method stops pursuing the closure as soon as an accept
        /// state is reached.
        /// </summary>
        /// <remarks>
        /// Since the alternatives within any lexer decision are ordered by
        /// preference, this method stops pursuing the closure as soon as an accept
        /// state is reached. After the first accept state is reached by depth-first
        /// search from
        /// <paramref name="config"/>
        /// , all other (potentially reachable) states for
        /// this rule would have a lower priority.
        /// </remarks>
        /// <returns>
        ///
        /// <see langword="true"/>
        /// if an accept state is reached, otherwise
        /// <see langword="false"/>
        /// .
        /// </returns>
        protected internal virtual bool Closure(ICharStream input, ATNConfig config, ATNConfigSet configs, bool currentAltReachedAcceptState, bool speculative, bool treatEofAsEpsilon)
        {
            if (config.State is RuleStopState)
            {
                PredictionContext context = config.Context;
                if (context.IsEmpty)
                {
                    configs.Add(config);
                    return(true);
                }
                else
                {
                    if (context.HasEmpty)
                    {
                        configs.Add(config.Transform(config.State, PredictionContext.EmptyFull, true));
                        currentAltReachedAcceptState = true;
                    }
                }
                for (int i = 0; i < context.Size; i++)
                {
                    int returnStateNumber = context.GetReturnState(i);
                    if (returnStateNumber == PredictionContext.EmptyFullStateKey)
                    {
                        continue;
                    }
                    PredictionContext newContext = context.GetParent(i);
                    // "pop" return state
                    ATNState  returnState = atn.states[returnStateNumber];
                    ATNConfig c           = config.Transform(returnState, newContext, false);
                    currentAltReachedAcceptState = Closure(input, c, configs, currentAltReachedAcceptState, speculative, treatEofAsEpsilon);
                }
                return(currentAltReachedAcceptState);
            }
            // optimization
            if (!config.State.OnlyHasEpsilonTransitions)
            {
                if (!currentAltReachedAcceptState || !config.PassedThroughNonGreedyDecision)
                {
                    configs.Add(config);
                }
            }
            ATNState p = config.State;

            for (int i_1 = 0; i_1 < p.NumberOfOptimizedTransitions; i_1++)
            {
                Transition t = p.GetOptimizedTransition(i_1);
                ATNConfig  c = GetEpsilonTarget(input, config, t, configs, speculative, treatEofAsEpsilon);
                if (c != null)
                {
                    currentAltReachedAcceptState = Closure(input, c, configs, currentAltReachedAcceptState, speculative, treatEofAsEpsilon);
                }
            }
            return(currentAltReachedAcceptState);
        }
Exemplo n.º 3
0
        private static int CombineChainedEpsilons(ATN atn)
        {
            int removedEdges = 0;

            foreach (ATNState state in atn.states)
            {
                if (!state.OnlyHasEpsilonTransitions || state is RuleStopState)
                {
                    continue;
                }
                IList <Transition> optimizedTransitions = null;
                for (int i = 0; i < state.NumberOfOptimizedTransitions; i++)
                {
                    Transition transition   = state.GetOptimizedTransition(i);
                    ATNState   intermediate = transition.target;
                    if (transition.TransitionType != TransitionType.EPSILON || ((EpsilonTransition)transition).OutermostPrecedenceReturn != -1 || intermediate.StateType != StateType.Basic || !intermediate.OnlyHasEpsilonTransitions)
                    {
                        if (optimizedTransitions != null)
                        {
                            optimizedTransitions.Add(transition);
                        }
                        goto nextTransition_continue;
                    }
                    for (int j = 0; j < intermediate.NumberOfOptimizedTransitions; j++)
                    {
                        if (intermediate.GetOptimizedTransition(j).TransitionType != TransitionType.EPSILON || ((EpsilonTransition)intermediate.GetOptimizedTransition(j)).OutermostPrecedenceReturn != -1)
                        {
                            if (optimizedTransitions != null)
                            {
                                optimizedTransitions.Add(transition);
                            }
                            goto nextTransition_continue;
                        }
                    }
                    removedEdges++;
                    if (optimizedTransitions == null)
                    {
                        optimizedTransitions = new List <Transition>();
                        for (int j_1 = 0; j_1 < i; j_1++)
                        {
                            optimizedTransitions.Add(state.GetOptimizedTransition(j_1));
                        }
                    }
                    for (int j_2 = 0; j_2 < intermediate.NumberOfOptimizedTransitions; j_2++)
                    {
                        ATNState target = intermediate.GetOptimizedTransition(j_2).target;
                        optimizedTransitions.Add(new EpsilonTransition(target));
                    }
                    nextTransition_continue :;
                }

                if (optimizedTransitions != null)
                {
                    if (state.IsOptimized)
                    {
                        while (state.NumberOfOptimizedTransitions > 0)
                        {
                            state.RemoveOptimizedTransition(state.NumberOfOptimizedTransitions - 1);
                        }
                    }
                    foreach (Transition transition in optimizedTransitions)
                    {
                        state.AddOptimizedTransition(transition);
                    }
                }
            }

            return(removedEdges);
        }
Exemplo n.º 4
0
        private static int InlineSetRules(ATN atn)
        {
            int inlinedCalls = 0;

            Transition[] ruleToInlineTransition = new Transition[atn.ruleToStartState.Length];
            for (int i = 0; i < atn.ruleToStartState.Length; i++)
            {
                RuleStartState startState  = atn.ruleToStartState[i];
                ATNState       middleState = startState;
                while (middleState.OnlyHasEpsilonTransitions && middleState.NumberOfOptimizedTransitions == 1 && middleState.GetOptimizedTransition(0).TransitionType == TransitionType.EPSILON)
                {
                    middleState = middleState.GetOptimizedTransition(0).target;
                }
                if (middleState.NumberOfOptimizedTransitions != 1)
                {
                    continue;
                }
                Transition matchTransition = middleState.GetOptimizedTransition(0);
                ATNState   matchTarget     = matchTransition.target;
                if (matchTransition.IsEpsilon || !matchTarget.OnlyHasEpsilonTransitions || matchTarget.NumberOfOptimizedTransitions != 1 || !(matchTarget.GetOptimizedTransition(0).target is RuleStopState))
                {
                    continue;
                }
                switch (matchTransition.TransitionType)
                {
                case TransitionType.ATOM:
                case TransitionType.RANGE:
                case TransitionType.SET:
                {
                    ruleToInlineTransition[i] = matchTransition;
                    break;
                }

                case TransitionType.NOT_SET:
                case TransitionType.WILDCARD:
                {
                    // not implemented yet
                    continue;
                }

                default:
                {
                    continue;
                }
                }
            }
            for (int stateNumber = 0; stateNumber < atn.states.Count; stateNumber++)
            {
                ATNState state = atn.states[stateNumber];
                if (state.ruleIndex < 0)
                {
                    continue;
                }
                IList <Transition> optimizedTransitions = null;
                for (int i_1 = 0; i_1 < state.NumberOfOptimizedTransitions; i_1++)
                {
                    Transition transition = state.GetOptimizedTransition(i_1);
                    if (!(transition is RuleTransition))
                    {
                        if (optimizedTransitions != null)
                        {
                            optimizedTransitions.Add(transition);
                        }
                        continue;
                    }
                    RuleTransition ruleTransition = (RuleTransition)transition;
                    Transition     effective      = ruleToInlineTransition[ruleTransition.target.ruleIndex];
                    if (effective == null)
                    {
                        if (optimizedTransitions != null)
                        {
                            optimizedTransitions.Add(transition);
                        }
                        continue;
                    }
                    if (optimizedTransitions == null)
                    {
                        optimizedTransitions = new List <Transition>();
                        for (int j = 0; j < i_1; j++)
                        {
                            optimizedTransitions.Add(state.GetOptimizedTransition(i_1));
                        }
                    }
                    inlinedCalls++;
                    ATNState target            = ruleTransition.followState;
                    ATNState intermediateState = new BasicState();
                    intermediateState.SetRuleIndex(target.ruleIndex);
                    atn.AddState(intermediateState);
                    optimizedTransitions.Add(new EpsilonTransition(intermediateState));
                    switch (effective.TransitionType)
                    {
                    case TransitionType.ATOM:
                    {
                        intermediateState.AddTransition(new AtomTransition(target, ((AtomTransition)effective).token));
                        break;
                    }

                    case TransitionType.RANGE:
                    {
                        intermediateState.AddTransition(new RangeTransition(target, ((RangeTransition)effective).from, ((RangeTransition)effective).to));
                        break;
                    }

                    case TransitionType.SET:
                    {
                        intermediateState.AddTransition(new SetTransition(target, effective.Label));
                        break;
                    }

                    default:
                    {
                        throw new NotSupportedException();
                    }
                    }
                }
                if (optimizedTransitions != null)
                {
                    if (state.IsOptimized)
                    {
                        while (state.NumberOfOptimizedTransitions > 0)
                        {
                            state.RemoveOptimizedTransition(state.NumberOfOptimizedTransitions - 1);
                        }
                    }
                    foreach (Transition transition in optimizedTransitions)
                    {
                        state.AddOptimizedTransition(transition);
                    }
                }
            }
            return(inlinedCalls);
        }