def _find_end(tokens):
    """
    Find index of closing parenthesis. Based on function from Rhomboid at
    https://gist.github.com/Rhomboid/5994999.

    Parameters
    ----------
    tokens : list of str
        List of strings representing molecular formula.

    Return
    ------
    idx : int
        Index of closing parenthesis.
    """
    count = 0
    for idx, t in enumerate(tokens):
        if t == ')':
            count -= 1
            if count == 0:
                return idx
        elif t == '(':
            count += 1
    raise ValueError('Unmatched parentheses')
