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