antlr/antlr4 · error · NotSupportedException

The specified start and stop symbols are not supported.

Error message

The specified start and stop symbols are not supported.

What it means

UnbufferedTokenStream.GetText(IToken start, IToken stop) supports only a concrete start and stop token pair; if either argument is null it cannot form a token-index interval and throws NotSupportedException. This is stricter than BufferedTokenStream, which returns an empty string for null arguments.

Source

Thrown at runtime/CSharp/src/UnbufferedTokenStream.cs:183

        public virtual string GetText()
        {
            return string.Empty;
        }

        [return: NotNull]
        public virtual string GetText(RuleContext ctx)
        {
            return GetText(ctx.SourceInterval);
        }

        [return: NotNull]
        public virtual string GetText(IToken start, IToken stop)
        {
            if (start != null && stop != null)
            {
                return GetText(Interval.Of(start.TokenIndex, stop.TokenIndex));
            }
            throw new NotSupportedException("The specified start and stop symbols are not supported.");
        }

        public virtual void Consume()
        {
            if (LA(1) == TokenConstants.EOF)
            {
                throw new InvalidOperationException("cannot consume EOF");
            }
            // buf always has at least tokens[p==0] in this method due to ctor
            lastToken = tokens[p];
            // track last token for LT(-1)
            // if we're at last token and no markers, opportunity to flush buffer
            if (p == n - 1 && numMarkers == 0)
            {
                n = 0;
                p = -1;
                // p++ will leave this at 0
                lastTokenBufferStart = lastToken;

View on GitHub (pinned to 7d5770395b)

Solutions

  1. Pass non-null IToken instances for both start and stop.
  2. Use GetText() with no arguments for the stream's text where appropriate.
  3. If null means all text, substitute explicit first/last tokens or switch to BufferedTokenStream.

Example fix

// before
string all = tokens.GetText(null, null);

// after
string all = tokens.GetText();
Defensive patterns

Strategy: validation

Validate before calling

if (start == null || stop == null)
    text = tokens.GetText();
else
    text = tokens.GetText(start, stop);

Try / catch

try { text = tokens.GetText(start, stop); }
catch (NotSupportedException ex) when (ex.Message == "The specified start and stop symbols are not supported.") { /* supply non-null tokens or use GetText() */ }

Prevention

When it happens

Trigger: Calling GetText(null, null) expecting all text; passing null for either endpoint when a token was unavailable; or porting BufferedTokenStream call sites directly to UnbufferedTokenStream.

Common situations: Optional start/stop tokens from error handling; code that uses null to mean beginning/end of stream; runtime migration from buffered to unbuffered token streams.

Related errors


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