{"record":{"id":"52e50ccfb4a02216","repo":"antlr/antlr4","slug":"word-at-index-startindex-isn-t-a-valid-token","errorCode":null,"errorMessage":"${word} at index ${startIndex} isn't a valid token name","messagePattern":"(.+?) at index (.+?) isn't a valid token name","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/Tree/Xpath/XPath.cs","lineNumber":221,"sourceCode":"            {\n                throw new ArgumentException(\"Missing path element at end of path\");\n            }\n            string word = wordToken.Text;\n            int ttype = parser.GetTokenType(word);\n            int ruleIndex = parser.GetRuleIndex(word);\n            switch (wordToken.Type)\n            {\n                case XPathLexer.Wildcard:\n                {\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);","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/Tree/Xpath/XPath.cs#L203-L239","documentation":"A word starting with an uppercase character is lexed as a TokenRef, and quoted strings are also treated as token references. Before building the XPath element, the runtime asks the supplied Parser for the token type; TokenConstants.InvalidType means that grammar has no token with that name. Therefore the reference is a syntactically valid token name but is unknown to the target parser.","triggerScenarios":"Calling XPath with \"/ID\" when the grammar defines no ID token; using a token renamed between grammar versions; passing a parser generated from a different grammar; or using an uppercase rule name, which ANTLR interprets as a token reference.","commonSituations":"Misspelled token names; stale generated parser code after editing a grammar; mixed grammars in one solution; or confusion about the convention that token references start uppercase while rule references start lowercase.","solutions":["Check the generated parser/token vocabulary and correct the token name.","If the name is a parser rule, make its first character lowercase so it is treated as a RuleRef.","Regenerate the parser from the same grammar version used by the runtime before constructing XPath.","Verify parser.GetTokenType(word) does not return TokenConstants.InvalidType before use."],"exampleFix":"// before\nvar xpath = new Antlr4.Runtime.Tree.Xpath.XPath(parser, \"/Ident\");\n\n// after\nvar xpath = new Antlr4.Runtime.Tree.Xpath.XPath(parser, \"/ident\");\n// Or use the actual token name, e.g. \"/IDENT\".","handlingStrategy":"validation","validationCode":"static bool IsKnownTokenName(Parser parser, string word) {\n    return char.IsUpper(word[0]) &&\n           parser.GetTokenType(word) != TokenConstants.InvalidType;\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 token name\")) { /* report unknown token and regenerate parser */ }","preventionTips":["Derive token names from generated vocabulary, not hand-typed strings.","Regenerate parser sources whenever token names change.","Remember uppercase-initial words are token references in ANTLR XPath."],"tags":["antlr","csharp","xpath","tokens","code-generation"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}