def doc_locate(doc, path):
    """Walk a path down a document.

    The path is a list of bytestring segments.
    Starting from the root node, a cursor points at the first segment.

    At each node, the segment under the cursor is used
    to access the node to go deeper.

    As the cursor moves from the begining towards the end of the segment list,
    the list is logically split into two parts.

    The first part, the trail, is the prefix of segments
    that have already been used in descending through the document hierarchy
    and corresponds to the current path.

    The second part of the path, the feed, is the suffix of segments
    that have not yet been accessed to descend to the full path.

    There are two outcomes:
        1. feed is empty, meaning that the given path was found
        2. feed is non-empty, meaning that the first segment in the feed was
           not found in the document at the path formed by the trail.

    Args:
        doc (dict):
            A recursive object-document
        path (tuple):
            A sequence of path segments as in ('path', 'to', 'somewhere')

    Returns:
        tuple of lists:
            feed (list):
                List of path segments left that have not yet been accessed
            trail (list):
                List of path segments already accessed. It corresponds to the
                current path of the node where no further descent was possible
                and thus doc_locate() was terminated.
            nodes (list):
                List of nodes already accessed, in order.
    """
    feed = list(reversed(path))
    trail = []
    nodes = [doc]
    while feed:
        segment = feed.pop()
        if not segment:
            continue
        if not isinstance(doc, dict) or segment not in doc:
            feed.append(segment)
            break
        trail.append(segment)
        doc = doc[segment]
        nodes.append(doc)
    return feed, trail, nodes
