antlr/antlr4 · error · IndexOutOfBoundsException

{} not in 0..{}

Error message

{} not in 0..{}

What it means

getHiddenTokensToRight(int tokenIndex, int channel) throws IndexOutOfBoundsException when tokenIndex is outside 0..size()-1. It inspects off-channel tokens (comments, whitespace on hidden channels) to the right of the given index, and only valid on-stream indices may anchor that scan. The one-argument overload delegates here with channel -1 (any non-default channel).

Source

Thrown at runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java:368

			Token token = tokens.get(i);
			if (token.getType() == Token.EOF || token.getChannel() == channel) {
				return i;
			}

			i--;
		}

		return i;
	}

	/** Collect all tokens on specified channel to the right of
	 *  the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or
	 *  EOF. If channel is -1, find any non default channel token.
	 */
	public List<Token> getHiddenTokensToRight(int tokenIndex, int channel) {
		lazyInit();
		if ( tokenIndex<0 || tokenIndex>=tokens.size() ) {
			throw new IndexOutOfBoundsException(tokenIndex+" not in 0.."+(tokens.size()-1));
		}

		int nextOnChannel =
			nextTokenOnChannel(tokenIndex + 1, Lexer.DEFAULT_TOKEN_CHANNEL);
		int to;
		int from = tokenIndex+1;
		// if none onchannel to right, nextOnChannel=-1 so set to = last token
		if ( nextOnChannel == -1 ) to = size()-1;
		else to = nextOnChannel;

		return filterForChannel(from, to, channel);
	}

	/** Collect all hidden tokens (any off-default channel) to the right of
	 *  the current token up until we see a token on DEFAULT_TOKEN_CHANNEL
	 *  or EOF.
	 */
	public List<Token> getHiddenTokensToRight(int tokenIndex) {

View on GitHub (pinned to 7d5770395b)

Solutions

  1. Validate 0 <= tokenIndex < tokens.size() before the call
  2. Derive the index from a real token: token.getTokenIndex() on a token from this same stream
  3. Call tokens.fill() first so the buffer is complete when scanning near the end of input

Example fix

// before
List<Token> hidden = tokens.getHiddenTokensToRight(idx); // idx == size() -> throws

// after
if (idx >= 0 && idx < tokens.size()) {
  List<Token> hidden = tokens.getHiddenTokensToRight(idx);
}
Defensive patterns

Strategy: validation

Validate before calling

if (tokenIndex >= 0 && tokenIndex < tokens.size()) {
  List<Token> hidden = tokens.getHiddenTokensToRight(tokenIndex);
}

Prevention

When it happens

Trigger: Calling getHiddenTokensToRight(tokens.size(), ...) (the EOF index is size()-1 after fill, but before fill the buffer is smaller); passing a token index taken from a different stream; passing -1 as a sentinel.

Common situations: Comment-attachment logic in listeners/visitors that grabs hidden tokens around a statement; reformatting or doc-extraction tools; off-by-one after using size() instead of size()-1.

Related errors


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