#!/usr/bin/env python

# 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 annotations

import sys

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


KEYWORDS = {}  # type: ignore


class PddlLogicBuffer(Buffer):
    def __init__(self, text, /, config: ParserConfig = None, **settings):
        config = ParserConfig.new(
            config,
            owner=self,
            whitespace=None,
            nameguard=None,
            comments_re=None,
            eol_comments_re='#.*?$',
            ignorecase=False,
            namechars='',
            parseinfo=False,
        )
        config = config.replace(**settings)
        super().__init__(text, config=config)


class PddlLogicParser(Parser):
    def __init__(self, /, config: ParserConfig = None, **settings):
        config = ParserConfig.new(
            config,
            owner=self,
            whitespace=None,
            nameguard=None,
            comments_re=None,
            eol_comments_re='#.*?$',
            ignorecase=False,
            namechars='',
            parseinfo=False,
            keywords=KEYWORDS,
            start='start',
        )
        config = config.replace(**settings)
        super().__init__(config=config)

    @tatsumasu()
    def _start_(self):  # noqa
        self._pddlDocument_()

    @tatsumasu()
    def _str_(self):  # noqa
        self._pattern('"[^"]*"')

    @tatsumasu()
    def _strBlock_(self):  # noqa
        self._pattern('"""(?:.|\\n)*?"""')

    @tatsumasu()
    def _name_(self):  # noqa
        self._pattern('[\\w/-]+')

    @tatsumasu('ActionTemplateNode')
    def _template_(self):  # noqa
        self._token('template')
        self._token('::')
        self._str_()
        self.name_last_node('template')
        self._token(';')

        self._define(
            ['template'],
            []
        )

    @tatsumasu('ActionFeedbackNode')
    def _feedback_(self):  # noqa
        self._token('feedback')
        self._token('::')
        self._str_()
        self.name_last_node('name')
        self._token(';')

        self._define(
            ['name'],
            []
        )

    @tatsumasu('ActionPddlNode')
    def _pddl_(self):  # noqa
        self._token('pddl')
        self._token('::')
        self._strBlock_()
        self.name_last_node('code')
        self._token(';')

        self._define(
            ['code'],
            []
        )

    @tatsumasu('ActionGrammarNode')
    def _grammar_(self):  # noqa
        self._token('grammar')
        self._token('::')
        self._strBlock_()
        self.name_last_node('code')
        self._token(';')

        self._define(
            ['code'],
            []
        )

    @tatsumasu('ActionTypeNode')
    def _actionType_(self):  # noqa
        self._token('action')
        self._name_()
        self.name_last_node('name')
        self._token('{')

        def block1():
            with self._group():
                with self._choice():
                    with self._option():
                        self._template_()
                        self.name_last_node('template')
                    with self._option():
                        self._feedback_()
                        self.name_last_node('feedback')
                    with self._option():
                        self._pddl_()
                        self.name_last_node('pddl')
                    with self._option():
                        self._grammar_()
                        self.name_last_node('grammar')
                    self._error(
                        'expecting one of: '
                        '<feedback> <grammar> <pddl> <template>'
                    )
        self._closure(block1)
        self._token('}')

        self._define(
            ['feedback', 'grammar', 'name', 'pddl', 'template'],
            []
        )

    @tatsumasu('PddlDocumentNode')
    def _pddlDocument_(self):  # noqa

        def block1():
            with self._group():
                with self._choice():
                    with self._option():
                        self._actionType_()
                    with self._option():
                        self._grammar_()
                    self._error(
                        'expecting one of: '
                        '<actionType> <grammar>'
                    )
        self._closure(block1)
        self.name_last_node('parts')
        self._check_eof()

        self._define(
            ['parts'],
            []
        )

    @tatsumasu()
    def _pddlStart_(self):  # noqa
        self._pddlDocument_()


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

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

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

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

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

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

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

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

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

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

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


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


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

    ast = generic_main(main, PddlLogicParser, name='PddlLogic')
    data = asjson(ast)
    print(json.dumps(data, indent=2))
