def parse_structure(astr, level):
    """
    The returned line number is from the beginning of the string, starting
    at zero. Returns an empty list if no loops found.

    """
    if level == 0:
        loopbeg = '/**begin repeat'
        loopend = '/**end repeat**/'
    else:
        loopbeg = '/**begin repeat%d' % level
        loopend = '/**end repeat%d**/' % level
    ind = 0
    line = 0
    spanlist = []
    while True:
        start = astr.find(loopbeg, ind)
        if start == -1:
            break
        start2 = astr.find('*/', start)
        start2 = astr.find('\n', start2)
        fini1 = astr.find(loopend, start2)
        fini2 = astr.find('\n', fini1)
        line += astr.count('\n', ind, start2 + 1)
        spanlist.append((start, start2 + 1, fini1, fini2 + 1, line))
        line += astr.count('\n', start2 + 1, fini2)
        ind = fini2
    spanlist.sort()
    return spanlist
