stripChildrenOutOfRange static method

void stripChildrenOutOfRange(
  1. ParserRuleContext? t,
  2. ParserRuleContext root,
  3. int startIndex,
  4. int stopIndex,
)

Replace any subtree siblings of root that are completely to left or right of lookahead range with a CommonToken(Token.INVALID_TYPE,"...") node. The source interval for t is not altered to suit smaller range!

WARNING: destructive to t.

@since 4.5.1

Implementation

static void stripChildrenOutOfRange(
  ParserRuleContext? t,
  ParserRuleContext root,
  int startIndex,
  int stopIndex,
) {
  if (t == null) return;
  for (var i = 0; i < t.childCount; i++) {
    final child = t.getChild(i)!;
    final range = child.sourceInterval;
    if (child is ParserRuleContext &&
        (range.b < startIndex || range.a > stopIndex)) {
      if (isAncestorOf(child, root)) {
        // replace only if subtree doesn't have displayed root
        final abbrev = CommonToken(Token.INVALID_TYPE, text: '...');
        t.children![i] = TerminalNodeImpl(abbrev);
      }
    }
  }
}