copyFrom method

void copyFrom(
  1. ParserRuleContext ctx
)

COPY a ctx (I'm deliberately not using copy constructor) to avoid confusion with creating node with parent. Does not copy children (except error leaves).

This is used in the generated parser code to flip a generic XContext node for rule X to a YContext for alt label Y. In that sense, it is not really a generic copy function.

If we do an error sync() at start of a rule, we might add error nodes to the generic XContext so this function must copy those nodes to the YContext as well else they are lost!

Implementation

void copyFrom(ParserRuleContext ctx) {
  parent = ctx.parent;
  invokingState = ctx.invokingState;

  start = ctx.start;
  stop = ctx.stop;

  // copy any error nodes to alt label node
  if (ctx.children != null) {
    children = [];
    // reset parent pointer for any error nodes
    for (var child in ctx.children!) {
      if (child is ErrorNode) {
        addChild(child);
      }
    }
  }
}