def _populate_spans_for_token(grouped_tokens, tables, aliases_to_table_names,
                              table_schemas, sql_spans):
  """Add SqlSpan tuples to sql_spans given grouped_tokens and schema."""
  previous_token = None
  in_from_clause_context = False
  for token in grouped_tokens:
    is_table_mention = False
    if isinstance(token, list):
      nested_spans = []
      _populate_spans_for_statement(token, nested_spans, table_schemas)
      sql_spans.append(make_sql_span(nested_statement=nested_spans))
      previous_token = None
      continue

    if _is_underspecified_table_mention(previous_token, token):
      for token_value in token.value.split(' '):
        sql_table = _get_sql_table(token_value)
        sql_spans.append(make_sql_span(table_name=sql_table.table_name))
    elif _is_table_mention(previous_token, token, in_from_clause_context):
      sql_table = _get_sql_table(token.value)
      sql_spans.append(make_sql_span(table_name=sql_table.table_name))
      is_table_mention = True
    elif _is_column_mention(token):
      _add_column_spans(token, tables, aliases_to_table_names, table_schemas,
                        sql_spans)
    elif token.is_group:
      _populate_spans_for_token(token, tables, aliases_to_table_names,
                                table_schemas, sql_spans)
    elif _is_value_literal(token):
      sql_spans.append(make_sql_span(value_literal=token.value))
    else:
      # Otherwise, assume token is a SQL token.
      # TODO(petershaw): Consider not splitting (*).
      sql_spans.append(make_sql_span(sql_token=token.value))

    if is_table_mention or in_from_clause_context and token.value == ',':
      in_from_clause_context = True
    else:
      in_from_clause_context = False

    previous_token = token
Add SqlSpan tuples to sql_spans given grouped_tokens and schema.
language/xsp/data_preprocessing/abstract_sql.py