Пример #1
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();
                    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;
            }
        }
Пример #2
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;
 }
Пример #3
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;
 }
Пример #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);
        }
Пример #5
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);
        }
Пример #6
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;
        }
Пример #7
0
 public RuleTransition(RuleStartState ruleStart, int ruleIndex, ATNState followState)
     : this(ruleStart, ruleIndex, 0, followState)
 {
 }
Пример #8
0
 public RuleTransition(RuleStartState ruleStart, int ruleIndex, ATNState followState)
     : this(ruleStart, ruleIndex, 0, followState)
 {
 }
Пример #9
0
        public static ATN Deserialize(char[] data, bool optimize)
        {
            data = (char[])data.Clone();
            // don't adjust the first value since that's the version number
            for (int i = 1; i < data.Length; i++)
            {
                data[i] = (char)(data[i] - 2);
            }
            int p       = 0;
            int version = ToInt(data[p++]);

            if (version != SerializedVersion)
            {
                string reason = string.Format(CultureInfo.CurrentCulture, "Could not deserialize ATN with version {0} (expected {1})."
                                              , version, SerializedVersion);
                throw new NotSupportedException(reason);
            }
            Guid uuid = ToUUID(data, p);

            p += 8;
            if (!uuid.Equals(SerializedUuid))
            {
                string reason = string.Format(CultureInfo.CurrentCulture, "Could not deserialize ATN with UUID {0} (expected {1})."
                                              , uuid, SerializedUuid);
                throw new NotSupportedException(reason);
            }
            ATNType grammarType  = (ATNType)ToInt(data[p++]);
            int     maxTokenType = ToInt(data[p++]);
            ATN     atn          = new ATN(grammarType, maxTokenType);
            //
            // STATES
            //
            IList <Tuple <LoopEndState, int> > loopBackStateNumbers = new List <Tuple <LoopEndState
                                                                                       , int> >();
            IList <Tuple <BlockStartState, int> > endStateNumbers = new List <Tuple <BlockStartState
                                                                                     , int> >();
            int nstates = ToInt(data[p++]);

            for (int i_1 = 0; i_1 < nstates; i_1++)
            {
                StateType stype = (StateType)ToInt(data[p++]);
                // ignore bad type of states
                if (stype == StateType.InvalidType)
                {
                    atn.AddState(null);
                    continue;
                }
                int ruleIndex = ToInt(data[p++]);
                if (ruleIndex == char.MaxValue)
                {
                    ruleIndex = -1;
                }
                ATNState s = StateFactory(stype, ruleIndex);
                if (stype == StateType.LoopEnd)
                {
                    // special case
                    int loopBackStateNumber = ToInt(data[p++]);
                    loopBackStateNumbers.Add(Tuple.Create((LoopEndState)s, loopBackStateNumber));
                }
                else
                {
                    if (s is BlockStartState)
                    {
                        int endStateNumber = ToInt(data[p++]);
                        endStateNumbers.Add(Tuple.Create((BlockStartState)s, endStateNumber));
                    }
                }
                atn.AddState(s);
            }
            // delay the assignment of loop back and end states until we know all the state instances have been initialized
            foreach (Tuple <LoopEndState, int> pair in loopBackStateNumbers)
            {
                pair.Item1.loopBackState = atn.states[pair.Item2];
            }
            foreach (Tuple <BlockStartState, int> pair_1 in endStateNumbers)
            {
                pair_1.Item1.endState = (BlockEndState)atn.states[pair_1.Item2];
            }
            int numNonGreedyStates = ToInt(data[p++]);

            for (int i_2 = 0; i_2 < numNonGreedyStates; i_2++)
            {
                int stateNumber = ToInt(data[p++]);
                ((DecisionState)atn.states[stateNumber]).nonGreedy = true;
            }
            int numSllDecisions = ToInt(data[p++]);

            for (int i_3 = 0; i_3 < numSllDecisions; i_3++)
            {
                int stateNumber = ToInt(data[p++]);
                ((DecisionState)atn.states[stateNumber]).sll = true;
            }
            int numPrecedenceStates = ToInt(data[p++]);

            for (int i_4 = 0; i_4 < numPrecedenceStates; i_4++)
            {
                int stateNumber = ToInt(data[p++]);
                ((RuleStartState)atn.states[stateNumber]).isPrecedenceRule = true;
            }
            //
            // RULES
            //
            int nrules = ToInt(data[p++]);

            if (atn.grammarType == ATNType.Lexer)
            {
                atn.ruleToTokenType   = new int[nrules];
                atn.ruleToActionIndex = new int[nrules];
            }
            atn.ruleToStartState = new RuleStartState[nrules];
            for (int i_5 = 0; i_5 < nrules; i_5++)
            {
                int            s          = ToInt(data[p++]);
                RuleStartState startState = (RuleStartState)atn.states[s];
                startState.leftFactored   = ToInt(data[p++]) != 0;
                atn.ruleToStartState[i_5] = startState;
                if (atn.grammarType == ATNType.Lexer)
                {
                    int tokenType = ToInt(data[p++]);
                    if (tokenType == unchecked ((int)(0xFFFF)))
                    {
                        tokenType = TokenConstants.Eof;
                    }
                    atn.ruleToTokenType[i_5] = tokenType;
                    int actionIndex = ToInt(data[p++]);
                    if (actionIndex == unchecked ((int)(0xFFFF)))
                    {
                        actionIndex = -1;
                    }
                    atn.ruleToActionIndex[i_5] = actionIndex;
                }
            }
            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;
            }
            //
            // MODES
            //
            int nmodes = ToInt(data[p++]);

            for (int i_6 = 0; i_6 < nmodes; i_6++)
            {
                int s = ToInt(data[p++]);
                atn.modeToStartState.Add((TokensStartState)atn.states[s]);
            }
            atn.modeToDFA = new DFA[nmodes];
            for (int i_7 = 0; i_7 < nmodes; i_7++)
            {
                atn.modeToDFA[i_7] = new DFA(atn.modeToStartState[i_7]);
            }
            //
            // SETS
            //
            IList <IntervalSet> sets = new List <IntervalSet>();
            int nsets = ToInt(data[p++]);

            for (int i_8 = 0; i_8 < nsets; i_8++)
            {
                int nintervals = ToInt(data[p]);
                p++;
                IntervalSet set = new IntervalSet();
                sets.Add(set);
                bool containsEof = ToInt(data[p++]) != 0;
                if (containsEof)
                {
                    set.Add(-1);
                }
                for (int j = 0; j < nintervals; j++)
                {
                    set.Add(ToInt(data[p]), ToInt(data[p + 1]));
                    p += 2;
                }
            }
            //
            // EDGES
            //
            int nedges = ToInt(data[p++]);

            for (int i_9 = 0; i_9 < nedges; i_9++)
            {
                int            src   = ToInt(data[p]);
                int            trg   = ToInt(data[p + 1]);
                TransitionType ttype = (TransitionType)ToInt(data[p + 2]);
                int            arg1  = ToInt(data[p + 3]);
                int            arg2  = ToInt(data[p + 4]);
                int            arg3  = ToInt(data[p + 5]);
                Transition     trans = EdgeFactory(atn, ttype, src, trg, arg1, arg2, arg3, sets);
                //			System.out.println("EDGE "+trans.getClass().getSimpleName()+" "+
                //							   src+"->"+trg+
                //					   " "+Transition.serializationNames[ttype]+
                //					   " "+arg1+","+arg2+","+arg3);
                ATNState srcState = atn.states[src];
                srcState.AddTransition(trans);
                p += 6;
            }
            // edges for rule stop states can be derived, so they aren't serialized
            foreach (ATNState state_1 in atn.states)
            {
                bool returningToLeftFactored = state_1.ruleIndex >= 0 && atn.ruleToStartState[state_1
                                                                                              .ruleIndex].leftFactored;
                for (int i_10 = 0; i_10 < state_1.NumberOfTransitions; i_10++)
                {
                    Transition t = state_1.Transition(i_10);
                    if (!(t is RuleTransition))
                    {
                        continue;
                    }
                    RuleTransition ruleTransition            = (RuleTransition)t;
                    bool           returningFromLeftFactored = atn.ruleToStartState[ruleTransition.target.ruleIndex
                                                               ].leftFactored;
                    if (!returningFromLeftFactored && returningToLeftFactored)
                    {
                        continue;
                    }
                    atn.ruleToStopState[ruleTransition.target.ruleIndex].AddTransition(new EpsilonTransition
                                                                                           (ruleTransition.followState));
                }
            }
            foreach (ATNState state_2 in atn.states)
            {
                if (state_2 is BlockStartState)
                {
                    // we need to know the end state to set its start state
                    if (((BlockStartState)state_2).endState == null)
                    {
                        throw new InvalidOperationException();
                    }
                    // block end states can only be associated to a single block start state
                    if (((BlockStartState)state_2).endState.startState != null)
                    {
                        throw new InvalidOperationException();
                    }
                    ((BlockStartState)state_2).endState.startState = (BlockStartState)state_2;
                }
                if (state_2 is PlusLoopbackState)
                {
                    PlusLoopbackState loopbackState = (PlusLoopbackState)state_2;
                    for (int i_10 = 0; i_10 < loopbackState.NumberOfTransitions; i_10++)
                    {
                        ATNState target = loopbackState.Transition(i_10).target;
                        if (target is PlusBlockStartState)
                        {
                            ((PlusBlockStartState)target).loopBackState = loopbackState;
                        }
                    }
                }
                else
                {
                    if (state_2 is StarLoopbackState)
                    {
                        StarLoopbackState loopbackState = (StarLoopbackState)state_2;
                        for (int i_10 = 0; i_10 < loopbackState.NumberOfTransitions; i_10++)
                        {
                            ATNState target = loopbackState.Transition(i_10).target;
                            if (target is StarLoopEntryState)
                            {
                                ((StarLoopEntryState)target).loopBackState = loopbackState;
                            }
                        }
                    }
                }
            }
            //
            // DECISIONS
            //
            int ndecisions = ToInt(data[p++]);

            for (int i_11 = 1; i_11 <= ndecisions; i_11++)
            {
                int           s        = ToInt(data[p++]);
                DecisionState decState = (DecisionState)atn.states[s];
                atn.decisionToState.Add(decState);
                decState.decision = i_11 - 1;
            }
            atn.decisionToDFA = new DFA[ndecisions];
            for (int i_12 = 0; i_12 < ndecisions; i_12++)
            {
                atn.decisionToDFA[i_12] = new DFA(atn.decisionToState[i_12], i_12);
            }
            if (optimize)
            {
                while (true)
                {
                    int optimizationCount = 0;
                    optimizationCount += InlineSetRules(atn);
                    optimizationCount += CombineChainedEpsilons(atn);
                    bool preserveOrder = atn.grammarType == ATNType.Lexer;
                    optimizationCount += OptimizeSets(atn, preserveOrder);
                    if (optimizationCount == 0)
                    {
                        break;
                    }
                }
            }
            IdentifyTailCalls(atn);
            VerifyATN(atn);
            return(atn);
        }
Пример #10
0
 public RuleTransition([NotNull] RuleStartState ruleStart, int ruleIndex, [NotNull] ATNState followState)
     : this(ruleStart, ruleIndex, 0, followState)
 {
 }