Exemple #1
0
        /// <summary>Begin parsing at startRuleIndex</summary>
        public virtual ParserRuleContext Parse(int startRuleIndex)
        {
            RuleStartState         startRuleStartState = _atn.ruleToStartState[startRuleIndex];
            InterpreterRuleContext rootContext         = new InterpreterRuleContext(null, ATNState.InvalidStateNumber, startRuleIndex);

            if (startRuleStartState.isPrecedenceRule)
            {
                EnterRecursionRule(rootContext, startRuleStartState.stateNumber, startRuleIndex, 0);
            }
            else
            {
                EnterRule(rootContext, startRuleStartState.stateNumber, startRuleIndex);
            }
            while (true)
            {
                ATNState p = AtnState;
                switch (p.StateType)
                {
                case StateType.RuleStop:
                {
                    // pop; return from rule
                    if (RuleContext.IsEmpty)
                    {
                        if (startRuleStartState.isPrecedenceRule)
                        {
                            ParserRuleContext result = RuleContext;
                            Tuple <ParserRuleContext, int> parentContext = _parentContextStack.Pop();
                            UnrollRecursionContexts(parentContext.Item1);
                            return(result);
                        }
                        else
                        {
                            ExitRule();
                            return(rootContext);
                        }
                    }
                    VisitRuleStopState(p);
                    break;
                }

                default:
                {
                    try
                    {
                        VisitState(p);
                    }
                    catch (RecognitionException e)
                    {
                        State             = _atn.ruleToStopState[p.ruleIndex].stateNumber;
                        Context.exception = e;
                        ErrorHandler.ReportError(this, e);
                        ErrorHandler.Recover(this, e);
                    }
                    break;
                }
                }
            }
        }
Exemple #2
0
        protected internal virtual void VisitState(ATNState p)
        {
            int edge;

            if (p.NumberOfTransitions > 1)
            {
                ErrorHandler.Sync(this);
                edge = Interpreter.AdaptivePredict(TokenStream, ((DecisionState)p).decision, RuleContext);
            }
            else
            {
                edge = 1;
            }
            Transition transition = p.Transition(edge - 1);

            switch (transition.TransitionType)
            {
            case TransitionType.EPSILON:
            {
                if (pushRecursionContextStates.Get(p.stateNumber) && !(transition.target is LoopEndState))
                {
                    InterpreterRuleContext ctx = new InterpreterRuleContext(_parentContextStack.Peek().Item1, _parentContextStack.Peek().Item2, RuleContext.RuleIndex);
                    PushNewRecursionContext(ctx, _atn.ruleToStartState[p.ruleIndex].stateNumber, RuleContext.RuleIndex);
                }
                break;
            }

            case TransitionType.ATOM:
            {
                Match(((AtomTransition)transition).token);
                break;
            }

            case TransitionType.RANGE:
            case TransitionType.SET:
            case TransitionType.NOT_SET:
            {
                if (!transition.Matches(TokenStream.LA(1), TokenConstants.MinUserTokenType, 65535))
                {
                    ErrorHandler.RecoverInline(this);
                }
                MatchWildcard();
                break;
            }

            case TransitionType.WILDCARD:
            {
                MatchWildcard();
                break;
            }

            case TransitionType.RULE:
            {
                RuleStartState         ruleStartState = (RuleStartState)transition.target;
                int                    ruleIndex      = ruleStartState.ruleIndex;
                InterpreterRuleContext ctx_1          = new InterpreterRuleContext(RuleContext, p.stateNumber, ruleIndex);
                if (ruleStartState.isPrecedenceRule)
                {
                    EnterRecursionRule(ctx_1, ruleStartState.stateNumber, ruleIndex, ((RuleTransition)transition).precedence);
                }
                else
                {
                    EnterRule(ctx_1, transition.target.stateNumber, ruleIndex);
                }
                break;
            }

            case TransitionType.PREDICATE:
            {
                PredicateTransition predicateTransition = (PredicateTransition)transition;
                if (!Sempred(RuleContext, predicateTransition.ruleIndex, predicateTransition.predIndex))
                {
                    throw new FailedPredicateException(this);
                }
                break;
            }

            case TransitionType.ACTION:
            {
                ActionTransition actionTransition = (ActionTransition)transition;
                Action(RuleContext, actionTransition.ruleIndex, actionTransition.actionIndex);
                break;
            }

            case TransitionType.PRECEDENCE:
            {
                if (!Precpred(RuleContext, ((PrecedencePredicateTransition)transition).precedence))
                {
                    throw new FailedPredicateException(this, string.Format("precpred(_ctx, {0})", ((PrecedencePredicateTransition)transition).precedence));
                }
                break;
            }

            default:
            {
                throw new NotSupportedException("Unrecognized ATN transition type.");
            }
            }
            State = transition.target.stateNumber;
        }