getTokens method

List<Token>? getTokens([
  1. int? start,
  2. int? stop,
  3. Set<int>? types
])

Given a start and stop index, return a List of all tokens in the token type BitSet. Return null if no tokens were found. This method looks at both on and off channel tokens.

Implementation

List<Token>? getTokens([
  int? start,
  int? stop,
  Set<int>? types,
]) {
  if (start == null && stop == null) {
    return tokens;
  }
  start = start!;
  stop = stop!;
  lazyInit();
  if (start < 0 || start >= tokens.length) {
    throw RangeError.index(start, tokens);
  } else if (stop < 0 || stop >= tokens.length) {
    throw RangeError.index(stop, tokens);
  }
  if (start > stop) return null;

  // list = tokens[start:stop]:{T t, t.getType() in types}
  List<Token>? filteredTokens = <Token>[];
  for (var i = start; i <= stop; i++) {
    final t = tokens[i];
    if (types == null || types.contains(t.type)) {
      filteredTokens.add(t);
    }
  }
  if (filteredTokens.isEmpty) {
    filteredTokens = null;
  }
  return filteredTokens;
}