{"record":{"id":"33effc35bd595e90","repo":"antlr/antlr4","slug":"s-at-index-d-isn-t-a-valid-token-name","errorCode":null,"errorMessage":"%s at index %d isn't a valid token name","messagePattern":"(.+?) at index (.+?) isn't a valid token name","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"runtime/Python3/src/antlr4/xpath/XPath.py","lineNumber":153,"sourceCode":"            raise Exception(\"Missing path element at end of path\")\n\n        word = wordToken.text\n        if wordToken.type==XPathLexer.WILDCARD :\n            return XPathWildcardAnywhereElement() if anywhere else XPathWildcardElement()\n\n        elif wordToken.type in [XPathLexer.TOKEN_REF, XPathLexer.STRING]:\n            tsource = self.parser.getTokenStream().tokenSource\n\n            ttype = Token.INVALID_TYPE\n            if wordToken.type == XPathLexer.TOKEN_REF:\n                if word in tsource.ruleNames:\n                    ttype = tsource.ruleNames.index(word) + 1\n            else:\n                if word in tsource.literalNames:\n                    ttype = tsource.literalNames.index(word)\n\n            if ttype == Token.INVALID_TYPE:\n                raise Exception(\"%s at index %d isn't a valid token name\" % (word, wordToken.tokenIndex))\n            return XPathTokenAnywhereElement(word, ttype) if anywhere else XPathTokenElement(word, ttype)\n\n        else:\n            ruleIndex = self.parser.ruleNames.index(word) if word in self.parser.ruleNames else -1\n\n            if ruleIndex == -1:\n                raise Exception(\"%s at index %d isn't a valid rule name\" % (word, wordToken.tokenIndex))\n            return XPathRuleAnywhereElement(word, ruleIndex) if anywhere else XPathRuleElement(word, ruleIndex)\n\n\n    @staticmethod\n    def findAll(tree:ParseTree, xpath:str, parser:Parser):\n        p = XPath(parser, xpath)\n        return p.evaluate(tree)\n\n    #\n    # Return a list of all nodes starting at {@code t} as root that satisfy the\n    # path. The root {@code /} is relative to the node passed to","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Python3/src/antlr4/xpath/XPath.py#L135-L171","documentation":"Raised by XPath.getXPathElement() in the Python3 runtime when a TOKEN_REF (or string) in the path does not name a token in the lexer's rule names / literal names. The XPath evaluator must map the name to a token type to match terminal nodes; an unknown name cannot be mapped and ttype stays INVALID_TYPE.","triggerScenarios":"Calling XPath with a path like '/unknownName' where the first character convention marks it a token reference, or '/\"literal\"' whose text does not appear in the lexer's literal names.","commonSituations":"Grammar changes renamed or removed tokens; using a rule name in token position; string literals that do not exactly match a literal token name.","solutions":["Check the name against the generated lexer's ruleNames / literalNames lists","If the name is a parser rule, use it in rule position (lowercase, no '#')","Regenerate and inspect the lexer vocabulary if the grammar recently changed"],"exampleFix":"# before: grammar defines ID, not IDENT\nnodes = XPath.findAll(tree, \"/IDENT\", parser)\n\n# after\nnodes = XPath.findAll(tree, \"/ID\", parser)","handlingStrategy":"validation","validationCode":"known = set(l for l in lexer.ruleNames if l) | set(l for l in getattr(lexer, 'literalNames', []) if l)\nif name not in known:\n    raise ValueError(f'{name} is not a token name')","typeGuard":"def is_known_token_name(name: str, lexer) -> bool:\n    return name in (lexer.ruleNames or []) or name in (getattr(lexer, 'literalNames', None) or [])","tryCatchPattern":"try:\n    nodes = XPath.findAll(tree, xpath, parser)\nexcept Exception as e:\n    if \"isn't a valid token name\" in str(e):\n        # look up correct token name in lexer vocabulary\n        ...","preventionTips":["Check names against the generated lexer vocabulary","Update XPath strings after grammar renames"],"tags":["antlr","python","xpath","validation"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}