{"record":{"id":"31704a5735673615","repo":"antlr/antlr4","slug":"invalid-state-number-31704a","errorCode":null,"errorMessage":"Invalid state number.","messagePattern":"Invalid state number\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/Atn/ATN.cs","lineNumber":234,"sourceCode":"        /// .</p>\n        /// </summary>\n        /// <param name=\"stateNumber\">the ATN state number</param>\n        /// <param name=\"context\">the full parse context</param>\n        /// <returns>\n        /// The set of potentially valid input symbols which could follow the\n        /// specified state in the specified context.\n        /// </returns>\n        /// <exception cref=\"System.ArgumentException\">\n        /// if the ATN does not contain a state with\n        /// number\n        /// <paramref name=\"stateNumber\"/>\n        /// </exception>\n        [return: NotNull]\n        public virtual IntervalSet GetExpectedTokens(int stateNumber, RuleContext context)\n        {\n            if (stateNumber < 0 || stateNumber >= states.Count)\n            {\n                throw new ArgumentException(\"Invalid state number.\");\n            }\n            RuleContext ctx = context;\n            ATNState s = states[stateNumber];\n            IntervalSet following = NextTokens(s);\n            if (!following.Contains(TokenConstants.EPSILON))\n            {\n                return following;\n            }\n            IntervalSet expected = new IntervalSet();\n            expected.AddAll(following);\n            expected.Remove(TokenConstants.EPSILON);\n            while (ctx != null && ctx.invokingState >= 0 && following.Contains(TokenConstants.EPSILON))\n            {\n                ATNState invokingState = states[ctx.invokingState];\n                RuleTransition rt = (RuleTransition)invokingState.Transition(0);\n                following = NextTokens(rt.followState);\n                expected.AddAll(following);\n                expected.Remove(TokenConstants.EPSILON);","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/Atn/ATN.cs#L216-L252","documentation":"ATN.GetExpectedTokens(stateNumber, context) computes the set of tokens acceptable in a given ATN state. It throws ArgumentException when stateNumber is outside [0, states.Count), because there is no such state to consult. State numbers come from ATNState.stateNumber assigned during ATN construction/deserialization.","triggerScenarios":"Calling atn.GetExpectedTokens(n, ctx) with n < 0 or n >= atn.states.Count; commonly indirectly via Parser.GetExpectedTokens / error listeners when a DFA/ATN cache or serialized ATN is inconsistent with the runtime version.","commonSituations":"Mixing a C# runtime version with serialized ATN data or generated code from a different version, hand-manipulating ATN state numbers, or stale generated parsers after upgrading the runtime NuGet package.","solutions":["Validate 0 <= stateNumber < atn.states.Count before calling","Regenerate parsers with the same ANTLR version as the runtime package and clean bin/obj","If it surfaces from deep inside the runtime, check for mismatched Antlr4.Runtime versions across projects in the solution"],"exampleFix":"// before\nvar expected = atn.GetExpectedTokens(stateNumber, ctx);\n\n// after\nvar expected = (stateNumber >= 0 && stateNumber < atn.states.Count)\n    ? atn.GetExpectedTokens(stateNumber, ctx)\n    : new IntervalSet();","handlingStrategy":"validation","validationCode":"bool valid = stateNumber >= 0 && stateNumber < atn.states.Count;","typeGuard":"static bool IsValidStateNumber(ATN atn, int n) => n >= 0 && n < atn.states.Count;","tryCatchPattern":"try { atn.GetExpectedTokens(n, ctx); } catch (ArgumentException) { /* state number out of range; re-sync parser/ATN */ }","preventionTips":["Never hand-pick ATN state numbers; carry them from ATNState.stateNumber","Regenerate parsers and clean bin/obj after runtime upgrades"],"tags":["antlr","atn","csharp","validation"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}