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

# CAVEAT UTILITOR
#
# This file was automatically generated by TatSu.
#
#    https://pypi.python.org/pypi/tatsu/
#
# 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

import sys

from tatsu.buffering import Buffer
from tatsu.parsing import Parser
from tatsu.parsing import tatsumasu, leftrec, nomemo
from tatsu.parsing import leftrec, nomemo  # noqa
from tatsu.util import re, generic_main  # noqa


KEYWORDS = {}  # type: ignore


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


class BugParser(Parser):
    def __init__(
        self,
        whitespace=None,
        nameguard=None,
        comments_re=None,
        eol_comments_re=None,
        ignorecase=None,
        left_recursion=True,
        parseinfo=True,
        keywords=None,
        namechars='',
        buffer_class=BugBuffer,
        **kwargs
    ):
        if keywords is None:
            keywords = KEYWORDS
        super(BugParser, 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
        )

    @tatsumasu()
    def _start_(self):  # noqa
        self._sequence_()
        self._check_eof()

    @tatsumasu('Sequence')
    def _sequence_(self):  # noqa

        def block1():
            self._term_()
            with self._optional():
                self._token(',')
        self._positive_closure(block1)
        self.name_last_node('children')
        self.ast._define(
            ['children'],
            []
        )

    @tatsumasu()
    def _term_(self):  # noqa
        with self._choice():
            with self._option():
                self._parens_()
            with self._option():
                self._word_()
            self._error('no available options')

    @tatsumasu()
    def _parens_(self):  # noqa
        self._token('(')
        self._cut()
        self._sequence_()
        self.name_last_node('@')
        self._token(')')

    @tatsumasu('Word')
    def _word_(self):  # noqa
        self._pattern('\\w+')
        self.name_last_node('val')
        self.ast._define(
            ['val'],
            []
        )


class BugSemantics(object):
    def start(self, ast):  # noqa
        return ast

    def sequence(self, ast):  # noqa
        return ast

    def term(self, ast):  # noqa
        return ast

    def parens(self, ast):  # noqa
        return ast

    def word(self, ast):  # noqa
        return ast


def main(filename, start=None, **kwargs):
    if start is None:
        start = 'start'
    if not filename or filename == '-':
        text = sys.stdin.read()
    else:
        with open(filename) as f:
            text = f.read()
    parser = BugParser()
    return parser.parse(text, rule_name=start, filename=filename, **kwargs)


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

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