'Returns the previous leaf in the parser tree.
Raises an IndexError if it\'s the first element in the parser tree.'

def get_previous_leaf(self):

    node = self
    while True:
        c = node.parent.children
        i = c.index(node)
        if (i == 0):
            node = node.parent
            if (node.parent is None):
                return None
        else:
            node = c[(i - 1)]
            break
    while True:
        try:
            node = node.children[(-1)]
        except AttributeError:
            return node
