"""
An implementation of Python's indentation tracking algorithm in Python.
"""

import re

# ASCII shift-in and shift-out
INDENT = chr(0017)
DEDENT = chr(0016)

def mark_indentation(input):
    """Mark input with INDENT/DEDENT indicators.

    I use this for preprocessing input to SimpleParse for indentation
    based grammars.  It strips all leading whitespace from input and
    adds INDENT/DEDENT indicators at the correct places.
    """
    lines = input.splitlines()
    indent = [0]
    pat = re.compile('[ \t]*')
    outlines = []
    for line in lines:
        wslen = len(pat.match(line).group())
        if wslen > indent[-1]:
            indent.append(wslen)
            line = pat.sub(INDENT, line, 1)
        elif wslen < indent[-1]:
            dedents = 0
            while wslen < indent[-1]:
                indent.pop()
                dedents += 1
            toks = DEDENT * dedents
            line = pat.sub(toks, line, 1)
        else:
            line = pat.sub('', line, 1)
        outlines.append(line)
    final = ''
    while len(indent) > 1:
        final += DEDENT
        indent.pop()
    outlines.append(final)
    return '\n'.join(outlines)


