#!/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


__version__ = (2016, 9, 19, 4, 9, 53, 0)

__all__ = [
    'CFGMarkupParser',
    'CFGMarkupSemantics',
    'main'
]

KEYWORDS = set([])


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


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

    def parse(self, text, *args, **kwargs):
        if not isinstance(text, Buffer):
            text = CFGMarkupBuffer(text, **kwargs)
        return super(CFGMarkupParser, self).parse(text, *args, **kwargs)

    @graken()
    def _reference_(self):
        self._pattern(r'[a-zA-Z0-9]+')
        self.name_last_node('key')
        self.ast._define(
            ['key'],
            []
        )

    @graken()
    def _terminal_(self):
        self._pattern(r'"[^"]*"')
        self.name_last_node('value')
        self.ast._define(
            ['value'],
            []
        )

    @graken()
    def _single_production_(self):
        with self._choice():
            with self._option():
                self._reference_()
            with self._option():
                self._terminal_()
            self._error('no available options')

    @graken()
    def _production_(self):

        def block1():
            self._single_production_()
        self._positive_closure(block1)
        self.name_last_node('production_list')
        self.ast._define(
            ['production_list'],
            []
        )


class CFGMarkupSemantics(object):
    def reference(self, ast):
        return ast

    def terminal(self, ast):
        return ast

    def single_production(self, ast):
        return ast

    def production(self, ast):
        return ast


def main(
        filename,
        startrule,
        trace=False,
        whitespace=None,
        nameguard=None,
        comments_re=None,
        eol_comments_re=None,
        ignorecase=None,
        left_recursion=True,
        parseinfo=True,
        **kwargs):

    with open(filename) as f:
        text = f.read()
    whitespace = whitespace or None
    parser = CFGMarkupParser(parseinfo=False)
    ast = parser.parse(
        text,
        startrule,
        filename=filename,
        trace=trace,
        whitespace=whitespace,
        nameguard=nameguard,
        ignorecase=ignorecase,
        **kwargs)
    return ast

if __name__ == '__main__':
    import json
    ast = generic_main(main, CFGMarkupParser, name='CFGMarkup')
    print('AST:')
    print(ast)
    print()
    print('JSON:')
    print(json.dumps(ast, indent=2))
    print()
