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 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); }