public virtual string[] ToStrings(IRecognizer recognizer, PredictionContext stop, int currentState) { List <string> result = new List <string>(); for (int perm = 0; ; perm++) { int offset = 0; bool last = true; 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) != EMPTY_RETURN_STATE) { 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()); }
public LL1Analyzer(ATN atn) { this.atn = atn; }
public ATNSimulator(ATN atn, PredictionContextCache sharedContextCache) { this.atn = atn; this.sharedContextCache = sharedContextCache; }
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); }
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); }
protected internal virtual void VerifyATN(ATN atn) { // verify assumptions foreach (ATNState state in atn.states) { if (state == null) { continue; } CheckCondition(state.OnlyHasEpsilonTransitions || state.NumberOfTransitions <= 1); if (state is PlusBlockStartState) { CheckCondition(((PlusBlockStartState)state).loopBackState != null); } if (state is StarLoopEntryState) { StarLoopEntryState starLoopEntryState = (StarLoopEntryState)state; CheckCondition(starLoopEntryState.loopBackState != null); CheckCondition(starLoopEntryState.NumberOfTransitions == 2); if (starLoopEntryState.Transition(0).target is StarBlockStartState) { CheckCondition(starLoopEntryState.Transition(1).target is LoopEndState); CheckCondition(!starLoopEntryState.nonGreedy); } else { if (starLoopEntryState.Transition(0).target is LoopEndState) { CheckCondition(starLoopEntryState.Transition(1).target is StarBlockStartState); CheckCondition(starLoopEntryState.nonGreedy); } else { throw new InvalidOperationException(); } } } if (state is StarLoopbackState) { CheckCondition(state.NumberOfTransitions == 1); CheckCondition(state.Transition(0).target is StarLoopEntryState); } if (state is LoopEndState) { CheckCondition(((LoopEndState)state).loopBackState != null); } if (state is RuleStartState) { CheckCondition(((RuleStartState)state).stopState != null); } if (state is BlockStartState) { CheckCondition(((BlockStartState)state).endState != null); } if (state is BlockEndState) { CheckCondition(((BlockEndState)state).startState != null); } if (state is DecisionState) { DecisionState decisionState = (DecisionState)state; CheckCondition(decisionState.NumberOfTransitions <= 1 || decisionState.decision >= 0); } else { CheckCondition(state.NumberOfTransitions <= 1 || state is RuleStopState); } } }
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); }
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; } }
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; } } } } }
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 Transition EdgeFactory(ATN atn, TransitionType type, int src, int trg, int arg1, int arg2, int arg3, IList <IntervalSet> sets) { ATNState target = atn.states[trg]; switch (type) { case TransitionType.EPSILON: { return(new EpsilonTransition(target)); } case TransitionType.RANGE: { if (arg3 != 0) { return(new RangeTransition(target, TokenConstants.EOF, arg2)); } else { return(new RangeTransition(target, arg1, arg2)); } } case TransitionType.RULE: { RuleTransition rt = new RuleTransition((RuleStartState)atn.states[arg1], arg2, arg3, target); return(rt); } case TransitionType.PREDICATE: { PredicateTransition pt = new PredicateTransition(target, arg1, arg2, arg3 != 0); return(pt); } case TransitionType.PRECEDENCE: { return(new PrecedencePredicateTransition(target, arg1)); } case TransitionType.ATOM: { if (arg3 != 0) { return(new AtomTransition(target, TokenConstants.EOF)); } else { return(new AtomTransition(target, arg1)); } } case TransitionType.ACTION: { ActionTransition a = new ActionTransition(target, arg1, arg2, arg3 != 0); return(a); } case TransitionType.SET: { return(new SetTransition(target, sets[arg1])); } case TransitionType.NOT_SET: { return(new NotSetTransition(target, sets[arg1])); } case TransitionType.WILDCARD: { return(new WildcardTransition(target)); } } throw new ArgumentException("The specified transition type is not valid."); }
public LexerATNSimulator(ATN atn, DFA[] decisionToDFA, PredictionContextCache sharedContextCache) : this(null, atn, decisionToDFA, sharedContextCache) { }