{"record":{"id":"23fd314d5211e0f6","repo":"antlr/antlr4","slug":"start-start-or-stop-stop-not-in-0-tokens-cou","errorCode":null,"errorMessage":"start {start} or stop {stop} not in 0..{tokens.Count - 1}","messagePattern":"start (.+?) or stop (.+?) not in 0\\.\\.(.+?)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/BufferedTokenStream.cs","lineNumber":403,"sourceCode":"        }\n\n        /// <summary>\n        /// Given a start and stop index, return a\n        /// <c>List</c>\n        /// of all tokens in\n        /// the token type\n        /// <c>BitSet</c>\n        /// .  Return\n        /// <see langword=\"null\"/>\n        /// if no tokens were found.  This\n        /// method looks at both on and off channel tokens.\n        /// </summary>\n        public virtual IList<IToken> GetTokens(int start, int stop, BitSet types)\n        {\n            LazyInit();\n            if (start < 0 || stop >= tokens.Count || stop < 0 || start >= tokens.Count)\n            {\n                throw new ArgumentOutOfRangeException(\"start \" + start + \" or stop \" + stop + \" not in 0..\" + (tokens.Count - 1));\n            }\n            if (start > stop)\n            {\n                return null;\n            }\n            // list = tokens[start:stop]:{T t, t.getType() in types}\n            IList<IToken> filteredTokens = new List<IToken>();\n            for (int i = start; i <= stop; i++)\n            {\n                IToken t = tokens[i];\n                if (types == null || types.Get(t.Type))\n                {\n                    filteredTokens.Add(t);\n                }\n            }\n            if (filteredTokens.Count == 0)\n            {\n                filteredTokens = null;","sourceCodeStart":385,"sourceCodeEnd":421,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/BufferedTokenStream.cs#L385-L421","documentation":"GetTokens(start, stop, types) filters tokens in the inclusive range start..stop and requires both endpoints to be within the currently buffered list (0 <= start, stop < tokens.Count). Out-of-range endpoints indicate the caller computed the range from stale or external positions. Note the quirk that start > stop returns null rather than throwing — only out-of-bounds bounds throw.","triggerScenarios":"Calling GetTokens with start or stop beyond the buffered tokens, e.g. stop taken from a token of a different (unbuffered or re-created) stream, negative indices, or calling before LazyInit/Size has fetched enough tokens (LazyInit runs first, but the buffer only extends to what has been consumed).","commonSituations":"Syntax-error highlighters asking for GetTokens(badToken.TokenIndex - 5, badToken.TokenIndex + 5) near the end of input; token-stream rewriting code that mixes indices from a before/after stream; IDE integrations computing ranges from editor positions instead of token indices.","solutions":["Clamp both endpoints to [0, stream.Size - 1] before calling GetTokens","Always derive start/stop from TokenIndex values of tokens obtained from the same stream instance","Handle the null return for start > stop explicitly rather than expecting an empty list"],"exampleFix":"// before\nIList<IToken> ctx = stream.GetTokens(errOffendingToken.TokenIndex - 5, errOffendingToken.TokenIndex + 5, null); // throws near EOF\n\n// after\nint start = Math.Max(0, errToken.TokenIndex - 5);\nint stop = Math.Min(errToken.TokenIndex + 5, stream.Size - 1);\nIList<IToken> ctx = start <= stop ? stream.GetTokens(start, stop, null) : null;","handlingStrategy":"validation","validationCode":"int start = Math.Max(0, requestedStart);\nint stop = Math.Min(requestedStop, stream.Size - 1);\nIList<IToken> tokens = start <= stop ? stream.GetTokens(start, stop, types) : null;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compute token ranges only from TokenIndex values of tokens from the same stream","Remember start > stop returns null rather than throwing — branch on it explicitly"],"tags":["antlr","csharp","token-stream","index-out-of-range"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}