private string GetWhiteSpaceFromCurrentContext(ParserRuleContext context) { int a = context.Start.StartIndex; int b = context.Stop.StopIndex; a.Interval interval = new a.Interval(a, b); _charStream = context.Start.InputStream; return(_charStream.GetText(interval)); }
private string GetWhitespaceStringFromTokenInterval(a.Interval interval) { try { var start = TokenStream.Get(interval.a).StartIndex; var end = TokenStream.Get(interval.b).StopIndex; a.Interval i = new a.Interval(start, end); return(_charStream.GetText(i)); } catch { return(string.Empty); } }
public virtual string GetText(Interval interval) { if (interval.a < 0 || interval.b < interval.a - 1) { throw new ArgumentException("invalid interval"); } int bufferStartIndex = GetBufferStartIndex(); if (n > 0 && data[n - 1] == char.MaxValue) { if (interval.a + interval.Length > bufferStartIndex + n) { throw new ArgumentException("the interval extends past the end of the stream"); } } if (interval.a < bufferStartIndex || interval.b >= bufferStartIndex + n) { throw new NotSupportedException("interval " + interval + " outside buffer: " + bufferStartIndex + ".." + (bufferStartIndex + n - 1)); } // convert from absolute to local index int i = interval.a - bufferStartIndex; return new string(data, i, interval.Length); }
public virtual string GetText(Interval interval) { int bufferStartIndex = GetBufferStartIndex(); int bufferStopIndex = bufferStartIndex + tokens.Length - 1; int start = interval.a; int stop = interval.b; if (start < bufferStartIndex || stop > bufferStopIndex) { throw new NotSupportedException("interval " + interval + " not in token buffer window: " + bufferStartIndex + ".." + bufferStopIndex); } int a = start - bufferStartIndex; int b = stop - bufferStartIndex; StringBuilder buf = new StringBuilder(); for (int i = a; i <= b; i++) { IToken t = tokens[i]; buf.Append(t.Text); } return buf.ToString(); }
public virtual string GetText(Interval interval) { int start = interval.a; int stop = interval.b; if (stop >= n) { stop = n - 1; } int count = stop - start + 1; if (start >= n) { return string.Empty; } // System.err.println("data: "+Arrays.toString(data)+", n="+n+ // ", start="+start+ // ", stop="+stop); return new string(data, start, count); }
public virtual string GetText(Interval interval) { int start = interval.a; int stop = interval.b; if (stop >= n) { stop = n - 1; } int count = stop - start + 1; if (start >= n) { return string.Empty; } return data.TextSegment(start, stop); }
// copy on write so we can cache a..a intervals and sets of that protected internal virtual void Add(Interval addition) { if (@readonly) { throw new InvalidOperationException("can't alter readonly IntervalSet"); } //System.out.println("add "+addition+" to "+intervals.toString()); if (addition.b < addition.a) { return; } // find position in list // Use iterators as we modify list in place for (int i = 0; i < intervals.Count; i++) { Interval r = intervals[i]; if (addition.Equals(r)) { return; } if (addition.Adjacent(r) || !addition.Disjoint(r)) { // next to each other, make a single larger interval Interval bigger = addition.Union(r); intervals[i] = bigger; // make sure we didn't just create an interval that // should be merged with next interval in list while (i < intervals.Count - 1) { i++; Interval next = intervals[i]; if (!bigger.Adjacent(next) && bigger.Disjoint(next)) { break; } // if we bump up against or overlap next, merge intervals.RemoveAt(i); // remove this one i--; // move backwards to what we just set intervals[i] = bigger.Union(next); // set to 3 merged ones } // first call to next after previous duplicates the result return; } if (addition.StartsBeforeDisjoint(r)) { // insert before r intervals.Insert(i, addition); return; } } // if disjoint and after r, a future iteration will handle it // ok, must be after last interval (and disjoint from last interval) // just add it intervals.Add(addition); }
public String GetText(Interval interval) { return _source.GetText(interval); }
public static Antlr4.Runtime.Misc.IntervalSet Subtract(Antlr4.Runtime.Misc.IntervalSet left, Antlr4.Runtime.Misc.IntervalSet right) { if (left == null || left.IsNil) { return new Antlr4.Runtime.Misc.IntervalSet(); } Antlr4.Runtime.Misc.IntervalSet result = new Antlr4.Runtime.Misc.IntervalSet(left); if (right == null || right.IsNil) { // right set has no elements; just return the copy of the current set return result; } int resultI = 0; int rightI = 0; while (resultI < result.intervals.Count && rightI < right.intervals.Count) { Interval resultInterval = result.intervals[resultI]; Interval rightInterval = right.intervals[rightI]; // operation: (resultInterval - rightInterval) and update indexes if (rightInterval.b < resultInterval.a) { rightI++; continue; } if (rightInterval.a > resultInterval.b) { resultI++; continue; } Interval? beforeCurrent = null; Interval? afterCurrent = null; if (rightInterval.a > resultInterval.a) { beforeCurrent = new Interval(resultInterval.a, rightInterval.a - 1); } if (rightInterval.b < resultInterval.b) { afterCurrent = new Interval(rightInterval.b + 1, resultInterval.b); } if (beforeCurrent != null) { if (afterCurrent != null) { // split the current interval into two result.intervals[resultI] = beforeCurrent.Value; result.intervals.Insert(resultI + 1, afterCurrent.Value); resultI++; rightI++; continue; } else { // replace the current interval result.intervals[resultI] = beforeCurrent.Value; resultI++; continue; } } else { if (afterCurrent != null) { // replace the current interval result.intervals[resultI] = afterCurrent.Value; rightI++; continue; } else { // remove the current interval (thus no need to increment resultI) result.intervals.RemoveAt(resultI); continue; } } } // If rightI reached right.intervals.size(), no more intervals to subtract from result. // If resultI reached result.intervals.size(), we would be subtracting from an empty set. // Either way, we are done. return result; }
public virtual string GetText(Interval interval) { int start = interval.a; int stop = interval.b; if (start < 0 || stop < 0) { return string.Empty; } LazyInit(); if (stop >= tokens.Count) { stop = tokens.Count - 1; } StringBuilder buf = new StringBuilder(); for (int i = start; i <= stop; i++) { IToken t = tokens[i]; if (t.Type == TokenConstants.EOF) { break; } buf.Append(t.Text); } return buf.ToString(); }
public string GetText(Interval interval) { return Substring(interval.a, interval.Length); }
/// <summary>Return a new set with the intersection of this set with other.</summary> /// <remarks> /// Return a new set with the intersection of this set with other. Because /// the intervals are sorted, we can use an iterator for each list and /// just walk them together. This is roughly O(min(n,m)) for interval /// list lengths n and m. /// </remarks> public virtual Antlr4.Runtime.Misc.IntervalSet And(IIntSet other) { if (other == null) { //|| !(other instanceof IntervalSet) ) { return(null); } // nothing in common with null set IList <Interval> myIntervals = this.intervals; IList <Interval> theirIntervals = ((Antlr4.Runtime.Misc.IntervalSet)other).intervals; Antlr4.Runtime.Misc.IntervalSet intersection = null; int mySize = myIntervals.Count; int theirSize = theirIntervals.Count; int i = 0; int j = 0; // iterate down both interval lists looking for nondisjoint intervals while (i < mySize && j < theirSize) { Interval mine = myIntervals[i]; Interval theirs = theirIntervals[j]; //System.out.println("mine="+mine+" and theirs="+theirs); if (mine.StartsBeforeDisjoint(theirs)) { // move this iterator looking for interval that might overlap i++; } else { if (theirs.StartsBeforeDisjoint(mine)) { // move other iterator looking for interval that might overlap j++; } else { if (mine.ProperlyContains(theirs)) { // overlap, add intersection, get next theirs if (intersection == null) { intersection = new Antlr4.Runtime.Misc.IntervalSet(); } intersection.Add(mine.Intersection(theirs)); j++; } else { if (theirs.ProperlyContains(mine)) { // overlap, add intersection, get next mine if (intersection == null) { intersection = new Antlr4.Runtime.Misc.IntervalSet(); } intersection.Add(mine.Intersection(theirs)); i++; } else { if (!mine.Disjoint(theirs)) { // overlap, add intersection if (intersection == null) { intersection = new Antlr4.Runtime.Misc.IntervalSet(); } intersection.Add(mine.Intersection(theirs)); // Move the iterator of lower range [a..b], but not // the upper range as it may contain elements that will collide // with the next iterator. So, if mine=[0..115] and // theirs=[115..200], then intersection is 115 and move mine // but not theirs as theirs may collide with the next range // in thisIter. // move both iterators to next ranges if (mine.StartsAfterNonDisjoint(theirs)) { j++; } else { if (theirs.StartsAfterNonDisjoint(mine)) { i++; } } } } } } } } if (intersection == null) { return(new Antlr4.Runtime.Misc.IntervalSet()); } return(intersection); }
/// <summary>Add interval; i.e., add all integers from a to b to set.</summary> /// <remarks> /// Add interval; i.e., add all integers from a to b to set. /// If b<a, do nothing. /// Keep list in sorted order (by left range value). /// If overlap, combine ranges. For example, /// If this is {1..5, 10..20}, adding 6..7 yields /// {1..5, 6..7, 10..20}. Adding 4..8 yields {1..8, 10..20}. /// </remarks> public virtual void Add(int a, int b) { Add(Interval.Of(a, b)); }