{"record":{"id":"3c40731ce47c1925","repo":"antlr/antlr4","slug":"word-at-index-startindex-isn-t-a-valid-token-n","errorCode":null,"errorMessage":"{word} at index {startIndex} isn't a valid token name","messagePattern":"(.+?) at index (.+?) isn't a valid token name","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/Java/src/org/antlr/v4/runtime/tree/xpath/XPath.java","lineNumber":172,"sourceCode":"\t * element. {@code anywhere} is {@code true} if {@code //} precedes the\n\t * word.\n\t */\n\tprotected XPathElement getXPathElement(Token wordToken, boolean anywhere) {\n\t\tif ( wordToken.getType()==Token.EOF ) {\n\t\t\tthrow new IllegalArgumentException(\"Missing path element at end of path\");\n\t\t}\n\t\tString word = wordToken.getText();\n\t\tint ttype = parser.getTokenType(word);\n\t\tint ruleIndex = parser.getRuleIndex(word);\n\t\tswitch ( wordToken.getType() ) {\n\t\t\tcase XPathLexer.WILDCARD :\n\t\t\t\treturn anywhere ?\n\t\t\t\t\tnew XPathWildcardAnywhereElement() :\n\t\t\t\t\tnew XPathWildcardElement();\n\t\t\tcase XPathLexer.TOKEN_REF :\n\t\t\tcase XPathLexer.STRING :\n\t\t\t\tif ( ttype==Token.INVALID_TYPE ) {\n\t\t\t\t\tthrow new IllegalArgumentException(word+\n\t\t\t\t\t\t\t\t\t\t\t\t\t   \" at index \"+\n\t\t\t\t\t\t\t\t\t\t\t\t\t   wordToken.getStartIndex()+\n\t\t\t\t\t\t\t\t\t\t\t\t\t   \" isn't a valid token name\");\n\t\t\t\t}\n\t\t\t\treturn anywhere ?\n\t\t\t\t\tnew XPathTokenAnywhereElement(word, ttype) :\n\t\t\t\t\tnew XPathTokenElement(word, ttype);\n\t\t\tdefault :\n\t\t\t\tif ( ruleIndex==-1 ) {\n\t\t\t\t\tthrow new IllegalArgumentException(word+\n\t\t\t\t\t\t\t\t\t\t\t\t\t   \" at index \"+\n\t\t\t\t\t\t\t\t\t\t\t\t\t   wordToken.getStartIndex()+\n\t\t\t\t\t\t\t\t\t\t\t\t\t   \" isn't a valid rule name\");\n\t\t\t\t}\n\t\t\t\treturn anywhere ?\n\t\t\t\t\tnew XPathRuleAnywhereElement(word, ruleIndex) :\n\t\t\t\t\tnew XPathRuleElement(word, ruleIndex);\n\t\t}","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/tree/xpath/XPath.java#L154-L190","documentation":"For an uppercase token reference (or string literal) in an XPath path, the parser's vocabulary is consulted via getTokenType(word); when it returns Token.INVALID_TYPE the name is unknown to that grammar, and IllegalArgumentException is thrown with the word and its index in the path.","triggerScenarios":"XPath.findAll(tree, \"/IDENT/expr\", parser) where the grammar defines no token named IDENT (maybe it is called ID); also literal text like 'foo:\"bar\"' where foo is not a token.","commonSituations":"Querying a parse tree with token names from a different grammar version; guessing token names instead of checking the generated vocabulary; using a rule name in uppercase (case matters: uppercase = token, lowercase = rule).","solutions":["Use the exact token name from the generated parser (check parser.getVocabulary() or the generated *.tokens file)","Remember case convention: TOKEN refs are uppercase, rule refs lowercase","If the grammar changed, regenerate and re-check names before running XPath queries"],"exampleFix":"// before\nXPath.findAll(tree, \"/IDENT\", parser); // grammar has ID, not IDENT\n\n// after\nXPath.findAll(tree, \"/ID\", parser);","handlingStrategy":"validation","validationCode":"boolean knownToken = parser.getVocabulary().getSymbolicName(parser.getTokenType(word)) != null;\n// stronger: int t = parser.getTokenType(word); boolean ok = t != Token.INVALID_TYPE;","typeGuard":null,"tryCatchPattern":"try { XPath.findAll(tree, path, parser); } catch (IllegalArgumentException e) { /* message names the invalid token and index; correct the query */ }","preventionTips":["Derive token names from parser.getVocabulary(), not from memory","Uppercase = token, lowercase = rule in tree XPath"],"tags":["antlr","xpath","token-names","java"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}