{"record":{"id":"7b9b85baa87b56a9","repo":"antlr/antlr4","slug":"the-current-recognizer-does-not-provide-a-list-of","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/Java/src/org/antlr/v4/runtime/Recognizer.java","lineNumber":101,"sourceCode":"\n\t\t\t\tresult.put(\"EOF\", Token.EOF);\n\t\t\t\tresult = Collections.unmodifiableMap(result);\n\t\t\t\ttokenTypeMapCache.put(vocabulary, result);\n\t\t\t}\n\n\t\t\treturn result;\n\t\t}\n\t}\n\n\t/**\n\t * Get a map from rule names to rule indexes.\n\t *\n\t * <p>Used for XPath and tree pattern compilation.</p>\n\t */\n\tpublic Map<String, Integer> getRuleIndexMap() {\n\t\tString[] ruleNames = getRuleNames();\n\t\tif (ruleNames == null) {\n\t\t\tthrow new UnsupportedOperationException(\"The current recognizer does not provide a list of rule names.\");\n\t\t}\n\n\t\tsynchronized (ruleIndexMapCache) {\n\t\t\tMap<String, Integer> result = ruleIndexMapCache.get(ruleNames);\n\t\t\tif (result == null) {\n\t\t\t\tresult = Collections.unmodifiableMap(Utils.toMap(ruleNames));\n\t\t\t\truleIndexMapCache.put(ruleNames, result);\n\t\t\t}\n\n\t\t\treturn result;\n\t\t}\n\t}\n\n\tpublic int getTokenType(String tokenName) {\n\t\tInteger ttype = getTokenTypeMap().get(tokenName);\n\t\tif ( ttype!=null ) return ttype;\n\t\treturn Token.INVALID_TYPE;\n\t}","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java#L83-L119","documentation":"Thrown by Recognizer.getRuleIndexMap() when getRuleNames() returns null, i.e. the recognizer cannot supply the grammar's rule names. The map is required by XPath compilation and tree pattern matching, which resolve rule names to the integer indexes used in parse trees. Only generated recognizers (or interpreters constructed with a rule-name array) can provide it.","triggerScenarios":"Calling XPath.compile(path, recognizer) or new TreePatternMatcher(parser) on a recognizer whose getRuleNames() returns null; using a bare Recognizer subclass or an interpreter built without rule names; calling parser.getRuleIndexMap() directly on such a recognizer.","commonSituations":"Running tree pattern matching or XPath queries against a ParserInterpreter/LexerInterpreter constructed from only a grammar-name/ATN; hand-rolled Recognizer subclasses used for testing; tooling that assumes every recognizer is generated code.","solutions":["Pass a generated parser (e.g. MyParser) which implements getRuleNames() instead of an interpreter or base Recognizer","When using ParserInterpreter, construct it from a grammar (Grammar g = ...; new ParserInterpreter(g, atn)) so rule names are available, rather than from a bare ATN","If you subclass Recognizer yourself, override getRuleNames() to return the grammar's rule-name array"],"exampleFix":"// before\nRecognizer rec = new LexerInterpreter(\"T\", null, null, \"T\", atn); // ruleNames == null\nXPath xpath = XPath.compile(path, rec); // throws\n\n// after\nGrammar g = loadGrammar(\"T.g4\");\nParserInterpreter rec = g.newParserInterpreter(atnDeserialized);\nXPath xpath = XPath.compile(path, rec);","handlingStrategy":"validation","validationCode":"if (recognizer.getRuleNames() == null) {\n    throw new IllegalStateException(\n        \"Recognizer provides no rule names; use a generated parser or an interpreter built from a Grammar\");\n}\nMap<String, Integer> map = recognizer.getRuleIndexMap();","typeGuard":"static boolean hasRuleNames(Recognizer<?, ?> r) {\n    return r != null && r.getRuleNames() != null;\n}","tryCatchPattern":"try { recognizer.getRuleIndexMap(); } catch (UnsupportedOperationException e) { /* fall back to name-based tree walking without rule indexes */ }","preventionTips":["Build interpreters from a Grammar object so rule names are always available","Prefer generated recognizer classes for XPath/tree-pattern work","Document which recognizer implementations support rule indexes"],"tags":["antlr","parser","xpath","tree-pattern","rule-names"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}