Skip to content
Snippets Groups Projects
generated_openccg_parser.py 8.62 KiB
Newer Older
#!/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
from tatsu.util import re, generic_main  # noqa


KEYWORDS = {}  # type: ignore


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


class OpenCCGParser(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=OpenCCGBuffer,
        **kwargs
    ):
        if keywords is None:
            keywords = KEYWORDS
        super(OpenCCGParser, 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._semspec_()
        self._check_eof()

    @tatsumasu()
    def _semspec_(self):  # noqa
        with self._choice():
            with self._option():
                self._term_()
            with self._option():
                self._nominal_()
            with self._option():
                self._variable_()
            self._error('no available options')

    @tatsumasu()
        with self._choice():
            with self._option():
                self._token('(')

                def sep1():
                    self._token('^')

                def block1():
                    self._nominal_()
                self._gather(block1, sep1)
    @tatsumasu()
    def _variable_string_(self):  # noqa
        with self._choice():
            with self._option():
                self._variable_name_()
                self.name_last_node('name')
                self._variable_type_()
                self.name_last_node('type')
            with self._option():
                self._variable_name_()
                self.name_last_node('name')
            self._error('no available options')
        self.ast._define(
            ['name', 'type'],
            []
        )

    @tatsumasu('Variable')
    def _variable_(self):  # noqa
        with self._choice():
            with self._option():
                with self._choice():
                    with self._option():
                        self._variable_name_()
                        self.name_last_node('name')
                        self._token(':')
                        self._cut()
                        self._variable_type_()
                        self.name_last_node('type')
                    with self._option():
                        self._variable_name_()
                        self.name_last_node('name')
                    self._error('no available options')

                def block5():
                    self._role_()
                self._gather(block5, sep5)
                self.name_last_node('roles')
            with self._option():
                self._token('(')
                with self._choice():
                    with self._option():
                        self._variable_name_()
                        self.name_last_node('name')
                        self._token(':')
                        self._cut()
                        self._variable_type_()
                        self.name_last_node('type')
                    with self._option():
                        self._variable_name_()
                        self.name_last_node('name')
                    self._error('no available options')


                def sep11():
                    self._token('^')

                def block11():
                    self._role_()
                self._gather(block11, sep11)
                self.name_last_node('roles')
                self._token(')')
            self._error('no available options')
    def _nominal_(self):  # noqa
        self._token('@')
        with self._choice():
            with self._option():
                self._variable_name_()
                self.name_last_node('name')
                self._token(':')
                self._cut()
                self._variable_type_()
                self.name_last_node('type')
            with self._option():
                self._variable_name_()
                self.name_last_node('name')
            self._error('no available options')

    def _role_(self):  # noqa
        with self._choice():
            with self._option():
                self._token('<')
                self._atom_()
                self.name_last_node('type')
                self._token('>')
                self._variable_()
                self.name_last_node('target')
            with self._option():
                self._token('<')
                self._atom_()
                self.name_last_node('type')
                self._token('>')
                self.name_last_node('target')
            with self._option():
                self.name_last_node('type')
                self._atom_()
                self.name_last_node('target')
            self._error('no available options')
        self.ast._define(
            ['target', 'type'],
            []
        )

    @tatsumasu('str')
        self._pattern(r'[a-z]\d+')

    @tatsumasu('str')
    def _variable_type_(self):  # noqa
        self._pattern(r'[a-zA-Z\-]+')
    @tatsumasu('str')
        self._pattern(r'[a-zA-Z\-\._0-9]+')


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

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

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

        return ast

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

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

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

    def atom(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 = OpenCCGParser()
    return parser.parse(text, rule_name=start, filename=filename, **kwargs)


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

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