getRuleInvocationStack method

List<String> getRuleInvocationStack([
  1. RuleContext? p
])

Return List<String> of the rule names in your parser instance leading up to a call to the current rule. You could override if you want more details such as the file/line info of where in the ATN a rule is invoked.

This is very useful for error messages.

Implementation

List<String> getRuleInvocationStack([RuleContext? p]) {
  p = p ?? context;
  final _ruleNames = ruleNames;
  final stack = <String>[];
  while (p != null) {
    // compute what follows who invoked us
    final ruleIndex = p.ruleIndex;
    if (ruleIndex < 0) {
      stack.add('n/a');
    } else {
      stack.add(_ruleNames[ruleIndex]);
    }
    p = p.parent;
  }
  return stack;
}