antlr/antlr4 · error · ArgumentException

invalid interval

Error message

invalid interval

What it means

GetText(Interval) first validates the interval itself. The start must be non-negative and the stop must not precede the start by more than one position; an empty interval (b == a-1) is allowed, but a negative start or reversed range is not. This catches malformed intervals before buffer bounds are considered.

Source

Thrown at runtime/CSharp/src/UnbufferedCharStream.cs:430

        }

        public virtual string SourceName
        {
            get
            {
                if (string.IsNullOrEmpty(name))
                {
                    return IntStreamConstants.UnknownSourceName;
                }
                return name;
            }
        }

        public virtual string GetText(Interval interval)
        {
            if (interval.a < 0 || interval.b < interval.a - 1)
            {
                throw new ArgumentException("invalid interval");
            }
            int bufferStartIndex = BufferStartIndex;
            if (n > 0 && data[n - 1] == IntStreamConstants.EOF)
            {
                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;
            // build a UTF-16 string from the Unicode code points in data
            var sb = new StringBuilder(interval.Length);
            for (int offset = 0; offset < interval.Length; offset++) {

View on GitHub (pinned to 7d5770395b)

Solutions

  1. Check interval.a >= 0 and interval.b >= interval.a - 1 before calling GetText.
  2. Skip text extraction for invalid/synthetic parse-tree nodes.
  3. Fix interval construction so stop is never before start-1.

Example fix

// before
string text = chars.GetText(Interval.Of(-1, 2));

// after
var interval = Interval.Of(-1, 2);
if (interval.a >= 0 && interval.b >= interval.a - 1)
    string text = chars.GetText(interval);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidInterval(Interval i) => i.a >= 0 && i.b >= i.a - 1;

if (IsValidInterval(interval)) text = chars.GetText(interval);

Try / catch

try { text = chars.GetText(interval); }
catch (ArgumentException ex) when (ex.Message == "invalid interval") { /* skip invalid/synthetic node text */ }

Prevention

When it happens

Trigger: Passing Interval.Of(-1, x); passing an interval produced for an invalid/empty source range whose a is negative; or constructing b < a-1 through incorrect token-index arithmetic.

Common situations: Using ParserRuleContext.SourceInterval from contexts without a real source interval; error nodes or synthetic nodes during recovery; custom interval math that subtracts one too many; or default/uninitialized Interval values.

Related errors


AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14). Data as JSON: /api/errors/4212577794371b6d. Report an issue: GitHub.