public AddAll ( IIntSet set ) : Antlr4.Runtime.Misc.IntervalSet | ||
set | IIntSet | |
return | Antlr4.Runtime.Misc.IntervalSet |
public virtual Antlr4.Runtime.Misc.IntervalSet Or(IIntSet a) { Antlr4.Runtime.Misc.IntervalSet o = new Antlr4.Runtime.Misc.IntervalSet(); o.AddAll(this); o.AddAll(a); return(o); }
/// <summary> /// Given the set of possible values (rather than, say UNICODE or MAXINT), /// return a new set containing all elements in vocabulary, but not in /// this. /// </summary> /// <remarks> /// Given the set of possible values (rather than, say UNICODE or MAXINT), /// return a new set containing all elements in vocabulary, but not in /// this. The computation is (vocabulary - this). /// 'this' is assumed to be either a subset or equal to vocabulary. /// </remarks> public virtual Antlr4.Runtime.Misc.IntervalSet Complement(IIntSet vocabulary) { if (vocabulary == null) { return(null); } // nothing in common with null set if (!(vocabulary is Antlr4.Runtime.Misc.IntervalSet)) { throw new ArgumentException("can't complement with non IntervalSet (" + vocabulary .GetType().FullName + ")"); } Antlr4.Runtime.Misc.IntervalSet vocabularyIS = ((Antlr4.Runtime.Misc.IntervalSet) vocabulary); int maxElement = vocabularyIS.GetMaxElement(); Antlr4.Runtime.Misc.IntervalSet compl = new Antlr4.Runtime.Misc.IntervalSet(); int n = intervals.Count; if (n == 0) { return(compl); } Interval first = intervals[0]; // add a range from 0 to first.a constrained to vocab if (first.a > 0) { Antlr4.Runtime.Misc.IntervalSet s = Antlr4.Runtime.Misc.IntervalSet.Of(0, first.a - 1); Antlr4.Runtime.Misc.IntervalSet a = s.And(vocabularyIS); compl.AddAll(a); } for (int i = 1; i < n; i++) { // from 2nd interval .. nth Interval previous = intervals[i - 1]; Interval current = intervals[i]; Antlr4.Runtime.Misc.IntervalSet s = Antlr4.Runtime.Misc.IntervalSet.Of(previous.b + 1, current.a - 1); Antlr4.Runtime.Misc.IntervalSet a = s.And(vocabularyIS); compl.AddAll(a); } Interval last = intervals[n - 1]; // add a range from last.b to maxElement constrained to vocab if (last.b < maxElement) { Antlr4.Runtime.Misc.IntervalSet s = Antlr4.Runtime.Misc.IntervalSet.Of(last.b + 1 , maxElement); Antlr4.Runtime.Misc.IntervalSet a = s.And(vocabularyIS); compl.AddAll(a); } return(compl); }
/// <summary>combine all sets in the array returned the or'd value</summary> public static Antlr4.Runtime.Misc.IntervalSet Or(Antlr4.Runtime.Misc.IntervalSet[] sets) { Antlr4.Runtime.Misc.IntervalSet r = new Antlr4.Runtime.Misc.IntervalSet(); foreach (Antlr4.Runtime.Misc.IntervalSet s in sets) { r.AddAll(s); } return(r); }
public virtual Antlr4.Runtime.Misc.IntervalSet Subtract(IIntSet a) { if (a == null || a.IsNil) { return(new Antlr4.Runtime.Misc.IntervalSet(this)); } if (a is Antlr4.Runtime.Misc.IntervalSet) { return(Subtract(this, (Antlr4.Runtime.Misc.IntervalSet)a)); } Antlr4.Runtime.Misc.IntervalSet other = new Antlr4.Runtime.Misc.IntervalSet(); other.AddAll(a); return(Subtract(this, other)); }
/// <summary> /// <inheritDoc/> /// /// </summary> public virtual Antlr4.Runtime.Misc.IntervalSet Complement(IIntSet vocabulary) { if (vocabulary == null || vocabulary.IsNil) { return(null); } // nothing in common with null set Antlr4.Runtime.Misc.IntervalSet vocabularyIS; if (vocabulary is Antlr4.Runtime.Misc.IntervalSet) { vocabularyIS = (Antlr4.Runtime.Misc.IntervalSet)vocabulary; } else { vocabularyIS = new Antlr4.Runtime.Misc.IntervalSet(); vocabularyIS.AddAll(vocabulary); } return(vocabularyIS.Subtract(this)); }
protected internal virtual IntervalSet GetErrorRecoverySet(Parser recognizer) { ATN atn = recognizer.Interpreter.atn; RuleContext ctx = recognizer._ctx; IntervalSet recoverSet = new IntervalSet(); while (ctx != null && ctx.invokingState >= 0) { // compute what follows who invoked us ATNState invokingState = atn.states[ctx.invokingState]; RuleTransition rt = (RuleTransition)invokingState.Transition(0); IntervalSet follow = atn.NextTokens(rt.followState); recoverSet.AddAll(follow); ctx = ctx.parent; } recoverSet.Remove(TokenConstants.Epsilon); // System.out.println("recover set "+recoverSet.toString(recognizer.getTokenNames())); return recoverSet; }
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 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 Antlr4.Runtime.Misc.IntervalSet Or(IIntSet a) { Antlr4.Runtime.Misc.IntervalSet o = new Antlr4.Runtime.Misc.IntervalSet(); o.AddAll(this); o.AddAll(a); return o; }
/// <summary> /// Given the set of possible values (rather than, say UNICODE or MAXINT), /// return a new set containing all elements in vocabulary, but not in /// this. /// </summary> /// <remarks> /// Given the set of possible values (rather than, say UNICODE or MAXINT), /// return a new set containing all elements in vocabulary, but not in /// this. The computation is (vocabulary - this). /// 'this' is assumed to be either a subset or equal to vocabulary. /// </remarks> public virtual Antlr4.Runtime.Misc.IntervalSet Complement(IIntSet vocabulary) { if (vocabulary == null) { return null; } // nothing in common with null set if (!(vocabulary is Antlr4.Runtime.Misc.IntervalSet)) { throw new ArgumentException("can't complement with non IntervalSet (" + vocabulary .GetType().FullName + ")"); } Antlr4.Runtime.Misc.IntervalSet vocabularyIS = ((Antlr4.Runtime.Misc.IntervalSet) vocabulary); int maxElement = vocabularyIS.GetMaxElement(); Antlr4.Runtime.Misc.IntervalSet compl = new Antlr4.Runtime.Misc.IntervalSet(); int n = intervals.Count; if (n == 0) { return compl; } Interval first = intervals[0]; // add a range from 0 to first.a constrained to vocab if (first.a > 0) { Antlr4.Runtime.Misc.IntervalSet s = Antlr4.Runtime.Misc.IntervalSet.Of(0, first.a - 1); Antlr4.Runtime.Misc.IntervalSet a = s.And(vocabularyIS); compl.AddAll(a); } for (int i = 1; i < n; i++) { // from 2nd interval .. nth Interval previous = intervals[i - 1]; Interval current = intervals[i]; Antlr4.Runtime.Misc.IntervalSet s = Antlr4.Runtime.Misc.IntervalSet.Of(previous.b + 1, current.a - 1); Antlr4.Runtime.Misc.IntervalSet a = s.And(vocabularyIS); compl.AddAll(a); } Interval last = intervals[n - 1]; // add a range from last.b to maxElement constrained to vocab if (last.b < maxElement) { Antlr4.Runtime.Misc.IntervalSet s = Antlr4.Runtime.Misc.IntervalSet.Of(last.b + 1 , maxElement); Antlr4.Runtime.Misc.IntervalSet a = s.And(vocabularyIS); compl.AddAll(a); } return compl; }
/// <summary>combine all sets in the array returned the or'd value</summary> public static Antlr4.Runtime.Misc.IntervalSet Or(Antlr4.Runtime.Misc.IntervalSet[] sets) { Antlr4.Runtime.Misc.IntervalSet r = new Antlr4.Runtime.Misc.IntervalSet(); foreach (Antlr4.Runtime.Misc.IntervalSet s in sets) { r.AddAll(s); } return r; }
public virtual Antlr4.Runtime.Misc.IntervalSet Subtract(IIntSet a) { if (a == null || a.IsNil) { return new Antlr4.Runtime.Misc.IntervalSet(this); } if (a is Antlr4.Runtime.Misc.IntervalSet) { return Subtract(this, (Antlr4.Runtime.Misc.IntervalSet)a); } Antlr4.Runtime.Misc.IntervalSet other = new Antlr4.Runtime.Misc.IntervalSet(); other.AddAll(a); return Subtract(this, other); }
/// <summary> /// <inheritDoc/> /// /// </summary> public virtual Antlr4.Runtime.Misc.IntervalSet Complement(IIntSet vocabulary) { if (vocabulary == null || vocabulary.IsNil) { return null; } // nothing in common with null set Antlr4.Runtime.Misc.IntervalSet vocabularyIS; if (vocabulary is Antlr4.Runtime.Misc.IntervalSet) { vocabularyIS = (Antlr4.Runtime.Misc.IntervalSet)vocabulary; } else { vocabularyIS = new Antlr4.Runtime.Misc.IntervalSet(); vocabularyIS.AddAll(vocabulary); } return vocabularyIS.Subtract(this); }
/// <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); } } } } } } }