antlr/antlr4 · error · UnsupportedOperationException
The current recognizer does not provide a list of rule names
Error message
The current recognizer does not provide a list of rule names.
What it means
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.
Source
Thrown at runtime/Python3/src/antlr4/Recognizer.py:72
if tokenNames is None:
from .error.Errors import UnsupportedOperationException
raise UnsupportedOperationException("The current recognizer does not provide a list of token names.")
result = self.tokenTypeMapCache.get(tokenNames, None)
if result is None:
result = zip( tokenNames, range(0, len(tokenNames)))
result["EOF"] = Token.EOF
self.tokenTypeMapCache[tokenNames] = result
return result
# Get a map from rule names to rule indexes.
#
# <p>Used for XPath and tree pattern compilation.</p>
#
def getRuleIndexMap(self):
ruleNames = self.getRuleNames()
if ruleNames is None:
from .error.Errors import UnsupportedOperationException
raise UnsupportedOperationException("The current recognizer does not provide a list of rule names.")
result = self.ruleIndexMapCache.get(ruleNames, None)
if result is None:
result = zip( ruleNames, range(0, len(ruleNames)))
self.ruleIndexMapCache[ruleNames] = result
return result
def getTokenType(self, tokenName:str):
ttype = self.getTokenTypeMap().get(tokenName, None)
if ttype is not None:
return ttype
else:
return Token.INVALID_TYPE
# What is the error header, normally line/character position information?#
def getErrorHeader(self, e:RecognitionException):
line = e.getOffendingToken().line
column = e.getOffendingToken().columnView on GitHub (pinned to 7d5770395b)
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
Example fix
# before
class MyParser(Parser):
ruleNames = None # getRuleIndexMap() raises
# after
class MyParser(Parser):
ruleNames = ["program", "statement", "expr"] # tool emission order Defensive patterns
Strategy: type-guard
Validate before calling
# Python: check rule names before XPath/pattern compilation
if recognizer.getRuleNames() is None:
raise ValueError("recognizer has no rule names; use a generated parser")
idx = recognizer.getRuleIndexMap()["expr"] Type guard
# Python
def has_rule_names(recognizer) -> bool:
return recognizer.getRuleNames() is not None Try / catch
try:
m = recognizer.getRuleIndexMap()
except UnsupportedOperationException as ex:
if "list of rule names" in str(ex):
# populate ruleNames (tool emission order) or switch to a generated parser
raise
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The current recognizer does not provide a list of token name
- The current recognizer does not provide a list of rule names
- {word} at index {startIndex} isn't a valid rule name
- Invalid tokens or characters at index %d in path '%s'
- Missing element after %s
AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14).
Data as JSON: /api/errors/9d61c027db767a69.
Report an issue: GitHub.