protected internal virtual IList <IntervalSet> ReadSets(ATN atn) { // // SETS // IList <IntervalSet> sets = new List <IntervalSet>(); int nsets = ReadInt(); for (int i_8 = 0; i_8 < nsets; i_8++) { IntervalSet set = new IntervalSet(); sets.Add(set); int nintervals = ReadInt(); bool containsEof = ReadInt() != 0; if (containsEof) { set.Add(-1); } for (int j = 0; j < nintervals; j++) { set.Add(ReadInt(), ReadInt()); } } return(sets); }
public void TestMixedRangesAndElements() { IntervalSet s = new IntervalSet(); s.Add(1); s.Add('a', 'z'); s.Add('0', '9'); String expecting = "{1, 48..57, 97..122}"; Assert.AreEqual(s.ToString(), expecting); }
public void TestMergeWithDoubleOverlap() { IntervalSet s = IntervalSet.Of(1, 10); s.Add(20, 30); s.Add(5, 25); // overlaps two! String expecting = "{1..30}"; String result = s.ToString(); Assert.AreEqual(expecting, result); }
public void TestSize() { IntervalSet s = IntervalSet.Of(20, 30); s.Add(50, 55); s.Add(5, 19); String expecting = "32"; String result = s.Count.ToString(); Assert.AreEqual(expecting, result); }
public void TestToList() { IntervalSet s = IntervalSet.Of(20, 25); s.Add(50, 55); s.Add(5, 5); int[] expecting = { 5, 20, 21, 22, 23, 24, 25, 50, 51, 52, 53, 54, 55 }; IList <int> result = s.ToList(); CollectionAssert.AreEquivalent(expecting, result.ToArray()); }
public void TestIsolatedElements() { IntervalSet s = new IntervalSet(); s.Add(1); s.Add('z'); s.Add('\uFFF0'); String expecting = "{1, 122, 65520}"; Assert.AreEqual(s.ToString(), expecting); }
public void TestMergeOfRangesAndSingleValuesReverse() { IntervalSet s = IntervalSet.Of(43, 65534); s.Add(42); s.Add(0, 41); String expecting = "{0..65534}"; String result = s.ToString(); Assert.AreEqual(expecting, result); }
public void TestNotSet() { IntervalSet vocabulary = IntervalSet.Of(1, 1000); IntervalSet s = IntervalSet.Of(50, 60); s.Add(5); s.Add(250, 300); String expecting = "{1..4, 6..49, 61..249, 301..1000}"; String result = (s.Complement(vocabulary)).ToString(); Assert.AreEqual(expecting, result); }
public void TestIntersectionWithTwoContainedElementsReversed() { IntervalSet s = IntervalSet.Of(10, 20); IntervalSet s2 = IntervalSet.Of(2, 2); s2.Add(15); s2.Add(18); String expecting = "{15, 18}"; String result = (s2.And(s)).ToString(); Assert.AreEqual(expecting, result); }
public void TestMergeWhereAdditionMergesThreeExistingIntervals() { IntervalSet s = new IntervalSet(); s.Add(0); s.Add(3); s.Add(5); s.Add(0, 7); String expecting = "{0..7}"; String result = s.ToString(); Assert.AreEqual(expecting, result); }
public void TestMergeWhereAdditionMergesTwoExistingIntervals() { // 42, 10, {0..9, 11..41, 43..65534} IntervalSet s = IntervalSet.Of(42); s.Add(10); s.Add(0, 9); s.Add(43, 65534); s.Add(11, 41); String expecting = "{0..65534}"; String result = s.ToString(); Assert.AreEqual(expecting, result); }
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 void TestNotSetFragmentedVocabulary() { IntervalSet vocabulary = IntervalSet.Of(1, 255); vocabulary.Add(1000, 2000); vocabulary.Add(9999); IntervalSet s = IntervalSet.Of(50, 60); s.Add(3); s.Add(250, 300); s.Add(10000); // this is outside range of vocab and should be ignored String expecting = "{1..2, 4..49, 61..249, 1000..2000, 9999}"; String result = (s.Complement(vocabulary)).ToString(); Assert.AreEqual(expecting, result); }
/// <summary> /// <inheritDoc/> /// <p>The default implementation resynchronizes the parser by consuming tokens /// until we find one in the resynchronization set--loosely the set of tokens /// that can follow the current rule.</p> /// </summary> public virtual void Recover(Parser recognizer, RecognitionException e) { // System.out.println("recover in "+recognizer.getRuleInvocationStack()+ // " index="+recognizer.getInputStream().index()+ // ", lastErrorIndex="+ // lastErrorIndex+ // ", states="+lastErrorStates); if (lastErrorIndex == ((ITokenStream)recognizer.InputStream).Index && lastErrorStates != null && lastErrorStates.Contains(recognizer.State)) { // uh oh, another error at same token index and previously-visited // state in ATN; must be a case where LT(1) is in the recovery // token set so nothing got consumed. Consume a single token // at least to prevent an infinite loop; this is a failsafe. // System.err.println("seen error condition before index="+ // lastErrorIndex+", states="+lastErrorStates); // System.err.println("FAILSAFE consumes "+recognizer.getTokenNames()[recognizer.getInputStream().LA(1)]); recognizer.Consume(); } lastErrorIndex = ((ITokenStream)recognizer.InputStream).Index; if (lastErrorStates == null) { lastErrorStates = new IntervalSet(); } lastErrorStates.Add(recognizer.State); IntervalSet followSet = GetErrorRecoverySet(recognizer); ConsumeUntil(recognizer, followSet); }
public void TestEquals() { IntervalSet s = IntervalSet.Of(10, 20); s.Add(2); s.Add(499, 501); IntervalSet s2 = IntervalSet.Of(10, 20); s2.Add(2); s2.Add(499, 501); Assert.AreEqual(s, s2); IntervalSet s3 = IntervalSet.Of(10, 20); s3.Add(2); Assert.AreNotEqual(s, s3); }
public void TestSubtractOfRangeSpanningMultipleRanges() { IntervalSet s = IntervalSet.Of(10, 20); s.Add(30, 40); s.Add(50, 60); // s has 3 ranges now: 10..20, 30..40, 50..60 IntervalSet s2 = IntervalSet.Of(5, 55); // covers one and touches 2nd range String expecting = "{56..60}"; String result = (s.Subtract(s2)).ToString(); Assert.AreEqual(expecting, result); IntervalSet s3 = IntervalSet.Of(15, 55); // touches both expecting = "{10..14, 56..60}"; result = (s.Subtract(s3)).ToString(); Assert.AreEqual(expecting, result); }
public void TestComplement3() { IntervalSet s = IntervalSet.Of(1, 96); s.Add(99, Lexer.MaxCharValue); String expecting = "{97..98}"; String result = (s.Complement(1, Lexer.MaxCharValue)).ToString(); Assert.AreEqual(expecting, result); }
public void TestRmSingleElement() { IntervalSet s = IntervalSet.Of(1, 10); s.Add(-3, -3); s.Remove(-3); String expecting = "{1..10}"; String result = s.ToString(); Assert.AreEqual(expecting, result); }
public void TestRmMiddleRange() { IntervalSet s = IntervalSet.Of(1, 10); s.Add(-3, -3); s.Remove(5); String expecting = "{-3, 1..4, 6..10}"; String result = s.ToString(); Assert.AreEqual(expecting, result); }
public void TestNotSingleElement() { IntervalSet vocabulary = IntervalSet.Of(1, 1000); vocabulary.Add(2000, 3000); IntervalSet s = IntervalSet.Of(50, 50); String expecting = "{1..49, 51..1000, 2000..3000}"; String result = (s.Complement(vocabulary)).ToString(); Assert.AreEqual(expecting, result); }
public void TestSingleElementMinusDisjointSet() { IntervalSet s = IntervalSet.Of(15, 15); IntervalSet s2 = IntervalSet.Of(1, 5); s2.Add(10, 20); String expecting = "{}"; // 15 - {1..5, 10..20} = {} String result = s.Subtract(s2).ToString(); Assert.AreEqual(expecting, result); }
public void TestComplement() { IntervalSet s = IntervalSet.Of(100, 100); s.Add(101, 101); IntervalSet s2 = IntervalSet.Of(100, 102); String expecting = "102"; String result = (s.Complement(s2)).ToString(); Assert.AreEqual(expecting, result); }
public void TestRmRightSide() { IntervalSet s = IntervalSet.Of(1, 10); s.Add(-3, -3); s.Remove(10); String expecting = "{-3, 1..9}"; String result = s.ToString(); Assert.AreEqual(expecting, result); }
/// <summary> /// Tries to add a reservation for the specified time interval. If there is an existing reservation for that interval, /// the state of this object will not be changed. /// </summary> /// <param name="startTime">reservation start time</param> /// <param name="endTime">reservation end time</param> /// <param name="instr">associated instruction</param> /// <returns>true if reservation was successful, i.e. no colliding reservation</returns> public bool TryReserve(long startTime, long endTime, XIL3Instr instr) { if (IsReserved(startTime, endTime)) { return(false); } _rset.Add((int)startTime, (int)endTime); _resList.Add(new Reservation(startTime, endTime, instr)); return(true); }
public void TestSubtractOfWackyRange() { IntervalSet s = IntervalSet.Of(0, 113); s.Add(115, 200); IntervalSet s2 = IntervalSet.Of(0, 115); s2.Add(117, 200); String expecting = "116"; String result = (s.Subtract(s2)).ToString(); Assert.AreEqual(expecting, result); }
public void TestMembership() { IntervalSet s = IntervalSet.Of(15, 15); s.Add(50, 60); Assert.IsFalse(s.Contains(0)); Assert.IsFalse(s.Contains(20)); Assert.IsFalse(s.Contains(100)); Assert.IsTrue(s.Contains(15)); Assert.IsTrue(s.Contains(55)); Assert.IsTrue(s.Contains(50)); Assert.IsTrue(s.Contains(60)); }
public void TestNotRIntersectionNotT() { IntervalSet s = IntervalSet.Of(0, 's'); s.Add('u', 200); IntervalSet s2 = IntervalSet.Of(0, 'q'); s2.Add('s', 200); String expecting = "{0..113, 115, 117..200}"; String result = (s.And(s2)).ToString(); Assert.AreEqual(expecting, result); }
protected internal virtual void ReadSets(ATN atn, IList <IntervalSet> sets, System.Func <int> readUnicode) { // // SETS // int nsets = ReadInt(); for (int i_8 = 0; i_8 < nsets; i_8++) { IntervalSet set = new IntervalSet(); sets.Add(set); int nintervals = ReadInt(); bool containsEof = ReadInt() != 0; if (containsEof) { set.Add(-1); } for (int j = 0; j < nintervals; j++) { set.Add(readUnicode(), readUnicode()); } } }
private IntervalSet Filter(IntervalSet original) { IntervalSet result = new IntervalSet(); foreach (Interval i in original.GetIntervals()) { for (int j = i.a; j <= i.b; ++j) { if (!_not_used.Contains(j)) { result.Add(j); } } } return(result); }
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); }