{"record":{"id":"9d61c027db767a69","repo":"antlr/antlr4","slug":"the-current-recognizer-does-not-provide-a-list-of-9d61c0","errorCode":null,"errorMessage":"The current recognizer does not provide a list of rule names.","messagePattern":"The current recognizer does not provide a list of rule names\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"runtime/Python3/src/antlr4/Recognizer.py","lineNumber":72,"sourceCode":"        if tokenNames is None:\n            from .error.Errors import UnsupportedOperationException\n            raise UnsupportedOperationException(\"The current recognizer does not provide a list of token names.\")\n        result = self.tokenTypeMapCache.get(tokenNames, None)\n        if result is None:\n            result = zip( tokenNames, range(0, len(tokenNames)))\n            result[\"EOF\"] = Token.EOF\n            self.tokenTypeMapCache[tokenNames] = result\n        return result\n\n    # Get a map from rule names to rule indexes.\n    #\n    # <p>Used for XPath and tree pattern compilation.</p>\n    #\n    def getRuleIndexMap(self):\n        ruleNames = self.getRuleNames()\n        if ruleNames is None:\n            from .error.Errors import UnsupportedOperationException\n            raise UnsupportedOperationException(\"The current recognizer does not provide a list of rule names.\")\n        result = self.ruleIndexMapCache.get(ruleNames, None)\n        if result is None:\n            result = zip( ruleNames, range(0, len(ruleNames)))\n            self.ruleIndexMapCache[ruleNames] = result\n        return result\n\n    def getTokenType(self, tokenName:str):\n        ttype = self.getTokenTypeMap().get(tokenName, None)\n        if ttype is not None:\n            return ttype\n        else:\n            return Token.INVALID_TYPE\n\n\n    # What is the error header, normally line/character position information?#\n    def getErrorHeader(self, e:RecognitionException):\n        line = e.getOffendingToken().line\n        column = e.getOffendingToken().column","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Python3/src/antlr4/Recognizer.py#L54-L90","documentation":"Recognizer.getRuleIndexMap() maps rule names to rule indexes (used by XPath and ParseTreePatternMatcher) and raises UnsupportedOperationException when getRuleNames() returns None. As with token names, rule names exist only on generated recognizers; the guard fires the first time a rule-name-based API is used on a recognizer without them.","triggerScenarios":"Calling getRuleIndexMap(), XPath rule resolution, or compileParseTreePattern() on a recognizer whose ruleNames is None; instantiating a parser base class directly instead of a generated subclass; stripping generated metadata for code-size reasons.","commonSituations":"Same family as token-name failures: hand-written parsers, version-skewed generated code, or wrappers that construct the parser without the generated constants; XPath utilities pointed at a minimal recognizer.","solutions":["Use the generated parser class, which embeds ruleNames in tool-emitted order","For custom recognizers, set self.ruleNames = [...] in the exact order the ATN/parse-tree builder expects (tool emission order)","Re-run the code generator after tool/runtime upgrades so ruleNames matches the ATN"],"exampleFix":"# before\nclass MyParser(Parser):\n    ruleNames = None  # getRuleIndexMap() raises\n\n# after\nclass MyParser(Parser):\n    ruleNames = [\"program\", \"statement\", \"expr\"]  # tool emission order","handlingStrategy":"type-guard","validationCode":"# Python: check rule names before XPath/pattern compilation\nif recognizer.getRuleNames() is None:\n    raise ValueError(\"recognizer has no rule names; use a generated parser\")\nidx = recognizer.getRuleIndexMap()[\"expr\"]","typeGuard":"# Python\ndef has_rule_names(recognizer) -> bool:\n    return recognizer.getRuleNames() is not None","tryCatchPattern":"try:\n    m = recognizer.getRuleIndexMap()\nexcept UnsupportedOperationException as ex:\n    if \"list of rule names\" in str(ex):\n        # populate ruleNames (tool emission order) or switch to a generated parser\n        raise\n    raise","preventionTips":["Rely on generated parsers whenever rule-name lookup (XPath, patterns) is used","When custom recognizers are unavoidable, define ruleNames in tool emission order","Version-lock tool and runtime so generated metadata keeps its expected shape"],"tags":["antlr","python","recognizer","rule-names","xpath"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}