#!/usr/bin/env python
# -*- coding: utf-8 -*-

# CAVEAT UTILITOR
#
# This file was automatically generated by Grako.
#
#    https://pypi.python.org/pypi/grako/
#
# Any changes you make to it will be overwritten the next time
# the file is generated.


from __future__ import print_function, division, absolute_import, unicode_literals

from grako.buffering import Buffer
from grako.parsing import graken, Parser
from grako.util import re, RE_FLAGS, generic_main  # noqa


KEYWORDS = {}


class PraxBuffer(Buffer):
    def __init__(
        self,
        text,
        whitespace=None,
        nameguard=None,
        comments_re=None,
        eol_comments_re=None,
        ignorecase=None,
        namechars='',
        **kwargs
    ):
        super(PraxBuffer, self).__init__(
            text,
            whitespace=whitespace,
            nameguard=nameguard,
            comments_re=comments_re,
            eol_comments_re=eol_comments_re,
            ignorecase=ignorecase,
            namechars=namechars,
            **kwargs
        )


class PraxParser(Parser):
    def __init__(
        self,
        whitespace=None,
        nameguard=None,
        comments_re=None,
        eol_comments_re=None,
        ignorecase=None,
        left_recursion=False,
        parseinfo=True,
        keywords=None,
        namechars='',
        buffer_class=PraxBuffer,
        **kwargs
    ):
        if keywords is None:
            keywords = KEYWORDS
        super(PraxParser, self).__init__(
            whitespace=whitespace,
            nameguard=nameguard,
            comments_re=comments_re,
            eol_comments_re=eol_comments_re,
            ignorecase=ignorecase,
            left_recursion=left_recursion,
            parseinfo=parseinfo,
            keywords=keywords,
            namechars=namechars,
            buffer_class=buffer_class,
            **kwargs
        )

    @graken()
    def _start_(self):
        self._expression_()
        self._check_eof()

    @graken()
    def _expression_(self):
        self._term_()
        self.name_last_node('head')

        def block2():
            with self._group():
                self._token('+')
            self._term_()
        self._closure(block2)
        self.name_last_node('tail')
        self.ast._define(
            ['head', 'tail'],
            []
        )

    @graken()
    def _term_(self):
        self._factor_()
        self.name_last_node('head')

        def block2():
            with self._group():
                with self._choice():
                    with self._option():
                        self._token('*')
                    with self._option():
                        self._token('@')
                    self._error('expecting one of: * @')
            self._factor_()
        self._closure(block2)
        self.name_last_node('tail')
        self.ast._define(
            ['head', 'tail'],
            []
        )

    @graken()
    def _factor_(self):
        with self._choice():
            with self._option():
                self._token('(')
                self._expression_()
                self.name_last_node('@')
                self._token(')')
            with self._option():
                self._literal_()
            self._error('no available options')

    @graken()
    def _literal_(self):
        self._pattern(r'(\\[\+*\(\)@]|[^\+*\(\)@])+')


class PraxSemantics(object):
    def start(self, ast):
        return ast

    def expression(self, ast):
        return ast

    def term(self, ast):
        return ast

    def factor(self, ast):
        return ast

    def literal(self, ast):
        return ast


def main(filename, startrule, **kwargs):
    with open(filename) as f:
        text = f.read()
    parser = PraxParser()
    return parser.parse(text, startrule, filename=filename, **kwargs)


if __name__ == '__main__':
    import json
    from grako.util import asjson

    ast = generic_main(main, PraxParser, name='Prax')
    print('AST:')
    print(ast)
    print()
    print('JSON:')
    print(json.dumps(asjson(ast), indent=2))
    print()
