{"record":{"id":"3334cf9a06290117","repo":"antlr/antlr4","slug":"not-in-0","errorCode":null,"errorMessage":"{} not in 0..{}","messagePattern":"(.+?) not in 0\\.\\.(.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java","lineNumber":368,"sourceCode":"\t\t\tToken token = tokens.get(i);\n\t\t\tif (token.getType() == Token.EOF || token.getChannel() == channel) {\n\t\t\t\treturn i;\n\t\t\t}\n\n\t\t\ti--;\n\t\t}\n\n\t\treturn i;\n\t}\n\n\t/** Collect all tokens on specified channel to the right of\n\t *  the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or\n\t *  EOF. If channel is -1, find any non default channel token.\n\t */\n\tpublic List<Token> getHiddenTokensToRight(int tokenIndex, int channel) {\n\t\tlazyInit();\n\t\tif ( tokenIndex<0 || tokenIndex>=tokens.size() ) {\n\t\t\tthrow new IndexOutOfBoundsException(tokenIndex+\" not in 0..\"+(tokens.size()-1));\n\t\t}\n\n\t\tint nextOnChannel =\n\t\t\tnextTokenOnChannel(tokenIndex + 1, Lexer.DEFAULT_TOKEN_CHANNEL);\n\t\tint to;\n\t\tint from = tokenIndex+1;\n\t\t// if none onchannel to right, nextOnChannel=-1 so set to = last token\n\t\tif ( nextOnChannel == -1 ) to = size()-1;\n\t\telse to = nextOnChannel;\n\n\t\treturn filterForChannel(from, to, channel);\n\t}\n\n\t/** Collect all hidden tokens (any off-default channel) to the right of\n\t *  the current token up until we see a token on DEFAULT_TOKEN_CHANNEL\n\t *  or EOF.\n\t */\n\tpublic List<Token> getHiddenTokensToRight(int tokenIndex) {","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java#L350-L386","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate 0 <= tokenIndex < tokens.size() before the call","Derive the index from a real token: token.getTokenIndex() on a token from this same stream","Call tokens.fill() first so the buffer is complete when scanning near the end of input"],"exampleFix":"// before\nList<Token> hidden = tokens.getHiddenTokensToRight(idx); // idx == size() -> throws\n\n// after\nif (idx >= 0 && idx < tokens.size()) {\n  List<Token> hidden = tokens.getHiddenTokensToRight(idx);\n}","handlingStrategy":"validation","validationCode":"if (tokenIndex >= 0 && tokenIndex < tokens.size()) {\n  List<Token> hidden = tokens.getHiddenTokensToRight(tokenIndex);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate 0 <= index < size() before any hidden-token query","Use token.getTokenIndex() from tokens of the same stream","Treat null returns (no hidden tokens) as normal, and out-of-range as a caller bug"],"tags":["antlr","token-stream","hidden-tokens","index-out-of-bounds"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}