Exemplo n.º 1
0
        /** identify the ATN states where we need to set the outer alt number.
         *  For regular rules, that's the block at the target to rule start state.
         *  For left-recursive rules, we track the primary block, which looks just
         *  like a regular rule's outer block, and the star loop block (always
         *  there even if 1 alt).
         */
        public virtual BitSet FindOuterMostDecisionStates()
        {
            BitSet track             = new BitSet(atn.states.Count);
            int    numberOfDecisions = atn.NumberOfDecisions;

            for (int i = 0; i < numberOfDecisions; i++)
            {
                DecisionState  decisionState = atn.GetDecisionState(i);
                RuleStartState startState    = atn.ruleToStartState[decisionState.ruleIndex];
                // Look for StarLoopEntryState that is in any left recursive rule
                if (decisionState is StarLoopEntryState)
                {
                    StarLoopEntryState loopEntry = (StarLoopEntryState)decisionState;
                    if (loopEntry.precedenceRuleDecision)
                    {
                        // Recursive alts always result in a (...)* in the transformed
                        // left recursive rule and that always has a BasicBlockStartState
                        // even if just 1 recursive alt exists.
                        ATNState blockStart = loopEntry.Transition(0).target;
                        // track the StarBlockStartState associated with the recursive alternatives
                        track.Set(blockStart.stateNumber);
                    }
                }
                else if (startState.Transition(0).target == decisionState)
                {
                    // always track outermost block for any rule if it exists
                    track.Set(decisionState.stateNumber);
                }
            }
            return(track);
        }
Exemplo n.º 2
0
 /** identify the ATN states where we need to set the outer alt number.
  *  For regular rules, that's the block at the target to rule start state.
  *  For left-recursive rules, we track the primary block, which looks just
  *  like a regular rule's outer block, and the star loop block (always
  *  there even if 1 alt).
  */
 public virtual BitSet FindOuterMostDecisionStates()
 {
     BitSet track = new BitSet(atn.states.Count);
     int numberOfDecisions = atn.NumberOfDecisions;
     for (int i = 0; i < numberOfDecisions; i++)
     {
         DecisionState decisionState = atn.GetDecisionState(i);
         RuleStartState startState = atn.ruleToStartState[decisionState.ruleIndex];
         // Look for StarLoopEntryState that is in any left recursive rule
         if (decisionState is StarLoopEntryState)
         {
             StarLoopEntryState loopEntry = (StarLoopEntryState)decisionState;
             if (loopEntry.precedenceRuleDecision)
             {
                 // Recursive alts always result in a (...)* in the transformed
                 // left recursive rule and that always has a BasicBlockStartState
                 // even if just 1 recursive alt exists.
                 ATNState blockStart = loopEntry.Transition(0).target;
                 // track the StarBlockStartState associated with the recursive alternatives
                 track.Set(blockStart.stateNumber);
             }
         }
         else if (startState.Transition(0).target == decisionState)
         {
             // always track outermost block for any rule if it exists
             track.Set(decisionState.stateNumber);
         }
     }
     return track;
 }