{"record":{"id":"c1fcc98f9841c5b0","repo":"antlr/antlr4","slug":"replace-range-invalid-from-to-size-size-c1fcc9","errorCode":null,"errorMessage":"replace: range invalid: {from}..{to}(size={size})","messagePattern":"replace: range invalid: (.+?)\\.\\.(.+?)\\(size=(.+?)\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/TokenStreamRewriter.cs","lineNumber":341,"sourceCode":"        {\n            Replace(DefaultProgramName, from, to, text);\n        }\n\n        public virtual void Replace(IToken indexT, object text)\n        {\n            Replace(DefaultProgramName, indexT, indexT, text);\n        }\n\n        public virtual void Replace(IToken from, IToken to, object text)\n        {\n            Replace(DefaultProgramName, from, to, text);\n        }\n\n        public virtual void Replace(string programName, int from, int to, object text)\n        {\n            if (from > to || from < 0 || to < 0 || to >= tokens.Size)\n            {\n                throw new ArgumentException(\"replace: range invalid: \" + from + \"..\" + to + \"(size=\" + tokens.Size + \")\");\n            }\n            TokenStreamRewriter.RewriteOperation op = new TokenStreamRewriter.ReplaceOp(tokens, from, to, text);\n            IList<TokenStreamRewriter.RewriteOperation> rewrites = GetProgram(programName);\n            op.instructionIndex = rewrites.Count;\n            rewrites.Add(op);\n        }\n\n        public virtual void Replace(string programName, IToken from, IToken to, object text)\n        {\n            Replace(programName, from.TokenIndex, to.TokenIndex, text);\n        }\n\n        public virtual void Delete(int index)\n        {\n            Delete(DefaultProgramName, index, index);\n        }\n\n        public virtual void Delete(int from, int to)","sourceCodeStart":323,"sourceCodeEnd":359,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/TokenStreamRewriter.cs#L323-L359","documentation":"Error \"replace: range invalid: {from}..{to}(size={size})\" thrown in antlr/antlr4.","triggerScenarios":"TokenStreamRewriter.Replace called with from/to indices outside 0..tokens.Count-1.","commonSituations":"Validate from and to against the token stream size before Replace; remember stream is 0-based and to is inclusive.","solutions":["Ensure the replace range [from..to] lies within 0..tokens.Count-1 and from <= to before calling Replace.","Validate indices against the token stream size before rewriting."],"exampleFix":"if (from >= 0 && to < tokens.Size && from <= to)\n    rewriter.Replace(from, to, \"newText\");","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}