def collect_nodes(kind, collection, gen):
    """
    Collects nodes with a given kind from a generator,
    and puts them in the provided list.
    Returns the first node from the generator that does not
    match the provided kind.
    """
    tail = None
    for node in gen:
        if node.kind is not kind:
            tail = node
            break
        else:
            collection.append(node)
    return tail