public static Antlr4.Runtime.Atn.ATNConfig Create(ATNState state, int alt, PredictionContext context, Antlr4.Runtime.Atn.SemanticContext semanticContext, LexerActionExecutor lexerActionExecutor) { if (semanticContext != Antlr4.Runtime.Atn.SemanticContext.None) { if (lexerActionExecutor != null) { return new ATNConfig.ActionSemanticContextATNConfig(lexerActionExecutor, semanticContext, state, alt, context, false); } else { return new ATNConfig.SemanticContextATNConfig(semanticContext, state, alt, context); } } else { if (lexerActionExecutor != null) { return new ATNConfig.ActionATNConfig(lexerActionExecutor, state, alt, context, false); } else { return new Antlr4.Runtime.Atn.ATNConfig(state, alt, context); } } }
protected internal ATNConfig(ATNState state, int alt, PredictionContext context) { System.Diagnostics.Debug.Assert((alt & unchecked((int)(0xFFFFFF))) == alt); this.state = state; this.altAndOuterContextDepth = alt & unchecked((int)(0x7FFFFFFF)); this.context = context; }
public GrammarAST(GrammarAST node) : base(node) { this.g = node.g; this.atnState = node.atnState; this.textOverride = node.textOverride; }
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.EmptyFull, 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].Size() == 0 || look[alt].Contains(HitPred)) { look[alt] = null; } } return look; }
public DFA(ATNState atnStartState, int decision) { this.atnStartState = atnStartState; this.decision = decision; if (this.atnStartState.atn.grammarType == ATNType.Lexer) { minDfaEdge = LexerATNSimulator.MinDfaEdge; maxDfaEdge = LexerATNSimulator.MaxDfaEdge; } else { minDfaEdge = TokenConstants.Eof; maxDfaEdge = atnStartState.atn.maxTokenType; } this.emptyEdgeMap = new Antlr4.Runtime.Dfa.EmptyEdgeMap<DFAState>(minDfaEdge, maxDfaEdge); this.emptyContextEdgeMap = new Antlr4.Runtime.Dfa.EmptyEdgeMap<DFAState>(-1, atnStartState.atn.states.Count - 1); bool isPrecedenceDfa = false; if (atnStartState is StarLoopEntryState) { if (((StarLoopEntryState)atnStartState).precedenceRuleDecision) { isPrecedenceDfa = true; this.s0.Set(new DFAState(emptyPrecedenceEdges, EmptyContextEdgeMap, new ATNConfigSet())); this.s0full.Set(new DFAState(emptyPrecedenceEdges, EmptyContextEdgeMap, new ATNConfigSet())); } } this.precedenceDfa = isPrecedenceDfa; }
public ActionTransition(ATNState target, int ruleIndex, int actionIndex, bool isCtxDependent) : base(target) { // e.g., $i ref in action this.ruleIndex = ruleIndex; this.actionIndex = actionIndex; this.isCtxDependent = isCtxDependent; }
protected internal ATNConfig(Antlr4.Runtime.Atn.ATNConfig c, ATNState state, PredictionContext context) { this.state = state; this.altAndOuterContextDepth = c.altAndOuterContextDepth & unchecked((int)(0x7FFFFFFF )); this.context = context; }
public PredicateTransition(ATNState target, int ruleIndex, int predIndex, bool isCtxDependent) : base(target) { // e.g., $i ref in pred this.ruleIndex = ruleIndex; this.predIndex = predIndex; this.isCtxDependent = isCtxDependent; }
public RuleTransition(RuleStartState ruleStart, int ruleIndex, int precedence, ATNState followState) : base(ruleStart) { // no Rule object at runtime this.ruleIndex = ruleIndex; this.precedence = precedence; this.followState = followState; }
protected internal Transition(ATNState target) { if (target == null) { throw new ArgumentNullException("target cannot be null."); } this.target = target; }
public SetTransition(ATNState target, IntervalSet set) : base(target) { // TODO (sam): should we really allow null here? if (set == null) { set = IntervalSet.Of(TokenConstants.InvalidType); } this.set = set; }
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)); }
/** Given a starting configuration set, figure out all ATN configurations * we can reach upon input {@code t}. Parameter {@code reach} is a return * parameter. */ protected void GetReachableConfigSet(ICharStream input, ATNConfigSet closure, ATNConfigSet reach, int t) { // this is used to skip processing for configs which have a lower priority // than a config that already reached an accept state for the same rule int skipAlt = ATN.INVALID_ALT_NUMBER; foreach (ATNConfig c in closure.configs) { bool currentAltReachedAcceptState = c.alt == skipAlt; if (currentAltReachedAcceptState && ((LexerATNConfig)c).hasPassedThroughNonGreedyDecision()) { continue; } if (debug) { ConsoleWriteLine("testing " + GetTokenName(t) + " at " + c.ToString(recog, true)); } int n = c.state.NumberOfTransitions; for (int ti = 0; ti < n; ti++) { // for each transition Transition trans = c.state.Transition(ti); ATNState target = GetReachableTarget(trans, t); if (target != null) { LexerActionExecutor lexerActionExecutor = ((LexerATNConfig)c).getLexerActionExecutor(); if (lexerActionExecutor != null) { lexerActionExecutor = lexerActionExecutor.FixOffsetBeforeMatch(input.Index - startIndex); } bool treatEofAsEpsilon = t == IntStreamConstants.EOF; if (Closure(input, new LexerATNConfig((LexerATNConfig)c, target, lexerActionExecutor), reach, currentAltReachedAcceptState, true, treatEofAsEpsilon)) { // any remaining configs for this alt have a lower priority than // the one that just reached an accept state. skipAlt = c.alt; break; } } } } }
/** 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; }
private static bool TestTailCall(ATN atn, RuleTransition transition, bool optimizedPath ) { if (!optimizedPath && transition.tailCall) { return(true); } if (optimizedPath && transition.optimizedTailCall) { return(true); } BitSet reachable = new BitSet(atn.states.Count); Stack <ATNState> worklist = new Stack <ATNState>(); worklist.Push(transition.followState); while (worklist.Count > 0) { ATNState state = worklist.Pop(); if (reachable.Get(state.stateNumber)) { continue; } if (state is RuleStopState) { continue; } if (!state.OnlyHasEpsilonTransitions) { return(false); } IList <Transition> transitions = optimizedPath ? state.optimizedTransitions : state .transitions; foreach (Transition t in transitions) { if (t.TransitionType != TransitionType.Epsilon) { return(false); } worklist.Push(t.target); } } return(true); }
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; } } } } }
/// <summary> /// Given a starting configuration set, figure out all ATN configurations /// we can reach upon input /// <paramref name="t"/> /// . Parameter /// <paramref name="reach"/> /// is a return /// parameter. /// </summary> protected internal virtual void GetReachableConfigSet(ICharStream input, ATNConfigSet closure, ATNConfigSet reach, int t) { // this is used to skip processing for configs which have a lower priority // than a config that already reached an accept state for the same rule int skipAlt = ATN.InvalidAltNumber; foreach (ATNConfig c in closure) { bool currentAltReachedAcceptState = c.Alt == skipAlt; if (currentAltReachedAcceptState && c.PassedThroughNonGreedyDecision) { continue; } int n = c.State.NumberOfOptimizedTransitions; for (int ti = 0; ti < n; ti++) { // for each optimized transition Transition trans = c.State.GetOptimizedTransition(ti); ATNState target = GetReachableTarget(trans, t); if (target != null) { LexerActionExecutor lexerActionExecutor = c.ActionExecutor; if (lexerActionExecutor != null) { lexerActionExecutor = lexerActionExecutor.FixOffsetBeforeMatch(input.Index - startIndex); } bool treatEofAsEpsilon = t == IntStreamConstants.Eof; if (Closure(input, c.Transform(target, lexerActionExecutor, true), reach, currentAltReachedAcceptState, true, treatEofAsEpsilon)) { // any remaining configs for this alt have a lower priority than // the one that just reached an accept state. skipAlt = c.Alt; break; } } } } }
protected internal virtual int MatchATN(ICharStream input) { ATNState startState = atn.modeToStartState[mode]; ATNConfigSet s0_closure = ComputeStartState(input, startState); bool suppressEdge = s0_closure.HasSemanticContext; if (suppressEdge) { s0_closure.ClearExplicitSemanticContext(); } DFAState next = AddDFAState(s0_closure); if (!suppressEdge) { if (!atn.modeToDFA[mode].s0.CompareAndSet(null, next)) { next = atn.modeToDFA[mode].s0.Get(); } } int predict = ExecATN(input, next); return(predict); }
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; }
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 Antlr4.Runtime.Atn.ATNConfig Transform(ATNState state, LexerActionExecutor lexerActionExecutor, bool checkNonGreedy) { return(Transform(state, context, this.SemanticContext, checkNonGreedy, lexerActionExecutor)); }
public SemanticContextATNConfig(Antlr4.Runtime.Atn.SemanticContext semanticContext, ATNConfig c, ATNState state, PredictionContext context) : base(c, state, context) { this.semanticContext = semanticContext; }
public DFA(ATNState atnStartState, int decision) { this.atnStartState = atnStartState; this.decision = decision; if (this.atnStartState.atn.grammarType == ATNType.Lexer) { minDfaEdge = LexerATNSimulator.MinDfaEdge; maxDfaEdge = LexerATNSimulator.MaxDfaEdge; } else { minDfaEdge = TokenConstants.Eof; maxDfaEdge = atnStartState.atn.maxTokenType; } this.emptyEdgeMap = new Antlr4.Runtime.Dfa.EmptyEdgeMap<DFAState>(minDfaEdge, maxDfaEdge); this.emptyContextEdgeMap = new Antlr4.Runtime.Dfa.EmptyEdgeMap<DFAState>(-1, atnStartState.atn.states.Count - 1); }
public virtual string[] ToStrings(IRecognizer recognizer, Antlr4.Runtime.Atn.PredictionContext stop, int currentState) { List <string> result = new List <string>(); for (int perm = 0; ; perm++) { int offset = 0; bool last = true; Antlr4.Runtime.Atn.PredictionContext p = this; int stateNumber = currentState; StringBuilder localBuffer = new StringBuilder(); localBuffer.Append("["); while (!p.IsEmpty && p != stop) { int index = 0; if (p.Size > 0) { int bits = 1; while ((1 << bits) < p.Size) { bits++; } int mask = (1 << bits) - 1; index = (perm >> offset) & mask; last &= index >= p.Size - 1; if (index >= p.Size) { goto outer_continue; } offset += bits; } if (recognizer != null) { if (localBuffer.Length > 1) { // first char is '[', if more than that this isn't the first rule localBuffer.Append(' '); } ATN atn = recognizer.Atn; ATNState s = atn.states[stateNumber]; string ruleName = recognizer.RuleNames[s.ruleIndex]; localBuffer.Append(ruleName); } else { if (p.GetReturnState(index) != EmptyFullStateKey) { if (!p.IsEmpty) { if (localBuffer.Length > 1) { // first char is '[', if more than that this isn't the first rule localBuffer.Append(' '); } localBuffer.Append(p.GetReturnState(index)); } } } stateNumber = p.GetReturnState(index); p = p.GetParent(index); } localBuffer.Append("]"); result.Add(localBuffer.ToString()); if (last) { break; } outer_continue :; } return(result.ToArray()); }
private static int CombineChainedEpsilons(ATN atn) { int removedEdges = 0; foreach (ATNState state in atn.states) { if (!state.OnlyHasEpsilonTransitions || state is RuleStopState) { continue; } IList <Transition> optimizedTransitions = null; for (int i = 0; i < state.NumberOfOptimizedTransitions; i++) { Transition transition = state.GetOptimizedTransition(i); ATNState intermediate = transition.target; if (transition.TransitionType != TransitionType.EPSILON || ((EpsilonTransition)transition).OutermostPrecedenceReturn != -1 || intermediate.StateType != StateType.Basic || !intermediate.OnlyHasEpsilonTransitions) { if (optimizedTransitions != null) { optimizedTransitions.Add(transition); } goto nextTransition_continue; } for (int j = 0; j < intermediate.NumberOfOptimizedTransitions; j++) { if (intermediate.GetOptimizedTransition(j).TransitionType != TransitionType.EPSILON || ((EpsilonTransition)intermediate.GetOptimizedTransition(j)).OutermostPrecedenceReturn != -1) { if (optimizedTransitions != null) { optimizedTransitions.Add(transition); } goto nextTransition_continue; } } removedEdges++; if (optimizedTransitions == null) { optimizedTransitions = new List <Transition>(); for (int j_1 = 0; j_1 < i; j_1++) { optimizedTransitions.Add(state.GetOptimizedTransition(j_1)); } } for (int j_2 = 0; j_2 < intermediate.NumberOfOptimizedTransitions; j_2++) { ATNState target = intermediate.GetOptimizedTransition(j_2).target; optimizedTransitions.Add(new EpsilonTransition(target)); } nextTransition_continue :; } if (optimizedTransitions != null) { if (state.IsOptimized) { while (state.NumberOfOptimizedTransitions > 0) { state.RemoveOptimizedTransition(state.NumberOfOptimizedTransitions - 1); } } foreach (Transition transition in optimizedTransitions) { state.AddOptimizedTransition(transition); } } } return(removedEdges); }
public ActionTransition(ATNState target, int ruleIndex) : this(target, ruleIndex, -1, false) { }
public WildcardTransition(ATNState target) : base(target) { }
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 internal ActionATNConfig(LexerActionExecutor lexerActionExecutor, ATNConfig c, ATNState state, PredictionContext context, bool passedThroughNonGreedyDecision) : base(c, state, context) { if (c.SemanticContext != SemanticContext.None) { throw new NotSupportedException(); } this.lexerActionExecutor = lexerActionExecutor; this.passedThroughNonGreedyDecision = passedThroughNonGreedyDecision; }
protected internal ATNConfig(Antlr4.Runtime.Atn.ATNConfig c, ATNState state, PredictionContext context) { this.state = state; this.altAndOuterContextDepth = c.altAndOuterContextDepth & unchecked ((int)(0x7FFFFFFF)); this.context = context; }
protected internal virtual void VisitRuleStopState(ATNState p) { RuleStartState ruleStartState = atn.ruleToStartState[p.ruleIndex]; if (ruleStartState.isPrecedenceRule) { Tuple<ParserRuleContext, int> parentContext = _parentContextStack.Pop(); UnrollRecursionContexts(parentContext.Item1); State = parentContext.Item2; } else { ExitRule(); } RuleTransition ruleTransition = (RuleTransition)atn.states[State].Transition(0); State = ruleTransition.followState.stateNumber; }
public DFA(ATNState atnStartState) : this(atnStartState, 0) { }
public AtomTransition(ATNState target, int token) : base(target) { this.token = token; }
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; }
public EpsilonTransition(ATNState target) : this(target, -1) { }
protected internal virtual void ReadStates(ATN atn) { // // STATES // IList <Tuple <LoopEndState, int> > loopBackStateNumbers = new List <Tuple <LoopEndState, int> >(); IList <Tuple <BlockStartState, int> > endStateNumbers = new List <Tuple <BlockStartState, int> >(); int nstates = ReadInt(); for (int i_1 = 0; i_1 < nstates; i_1++) { StateType stype = (StateType)ReadInt(); // ignore bad type of states if (stype == StateType.InvalidType) { atn.AddState(null); continue; } int ruleIndex = ReadInt(); if (ruleIndex == char.MaxValue) { ruleIndex = -1; } ATNState s = StateFactory(stype, ruleIndex); if (stype == StateType.LoopEnd) { // special case int loopBackStateNumber = ReadInt(); loopBackStateNumbers.Add(Tuple.Create((LoopEndState)s, loopBackStateNumber)); } else { if (s is BlockStartState) { int endStateNumber = ReadInt(); endStateNumbers.Add(Tuple.Create((BlockStartState)s, endStateNumber)); } } atn.AddState(s); } // delay the assignment of loop back and end states until we know all the state instances have been initialized foreach (Tuple <LoopEndState, int> pair in loopBackStateNumbers) { pair.Item1.loopBackState = atn.states[pair.Item2]; } foreach (Tuple <BlockStartState, int> pair_1 in endStateNumbers) { pair_1.Item1.endState = (BlockEndState)atn.states[pair_1.Item2]; } int numNonGreedyStates = ReadInt(); for (int i_2 = 0; i_2 < numNonGreedyStates; i_2++) { int stateNumber = ReadInt(); ((DecisionState)atn.states[stateNumber]).nonGreedy = true; } int numPrecedenceStates = ReadInt(); for (int i_4 = 0; i_4 < numPrecedenceStates; i_4++) { int stateNumber = ReadInt(); ((RuleStartState)atn.states[stateNumber]).isPrecedenceRule = true; } }
public EpsilonTransition(ATNState target, int outermostPrecedenceReturn) : base(target) { this.outermostPrecedenceReturn = outermostPrecedenceReturn; }
private static int InlineSetRules(ATN atn) { int inlinedCalls = 0; Transition[] ruleToInlineTransition = new Transition[atn.ruleToStartState.Length]; for (int i = 0; i < atn.ruleToStartState.Length; i++) { RuleStartState startState = atn.ruleToStartState[i]; ATNState middleState = startState; while (middleState.OnlyHasEpsilonTransitions && middleState.NumberOfOptimizedTransitions == 1 && middleState.GetOptimizedTransition(0).TransitionType == TransitionType.EPSILON) { middleState = middleState.GetOptimizedTransition(0).target; } if (middleState.NumberOfOptimizedTransitions != 1) { continue; } Transition matchTransition = middleState.GetOptimizedTransition(0); ATNState matchTarget = matchTransition.target; if (matchTransition.IsEpsilon || !matchTarget.OnlyHasEpsilonTransitions || matchTarget.NumberOfOptimizedTransitions != 1 || !(matchTarget.GetOptimizedTransition(0).target is RuleStopState)) { continue; } switch (matchTransition.TransitionType) { case TransitionType.ATOM: case TransitionType.RANGE: case TransitionType.SET: { ruleToInlineTransition[i] = matchTransition; break; } case TransitionType.NOT_SET: case TransitionType.WILDCARD: { // not implemented yet continue; } default: { continue; } } } for (int stateNumber = 0; stateNumber < atn.states.Count; stateNumber++) { ATNState state = atn.states[stateNumber]; if (state.ruleIndex < 0) { continue; } IList <Transition> optimizedTransitions = null; for (int i_1 = 0; i_1 < state.NumberOfOptimizedTransitions; i_1++) { Transition transition = state.GetOptimizedTransition(i_1); if (!(transition is RuleTransition)) { if (optimizedTransitions != null) { optimizedTransitions.Add(transition); } continue; } RuleTransition ruleTransition = (RuleTransition)transition; Transition effective = ruleToInlineTransition[ruleTransition.target.ruleIndex]; if (effective == null) { if (optimizedTransitions != null) { optimizedTransitions.Add(transition); } continue; } if (optimizedTransitions == null) { optimizedTransitions = new List <Transition>(); for (int j = 0; j < i_1; j++) { optimizedTransitions.Add(state.GetOptimizedTransition(i_1)); } } inlinedCalls++; ATNState target = ruleTransition.followState; ATNState intermediateState = new BasicState(); intermediateState.SetRuleIndex(target.ruleIndex); atn.AddState(intermediateState); optimizedTransitions.Add(new EpsilonTransition(intermediateState)); switch (effective.TransitionType) { case TransitionType.ATOM: { intermediateState.AddTransition(new AtomTransition(target, ((AtomTransition)effective).token)); break; } case TransitionType.RANGE: { intermediateState.AddTransition(new RangeTransition(target, ((RangeTransition)effective).from, ((RangeTransition)effective).to)); break; } case TransitionType.SET: { intermediateState.AddTransition(new SetTransition(target, effective.Label)); break; } default: { throw new NotSupportedException(); } } } if (optimizedTransitions != null) { if (state.IsOptimized) { while (state.NumberOfOptimizedTransitions > 0) { state.RemoveOptimizedTransition(state.NumberOfOptimizedTransitions - 1); } } foreach (Transition transition in optimizedTransitions) { state.AddOptimizedTransition(transition); } } } return(inlinedCalls); }
private static bool checkNonGreedyDecision(LexerATNConfig source, ATNState target) { return(source.passedThroughNonGreedyDecision || target is DecisionState && ((DecisionState)target).nonGreedy); }
private static int OptimizeSets(ATN atn, bool preserveOrder) { if (preserveOrder) { // this optimization currently doesn't preserve edge order. return(0); } int removedPaths = 0; IList <DecisionState> decisions = atn.decisionToState; foreach (DecisionState decision in decisions) { IntervalSet setTransitions = new IntervalSet(); for (int i = 0; i < decision.NumberOfOptimizedTransitions; i++) { Transition epsTransition = decision.GetOptimizedTransition(i); if (!(epsTransition is EpsilonTransition)) { continue; } if (epsTransition.target.NumberOfOptimizedTransitions != 1) { continue; } Transition transition = epsTransition.target.GetOptimizedTransition(0); if (!(transition.target is BlockEndState)) { continue; } if (transition is NotSetTransition) { // TODO: not yet implemented continue; } if (transition is AtomTransition || transition is RangeTransition || transition is SetTransition) { setTransitions.Add(i); } } if (setTransitions.Count <= 1) { continue; } IList <Transition> optimizedTransitions = new List <Transition>(); for (int i_1 = 0; i_1 < decision.NumberOfOptimizedTransitions; i_1++) { if (!setTransitions.Contains(i_1)) { optimizedTransitions.Add(decision.GetOptimizedTransition(i_1)); } } ATNState blockEndState = decision.GetOptimizedTransition(setTransitions.MinElement).target.GetOptimizedTransition(0).target; IntervalSet matchSet = new IntervalSet(); for (int i_2 = 0; i_2 < setTransitions.GetIntervals().Count; i_2++) { Interval interval = setTransitions.GetIntervals()[i_2]; for (int j = interval.a; j <= interval.b; j++) { Transition matchTransition = decision.GetOptimizedTransition(j).target.GetOptimizedTransition(0); if (matchTransition is NotSetTransition) { throw new NotSupportedException("Not yet implemented."); } else { matchSet.AddAll(matchTransition.Label); } } } Transition newTransition; if (matchSet.GetIntervals().Count == 1) { if (matchSet.Count == 1) { newTransition = new AtomTransition(blockEndState, matchSet.MinElement); } else { Interval matchInterval = matchSet.GetIntervals()[0]; newTransition = new RangeTransition(blockEndState, matchInterval.a, matchInterval.b); } } else { newTransition = new SetTransition(blockEndState, matchSet); } ATNState setOptimizedState = new BasicState(); setOptimizedState.SetRuleIndex(decision.ruleIndex); atn.AddState(setOptimizedState); setOptimizedState.AddTransition(newTransition); optimizedTransitions.Add(new EpsilonTransition(setOptimizedState)); removedPaths += decision.NumberOfOptimizedTransitions - optimizedTransitions.Count; if (decision.IsOptimized) { while (decision.NumberOfOptimizedTransitions > 0) { decision.RemoveOptimizedTransition(decision.NumberOfOptimizedTransitions - 1); } } foreach (Transition transition_1 in optimizedTransitions) { decision.AddOptimizedTransition(transition_1); } } return(removedPaths); }
public virtual IntervalSet NextTokens(ATNState s, PredictionContext ctx) { Args.NotNull("ctx", ctx); LL1Analyzer anal = new LL1Analyzer(this); IntervalSet next = anal.Look(s, ctx); return next; }
public NotSetTransition(ATNState target, IntervalSet set) : base(target, set) { }
public LexerATNConfig(LexerATNConfig c, ATNState state) : base(c, state, c.context, c.semanticContext) { this.lexerActionExecutor = c.lexerActionExecutor; this.passedThroughNonGreedyDecision = checkNonGreedyDecision(c, state); }
public virtual IntervalSet NextTokens(ATNState s) { if (s.nextTokenWithinRule != null) { return s.nextTokenWithinRule; } s.nextTokenWithinRule = NextTokens(s, PredictionContext.EmptyLocal); s.nextTokenWithinRule.SetReadonly(true); return s.nextTokenWithinRule; }
public virtual void AddState(ATNState state) { if (state != null) { state.atn = this; state.stateNumber = states.Count; } states.Add(state); }
public Antlr4.Runtime.Atn.ATNConfig Transform(ATNState state, Antlr4.Runtime.Atn.SemanticContext semanticContext, bool checkNonGreedy) { return(Transform(state, this.context, semanticContext, checkNonGreedy, this.ActionExecutor)); }
public virtual void RemoveState(ATNState state) { states[state.stateNumber] = null; }
private static bool CheckNonGreedyDecision(Antlr4.Runtime.Atn.ATNConfig source, ATNState target) { return(source.PassedThroughNonGreedyDecision || target is DecisionState && ((DecisionState)target).nonGreedy); }
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; }
public ActionATNConfig(LexerActionExecutor lexerActionExecutor, ATNState state, int alt, PredictionContext context, bool passedThroughNonGreedyDecision) : base(state, alt, context) { this.lexerActionExecutor = lexerActionExecutor; this.passedThroughNonGreedyDecision = passedThroughNonGreedyDecision; }
protected internal virtual void ReadEdges(ATN atn, IList <IntervalSet> sets) { // // EDGES // int nedges = ReadInt(); for (int i_9 = 0; i_9 < nedges; i_9++) { int src = ReadInt(); int trg = ReadInt(); TransitionType ttype = (TransitionType)ReadInt(); int arg1 = ReadInt(); int arg2 = ReadInt(); int arg3 = ReadInt(); Transition trans = EdgeFactory(atn, ttype, src, trg, arg1, arg2, arg3, sets); ATNState srcState = atn.states[src]; srcState.AddTransition(trans); } // edges for rule stop states can be derived, so they aren't serialized foreach (ATNState state_1 in atn.states) { for (int i_10 = 0; i_10 < state_1.NumberOfTransitions; i_10++) { Transition t = state_1.Transition(i_10); if (!(t is RuleTransition)) { continue; } RuleTransition ruleTransition = (RuleTransition)t; int outermostPrecedenceReturn = -1; if (atn.ruleToStartState[ruleTransition.target.ruleIndex].isPrecedenceRule) { if (ruleTransition.precedence == 0) { outermostPrecedenceReturn = ruleTransition.target.ruleIndex; } } EpsilonTransition returnTransition = new EpsilonTransition(ruleTransition.followState, outermostPrecedenceReturn); atn.ruleToStopState[ruleTransition.target.ruleIndex].AddTransition(returnTransition); } } foreach (ATNState state_2 in atn.states) { if (state_2 is BlockStartState) { // we need to know the end state to set its start state if (((BlockStartState)state_2).endState == null) { throw new InvalidOperationException(); } // block end states can only be associated to a single block start state if (((BlockStartState)state_2).endState.startState != null) { throw new InvalidOperationException(); } ((BlockStartState)state_2).endState.startState = (BlockStartState)state_2; } else if (state_2 is PlusLoopbackState) { PlusLoopbackState loopbackState = (PlusLoopbackState)state_2; for (int i_10 = 0; i_10 < loopbackState.NumberOfTransitions; i_10++) { ATNState target = loopbackState.Transition(i_10).target; if (target is PlusBlockStartState) { ((PlusBlockStartState)target).loopBackState = loopbackState; } } } else if (state_2 is StarLoopbackState) { StarLoopbackState loopbackState = (StarLoopbackState)state_2; for (int i_10 = 0; i_10 < loopbackState.NumberOfTransitions; i_10++) { ATNState target = loopbackState.Transition(i_10).target; if (target is StarLoopEntryState) { ((StarLoopEntryState)target).loopBackState = loopbackState; } } } } }
public ActionSemanticContextATNConfig(LexerActionExecutor lexerActionExecutor, SemanticContext semanticContext, ATNConfig c, ATNState state, PredictionContext context, bool passedThroughNonGreedyDecision) : base(semanticContext, c, state, context) { this.lexerActionExecutor = lexerActionExecutor; this.passedThroughNonGreedyDecision = passedThroughNonGreedyDecision; }
public RuleFunction(OutputModelFactory factory, Rule r) : base(factory) { this.name = r.name; this.rule = r; if (r.modifiers != null && r.modifiers.Count > 0) { this.modifiers = new List<string>(); foreach (GrammarAST t in r.modifiers) modifiers.Add(t.Text); } modifiers = Utils.NodesToStrings(r.modifiers); index = r.index; int lfIndex = name.IndexOf(ATNSimulator.RuleVariantDelimiter); if (lfIndex >= 0) { variantOf = name.Substring(0, lfIndex); } if (r.name.Equals(r.GetBaseContext())) { ruleCtx = new StructDecl(factory, r); AddContextGetters(factory, r.g.contextASTs[r.name]); if (r.args != null) { ICollection<Attribute> decls = r.args.attributes.Values; if (decls.Count > 0) { args = new List<AttributeDecl>(); ruleCtx.AddDecls(decls); foreach (Attribute a in decls) { args.Add(new AttributeDecl(factory, a)); } ruleCtx.ctorAttrs = args; } } if (r.retvals != null) { ruleCtx.AddDecls(r.retvals.attributes.Values); } if (r.locals != null) { ruleCtx.AddDecls(r.locals.attributes.Values); } } else { if (r.args != null || r.retvals != null || r.locals != null) { throw new System.NotSupportedException("customized fields are not yet supported for customized context objects"); } } ruleLabels = r.GetElementLabelNames(); tokenLabels = r.GetTokenRefs(); if (r.exceptions != null) { exceptions = new List<ExceptionClause>(); foreach (GrammarAST e in r.exceptions) { ActionAST catchArg = (ActionAST)e.GetChild(0); ActionAST catchAction = (ActionAST)e.GetChild(1); exceptions.Add(new ExceptionClause(factory, catchArg, catchAction)); } } startState = factory.GetGrammar().atn.ruleToStartState[r.index]; }
public static Antlr4.Runtime.Atn.ATNConfig Create(ATNState state, int alt, PredictionContext context, Antlr4.Runtime.Atn.SemanticContext semanticContext) { return(Create(state, alt, context, semanticContext, null)); }
public AbstractPredicateTransition(ATNState target) : base(target) { }