forked from ali-alidoust/ScriptHookW3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignatureParser.py
358 lines (299 loc) · 9.06 KB
/
SignatureParser.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
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/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 SignatureBuffer(Buffer):
def __init__(
self,
text,
whitespace=None,
nameguard=None,
comments_re=None,
eol_comments_re=None,
ignorecase=None,
namechars='',
**kwargs
):
super(SignatureBuffer, self).__init__(
text,
whitespace=whitespace,
nameguard=nameguard,
comments_re=comments_re,
eol_comments_re=eol_comments_re,
ignorecase=ignorecase,
namechars=namechars,
**kwargs
)
class SignatureParser(Parser):
def __init__(
self,
whitespace=None,
nameguard=None,
comments_re=None,
eol_comments_re=None,
ignorecase=None,
left_recursion=False,
parseinfo=True,
keywords=None,
namechars='',
buffer_class=SignatureBuffer,
**kwargs
):
if keywords is None:
keywords = KEYWORDS
super(SignatureParser, 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 _start_(self):
self._function_definition_()
@graken()
def _function_definition_(self):
with self._optional():
self._access_modifier_()
self._return_type_()
with self._optional():
self._cdecl_()
self._full_function_name_()
self._function_arg_list_()
with self._optional():
self._function_modifier_()
@graken()
def _return_type_(self):
self._full_type_name_()
@graken()
def _access_modifier_(self):
with self._choice():
with self._option():
self._token('public:')
with self._option():
self._token('private:')
with self._option():
self._token('protected:')
self._error('expecting one of: private: protected: public:')
@graken()
def _cdecl_(self):
self._token('__cdecl')
@graken()
def _full_function_name_(self):
with self._choice():
with self._option():
with self._group():
self._full_namespace_()
self._function_name_()
with self._option():
self._function_name_()
self._error('no available options')
@graken()
def _function_arg_list_(self):
self._token('(')
with self._optional():
self._function_arguments_()
self._token(')')
@graken()
def _function_modifier_(self):
self._token('const')
@graken()
def _full_type_name_(self):
with self._group():
with self._optional():
self._class_key_()
with self._group():
with self._choice():
with self._option():
self._full_namespace_()
self._token('::')
self._type_name_()
with self._option():
self._type_name_()
self._error('no available options')
def block2():
self._pointer_or_ref_chars_()
self._closure(block2)
with self._optional():
self._token('__ptr64')
self.name_last_node('type')
self.ast._define(
['type'],
[]
)
@graken()
def _full_namespace_(self):
with self._choice():
with self._option():
with self._group():
self._namespace_()
self._token('::')
self._full_namespace_()
with self._option():
with self._group():
self._namespace_()
self._token('::')
self._error('no available options')
@graken()
def _namespace_(self):
self._full_type_name_()
self.name_last_node('namespace')
self.ast._define(
['namespace'],
[]
)
@graken()
def _function_name_(self):
self._identifier_()
@graken()
def _function_arguments_(self):
with self._choice():
with self._option():
with self._group():
self._full_type_name_()
self._token(',')
self._function_arguments_()
with self._option():
self._full_type_name_()
self._error('no available options')
@graken()
def _type_name_(self):
with self._choice():
with self._option():
with self._group():
self._identifier_()
self._token('<')
self._template_args_list_()
self._token('>')
with self._option():
self._identifier_()
self._error('no available options')
@graken()
def _template_args_list_(self):
with self._choice():
with self._option():
with self._group():
self._template_argument_()
self._token(',')
self._template_args_list_()
with self._option():
self._template_argument_()
self._error('no available options')
@graken()
def _template_argument_(self):
with self._choice():
with self._option():
with self._group():
with self._optional():
self._class_key_()
self._full_type_name_()
with self._option():
self._literal_int_()
self._error('no available options')
@graken()
def _identifier_(self):
self._nondigit_()
self._pattern(r'[A-Za-z0-9_]*')
@graken()
def _class_key_(self):
with self._choice():
with self._option():
self._token('class')
with self._option():
self._token('struct')
with self._option():
self._token('union')
self._error('expecting one of: class struct union')
@graken()
def _literal_int_(self):
self._pattern(r'[0-9]+')
@graken()
def _nondigit_(self):
self._pattern(r'[A-Za-z_]')
@graken()
def _pointer_or_ref_chars_(self):
with self._choice():
with self._option():
self._token('*')
with self._option():
self._token('&')
with self._option():
self._token('&&')
with self._option():
self._token('const')
self._error('expecting one of: & && * const')
class SignatureSemantics(object):
def start(self, ast):
return ast
def function_definition(self, ast):
return ast
def return_type(self, ast):
return ast
def access_modifier(self, ast):
return ast
def cdecl(self, ast):
return ast
def full_function_name(self, ast):
return ast
def function_arg_list(self, ast):
return ast
def function_modifier(self, ast):
return ast
def full_type_name(self, ast):
return ast
def full_namespace(self, ast):
return ast
def namespace(self, ast):
return ast
def function_name(self, ast):
return ast
def function_arguments(self, ast):
return ast
def type_name(self, ast):
return ast
def template_args_list(self, ast):
return ast
def template_argument(self, ast):
return ast
def identifier(self, ast):
return ast
def class_key(self, ast):
return ast
def literal_int(self, ast):
return ast
def nondigit(self, ast):
return ast
def pointer_or_ref_chars(self, ast):
return ast
def main(filename, startrule, **kwargs):
with open(filename) as f:
text = f.read()
parser = SignatureParser()
return parser.parse(text, startrule, filename=filename, **kwargs)
if __name__ == '__main__':
import json
from grako.util import asjson
ast = generic_main(main, SignatureParser, name='Signature')
print('AST:')
print(ast)
print()
print('JSON:')
print(json.dumps(asjson(ast), indent=2))
print()