sync method

  1. @override
void sync(
  1. Parser recognizer
)
override

The default implementation of {@link ANTLRErrorStrategy#sync} makes sure that the current lookahead symbol is consistent with what were expecting at this point in the ATN. You can call this anytime but ANTLR only generates code to check before subrules/loops and each iteration.

Implements Jim Idle's magic sync mechanism in closures and optional subrules. E.g.,

a : sync ( stuff sync )* ;
sync : {consume to what can follow sync} ;

At the start of a sub rule upon error, {@link #sync} performs single token deletion, if possible. If it can't do that, it bails on the current rule and uses the default error recovery, which consumes until the resynchronization set of the current rule.

If the sub rule is optional ({@code (...)?}, {@code (...)*}, or block with an empty alternative), then the expected set includes what follows the subrule.

During loop iteration, it consumes until it sees a token that can start a sub rule or what follows loop. Yes, that is pretty aggressive. We opt to stay in the loop as long as possible.

ORIGINS

Previous versions of ANTLR did a poor job of their recovery within loops. A single mismatch token or missing token would force the parser to bail out of the entire rules surrounding the loop. So, for rule

classDef : 'class' ID '{' member* '}'

input with an extra token between members would force the parser to consume until it found the next class definition rather than the next member definition of the current class.

This functionality cost a little bit of effort because the parser has to compare token set at the start of the loop and at each iteration. If for some reason speed is suffering for you, you can turn off this functionality by simply overriding this method as a blank { }.

Implementation

@override
void sync(Parser recognizer) {
  final s = recognizer.interpreter!.atn.states[recognizer.state]!;
//		log("sync @ "+s.stateNumber+"="+s.getClass().getSimpleName(), level: Level.SEVERE.value);
  // If already recovering, don't try to sync
  if (inErrorRecoveryMode(recognizer)) {
    return;
  }

  final tokens = recognizer.inputStream;
  final la = tokens.LA(1)!;

  // try cheaper subset first; might get lucky. seems to shave a wee bit off
  final nextTokens = recognizer.getATN().nextTokens(s);
  if (nextTokens.contains(la)) {
    // We are sure the token matches
    nextTokensContext = null;
    nextTokensState = ATNState.INVALID_STATE_NUMBER;
    return;
  }

  if (nextTokens.contains(Token.EPSILON)) {
    if (nextTokensContext == null) {
      // It's possible the next token won't match; information tracked
      // by sync is restricted for performance.
      nextTokensContext = recognizer.context;
      nextTokensState = recognizer.state;
    }
    return;
  }

  switch (s.stateType) {
    case StateType.BLOCK_START:
    case StateType.STAR_BLOCK_START:
    case StateType.PLUS_BLOCK_START:
    case StateType.STAR_LOOP_ENTRY:
      // report error and recover if possible
      if (singleTokenDeletion(recognizer) != null) {
        return;
      }

      throw InputMismatchException(recognizer);

    case StateType.PLUS_LOOP_BACK:
    case StateType.STAR_LOOP_BACK:
//			log("at loop back: "+s.getClass().getSimpleName(), level: Level.SEVERE.value);
      reportUnwantedToken(recognizer);
      final expecting = recognizer.expectedTokens;
      final whatFollowsLoopIterationOrRule =
          expecting | getErrorRecoverySet(recognizer);
      consumeUntil(recognizer, whatFollowsLoopIterationOrRule);
      break;

    default:
      // do nothing if we can't identify the exact kind of ATN state
      break;
  }
}