{"record":{"id":"406d065025fc7e0a","repo":"antlr/antlr4","slug":"word-at-index-startindex-isn-t-a-valid-rule","errorCode":null,"errorMessage":"${word} at index ${startIndex} isn't a valid rule name","messagePattern":"(.+?) at index (.+?) isn't a valid rule name","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/Tree/Xpath/XPath.cs","lineNumber":230,"sourceCode":"                {\n                    return anywhere ? new XPathWildcardAnywhereElement() : (XPathElement)new XPathWildcardElement();\n                }\n\n                case XPathLexer.TokenRef:\n                case XPathLexer.String:\n                {\n                    if (ttype == TokenConstants.InvalidType)\n                    {\n                        throw new ArgumentException(word + \" at index \" + wordToken.StartIndex + \" isn't a valid token name\");\n                    }\n                    return anywhere ? new XPathTokenAnywhereElement(word, ttype) : (XPathElement)new XPathTokenElement(word, ttype);\n                }\n\n                default:\n                {\n                    if (ruleIndex == -1)\n                    {\n                        throw new ArgumentException(word + \" at index \" + wordToken.StartIndex + \" isn't a valid rule name\");\n                    }\n                    return anywhere ? new XPathRuleAnywhereElement(word, ruleIndex) : (XPathElement)new XPathRuleElement(word, ruleIndex);\n                }\n            }\n        }\n\n        public static ICollection<IParseTree> FindAll(IParseTree tree, string xpath, Parser parser)\n        {\n            Antlr4.Runtime.Tree.Xpath.XPath p = new Antlr4.Runtime.Tree.Xpath.XPath(parser, xpath);\n            return p.Evaluate(tree);\n        }\n\n        /// <summary>\n        /// Return a list of all nodes starting at\n        /// <paramref name=\"t\"/>\n        /// as root that satisfy the\n        /// path. The root\n        /// <c>/</c>","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/Tree/Xpath/XPath.cs#L212-L248","documentation":"A word starting with a lowercase character is lexed as a RuleRef. The runtime asks the supplied Parser for its rule index; -1 means no rule with that name exists. The word is therefore a syntactically valid rule reference but is not defined by the target grammar.","triggerScenarios":"Calling XPath with \"/expr\" when the grammar has no expr rule; using a renamed or removed rule; passing a parser generated from different grammar sources; or lowercasing a token name so it is interpreted as a rule.","commonSituations":"Grammar refactoring changes rule names; stale generated parser files; examples copied from another grammar; or uncertainty about whether a vocabulary name is a token or a rule.","solutions":["Check parser.RuleNames and use the exact rule name from the generated parser.","If the name is a token, make its first character uppercase so it is treated as a TokenRef.","Regenerate the parser and lexer from the current grammar.","Verify parser.GetRuleIndex(word) is not -1 before constructing XPath."],"exampleFix":"// before\nvar xpath = new Antlr4.Runtime.Tree.Xpath.XPath(parser, \"/expression\");\n\n// after\nvar xpath = new Antlr4.Runtime.Tree.Xpath.XPath(parser, \"/expr\"); // exact generated rule name","handlingStrategy":"validation","validationCode":"static bool IsKnownRuleName(Parser parser, string word) {\n    return char.IsLower(word[0]) && parser.GetRuleIndex(word) != -1;\n}","typeGuard":null,"tryCatchPattern":"try { var p = new Antlr4.Runtime.Tree.Xpath.XPath(parser, xpath); }\ncatch (ArgumentException ex) when (ex.Message.EndsWith(\"isn't a valid rule name\")) { /* report unknown rule and check parser.RuleNames */ }","preventionTips":["Check parser.RuleNames before constructing XPath.","Lowercase-initial names identify rules; use exact generated spelling.","Keep grammar and generated parser versions synchronized."],"tags":["antlr","csharp","xpath","parser-rules","code-generation"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}