This repository has been archived by the owner on Jul 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAutoCompletionGenerator.py
403 lines (305 loc) · 8.8 KB
/
AutoCompletionGenerator.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import sys
import os
import glob
import string
# Regular expressions for defines, natives and publics.
# I decided against using regex eventually for a few reasons.
# re_define = r"\#define\s*(.*)[ ]"
# re_native = r"native\s*([A-Za-z0-9:]*)([A-Za-z0-9_]*)\((.*)\);"
# re_public = r"forward\s*([A-Za-z0-9:]*)([A-Za-z0-9_]*)\((.*)\);"
# quick and dirty debug prints that can be toggled
debug = False
def db(*args):
if not debug:
return
for i in args:
print(i, end=" ")
print("")
def gen_func(funcname, params):
"""generates a sublime-completions line based on a function"""
out = '\t\t{"trigger": "%s", "contents": "%s(' % (funcname, funcname)
for i, param in enumerate(params):
out += '${%d:%s}' % (i + 1, param.replace('\\', '\\\\').replace('"', '\\\"'))
if(i != len(params) - 1):
out += ', '
out += ')"},\n'
return out
def gen_const(string):
"""generates a sublime-completions line based on a single string"""
args = string
if "%" in string:
i = 0
for x in range(-1, 9):
if string.find("%%%d" % (x)) != -1:
i += 1
args = args.replace("%%%d" % (x), "${%d:%d}" % (i, x))
out = '\t\t{"trigger": "%s", "contents": "%s' % (string, args)
out += '"},\n'
else:
out = '\t\t"%s",\n' % string
return out
def is_char_valid_symbol_char(character):
return (
character in string.ascii_lowercase +
string.ascii_uppercase + string.digits + '_'
)
def scan_contents(contents):
"""
scans through a string character by character extracting defines and
functions
"""
output_contents = """
{
"scope": "source.pawn - variable.other.pawn",
"completions":
[
"""
skip_until_newline = False
skip_until_whitespace_start = False
skip_until_whitespace_end = False
skip_until_invalid_symbol_char = False
skip_until_valid_symbol_char = False
in_comment_block = False
in_directive = False
in_directive_define = False
pos_directive_define = -1
in_function = False
in_function_declare = False
in_function_params = False
pos_function_name = -1
pos_function_param = -1
data_function_name = ""
data_function_params = []
# count_function_params = 0
in_function_param_subscript = False
in_function_param = False
no_function_params = False
# in_enumerator = False
# in_enumerator_block = False
# pos_enumerator_item = -1
for i, c in enumerate(contents):
db(i, c)
if skip_until_newline:
if c == '\n':
skip_until_newline = False
db("skipped until newline found at ", i)
continue
else:
continue
if skip_until_whitespace_start:
if c.isspace():
skip_until_whitespace_start = False
db("skipped until whitespace found at ", i)
else:
continue
if skip_until_whitespace_end:
if not c.isspace():
skip_until_whitespace_end = False
db("skipped whitespace until ", i, c)
else:
continue
if skip_until_invalid_symbol_char:
if not is_char_valid_symbol_char(c):
skip_until_invalid_symbol_char = False
db("skipped until invalid symbol char found")
else:
continue
if skip_until_valid_symbol_char:
if is_char_valid_symbol_char(c):
skip_until_valid_symbol_char = False
db("skipped until valid symbol char found")
else:
continue
if in_comment_block:
if contents.startswith('*/', i):
db("comment block ends at ", i)
in_comment_block = False
continue
else:
if contents.startswith('/*', i):
db("comment block starts at ", i)
in_comment_block = True
continue
if contents.startswith('//', i):
skip_until_newline = True
continue
if in_directive:
db("in_directive")
for directive in ['include', 'defined', 'if', 'elseif', 'endif', 'emit']:
if contents.startswith(directive, i):
skip_until_newline = True
in_directive = False
continue
if contents.startswith('define', i):
skip_until_whitespace_start = True
skip_until_whitespace_end = True
in_directive_define = True
continue
if in_directive_define:
if pos_directive_define == -1:
# skip constants starting with '_',
# standard naming convention for internal-only symbols.
if c == '_':
in_directive_define = False
skip_until_newline = True
continue
skip_until_whitespace_start = True
pos_directive_define = i
db("reached define contents block at ", i, c)
else:
final = contents[pos_directive_define:i]
output_contents += gen_const(final)
print("[EXTRACTED] DIRECTIVE 'define' DATA: '%s'" % final)
in_directive = False
in_directive_define = False
skip_until_newline = True
pos_directive_define = -1
continue
if c == '\\':
db("reached newline symbol")
in_directive = False
skip_until_newline = True
else:
if c == '#':
in_directive = True
continue
if in_function:
db("in_function")
# Function name
if not in_function_declare:
db("not in function declare (in storage modifier) skipping")
skip_until_whitespace_end = True
in_function_declare = True
continue
if not data_function_name:
db("in function name")
if pos_function_name == -1:
if not is_char_valid_symbol_char(c):
db("invalid function name character, skipping until valid")
skip_until_valid_symbol_char = True
continue
if c == '_':
db("function name begins with _, skipping")
in_function = False
in_function_declare = False
skip_until_newline = True
continue
db("reading function name from ", i)
pos_function_name = i
skip_until_invalid_symbol_char = True
continue
else:
if c == ':' or c == ' ':
db("extracted string is tag, starting again")
pos_function_name = -1
skip_until_valid_symbol_char = True
continue
if c == '(':
data_function_name = contents[pos_function_name:i]
pos_function_name = -1
db("function name '%s'" % data_function_name)
else:
db("not a function, skip")
pos_function_name = -1
in_function = False
in_function_declare = False
skip_until_newline = True
continue
# Function parameters
if not in_function_params and not no_function_params:
db("reached start of params")
if not is_char_valid_symbol_char(c):
if c == ')':
no_function_params = True
else:
continue
db("invalid symbol character, skipping until valid")
else:
in_function_params = True
if not no_function_params and pos_function_param == -1:
db("function param start not set")
if in_function_param_subscript:
db("in_function_param_subscript")
if c == '}':
db("exit parameter subscript block")
in_function_param_subscript = False
continue
else:
db("not in_function_param_subscript")
if c == '{':
db("in parameter subscript block")
in_function_param_subscript = True
continue
if not is_char_valid_symbol_char(c):
if not in_function_param_subscript:
if c == '{':
db("invalid char check: in parameter subscript block")
in_function_param_subscript = True
continue
if contents.startswith('...', i):
pos_function_param = i
in_function_param = True
continue
pos_function_param = i
in_function_param = True
continue
if in_function_param:
db("in function param")
if c == ',' or c == ')':
data_function_params.append(contents[pos_function_param:i])
db("parameter found: '%s'" % contents[pos_function_param:i])
pos_function_param = -1
in_function_param = False
if c == ')':
output_contents += gen_func(data_function_name, data_function_params)
in_function = False
print("[EXTRACTED] FUNCTION '%s' PARAMS: %s" % (
data_function_name, data_function_params))
continue
else:
if (
contents.startswith('native', i) or
contents.startswith('forward', i) # or
# contents.startswith('stock', i)
):
skip_until_whitespace_start = True
in_function = True
in_function_params = False
data_function_name = ""
data_function_params = []
no_function_params = False
continue
output_contents = output_contents.rstrip("\n,")
output_contents += """
]
}
"""
return output_contents
def process_file(filename):
"""
processes a file for outputting extracted contents to a sublime-completions
file.
"""
print(filename)
contents = ""
with open(filename, 'r') as input_file:
contents = input_file.read()
output_contents = scan_contents(contents)
with open(filename + '.sublime-completions', 'w') as output_file:
output_file.write(output_contents)
def main():
if len(sys.argv) > 1:
path = sys.argv[1]
else:
path = "E:\\Games\\Projects\\SA-MP\\pawno\\include\\autocomplete\\"
print(path)
if os.path.isfile(path):
process_file(path)
else:
if not os.path.exists(path):
print("Directory not found.")
return
for f in glob.glob(path + '*.pwn'):
process_file(f)
if __name__ == '__main__':
main()