-
Notifications
You must be signed in to change notification settings - Fork 4
/
gracoparser.py
executable file
·345 lines (291 loc) · 8.22 KB
/
gracoparser.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/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
KEYWORDS = {}
class CropBuffer(Buffer):
def __init__(
self,
text,
whitespace=re.compile('[\\t\\n\\s]+', RE_FLAGS | re.DOTALL),
nameguard=False,
comments_re='\\/\\*(.|\\s|\\n)*?\\*\\/',
eol_comments_re='\\/\\/.*?$',
ignorecase=None,
namechars='',
**kwargs
):
super(CropBuffer, self).__init__(
text,
whitespace=whitespace,
nameguard=nameguard,
comments_re=comments_re,
eol_comments_re=eol_comments_re,
ignorecase=ignorecase,
namechars=namechars,
**kwargs
)
class CropParser(Parser):
def __init__(
self,
whitespace=re.compile('[\\t\\n\\s]+', RE_FLAGS | re.DOTALL),
nameguard=False,
comments_re='\\/\\*(.|\\s|\\n)*?\\*\\/',
eol_comments_re='\\/\\/.*?$',
ignorecase=None,
left_recursion=False,
parseinfo=True,
keywords=None,
namechars='',
buffer_class=CropBuffer,
**kwargs
):
if keywords is None:
keywords = KEYWORDS
super(CropParser, 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
)
@graken()
def _constant_string_(self):
self._pattern(r'".*"')
self.name_last_node('val')
self.ast._define(
['val'],
[]
)
@graken()
def _constant_numerical_(self):
self._pattern(r'[0-9]+')
self.name_last_node('val')
self.ast._define(
['val'],
[]
)
@graken()
def _constant_hexadecimal_(self):
self._pattern(r'0x([a-f]|[A-F]|[0-9])+')
self.name_last_node('val')
self.ast._define(
['val'],
[]
)
@graken()
def _identifier_(self):
self._pattern(r'[a-zA-Z][A-Za-z0-9_]*')
self.name_last_node('val')
self.ast._define(
['val'],
[]
)
@graken()
def _add_expr_(self):
self._expr_()
self._token('+')
self._expr_()
@graken()
def _sub_expr_(self):
self._expr_()
self._token('-')
self._expr_()
@graken()
def _mul_expr_(self):
self._expr_()
self._token('*')
self._expr_()
@graken()
def _div_expr_(self):
self._expr_()
self._token('/')
self._expr_()
@graken()
def _type_(self):
with self._group():
with self._choice():
with self._option():
self._token('int')
with self._option():
self._token('string')
with self._option():
self._token('float')
self._error('expecting one of: float int string')
@graken()
def _ARGLIST_(self):
self._token('(')
with self._optional():
self._complex_expr_()
self.add_last_node_to_name('@')
def block1():
self._token(',')
self._complex_expr_()
self.add_last_node_to_name('@')
self._closure(block1)
self._token(')')
@graken()
def _function_application_(self):
self._expr_()
self.name_last_node('id')
self._ARGLIST_()
self.name_last_node('args')
self.ast._define(
['args', 'id'],
[]
)
@graken()
def _inline_application_(self):
with self._group():
with self._choice():
with self._option():
self._add_expr_()
with self._option():
self._sub_expr_()
with self._option():
self._mul_expr_()
with self._option():
self._div_expr_()
self._error('no available options')
self.name_last_node('bin_function')
self.ast._define(
['bin_function'],
[]
)
@graken()
def _function_declaration_(self):
self._token('func')
self._identifier_()
self.name_last_node('id')
self._token('=')
self._complex_expr_()
self.name_last_node('rval')
self._token(':')
self._type_()
self.ast._define(
['id', 'rval'],
[]
)
@graken()
def _bind_(self):
self._token('let')
self._identifier_()
self.name_last_node('id')
self._token('=')
self._complex_expr_()
self.name_last_node('rval')
self.ast._define(
['id', 'rval'],
[]
)
@graken()
def _expr_(self):
with self._group():
with self._choice():
with self._option():
self._constant_string_()
with self._option():
self._constant_hexadecimal_()
with self._option():
self._constant_numerical_()
with self._option():
self._identifier_()
self._error('no available options')
@graken()
def _complex_expr_(self):
with self._choice():
with self._option():
self._inline_application_()
with self._option():
self._function_application_()
with self._option():
self._expr_()
self._error('no available options')
@graken()
def _line_(self):
with self._optional():
with self._choice():
with self._option():
self._bind_()
with self._option():
self._function_application_()
with self._option():
self._function_declaration_()
self._error('no available options')
self._token(';')
@graken()
def _start_(self):
self._line_()
self.add_last_node_to_name('@')
def block1():
self._line_()
self.add_last_node_to_name('@')
self._positive_closure(block1)
class CropSemantics(object):
def constant_string(self, ast):
return ast
def constant_numerical(self, ast):
return ast
def constant_hexadecimal(self, ast):
return ast
def identifier(self, ast):
return ast
def add_expr(self, ast):
return ast
def sub_expr(self, ast):
return ast
def mul_expr(self, ast):
return ast
def div_expr(self, ast):
return ast
def type(self, ast):
return ast
def ARGLIST(self, ast):
return ast
def function_application(self, ast):
return ast
def inline_application(self, ast):
return ast
def function_declaration(self, ast):
return ast
def bind(self, ast):
return ast
def expr(self, ast):
return ast
def complex_expr(self, ast):
return ast
def line(self, ast):
return ast
def start(self, ast):
return ast
def main(filename, startrule, **kwargs):
with open(filename) as f:
text = f.read()
parser = CropParser()
return parser.parse(text, startrule, filename=filename, **kwargs)
if __name__ == '__main__':
import json
from grako.util import asjson
ast = generic_main(main, CropParser, name='Crop')
print('AST:')
print(ast)
print()
print('JSON:')
print(json.dumps(asjson(ast), indent=2))
print()