"""
A sample parser for a Python-like 'if' statement.
"""

from simpleparse.parser import Parser
from simpleparse.common import numbers, strings
from util import mark_indentation

grammar = """\
file      := if_stmt+
if_stmt   := 'if', ts, boolexpr, ts, ':', ts, suite
def_stmt  := 'def', ts, 'foo()', ts, ':', ts, suite
>ts<      := [ \t]*
boolexpr  := atom, ts, '=='/'!='/'>='/'<=', ts, atom
suite     := '\n', indent, stmt+, dedent
atom      := number/string
indent    := '\017'
dedent    := '\016'
stmt      := if_stmt/def_stmt/atom_stmt
atom_stmt := atom, ts, '\n'
"""

parser = Parser(grammar, "file")

if __name__ == '__main__':
    from pprint import pprint
    src = '''\
if 1 == 1:
    123
    if 2 != 3:
        100
        def foo():
            44
    101
'''
    isrc = mark_indentation(src)
    result = parser.parse(isrc)
    assert len(isrc) == result[2], "did not finish parsing!"
    pprint(result)


