This class extends ParserRuleContext by allowing the value of RuleIndex() to be explicitly set for the context.

ParserRuleContext does not include field storage for the rule index since the context classes created by the code generator override the RuleIndex() method to return the correct value for that context. Since the parser interpreter does not use the context classes generated for a parser, this class (with slightly more memory overhead per node) is used to provide equivalent functionality.

Inheritance: ParserRuleContext
Ejemplo n.º 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;
                            Sharpen.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;
                }
                }
            }
        }
Ejemplo n.º 2
0
        public static Antlr4.Runtime.InterpreterRuleContext FromParserRuleContext(ParserRuleContext ctx)
        {
            if (ctx == null)
            {
                return(null);
            }
            Antlr4.Runtime.InterpreterRuleContext dup = new Antlr4.Runtime.InterpreterRuleContext(ctx.RuleIndex);

            dup.CopyFrom(ctx);
            dup.parent = FromParserRuleContext(((ParserRuleContext)ctx.Parent));
            return(dup);
        }
Ejemplo n.º 3
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.NotSet:
            {
                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.º 4
0
 public static Antlr4.Runtime.InterpreterRuleContext FromParserRuleContext(ParserRuleContext ctx)
 {
     if (ctx == null)
     {
         return null;
     }
     Antlr4.Runtime.InterpreterRuleContext dup = new Antlr4.Runtime.InterpreterRuleContext(ctx.RuleIndex);
     dup.CopyFrom(ctx);
     dup.parent = FromParserRuleContext(((ParserRuleContext)ctx.Parent));
     return dup;
 }
Ejemplo n.º 5
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.º 6
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 (_ctx.IsEmpty)
                        {
                            if (startRuleStartState.isPrecedenceRule)
                            {
                                ParserRuleContext result = _ctx;
                                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;
                    }
                }
            }
        }
Ejemplo n.º 7
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;
        }
Ejemplo n.º 8
0
 public override void Reset()
 {
     base.Reset();
     overrideDecisionReached = false;
     overrideDecisionRoot    = null;
 }
Ejemplo n.º 9
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.º 10
0
 public override void Reset()
 {
     base.Reset();
     overrideDecisionReached = false;
     overrideDecisionRoot = null;
 }