#!/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 UnknownBuffer(Buffer):
    def __init__(
        self,
        text,
        whitespace=None,
        nameguard=None,
        comments_re=None,
        eol_comments_re=None,
        ignorecase=None,
        namechars='',
        **kwargs
    ):
        super(UnknownBuffer, self).__init__(
            text,
            whitespace=whitespace,
            nameguard=nameguard,
            comments_re=comments_re,
            eol_comments_re=eol_comments_re,
            ignorecase=ignorecase,
            namechars=namechars,
            **kwargs
        )


class UnknownParser(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=UnknownBuffer,
        **kwargs
    ):
        if keywords is None:
            keywords = KEYWORDS
        super(UnknownParser, 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 _LETTER_(self):  # noqa
        self._pattern('[a-zA-Z]')

    @tatsumasu()
    def _DIGIT_(self):  # noqa
        self._pattern('[0-9]')

    @tatsumasu()
    def _pathsep_(self):  # noqa
        self._pattern('[/\\\\]')

    @tatsumasu()
    def _namesep_(self):  # noqa
        self._pattern('[_.-]')

    @tatsumasu()
    def _text_(self):  # noqa
        with self._choice():
            with self._option():
                self._char_()
            with self._option():
                self._pattern('[^/\\\\]')
            self._error('no available options')

    @tatsumasu()
    def _char_(self):  # noqa
        with self._choice():
            with self._option():
                self._alnum_()
            with self._option():
                self._pattern('[_.\\- ]')
            self._error('no available options')

    @tatsumasu()
    def _alnum_(self):  # noqa
        with self._choice():
            with self._option():
                self._LETTER_()
            with self._option():
                self._DIGIT_()
            self._error('no available options')

    @tatsumasu()
    def _INT_(self):  # noqa
        with self._optional():
            self._token('-')

        def block0():
            self._DIGIT_()
        self._positive_closure(block0)

    @tatsumasu()
    def _seq_(self):  # noqa
        self._dirname_()
        self._basename_()

    @tatsumasu()
    def _dirname_(self):  # noqa

        def block0():
            self._pathsep_()
        self._closure(block0)

        def block1():
            self._text_()
        self._positive_closure(block1)

        def block2():
            self._pathsep_()

            def block3():
                self._text_()
            self._positive_closure(block3)
        self._closure(block2)

        def block4():
            self._pathsep_()
        self._positive_closure(block4)

    @tatsumasu()
    def _basename_(self):  # noqa
        self._name_()
        self._frange_()
        self._ext_()

    @tatsumasu()
    def _name_(self):  # noqa

        def block0():
            self._text_()
        self._positive_closure(block0)

        def block1():
            self._namesep_()
        self._positive_closure(block1)

    @tatsumasu()
    def _frange_(self):  # noqa
        self._INT_()
        with self._optional():
            self._token('-')
            self._INT_()

    @tatsumasu()
    def _ext_(self):  # noqa
        self._token('.')

        def block0():
            self._alnum_()
        self._positive_closure(block0)


class UnknownSemantics(object):
    def LETTER(self, ast):  # noqa
        return ast

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

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

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

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

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

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

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

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

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

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

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

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

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


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


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

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

