{"record":{"id":"83084dbf56cdabc7","repo":"antlr/antlr4","slug":"interval-interval-outside-buffer-bufferstart","errorCode":null,"errorMessage":"interval ${interval} outside buffer: ${bufferStartIndex}..${bufferStartIndex+n-1}","messagePattern":"interval (.+?) outside buffer: (.+?)\\.\\.(.+?)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/UnbufferedCharStream.cs","lineNumber":442,"sourceCode":"        }\n\n        public virtual string GetText(Interval interval)\n        {\n            if (interval.a < 0 || interval.b < interval.a - 1)\n            {\n                throw new ArgumentException(\"invalid interval\");\n            }\n            int bufferStartIndex = BufferStartIndex;\n            if (n > 0 && data[n - 1] == IntStreamConstants.EOF)\n            {\n                if (interval.a + interval.Length > bufferStartIndex + n)\n                {\n                    throw new ArgumentException(\"the interval extends past the end of the stream\");\n                }\n            }\n            if (interval.a < bufferStartIndex || interval.b >= bufferStartIndex + n)\n            {\n                throw new NotSupportedException(\"interval \" + interval + \" outside buffer: \" + bufferStartIndex + \"..\" + (bufferStartIndex + n - 1));\n            }\n            // convert from absolute to local index\n            int i = interval.a - bufferStartIndex;\n            // build a UTF-16 string from the Unicode code points in data\n            var sb = new StringBuilder(interval.Length);\n            for (int offset = 0; offset < interval.Length; offset++) {\n                sb.Append(Char.ConvertFromUtf32(data[i + offset]));\n            }\n            return sb.ToString();\n        }\n\n        protected internal int BufferStartIndex\n        {\n            get\n            {\n                return currentCharIndex - p;\n            }\n        }","sourceCodeStart":424,"sourceCodeEnd":460,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/UnbufferedCharStream.cs#L424-L460","documentation":"Even a structurally valid interval must lie entirely inside UnbufferedCharStream's current rolling buffer. If its start precedes the buffer or its stop lies beyond buffered/filled data, GetText cannot reconstruct that text and throws NotSupportedException. This is the fundamental random-access limitation of an unbuffered stream.","triggerScenarios":"Calling GetText for characters already consumed and discarded; requesting text far ahead before synchronization; extracting source text from old tokens late during a streaming parse.","commonSituations":"Walking a parse tree and calling GetText() after the parser has advanced; token rewriters or diagnostics that reference old intervals; migration from BufferedTokenStream/AntlrInputStream to unbuffered streaming without redesign.","solutions":["Extract text while the interval is still buffered, before consuming beyond it.","Hold a Mark() across any interval that will be needed later.","Use AntlrInputStream or BufferedTokenStream when arbitrary delayed text extraction is required."],"exampleFix":"// before\nvar chars = new UnbufferedCharStream(reader);\n// ... consume far past interval ...\nstring text = chars.GetText(interval);\n\n// after\nvar chars = new AntlrInputStream(reader); // preserves all characters\nstring text = chars.GetText(interval);","handlingStrategy":"type-guard","validationCode":"if (stream is UnbufferedCharStream) {\n    // extract text now or hold a Mark(); otherwise use AntlrInputStream\n}","typeGuard":"static bool SupportsArbitraryGetText(ICharStream stream) => stream is not UnbufferedCharStream;","tryCatchPattern":"try { text = chars.GetText(interval); }\ncatch (NotSupportedException ex) when (ex.Message.StartsWith(\"interval \") && ex.Message.Contains(\"outside buffer\")) { /* switch to buffered input */ }","preventionTips":["Extract text before consuming past the interval.","Keep markers alive for intervals needed later.","Use buffered streams for delayed source-text extraction."],"tags":["antlr","csharp","char-stream","intervals","streaming"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}