antlr/antlr4 · error · NotSupportedException

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

Error "The current recognizer does not provide a list of rule names." thrown in antlr/antlr4.

Source

Thrown at runtime/CSharp/src/Recognizer.cs:108

            }
            result["EOF"] = TokenConstants.EOF;
            return result;
        }

        /// <summary>Get a map from rule names to rule indexes.</summary>
        /// <remarks>
        /// Get a map from rule names to rule indexes.
        /// <p>Used for XPath and tree pattern compilation.</p>
        /// </remarks>
        [NotNull]
        public virtual IDictionary<string, int> RuleIndexMap
        {
            get
            {
                string[] ruleNames = RuleNames;
                if (ruleNames == null)
                {
                    throw new NotSupportedException("The current recognizer does not provide a list of rule names.");
                }

                return ruleIndexMapCache.GetValue(ruleNames, Utils.ToMap);
            }
        }

        public virtual int GetTokenType(string tokenName)
        {
            int ttype;
            if (TokenTypeMap.TryGetValue(tokenName, out ttype))
            {
                return ttype;
            }
            return TokenConstants.InvalidType;
        }

        /// <summary>
        /// If this recognizer was generated, it will have a serialized ATN

View on GitHub (pinned to 7d5770395b)

Solutions

  1. Provide rule names: use a generated parser (which supplies RuleNames) instead of a bare Recognizer, or construct the recognizer subclass that defines rule names.
  2. Check the recognizer type before asking for rule names and handle the unsupported case.

Example fix

// use the generated parser whose RuleNames array is populated
var parser = new MyGrammarParser(tokens);

When it happens

Trigger: Recognizer.RuleNames accessed on a recognizer subclass that does not expose rule names.

Common situations: Override RuleNames in generated recognizers; do not call it on a bare Recognizer.


AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14). Data as JSON: /api/errors/361d230f754ae149. Report an issue: GitHub.