Ejemplo n.º 1
0
 public RuleTransition(RuleStartState ruleStart, int ruleIndex, int precedence, ATNState followState)
     : base(ruleStart)
 {
     // no Rule object at runtime
     this.ruleIndex   = ruleIndex;
     this.precedence  = precedence;
     this.followState = followState;
 }
Ejemplo n.º 2
0
        protected internal virtual void ReadRules(ATN atn)
        {
            //
            // RULES
            //
            int nrules = ReadInt();

            if (atn.grammarType == ATNType.Lexer)
            {
                atn.ruleToTokenType = new int[nrules];
            }
            atn.ruleToStartState = new RuleStartState[nrules];
            for (int i_5 = 0; i_5 < nrules; i_5++)
            {
                int            s          = ReadInt();
                RuleStartState startState = (RuleStartState)atn.states[s];
                atn.ruleToStartState[i_5] = startState;
                if (atn.grammarType == ATNType.Lexer)
                {
                    int tokenType = ReadInt();
                    if (tokenType == unchecked ((int)(0xFFFF)))
                    {
                        tokenType = TokenConstants.EOF;
                    }
                    atn.ruleToTokenType [i_5] = tokenType;
                }
            }
            atn.ruleToStopState = new RuleStopState[nrules];
            foreach (ATNState state in atn.states)
            {
                if (!(state is RuleStopState))
                {
                    continue;
                }
                RuleStopState stopState = (RuleStopState)state;
                atn.ruleToStopState[state.ruleIndex]            = stopState;
                atn.ruleToStartState[state.ruleIndex].stopState = stopState;
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        protected internal virtual ATNState StateFactory(StateType type, int ruleIndex)
        {
            ATNState s;

            switch (type)
            {
            case StateType.InvalidType:
            {
                return(null);
            }

            case StateType.Basic:
            {
                s = new BasicState();
                break;
            }

            case StateType.RuleStart:
            {
                s = new RuleStartState();
                break;
            }

            case StateType.BlockStart:
            {
                s = new BasicBlockStartState();
                break;
            }

            case StateType.PlusBlockStart:
            {
                s = new PlusBlockStartState();
                break;
            }

            case StateType.StarBlockStart:
            {
                s = new StarBlockStartState();
                break;
            }

            case StateType.TokenStart:
            {
                s = new TokensStartState();
                break;
            }

            case StateType.RuleStop:
            {
                s = new RuleStopState();
                break;
            }

            case StateType.BlockEnd:
            {
                s = new BlockEndState();
                break;
            }

            case StateType.StarLoopBack:
            {
                s = new StarLoopbackState();
                break;
            }

            case StateType.StarLoopEntry:
            {
                s = new StarLoopEntryState();
                break;
            }

            case StateType.PlusLoopBack:
            {
                s = new PlusLoopbackState();
                break;
            }

            case StateType.LoopEnd:
            {
                s = new LoopEndState();
                break;
            }

            default:
            {
                string message = string.Format(CultureInfo.CurrentCulture, "The specified state type {0} is not valid.", type);
                throw new ArgumentException(message);
            }
            }
            s.ruleIndex = ruleIndex;
            return(s);
        }
Ejemplo n.º 5
0
 public RuleTransition(RuleStartState ruleStart, int ruleIndex, ATNState followState)
     : this(ruleStart, ruleIndex, 0, followState)
 {
 }