{"record":{"id":"6a3af4da9387a664","repo":"antlr/antlr4","slug":"invalid-tokens-or-characters-at-index-d-in-path","errorCode":null,"errorMessage":"Invalid tokens or characters at index %d in path '%s'","messagePattern":"Invalid tokens or characters at index (.+?) in path '(.+?)'","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"runtime/Python3/src/antlr4/xpath/XPath.py","lineNumber":94,"sourceCode":"        self.parser = parser\n        self.path = path\n        self.elements = self.split(path)\n\n    def split(self, path:str):\n        input = InputStream(path)\n        lexer = XPathLexer(input)\n        def recover(self, e):\n            raise e\n        lexer.recover = recover\n        lexer.removeErrorListeners()\n        lexer.addErrorListener(ErrorListener()) # XPathErrorListener does no more\n        tokenStream = CommonTokenStream(lexer)\n        try:\n            tokenStream.fill()\n        except LexerNoViableAltException as e:\n            pos = lexer.column\n            msg = \"Invalid tokens or characters at index %d in path '%s'\" % (pos, path)\n            raise Exception(msg, e)\n\n        tokens = iter(tokenStream.tokens)\n        elements = list()\n        for el in tokens:\n            invert = False\n            anywhere = False\n            # Check for path separators, if none assume root\n            if el.type in [XPathLexer.ROOT, XPathLexer.ANYWHERE]:\n                anywhere = el.type == XPathLexer.ANYWHERE\n                next_el = next(tokens, None)\n                if not next_el:\n                    raise Exception('Missing element after %s' % el.getText())\n                else:\n                    el = next_el\n            # Check for bangs\n            if el.type == XPathLexer.BANG:\n                invert = True\n                next_el = next(tokens, None)","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Python3/src/antlr4/xpath/XPath.py#L76-L112","documentation":"Raised by XPath.py in the Python3 runtime when the XPathLexer throws LexerNoViableAltException while scanning the XPath string, i.e. the path contains a character the XPath grammar does not accept. Valid XPath input is limited to '/', '//', '!', '*', token/rule names, and quoted strings.","triggerScenarios":"Calling XPath(parser, path) or XPath.findAll(tree, path, parser) with a path containing illegal characters such as '..', '@', '[', ']', or ':'.","commonSituations":"Assuming ANTLR XPath supports standard XML/JSONPath syntax (predicates, '@attr', '..') when it is a much smaller language; pasting a file path or selector by mistake.","solutions":["Rewrite the XPath using only supported syntax: /, //, !, *, IDENT, 'string'","Remove XML/JSONPath-style predicates and attribute selectors","Check the path character-by-character at the reported column index"],"exampleFix":"# before\nnodes = XPath.findAll(tree, \"/stmt[@id=3]\", parser)\n\n# after\nnodes = XPath.findAll(tree, \"/stmt\", parser)","handlingStrategy":"try-catch","validationCode":"import re\nif re.search(r\"[^A-Za-z0-9_/'!*\\\" ]\", xpath):\n    raise ValueError('unsupported XPath syntax')","typeGuard":null,"tryCatchPattern":"try:\n    nodes = XPath.findAll(tree, path, parser)\nexcept Exception as e:\n    if 'Invalid tokens or characters' in str(e):\n        # path uses unsupported XPath features; rewrite it\n        ...","preventionTips":["Learn ANTLR XPath's tiny syntax (/, //, !, *, names, strings)","Do not assume XML/JSONPath predicates are supported"],"tags":["antlr","python","xpath","lexer"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}