antlr/antlr4 · error · ArgumentException
the interval extends past the end of the stream
Error message
the interval extends past the end of the stream
What it means
If UnbufferedCharStream's buffer already ends with EOF, GetText knows the true end of input. It rejects intervals whose start plus length extends beyond that end. In short, the requested text range reaches past EOF.
Source
Thrown at runtime/CSharp/src/UnbufferedCharStream.cs:437
{
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++) {
sb.Append(Char.ConvertFromUtf32(data[i + offset]));
}
return sb.ToString();
}
protected internal int BufferStartIndex
{View on GitHub (pinned to 7d5770395b)
Solutions
- Clamp the stop index to the last valid character index before calling GetText.
- Use the exact start/stop character indexes from tokens.
- Avoid sentinel values such as int.MaxValue for ranges on unbuffered input.
Example fix
// before string text = chars.GetText(Interval.Of(start, int.MaxValue)); // after int last = chars.Index - 1; // valid once EOF has been read string text = last >= start ? chars.GetText(Interval.Of(start, last)) : string.Empty;
Defensive patterns
Strategy: validation
Validate before calling
int last = chars.Index - 1; // meaningful after EOF has been reached if (interval.a > last) text = string.Empty; else text = chars.GetText(Interval.Of(interval.a, Math.Min(interval.b, last)));
Try / catch
try { text = chars.GetText(interval); }
catch (ArgumentException ex) when (ex.Message == "the interval extends past the end of the stream") { /* clamp stop index and retry */ } Prevention
- Never use sentinel stop indexes such as int.MaxValue.
- Extract text while at or before EOF with accurate token indexes.
- Clamp requested ranges to the last valid character index.
When it happens
Trigger: Calling GetText with a stop index beyond EOF; passing an oversized interval such as Interval.Of(0, int.MaxValue); or using stale/overlarge token character indexes after recovery at EOF.
Common situations: Error-recovery tokens with invalid stop indexes; code that uses a sentinel end index; or generic range extraction copied from a fully buffered implementation.
Related errors
- cannot consume EOF
- invalid interval
- interval ${interval} outside buffer: ${bufferStartIndex}..${
- cannot consume EOF
- cannot consume EOF
AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14).
Data as JSON: /api/errors/c0f1f82a40205876.
Report an issue: GitHub.