public virtual IntervalSet[] GetDecisionLookahead(ATNState s) { // System.out.println("LOOK("+s.stateNumber+")"); if (s == null) { return null; } IntervalSet[] look = new IntervalSet[s.NumberOfTransitions + 1]; for (int alt = 1; alt <= s.NumberOfTransitions; alt++) { look[alt] = new IntervalSet(); HashSet<ATNConfig> lookBusy = new HashSet<ATNConfig>(); bool seeThruPreds = false; // fail to get lookahead upon pred Look(s.Transition(alt - 1).target, PredictionContext.EmptyFull, look[alt], lookBusy , seeThruPreds, false); // Wipe out lookahead for this alternative if we found nothing // or we had a predicate when we !seeThruPreds if (look[alt].Size() == 0 || look[alt].Contains(HitPred)) { look[alt] = null; } } return look; }
protected ATNConfigSet ComputeStartState(ICharStream input, ATNState p) { PredictionContext initialContext = PredictionContext.EMPTY; ATNConfigSet configs = new OrderedATNConfigSet(); for (int i = 0; i < p.NumberOfTransitions; i++) { ATNState target = p.Transition(i).target; LexerATNConfig c = new LexerATNConfig(target, i + 1, initialContext); Closure(input, c, configs, false, false, false); } return(configs); }
protected internal virtual ATNConfigSet ComputeStartState(ICharStream input, ATNState p) { PredictionContext initialContext = PredictionContext.EmptyFull; ATNConfigSet configs = new OrderedATNConfigSet(); for (int i = 0; i < p.NumberOfTransitions; i++) { ATNState target = p.Transition(i).target; ATNConfig c = ATNConfig.Create(target, i + 1, initialContext); Closure(input, c, configs, false); } return(configs); }
public static PredictionContext FromRuleContext(ATN atn, RuleContext outerContext) { if (outerContext == null) { outerContext = ParserRuleContext.EMPTY; } if (outerContext.Parent == null || outerContext == ParserRuleContext.EMPTY) { return(PredictionContext.EMPTY); } PredictionContext parent = PredictionContext.FromRuleContext(atn, outerContext.Parent); ATNState state = atn.states[outerContext.invokingState]; RuleTransition transition = (RuleTransition)state.Transition(0); return(parent.GetChild(transition.followState.stateNumber)); }
/** From state s, look for any transition to a rule that is currently * being traced. When tracing r, visitedPerRuleCheck has r * initially. If you reach a rule stop state, return but notify the * invoking rule that the called rule is nullable. This implies that * invoking rule must look at follow transition for that invoking state. * * The visitedStates tracks visited states within a single rule so * we can avoid epsilon-loop-induced infinite recursion here. Keep * filling the cycles in listOfRecursiveCycles and also, as a * side-effect, set leftRecursiveRules. */ public virtual bool Check(Rule enclosingRule, ATNState s, ISet<ATNState> visitedStates) { if (s is RuleStopState) return true; if (visitedStates.Contains(s)) return false; visitedStates.Add(s); //System.out.println("visit "+s); int n = s.NumberOfTransitions; bool stateReachesStopState = false; for (int i = 0; i < n; i++) { Transition t = s.Transition(i); if (t is RuleTransition) { RuleTransition rt = (RuleTransition)t; Rule r = g.GetRule(rt.ruleIndex); if (rulesVisitedPerRuleCheck.Contains((RuleStartState)t.target)) { AddRulesToCycle(enclosingRule, r); } else { // must visit if not already visited; mark target, pop when done rulesVisitedPerRuleCheck.Add((RuleStartState)t.target); // send new visitedStates set per rule invocation bool nullable = Check(r, t.target, new HashSet<ATNState>()); // we're back from visiting that rule rulesVisitedPerRuleCheck.Remove((RuleStartState)t.target); if (nullable) { stateReachesStopState |= Check(enclosingRule, rt.followState, visitedStates); } } } else if (t.IsEpsilon) { stateReachesStopState |= Check(enclosingRule, t.target, visitedStates); } // else ignore non-epsilon transitions } return stateReachesStopState; }
public static Antlr4.Runtime.Atn.PredictionContext FromRuleContext(ATN atn, RuleContext outerContext, bool fullContext) { if (outerContext.IsEmpty()) { return(fullContext ? EmptyFull : EmptyLocal); } Antlr4.Runtime.Atn.PredictionContext parent; if (outerContext.parent != null) { parent = Antlr4.Runtime.Atn.PredictionContext.FromRuleContext(atn, outerContext.parent, fullContext); } else { parent = fullContext ? EmptyFull : EmptyLocal; } ATNState state = atn.states[outerContext.invokingState]; RuleTransition transition = (RuleTransition)state.Transition(0); return(parent.GetChild(transition.followState.stateNumber)); }
/// <summary> /// Analyze the /// <see cref="StarLoopEntryState"/> /// states in the specified ATN to set /// the /// <see cref="StarLoopEntryState.isPrecedenceDecision"/> /// field to the /// correct value. /// </summary> /// <param name="atn">The ATN.</param> protected internal virtual void MarkPrecedenceDecisions(ATN atn) { foreach (ATNState state in atn.states) { if (!(state is StarLoopEntryState)) { continue; } if (atn.ruleToStartState[state.ruleIndex].isPrecedenceRule) { ATNState maybeLoopEndState = state.Transition(state.NumberOfTransitions - 1).target; if (maybeLoopEndState is LoopEndState) { if (maybeLoopEndState.epsilonOnlyTransitions && maybeLoopEndState.Transition(0).target is RuleStopState) { ((StarLoopEntryState)state).isPrecedenceDecision = true; } } } } }
public virtual IntervalSet GetExpectedTokens(int stateNumber, RuleContext context ) { if (stateNumber < 0 || stateNumber >= states.Count) { throw new ArgumentException("Invalid state number."); } RuleContext ctx = context; ATNState s = states[stateNumber]; IntervalSet following = NextTokens(s); if (!following.Contains(TokenConstants.Epsilon)) { return(following); } IntervalSet expected = new IntervalSet(); expected.AddAll(following); expected.Remove(TokenConstants.Epsilon); while (ctx != null && ctx.invokingState >= 0 && following.Contains(TokenConstants .Epsilon)) { ATNState invokingState = states[ctx.invokingState]; RuleTransition rt = (RuleTransition)invokingState.Transition(0); following = NextTokens(rt.followState); expected.AddAll(following); expected.Remove(TokenConstants.Epsilon); ctx = ctx.parent; } if (following.Contains(TokenConstants.Epsilon)) { expected.Add(TokenConstants.Eof); } return(expected); }
public virtual IntervalSet[] GetDecisionLookahead(ATNState s) { // System.out.println("LOOK("+s.stateNumber+")"); if (s == null) { return null; } IntervalSet[] look = new IntervalSet[s.NumberOfTransitions]; for (int alt = 0; alt < s.NumberOfTransitions; alt++) { look[alt] = new IntervalSet(); HashSet<ATNConfig> lookBusy = new HashSet<ATNConfig>(); bool seeThruPreds = false; // fail to get lookahead upon pred Look_(s.Transition(alt).target, null, PredictionContext.EMPTY, look[alt], lookBusy, new BitSet(), seeThruPreds, false); // Wipe out lookahead for this alternative if we found nothing // or we had a predicate when we !seeThruPreds if (look[alt].Count == 0 || look[alt].Contains(HitPred)) { look[alt] = null; } } return look; }
/** * Since the alternatives within any lexer decision are ordered by * preference, this method stops pursuing the closure as soon as an accept * state is reached. After the first accept state is reached by depth-first * search from {@code config}, all other (potentially reachable) states for * this rule would have a lower priority. * * @return {@code true} if an accept state is reached, otherwise * {@code false}. */ protected bool Closure(ICharStream input, LexerATNConfig config, ATNConfigSet configs, bool currentAltReachedAcceptState, bool speculative, bool treatEofAsEpsilon) { if (debug) { ConsoleWriteLine("closure(" + config.ToString(recog, true) + ")"); } if (config.state is RuleStopState) { if (debug) { if (recog != null) { ConsoleWriteLine("closure at " + recog.RuleNames[config.state.ruleIndex] + " rule stop " + config); } else { ConsoleWriteLine("closure at rule stop " + config); } } if (config.context == null || config.context.HasEmptyPath) { if (config.context == null || config.context.IsEmpty) { configs.Add(config); return(true); } else { configs.Add(new LexerATNConfig(config, config.state, PredictionContext.EMPTY)); currentAltReachedAcceptState = true; } } if (config.context != null && !config.context.IsEmpty) { for (int i = 0; i < config.context.Size; i++) { if (config.context.GetReturnState(i) != PredictionContext.EMPTY_RETURN_STATE) { PredictionContext newContext = config.context.GetParent(i); // "pop" return state ATNState returnState = atn.states[config.context.GetReturnState(i)]; LexerATNConfig c = new LexerATNConfig(config, returnState, newContext); currentAltReachedAcceptState = Closure(input, c, configs, currentAltReachedAcceptState, speculative, treatEofAsEpsilon); } } } return(currentAltReachedAcceptState); } // optimization if (!config.state.OnlyHasEpsilonTransitions) { if (!currentAltReachedAcceptState || !config.hasPassedThroughNonGreedyDecision()) { configs.Add(config); } } ATNState p = config.state; for (int i = 0; i < p.NumberOfTransitions; i++) { Transition t = p.Transition(i); LexerATNConfig c = GetEpsilonTarget(input, config, t, configs, speculative, treatEofAsEpsilon); if (c != null) { currentAltReachedAcceptState = Closure(input, c, configs, currentAltReachedAcceptState, speculative, treatEofAsEpsilon); } } return(currentAltReachedAcceptState); }
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; }
/// <summary> /// Compute set of tokens that can follow /// <code>s</code> /// in the ATN in the /// specified /// <code>ctx</code> /// . /// <p/> /// If /// <code>ctx</code> /// is /// <see cref="PredictionContext.EmptyLocal">PredictionContext.EmptyLocal</see> /// and /// <code>stopState</code> /// or the end of the rule containing /// <code>s</code> /// is reached, /// <see cref="TokenConstants.Epsilon"/> /// is added to the result set. If /// <code>ctx</code> /// is not /// <see cref="PredictionContext.EmptyLocal">PredictionContext.EmptyLocal</see> /// and /// <code>addEOF</code> /// is /// <code>true</code> /// and /// <code>stopState</code> /// or the end of the outermost rule is reached, /// <see cref="TokenConstants.Eof"/> /// is added to the result set. /// </summary> /// <param name="s">the ATN state.</param> /// <param name="stopState"> /// the ATN state to stop at. This can be a /// <see cref="BlockEndState">BlockEndState</see> /// to detect epsilon paths through a closure. /// </param> /// <param name="ctx"> /// The outer context, or /// <see cref="PredictionContext.EmptyLocal">PredictionContext.EmptyLocal</see> /// if /// the outer context should not be used. /// </param> /// <param name="look">The result lookahead set.</param> /// <param name="lookBusy"> /// A set used for preventing epsilon closures in the ATN /// from causing a stack overflow. Outside code should pass /// <code>new HashSet<ATNConfig></code> /// for this argument. /// </param> /// <param name="calledRuleStack"> /// A set used for preventing left recursion in the /// ATN from causing a stack overflow. Outside code should pass /// <code>new BitSet()</code> /// for this argument. /// </param> /// <param name="seeThruPreds"> /// /// <code>true</code> /// to true semantic predicates as /// implicitly /// <code>true</code> /// and "see through them", otherwise /// <code>false</code> /// to treat semantic predicates as opaque and add /// <see cref="HitPred">HitPred</see> /// to the /// result if one is encountered. /// </param> /// <param name="addEOF"> /// Add /// <see cref="TokenConstants.Eof"/> /// to the result if the end of the /// outermost context is reached. This parameter has no effect if /// <code>ctx</code> /// is /// <see cref="PredictionContext.EmptyLocal">PredictionContext.EmptyLocal</see> /// . /// </param> protected internal virtual void Look(ATNState s, ATNState stopState, PredictionContext ctx, IntervalSet look, HashSet <ATNConfig> lookBusy, BitSet calledRuleStack, bool seeThruPreds, bool addEOF) { // System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx); ATNConfig c = ATNConfig.Create(s, 0, ctx); if (!lookBusy.Add(c)) { return; } if (s == stopState) { if (PredictionContext.IsEmptyLocal(ctx)) { look.Add(TokenConstants.Epsilon); return; } else { if (ctx.IsEmpty && addEOF) { look.Add(TokenConstants.Eof); return; } } } if (s is RuleStopState) { if (PredictionContext.IsEmptyLocal(ctx)) { look.Add(TokenConstants.Epsilon); return; } else { if (ctx.IsEmpty && addEOF) { look.Add(TokenConstants.Eof); return; } } for (int i = 0; i < ctx.Size; i++) { if (ctx.GetReturnState(i) != PredictionContext.EmptyFullStateKey) { ATNState returnState = atn.states[ctx.GetReturnState(i)]; // System.out.println("popping back to "+retState); for (int j = 0; j < ctx.Size; j++) { bool removed = calledRuleStack.Get(returnState.ruleIndex); try { calledRuleStack.Clear(returnState.ruleIndex); Look(returnState, stopState, ctx.GetParent(j), look, lookBusy, calledRuleStack, seeThruPreds , addEOF); } finally { if (removed) { calledRuleStack.Set(returnState.ruleIndex); } } } return; } } } int n = s.NumberOfTransitions; for (int i_1 = 0; i_1 < n; i_1++) { Transition t = s.Transition(i_1); if (t.GetType() == typeof(RuleTransition)) { if (calledRuleStack.Get(((RuleTransition)t).target.ruleIndex)) { continue; } PredictionContext newContext = ctx.GetChild(((RuleTransition)t).followState.stateNumber ); try { calledRuleStack.Set(((RuleTransition)t).target.ruleIndex); Look(t.target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds , addEOF); } finally { calledRuleStack.Clear(((RuleTransition)t).target.ruleIndex); } } else { if (t is AbstractPredicateTransition) { if (seeThruPreds) { Look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF ); } else { look.Add(HitPred); } } else { if (t.IsEpsilon) { Look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF ); } else { if (t.GetType() == typeof(WildcardTransition)) { look.AddAll(IntervalSet.Of(TokenConstants.MinUserTokenType, atn.maxTokenType)); } else { // System.out.println("adding "+ t); IntervalSet set = t.Label; if (set != null) { if (t is NotSetTransition) { set = set.Complement(IntervalSet.Of(TokenConstants.MinUserTokenType, atn.maxTokenType )); } look.AddAll(set); } } } } } } }
protected ATNConfigSet ComputeStartState(ATNState p, RuleContext ctx, bool fullCtx) { // always at least the implicit call to start rule PredictionContext initialContext = PredictionContext.FromRuleContext(atn, ctx); ATNConfigSet configs = new ATNConfigSet(fullCtx); for (int i = 0; i < p.NumberOfTransitions; i++) { ATNState target = p.Transition(i).target; ATNConfig c = new ATNConfig(target, i + 1, initialContext); HashSet<ATNConfig> closureBusy = new HashSet<ATNConfig>(); Closure(c, configs, closureBusy, true, fullCtx, false); } return configs; }
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; }
/// <summary> /// Compute set of tokens that can follow /// <paramref name="s"/> /// in the ATN in the /// specified /// <paramref name="ctx"/> /// . /// <p/> /// If /// <paramref name="ctx"/> /// is /// <see cref="PredictionContext.EmptyLocal"/> /// and /// <paramref name="stopState"/> /// or the end of the rule containing /// <paramref name="s"/> /// is reached, /// <see cref="TokenConstants.EPSILON"/> /// is added to the result set. If /// <paramref name="ctx"/> /// is not /// <see cref="PredictionContext.EmptyLocal"/> /// and /// <paramref name="addEOF"/> /// is /// <see langword="true"/> /// and /// <paramref name="stopState"/> /// or the end of the outermost rule is reached, /// <see cref="TokenConstants.EOF"/> /// is added to the result set. /// </summary> /// <param name="s">the ATN state.</param> /// <param name="stopState"> /// the ATN state to stop at. This can be a /// <see cref="BlockEndState"/> /// to detect epsilon paths through a closure. /// </param> /// <param name="ctx"> /// The outer context, or /// <see cref="PredictionContext.EmptyLocal"/> /// if /// the outer context should not be used. /// </param> /// <param name="look">The result lookahead set.</param> /// <param name="lookBusy"> /// A set used for preventing epsilon closures in the ATN /// from causing a stack overflow. Outside code should pass /// <c>new HashSet<ATNConfig></c> /// for this argument. /// </param> /// <param name="calledRuleStack"> /// A set used for preventing left recursion in the /// ATN from causing a stack overflow. Outside code should pass /// <c>new BitSet()</c> /// for this argument. /// </param> /// <param name="seeThruPreds"> /// /// <see langword="true"/> /// to true semantic predicates as /// implicitly /// <see langword="true"/> /// and "see through them", otherwise /// <see langword="false"/> /// to treat semantic predicates as opaque and add /// <see cref="HitPred"/> /// to the /// result if one is encountered. /// </param> /// <param name="addEOF"> /// Add /// <see cref="TokenConstants.EOF"/> /// to the result if the end of the /// outermost context is reached. This parameter has no effect if /// <paramref name="ctx"/> /// is /// <see cref="PredictionContext.EmptyLocal"/> /// . /// </param> protected internal virtual void Look(ATNState s, ATNState stopState, PredictionContext ctx, IntervalSet look, HashSet <ATNConfig> lookBusy, BitSet calledRuleStack, bool seeThruPreds, bool addEOF) { // System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx); ATNConfig c = new ATNConfig(s, 0, ctx); if (!lookBusy.Add(c)) { return; } if (s == stopState) { if (ctx == null) { look.Add(TokenConstants.EPSILON); return; } else if (ctx.IsEmpty && addEOF) { look.Add(TokenConstants.EOF); return; } } if (s is RuleStopState) { if (ctx == null) { look.Add(TokenConstants.EPSILON); return; } else if (ctx.IsEmpty && addEOF) { look.Add(TokenConstants.EOF); return; } if (ctx != PredictionContext.EMPTY) { for (int i = 0; i < ctx.Size; i++) { ATNState returnState = atn.states[ctx.GetReturnState(i)]; bool removed = calledRuleStack.Get(returnState.ruleIndex); try { calledRuleStack.Clear(returnState.ruleIndex); Look(returnState, stopState, ctx.GetParent(i), look, lookBusy, calledRuleStack, seeThruPreds, addEOF); } finally { if (removed) { calledRuleStack.Set(returnState.ruleIndex); } } } return; } } int n = s.NumberOfTransitions; for (int i_1 = 0; i_1 < n; i_1++) { Transition t = s.Transition(i_1); if (t is RuleTransition) { RuleTransition ruleTransition = (RuleTransition)t; if (calledRuleStack.Get(ruleTransition.ruleIndex)) { continue; } PredictionContext newContext = SingletonPredictionContext.Create(ctx, ruleTransition.followState.stateNumber); try { calledRuleStack.Set(ruleTransition.target.ruleIndex); Look(t.target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); } finally { calledRuleStack.Clear(ruleTransition.target.ruleIndex); } } else { if (t is AbstractPredicateTransition) { if (seeThruPreds) { Look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); } else { look.Add(HitPred); } } else { if (t.IsEpsilon) { Look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); } else { if (t is WildcardTransition) { look.AddAll(IntervalSet.Of(TokenConstants.MinUserTokenType, atn.maxTokenType)); } else { IntervalSet set = t.Label; if (set != null) { if (t is NotSetTransition) { set = set.Complement(IntervalSet.Of(TokenConstants.MinUserTokenType, atn.maxTokenType)); } look.AddAll(set); } } } } } } }
/// <summary> /// Compute set of tokens that can follow /// <code>s</code> /// in the ATN in the /// specified /// <code>ctx</code> /// . /// <p/> /// If /// <code>ctx</code> /// is /// <see cref="PredictionContext.EmptyLocal">PredictionContext.EmptyLocal</see> /// and /// <code>stopState</code> /// or the end of the rule containing /// <code>s</code> /// is reached, /// <see cref="TokenConstants.Epsilon"/> /// is added to the result set. If /// <code>ctx</code> /// is not /// <see cref="PredictionContext.EmptyLocal">PredictionContext.EmptyLocal</see> /// and /// <code>addEOF</code> /// is /// <code>true</code> /// and /// <code>stopState</code> /// or the end of the outermost rule is reached, /// <see cref="TokenConstants.Eof"/> /// is added to the result set. /// </summary> /// <param name="s">the ATN state.</param> /// <param name="stopState"> /// the ATN state to stop at. This can be a /// <see cref="BlockEndState">BlockEndState</see> /// to detect epsilon paths through a closure. /// </param> /// <param name="ctx"> /// The outer context, or /// <see cref="PredictionContext.EmptyLocal">PredictionContext.EmptyLocal</see> /// if /// the outer context should not be used. /// </param> /// <param name="look">The result lookahead set.</param> /// <param name="lookBusy"> /// A set used for preventing epsilon closures in the ATN /// from causing a stack overflow. Outside code should pass /// <code>new HashSet<ATNConfig></code> /// for this argument. /// </param> /// <param name="calledRuleStack"> /// A set used for preventing left recursion in the /// ATN from causing a stack overflow. Outside code should pass /// <code>new BitSet()</code> /// for this argument. /// </param> /// <param name="seeThruPreds"> /// /// <code>true</code> /// to true semantic predicates as /// implicitly /// <code>true</code> /// and "see through them", otherwise /// <code>false</code> /// to treat semantic predicates as opaque and add /// <see cref="HitPred">HitPred</see> /// to the /// result if one is encountered. /// </param> /// <param name="addEOF"> /// Add /// <see cref="TokenConstants.Eof"/> /// to the result if the end of the /// outermost context is reached. This parameter has no effect if /// <code>ctx</code> /// is /// <see cref="PredictionContext.EmptyLocal">PredictionContext.EmptyLocal</see> /// . /// </param> protected internal virtual void Look(ATNState s, ATNState stopState, PredictionContext ctx, IntervalSet look, HashSet<ATNConfig> lookBusy, BitSet calledRuleStack, bool seeThruPreds, bool addEOF) { // System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx); ATNConfig c = ATNConfig.Create(s, 0, ctx); if (!lookBusy.Add(c)) { return; } if (s == stopState) { if (PredictionContext.IsEmptyLocal(ctx)) { look.Add(TokenConstants.Epsilon); return; } else { if (ctx.IsEmpty && addEOF) { look.Add(TokenConstants.Eof); return; } } } if (s is RuleStopState) { if (PredictionContext.IsEmptyLocal(ctx)) { look.Add(TokenConstants.Epsilon); return; } else { if (ctx.IsEmpty && addEOF) { look.Add(TokenConstants.Eof); return; } } for (int i = 0; i < ctx.Size; i++) { if (ctx.GetReturnState(i) != PredictionContext.EmptyFullStateKey) { ATNState returnState = atn.states[ctx.GetReturnState(i)]; // System.out.println("popping back to "+retState); for (int j = 0; j < ctx.Size; j++) { bool removed = calledRuleStack.Get(returnState.ruleIndex); try { calledRuleStack.Clear(returnState.ruleIndex); Look(returnState, stopState, ctx.GetParent(j), look, lookBusy, calledRuleStack, seeThruPreds , addEOF); } finally { if (removed) { calledRuleStack.Set(returnState.ruleIndex); } } } return; } } } int n = s.NumberOfTransitions; for (int i_1 = 0; i_1 < n; i_1++) { Transition t = s.Transition(i_1); if (t.GetType() == typeof(RuleTransition)) { if (calledRuleStack.Get(((RuleTransition)t).target.ruleIndex)) { continue; } PredictionContext newContext = ctx.GetChild(((RuleTransition)t).followState.stateNumber ); try { calledRuleStack.Set(((RuleTransition)t).target.ruleIndex); Look(t.target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds , addEOF); } finally { calledRuleStack.Clear(((RuleTransition)t).target.ruleIndex); } } else { if (t is AbstractPredicateTransition) { if (seeThruPreds) { Look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF ); } else { look.Add(HitPred); } } else { if (t.IsEpsilon) { Look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF ); } else { if (t.GetType() == typeof(WildcardTransition)) { look.AddAll(IntervalSet.Of(TokenConstants.MinUserTokenType, atn.maxTokenType)); } else { // System.out.println("adding "+ t); IntervalSet set = t.Label; if (set != null) { if (t is NotSetTransition) { set = set.Complement(IntervalSet.Of(TokenConstants.MinUserTokenType, atn.maxTokenType )); } look.AddAll(set); } } } } } } }
/// <summary> /// Compute set of tokens that can follow /// <paramref name="s"/> /// in the ATN in the /// specified /// <paramref name="ctx"/> /// . /// <p/> /// If /// <paramref name="ctx"/> /// is /// <see cref="PredictionContext.EmptyLocal"/> /// and /// <paramref name="stopState"/> /// or the end of the rule containing /// <paramref name="s"/> /// is reached, /// <see cref="TokenConstants.EPSILON"/> /// is added to the result set. If /// <paramref name="ctx"/> /// is not /// <see cref="PredictionContext.EmptyLocal"/> /// and /// <paramref name="addEOF"/> /// is /// <see langword="true"/> /// and /// <paramref name="stopState"/> /// or the end of the outermost rule is reached, /// <see cref="TokenConstants.EOF"/> /// is added to the result set. /// </summary> /// <param name="s">the ATN state.</param> /// <param name="stopState"> /// the ATN state to stop at. This can be a /// <see cref="BlockEndState"/> /// to detect epsilon paths through a closure. /// </param> /// <param name="ctx"> /// The outer context, or /// <see cref="PredictionContext.EmptyLocal"/> /// if /// the outer context should not be used. /// </param> /// <param name="look">The result lookahead set.</param> /// <param name="lookBusy"> /// A set used for preventing epsilon closures in the ATN /// from causing a stack overflow. Outside code should pass /// <c>new HashSet<ATNConfig></c> /// for this argument. /// </param> /// <param name="calledRuleStack"> /// A set used for preventing left recursion in the /// ATN from causing a stack overflow. Outside code should pass /// <c>new BitSet()</c> /// for this argument. /// </param> /// <param name="seeThruPreds"> /// /// <see langword="true"/> /// to true semantic predicates as /// implicitly /// <see langword="true"/> /// and "see through them", otherwise /// <see langword="false"/> /// to treat semantic predicates as opaque and add /// <see cref="HitPred"/> /// to the /// result if one is encountered. /// </param> /// <param name="addEOF"> /// Add /// <see cref="TokenConstants.EOF"/> /// to the result if the end of the /// outermost context is reached. This parameter has no effect if /// <paramref name="ctx"/> /// is /// <see cref="PredictionContext.EmptyLocal"/> /// . /// </param> protected internal virtual void Look(ATNState s, ATNState stopState, PredictionContext ctx, IntervalSet look, HashSet<ATNConfig> lookBusy, BitSet calledRuleStack, bool seeThruPreds, bool addEOF) { // System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx); ATNConfig c = new ATNConfig(s, 0, ctx); if (!lookBusy.Add(c)) { return; } if (s == stopState) { if (ctx == null) { look.Add(TokenConstants.EPSILON); return; } else if (ctx.IsEmpty && addEOF) { look.Add(TokenConstants.EOF); return; } } if (s is RuleStopState) { if (ctx == null) { look.Add(TokenConstants.EPSILON); return; } else if (ctx.IsEmpty && addEOF) { look.Add(TokenConstants.EOF); return; } if (ctx != PredictionContext.EMPTY) { for (int i = 0; i < ctx.Size; i++) { ATNState returnState = atn.states[ctx.GetReturnState(i)]; bool removed = calledRuleStack.Get(returnState.ruleIndex); try { calledRuleStack.Clear(returnState.ruleIndex); Look(returnState, stopState, ctx.GetParent(i), look, lookBusy, calledRuleStack, seeThruPreds, addEOF); } finally { if (removed) { calledRuleStack.Set(returnState.ruleIndex); } } } return; } } int n = s.NumberOfTransitions; for (int i_1 = 0; i_1 < n; i_1++) { Transition t = s.Transition(i_1); if (t is RuleTransition) { RuleTransition ruleTransition = (RuleTransition)t; if (calledRuleStack.Get(ruleTransition.ruleIndex)) { continue; } PredictionContext newContext = SingletonPredictionContext.Create(ctx, ruleTransition.followState.stateNumber); try { calledRuleStack.Set(ruleTransition.target.ruleIndex); Look(t.target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); } finally { calledRuleStack.Clear(ruleTransition.target.ruleIndex); } } else { if (t is AbstractPredicateTransition) { if (seeThruPreds) { Look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); } else { look.Add(HitPred); } } else { if (t.IsEpsilon) { Look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); } else { if (t is WildcardTransition) { look.AddAll(IntervalSet.Of(TokenConstants.MinUserTokenType, atn.maxTokenType)); } else { IntervalSet set = t.Label; if (set != null) { if (t is NotSetTransition) { set = set.Complement(IntervalSet.Of(TokenConstants.MinUserTokenType, atn.maxTokenType)); } look.AddAll(set); } } } } } } }
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; }
protected internal virtual void GenerateRuleBypassTransitions(ATN atn) { atn.ruleToTokenType = new int[atn.ruleToStartState.Length]; for (int i_10 = 0; i_10 < atn.ruleToStartState.Length; i_10++) { atn.ruleToTokenType[i_10] = atn.maxTokenType + i_10 + 1; } for (int i_13 = 0; i_13 < atn.ruleToStartState.Length; i_13++) { BasicBlockStartState bypassStart = new BasicBlockStartState(); bypassStart.ruleIndex = i_13; atn.AddState(bypassStart); BlockEndState bypassStop = new BlockEndState(); bypassStop.ruleIndex = i_13; atn.AddState(bypassStop); bypassStart.endState = bypassStop; atn.DefineDecisionState(bypassStart); bypassStop.startState = bypassStart; ATNState endState; Transition excludeTransition = null; if (atn.ruleToStartState[i_13].isPrecedenceRule) { // wrap from the beginning of the rule to the StarLoopEntryState endState = null; foreach (ATNState state_3 in atn.states) { if (state_3.ruleIndex != i_13) { continue; } if (!(state_3 is StarLoopEntryState)) { continue; } ATNState maybeLoopEndState = state_3.Transition(state_3.NumberOfTransitions - 1).target; if (!(maybeLoopEndState is LoopEndState)) { continue; } if (maybeLoopEndState.epsilonOnlyTransitions && maybeLoopEndState.Transition(0).target is RuleStopState) { endState = state_3; break; } } if (endState == null) { throw new NotSupportedException("Couldn't identify final state of the precedence rule prefix section."); } excludeTransition = ((StarLoopEntryState)endState).loopBackState.Transition(0); } else { endState = atn.ruleToStopState[i_13]; } // all non-excluded transitions that currently target end state need to target blockEnd instead foreach (ATNState state_4 in atn.states) { foreach (Transition transition in state_4.transitions) { if (transition == excludeTransition) { continue; } if (transition.target == endState) { transition.target = bypassStop; } } } // all transitions leaving the rule start state need to leave blockStart instead while (atn.ruleToStartState[i_13].NumberOfTransitions > 0) { Transition transition = atn.ruleToStartState[i_13].Transition(atn.ruleToStartState[i_13].NumberOfTransitions - 1); atn.ruleToStartState[i_13].RemoveTransition(atn.ruleToStartState[i_13].NumberOfTransitions - 1); bypassStart.AddTransition(transition); } // link the new states atn.ruleToStartState[i_13].AddTransition(new EpsilonTransition(bypassStart)); bypassStop.AddTransition(new EpsilonTransition(endState)); ATNState matchState = new BasicState(); atn.AddState(matchState); matchState.AddTransition(new AtomTransition(bypassStop, atn.ruleToTokenType[i_13])); bypassStart.AddTransition(new EpsilonTransition(matchState)); } if (deserializationOptions.VerifyAtn) { // reverify after modification VerifyATN(atn); } }
protected internal virtual ATNConfigSet ComputeStartState(ICharStream input, ATNState p) { PredictionContext initialContext = PredictionContext.EmptyFull; ATNConfigSet configs = new OrderedATNConfigSet(); for (int i = 0; i < p.NumberOfTransitions; i++) { ATNState target = p.Transition(i).target; ATNConfig c = ATNConfig.Create(target, i + 1, initialContext); Closure(input, c, configs, false); } return configs; }
protected ATNConfigSet ComputeStartState(ICharStream input, ATNState p) { PredictionContext initialContext = PredictionContext.EMPTY; ATNConfigSet configs = new OrderedATNConfigSet(); for (int i = 0; i < p.NumberOfTransitions; i++) { ATNState target = p.Transition(i).target; LexerATNConfig c = new LexerATNConfig(target, i + 1, initialContext); Closure(input, c, configs, false, false, false); } return configs; }