{"record":{"id":"80c0cd6cfadc15c3","repo":"antlr/antlr4","slug":"tokenindex-not-in-0-tokens-count-1","errorCode":null,"errorMessage":"{tokenIndex} not in 0..{tokens.Count - 1}","messagePattern":"(.+?) not in 0\\.\\.(.+?)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/BufferedTokenStream.cs","lineNumber":524,"sourceCode":"        }\n\n        /// <summary>\n        /// Collect all tokens on specified channel to the right of\n        /// the current token up until we see a token on\n        /// <see cref=\"Lexer.DefaultTokenChannel\"/>\n        /// or\n        /// EOF. If\n        /// <paramref name=\"channel\"/>\n        /// is\n        /// <c>-1</c>\n        /// , find any non default channel token.\n        /// </summary>\n        public virtual IList<IToken> GetHiddenTokensToRight(int tokenIndex, int channel)\n        {\n            LazyInit();\n            if (tokenIndex < 0 || tokenIndex >= tokens.Count)\n            {\n                throw new ArgumentOutOfRangeException(tokenIndex + \" not in 0..\" + (tokens.Count - 1));\n            }\n            int nextOnChannel = NextTokenOnChannel(tokenIndex + 1, Lexer.DefaultTokenChannel);\n            int to;\n            int from = tokenIndex + 1;\n            // if none onchannel to right, nextOnChannel=-1 so set to = last token\n            if (nextOnChannel == -1)\n            {\n                to = Size - 1;\n            }\n            else\n            {\n                to = nextOnChannel;\n            }\n            return FilterForChannel(from, to, channel);\n        }\n\n        /// <summary>\n        /// Collect all hidden tokens (any off-default channel) to the right of","sourceCodeStart":506,"sourceCodeEnd":542,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/BufferedTokenStream.cs#L506-L542","documentation":"GetHiddenTokensToRight(tokenIndex, channel) returns the hidden-channel tokens following tokenIndex on the default channel; tokenIndex itself must address a real token in the buffered list (0 <= tokenIndex < tokens.Count). Because the method needs the surrounding on-channel context, an out-of-range anchor index is rejected immediately after LazyInit().","triggerScenarios":"Calling GetHiddenTokensToRight with a negative index, an index >= tokens.Count (e.g. tokenIndex+1 computed by the caller, or an index from a different stream), or passing a raw character offset instead of a token index.","commonSituations":"Whitespace/comment handling code that iterates tokens and probes neighbours past the last token; mixing editor character offsets with token indices; reusing indices captured before a stream reset.","solutions":["Validate 0 <= tokenIndex < stream.Size before the call","When probing neighbours, special-case the last token instead of calling with tokenIndex + 1","Capture indices from tokens of the same stream immediately before use, not across resets"],"exampleFix":"// before\nvar hidden = stream.GetHiddenTokensToRight(i + 1, Lexer.Hidden); // i+1 may equal Size\n\n// after\nvar hidden = i + 1 < stream.Size ? stream.GetHiddenTokensToRight(i + 1, Lexer.Hidden) : null;","handlingStrategy":"validation","validationCode":"IList<IToken> hidden = tokenIndex >= 0 && tokenIndex < stream.Size\n    ? stream.GetHiddenTokensToRight(tokenIndex, channel)\n    : null;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Bounds-check every neighbour probe (tokenIndex + 1) against stream.Size","Never pass character offsets where token indices are expected"],"tags":["antlr","csharp","token-stream","hidden-tokens","index-out-of-range"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}