def _find_statement_end_idx(tokens, start_idx):
  """Return end index of nested statement starting at start_idx."""
  open_parens = 0
  for idx in range(start_idx, len(tokens)):
    token = tokens[idx]
    if token.value == '(':
      open_parens += 1
    elif token.value == ')':
      if open_parens:
        open_parens -= 1
      else:
        return idx
    elif token.value.lower() in ('exclude', 'intersect', 'union'):
      return idx
  return len(tokens)
Return end index of nested statement starting at start_idx.
language/xsp/data_preprocessing/abstract_sql.py