Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
0
        protected internal virtual void VisitState(ATNState p)
        {
            int edge;

            if (p.NumberOfTransitions > 1)
            {
                ErrorHandler.Sync(this);
                edge = Interpreter.AdaptivePredict(_input, ((DecisionState)p).decision, _ctx);
            }
            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, _ctx.RuleIndex);
                    PushNewRecursionContext(ctx, atn.ruleToStartState[p.ruleIndex].stateNumber, _ctx.RuleIndex);
                }
                break;
            }

            case TransitionType.Atom:
            {
                Match(((AtomTransition)transition).label);
                break;
            }

            case TransitionType.Range:
            case TransitionType.Set:
            case TransitionType.NotSet:
            {
                if (!transition.Matches(_input.La(1), TokenConstants.MinUserTokenType, 65535))
                {
                    _errHandler.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(_ctx, 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(_ctx, predicateTransition.ruleIndex, predicateTransition.predIndex))
                {
                    throw new FailedPredicateException(this);
                }
                break;
            }

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

            case TransitionType.Precedence:
            {
                if (!Precpred(_ctx, ((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;
        }
Ejemplo n.º 3
0
 private bool CheckPredicate(PredicateTransition transition)
 {
     return(transition.Predicate.Eval(_parser, ParserRuleContext.EmptyContext));
 }
 /// <summary>
 /// Check if the predicate associated with the given transition evaluates to true.
 /// </summary>
 private bool CheckPredicate(PredicateTransition transition) => transition.Predicate.Eval(this.parser, ParserRuleContext.EmptyContext);
Ejemplo n.º 5
0
        protected internal virtual void VisitState(ATNState p)
        {
            int predictedAlt = 1;

            if (p.NumberOfTransitions > 1)
            {
                predictedAlt = VisitDecisionState((DecisionState)p);
            }
            Transition transition = p.Transition(predictedAlt - 1);

            switch (transition.TransitionType)
            {
            case TransitionType.Epsilon:
            {
                if (pushRecursionContextStates.Get(p.stateNumber) && !(transition.target is LoopEndState))
                {
                    // We are at the start of a left recursive rule's (...)* loop
                    // and we're not taking the exit branch of loop.
                    InterpreterRuleContext localctx = CreateInterpreterRuleContext(_parentContextStack.Peek().Item1, _parentContextStack.Peek().Item2, _ctx.RuleIndex);
                    PushNewRecursionContext(localctx, atn.ruleToStartState[p.ruleIndex].stateNumber, _ctx.RuleIndex);
                }
                break;
            }

            case TransitionType.Atom:
            {
                Match(((AtomTransition)transition).label);
                break;
            }

            case TransitionType.Range:
            case TransitionType.Set:
            case TransitionType.NotSet:
            {
                if (!transition.Matches(_input.La(1), TokenConstants.MinUserTokenType, 65535))
                {
                    RecoverInline();
                }
                MatchWildcard();
                break;
            }

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

            case TransitionType.Rule:
            {
                RuleStartState         ruleStartState = (RuleStartState)transition.target;
                int                    ruleIndex      = ruleStartState.ruleIndex;
                InterpreterRuleContext newctx         = CreateInterpreterRuleContext(_ctx, p.stateNumber, ruleIndex);
                if (ruleStartState.isPrecedenceRule)
                {
                    EnterRecursionRule(newctx, ruleStartState.stateNumber, ruleIndex, ((RuleTransition)transition).precedence);
                }
                else
                {
                    EnterRule(newctx, transition.target.stateNumber, ruleIndex);
                }
                break;
            }

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

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

            case TransitionType.Precedence:
            {
                if (!Precpred(_ctx, ((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;
        }