def _pop_completed_blocks(token, stack, indent_stack):
    """
    Pop any completed blocks off the stack.  This includes any
    blocks that we have dedented past, as well as any list item
    blocks that we've dedented to.  The top element on the stack
    should only be a list if we're about to start a new list
    item (i.e., if the next token is a bullet).
    """
    indent = token.indent
    if indent is not None:
        while len(stack) > 2:
            pop = False
            if indent_stack[-1] != None and indent < indent_stack[-1]:
                pop = True
            elif indent_stack[-1] == None and indent < indent_stack[-2]:
                pop = True
            elif token.tag == 'bullet' and indent == indent_stack[-2
                ] and stack[-1].tag in ('li', 'field'):
                pop = True
            elif stack[-1].tag in ('ulist', 'olist') and (token.tag !=
                'bullet' or token.contents[-1] == ':'):
                pop = True
            if not pop:
                return
            stack.pop()
            indent_stack.pop()
