previousTokenOnChannel method

int previousTokenOnChannel(
  1. int i,
  2. int channel
)

Given a starting index, return the index of the previous token on channel. Return i if {@code tokensi} is on channel. Return -1 if there are no tokens on channel between i and 0.

If [i] specifies an index at or after the EOF token, the EOF token index is returned. This is due to the fact that the EOF token is treated as though it were on every channel.

Implementation

int previousTokenOnChannel(int i, int channel) {
  sync(i);
  if (i >= size) {
    // the EOF token is on every channel
    return size - 1;
  }

  while (i >= 0) {
    final token = tokens[i];
    if (token.type == Token.EOF || token.channel == channel) {
      return i;
    }

    i--;
  }

  return i;
}