parseUpToError method

void parseUpToError(
  1. IntTuple action,
  2. int current_kind,
  3. int error_token
)

Implementation

void parseUpToError(IntTuple action, int current_kind, int error_token) {
  //
  // Assume predecessor of next token and compute initial action
  //
  var curtok = tokStream.getPrevious(tokStream.peek());
  var act = tAction(tempStack[tempStackTop], current_kind);

  //
  // Allocate configuration stack.
  //
  var configuration_stack = ConfigurationStack(prs);

  //
  // Keep parsing until we reach the end of file and succeed or
  // an error is encountered. The list of actions executed will
  // be store in the "action" tuple.
  //
  action.reset();
  for (;;) {
    if (act <= NUM_RULES) {
      action.add(act); // save this reduce action
      tempStackTop--;

      do {
        tempStackTop -= (rhs(act) - 1);
        act = ntAction(tempStack[tempStackTop], lhs(act));
      } while (act <= NUM_RULES);
    } else if (act > ERROR_ACTION) {
      action.add(act); // save this shift-reduce action
      curtok = tokStream.getToken();
      current_kind = tokStream.getKind(curtok);
      act -= ERROR_ACTION;

      do {
        tempStackTop -= (rhs(act) - 1);
        act = ntAction(tempStack[tempStackTop], lhs(act));
      } while (act <= NUM_RULES);
    } else if (act < ACCEPT_ACTION) {
      action.add(act); // save this shift action
      curtok = tokStream.getToken();
      current_kind = tokStream.getKind(curtok);
    } else if (act == ERROR_ACTION) {
      if (curtok != error_token) {
        var configuration = configuration_stack.pop();
        if (configuration == null) {
          act = ERROR_ACTION;
        } else {
          tempStackTop = configuration.stack_top;
          configuration.retrieveStack(tempStack);
          act = configuration.act;
          curtok = configuration.curtok;
          action.reset(configuration.action_length);
          current_kind = tokStream.getKind(curtok);
          tokStream.reset(tokStream.getNext(curtok));
          continue;
        }
      }
      break;
    } else if (act > ACCEPT_ACTION) {
      if (configuration_stack.findConfiguration(
          tempStack, tempStackTop, curtok)) {
        act = ERROR_ACTION;
      } else {
        configuration_stack.push(
            tempStack, tempStackTop, act + 1, curtok, action.size());
        act = baseAction(act);
      }
      continue;
    } else {
      break;
    } // assert(act == ACCEPT_ACTION);

    try {
      tempStack[++tempStackTop] = act;
    } on RangeError {
      reallocateStacks();
      tempStack[tempStackTop] = act;
    }
    act = tAction(act, current_kind);
  }

  action.add(ERROR_ACTION);

  return;
}