getTermIndex method

int getTermIndex(
  1. List<int> stck,
  2. int stack_top,
  3. int tok,
  4. int buffer_position,
)

Implementation

int getTermIndex(
    List<int> stck, int stack_top, int tok, int buffer_position) {
  //
  // Initialize stack index of temp_stack and initialize maximum
  // position of state stack that is still useful.
  //
  var act = stck[stack_top], max_pos = stack_top, highest_symbol = tok;

  tempStackTop = stack_top - 1;

  //
  // Compute all reduce and associated actions induced by the
  // candidate until a SHIFT or SHIFT-REDUCE is computed. ERROR
  // and ACCEPT actions cannot be computed on the candidate in
  // this context, since we know that it is suitable for recovery.
  //
  tokStream.reset(buffer[buffer_position]);
  act = tAction(act, tok);
  while (act <= NUM_RULES) {
    //
    // Process all goto-reduce actions following reduction,
    // until a goto action is computed ...
    //
    do {
      var lhs_symbol = lhs(act);
      tempStackTop -= (rhs(act) - 1);
      act = (tempStackTop > max_pos
          ? tempStack[tempStackTop]
          : stck[tempStackTop]);
      act = ntAction(act, lhs_symbol);
    } while (act <= NUM_RULES);

    //
    // Compute new maximum useful position of (STATE_)stack,
    // push goto state into the stack, and compute next
    // action on candidate ...
    //
    max_pos = max_pos < tempStackTop ? max_pos : tempStackTop;
    tempStack[tempStackTop + 1] = act;
    act = tAction(act, tok);
  }

  //
  // At this stage, we have simulated all actions induced by the
  // candidate and we are ready to shift or shift-reduce it. First,
  // set tok and next_ptr appropriately and identify the candidate
  // as the initial highest_symbol. If a shift action was computed
  // on the candidate, update the stack and compute the next
  // action. Next, simulate all actions possible on the next input
  // token until we either have to shift it or are about to reduce
  // below the initial starting point in the stack (indicated by
  // max_pos as computed in the previous loop).  At that point,
  // return the highest_symbol computed.
  //
  tempStackTop++; // adjust top of stack to reflect last goto
  // next move is shift or shift-reduce.
  var threshold = tempStackTop;

  tok = tokStream.getKind(buffer[buffer_position]);
  tokStream.reset(buffer[buffer_position + 1]);

  if (act > ERROR_ACTION) {
    act -= ERROR_ACTION;
  } else if (act < ACCEPT_ACTION) // shift on candidate
  {
    tempStack[tempStackTop + 1] = act;
    act = tAction(act, tok);
  }

  while (act <= NUM_RULES) {
    //
    // Process all goto-reduce actions following reduction,
    // until a goto action is computed ...
    //
    do {
      var lhs_symbol = lhs(act);
      tempStackTop -= (rhs(act) - 1);

      if (tempStackTop < threshold) {
        return (highest_symbol > NT_OFFSET
            ? nonterminalIndex(highest_symbol - NT_OFFSET)
            : terminalIndex(highest_symbol));
      }

      if (tempStackTop == threshold) highest_symbol = lhs_symbol + NT_OFFSET;
      act = (tempStackTop > max_pos
          ? tempStack[tempStackTop]
          : stck[tempStackTop]);
      act = ntAction(act, lhs_symbol);
    } while (act <= NUM_RULES);

    tempStack[tempStackTop + 1] = act;
    act = tAction(act, tok);
  }

  return (highest_symbol > NT_OFFSET
      ? nonterminalIndex(highest_symbol - NT_OFFSET)
      : terminalIndex(highest_symbol));
}