Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/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_()
with self._option():
self._role_()
self._error('no available options')
@tatsumasu()
def _term_(self): # noqa
with self._choice():
with self._option():
self._token('(')
def sep1():
self._token('^')
def block1():
self._nominal_()
self._gather(block1, sep1)
self.name_last_node('@')
self._token(')')
with self._option():
self._token('(')
self._semspec_()
self.name_last_node('@')
self._token(')')
self._error('no available options')
@tatsumasu()
def _variable_string_(self): # noqa
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')
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 sep5():
self._token('^')
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')
self.ast._define(
['name', 'roles', 'type'],
[]
)
@tatsumasu('Nominal')
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')
self._token('(')
self._cut()
def sep5():
self._token('^')
def block5():
self._role_()
self._gather(block5, sep5)
self.name_last_node('roles')
self._token(')')
self.ast._define(
['name', 'roles', 'type'],
[]
)
@tatsumasu('Role')
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._atom_()
self.name_last_node('target')
with self._option():
self._constant('entity')
self.name_last_node('type')
self._atom_()
self.name_last_node('target')
self._error('no available options')
self.ast._define(
['target', 'type'],
[]
)
def _variable_name_(self): # noqa
self._pattern(r'[a-z]\d+')
@tatsumasu('str')
def _variable_type_(self): # noqa
self._pattern(r'[a-zA-Z\-]+')
def _atom_(self): # noqa
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 term(self, ast): # noqa
return ast
def variable_string(self, ast): # noqa
return ast
def variable(self, ast): # noqa
return ast
def nominal(self, ast): # noqa
return ast
def role(self, ast): # noqa
return ast
def variable_name(self, ast): # noqa
return ast
def variable_type(self, ast): # noqa
return ast
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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()