-
Notifications
You must be signed in to change notification settings - Fork 0
/
handleKeyword.py
423 lines (383 loc) · 21.4 KB
/
handleKeyword.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/local/bin/python3
# Made in python 3.7.7 64 bit, use only this version
# ---
# Copyright (c) 2021, Pranjal Rastogi
# All rights reserved.
# check LICENSE for more details
# ---
# Part of The codeStyliser utility
# ---
# handleKeyword.py
# The brain for CodeStyliser.py
# ---
import utilities as utils
import re
import models
import constants as consts
import os
import exceptions
def handle_keyword(keyword, line, line_index, lines, file_to_edit, is_current_line_comment,
comment_of_current_line, is_multiline, keyword_index):
if consts.to_LOG:
print("LOG: " + f'file path: {file_to_edit.name}, lineIndex: {line_index}, keyword: {keyword}')
# found a "KEYWORD"
error_print_data = (keyword, (line_index + 1), file_to_edit.name)
has_hash = utils.check_for_hash(line_index, lines)
if has_hash != -1:
# has hash
if line.find("{") != -1:
# has {
return None
else:
print("WARN: Ignored %s loop/ condition at line %d in %s as \'#\' found in next line" % error_print_data)
raise exceptions.HashIgnore
# we must now skip over all parentheses to find the end of the (condition)
if keyword == "else -":
if line.find("if") != -1:
# line is else if
is_else_if = True
check_parenth_result = utils.check_for_parentheses(line, line_index, lines, "p")
else:
is_else_if = False
check_parenth_result = models.ParenthResult(True, line_index, None)
else:
is_else_if = False
check_parenth_result = utils.check_for_parentheses(line, line_index, lines, "p")
if check_parenth_result is None:
print("ERROR: While checking parentheses: ignored %s loop/ condition at %d in file %s" % error_print_data)
# raise ParenthesesCheckingError
return None
elif not check_parenth_result.isOnSameLine:
# (condition) doesnt end on same line
is_on_same_line = check_parenth_result.isOnSameLine
nxt_ln_index = check_parenth_result.lineIndex
next_line_index = nxt_ln_index
open_curly_brace_index = lines[nxt_ln_index - 1][check_parenth_result.lastCloseParenthIndex:].find("{")
if not is_else_if and keyword == "else -":
last_close_parenth_index = 0
else:
last_close_parenth_index = check_parenth_result.lastCloseParenthIndex + 2
else:
# (condition) ends on same line
is_on_same_line = check_parenth_result.isOnSameLine
next_line_index = line_index + 1
open_curly_brace_index = line[check_parenth_result.lastCloseParenthIndex:].find("{")
if not is_else_if and keyword == "else -":
last_close_parenth_re = re.search(r"\b(else)\b", line)
if last_close_parenth_re:
last_close_parenth_index = last_close_parenth_re.end()
else:
last_close_parenth_index = -1
else:
last_close_parenth_index = check_parenth_result.lastCloseParenthIndex + 1
if open_curly_brace_index == -1 and utils.check_for_open_brace(next_line_index, lines)[0] == -1:
# no { on same line and on subsequent lines, we must add {} if possible
if is_on_same_line:
last_semi_colon_index = line[last_close_parenth_index:].find(";")
is_back_slash_present = line.rstrip().endswith("\\")
if is_back_slash_present:
# backSLASH!
if last_semi_colon_index != -1:
# semicolon found on same line
if is_current_line_comment:
if is_multiline:
to_add_line = comment_of_current_line + line[:last_close_parenth_index] + \
" { " + line[
last_close_parenth_index:(last_semi_colon_index +
len(line[:last_close_parenth_index]) + 1)] \
+ " } " + line[(last_semi_colon_index + len(line[:last_close_parenth_index])
+ 1):].rstrip() + "\n"
else:
to_add_line = line[:last_close_parenth_index] + " { " + \
line[last_close_parenth_index:(last_semi_colon_index
+ len(line[:last_close_parenth_index]) + 1)] \
+ " } " + line[(last_semi_colon_index + len(line[:last_close_parenth_index])
+ 1):] + comment_of_current_line + "\n"
else:
to_add_line = line[:last_close_parenth_index] + " { " + \
line[last_close_parenth_index:(last_semi_colon_index +
len(line[:last_close_parenth_index]) + 1)] + \
" } " + line[(last_semi_colon_index + len(line[:last_close_parenth_index]) +
1):].rstrip() + "\n"
# insert toAddLine and return
del lines[line_index]
lines.insert(line_index, to_add_line)
return lines
elif last_semi_colon_index == -1:
if is_current_line_comment:
if is_multiline:
to_add_line = comment_of_current_line + line.rstrip()[:-1] + " { \\" + "\n"
else:
to_add_line = line.rstrip() + " { " + comment_of_current_line + "\\\n"
else:
to_add_line = line.rstrip()[:-1] + " { \\\n"
del lines[line_index]
lines.insert(line_index, to_add_line)
check_for_semi_colon_index = line_index + 1
else:
# raise SemiColonError
return None
elif not is_back_slash_present:
if lines[line_index - 2].isspace() or len(lines[line_index - 2]) == 0:
pass
else:
if lines[line_index - 2].rstrip().endswith("\\"):
if last_semi_colon_index == -1:
# not semicolon ending
return None
if last_semi_colon_index == -1:
# if there is nothing on line except the keyword()
if is_current_line_comment:
if is_multiline:
to_add_line = comment_of_current_line + line.rstrip() + " { " + "\n"
else:
to_add_line = line.rstrip() + " { " + comment_of_current_line + "\n"
else:
to_add_line = line.rstrip() + " {\n"
del lines[line_index]
lines.insert(line_index, to_add_line)
check_for_semi_colon_index = line_index + 1
elif last_semi_colon_index != -1:
# semicolon found on same line
if is_current_line_comment:
if is_multiline:
to_add_line = comment_of_current_line + line[:last_close_parenth_index] + " { " \
+ line[last_close_parenth_index:(last_semi_colon_index +
len(line[:last_close_parenth_index]) + 1)] \
+ " } " + line[(last_semi_colon_index + len(line[:last_close_parenth_index])
+ 1):].rstrip() + "\n"
else:
to_add_line = line[:last_close_parenth_index] + " { " + line[last_close_parenth_index:(
last_semi_colon_index + len(line[:last_close_parenth_index]) + 1)] + " } " \
+ line[(last_semi_colon_index + len(line[:last_close_parenth_index]) + 1):] \
+ comment_of_current_line + "\n"
else:
to_add_line = line[:last_close_parenth_index] + " { " + line[last_close_parenth_index:(
last_semi_colon_index + len(line[:last_close_parenth_index]) + 1)] + " } " \
+ line[(last_semi_colon_index
+ len(line[:last_close_parenth_index]) + 1):].rstrip() \
+ "\n"
# insert toAddLine and return
del lines[line_index]
lines.insert(line_index, to_add_line)
return lines
else:
# raise SemiColonError
return None
else:
# raise BackSlashError
return None
else:
# the (condition) doesnt end on same line
is_back_slash_present = lines[nxt_ln_index - 1].rstrip().endswith("\\")
last_semi_colon_index = lines[nxt_ln_index - 1][last_close_parenth_index:].find(";")
if is_back_slash_present:
if last_semi_colon_index != -1:
# same line semicolon
nxt_ln_trim_comment = utils.trim_comment(lines[nxt_ln_index - 1], (nxt_ln_index - 1), lines)
if nxt_ln_trim_comment.hasComment:
if nxt_ln_trim_comment.isMultiline:
to_add_line = nxt_ln_trim_comment.comment + \
nxt_ln_trim_comment.line[:last_close_parenth_index] + \
" { " + nxt_ln_trim_comment.line[last_close_parenth_index:].rstrip()[:-1] \
+ " } " + "\\\n"
else:
to_add_line = nxt_ln_trim_comment.line[:last_close_parenth_index] + \
" { " + nxt_ln_trim_comment.line[last_close_parenth_index:].rstrip() + \
" } " + nxt_ln_trim_comment.comment + "\\\n"
else:
to_add_line = lines[nxt_ln_index - 1][:last_close_parenth_index] + " { " + \
lines[nxt_ln_index - 1][last_close_parenth_index:].rstrip()[:-1] + " } \\\n"
has_hash = utils.check_for_hash(nxt_ln_index - 1, lines)
if has_hash != -1:
# has hash
if lines[nxt_ln_index - 1].find("{") != -1:
# has {
# return CurlyBracesPresent
return None
else:
print("WARN: Ignored %s loop/ condition at line %d in %s as \'#\' found in next line"
% error_print_data)
# return HashError
return None
del lines[nxt_ln_index - 1]
lines.insert(nxt_ln_index - 1, to_add_line)
return lines
elif last_semi_colon_index == -1:
nxt_ln_trim_comment = utils.trim_comment(lines[nxt_ln_index - 1], (nxt_ln_index - 1), lines)
if nxt_ln_trim_comment.hasComment:
if nxt_ln_trim_comment.isMultiline:
to_add_line = nxt_ln_trim_comment.comment + nxt_ln_trim_comment.line.rstrip()[:-1] \
+ " { " + "\\\n"
else:
to_add_line = nxt_ln_trim_comment.line.rstrip() + " { " + nxt_ln_trim_comment.comment \
+ "\\\n"
else:
to_add_line = lines[nxt_ln_index - 1].rstrip()[:-1] + " { \\\n"
has_hash = utils.check_for_hash(nxt_ln_index - 1, lines)
if has_hash != -1:
# has hash
if lines[nxt_ln_index - 1].find("{") != -1:
# has {
# raise CurlyBracePresent
return None
else:
print(
"WARN: Ignored %s loop/ condition at line %d in %s as \'#\' found in next line"
% error_print_data)
# return HashError
return None
del lines[nxt_ln_index - 1]
lines.insert(nxt_ln_index - 1, to_add_line)
check_for_semi_colon_index = nxt_ln_index
elif not is_back_slash_present:
if lines[nxt_ln_index - 2].isspace() or len(lines[nxt_ln_index - 2]) == 0:
pass
else:
if lines[nxt_ln_index - 2].rstrip().endswith("\\"):
if last_semi_colon_index == -1:
# not semicolon ending
return None
if last_semi_colon_index == -1:
nxt_ln_trim_comment = utils.trim_comment(lines[nxt_ln_index - 1], (nxt_ln_index - 1), lines)
if nxt_ln_trim_comment.hasComment:
if nxt_ln_trim_comment.isMultiline:
to_add_line = nxt_ln_trim_comment.comment + nxt_ln_trim_comment.line.rstrip() + " { " + "\n"
else:
to_add_line = nxt_ln_trim_comment.line.rstrip() + " { " + nxt_ln_trim_comment.comment + "\n"
else:
to_add_line = lines[nxt_ln_index - 1].rstrip() + " {\n"
has_hash = utils.check_for_hash(nxt_ln_index - 1, lines)
if has_hash != -1:
# has hash
if lines[nxt_ln_index - 1].find("{") != -1:
# has {
# raise HasCurlyBrace
return None
else:
print(
"WARN: Ignored %s loop/ condition at line %d in %s as \'#\' found in next line"
% error_print_data)
# raise HashError
return None
del lines[nxt_ln_index - 1]
lines.insert(nxt_ln_index - 1, to_add_line)
check_for_semi_colon_index = nxt_ln_index
elif last_semi_colon_index != -1:
nxt_ln_trim_comment = utils.trim_comment(lines[nxt_ln_index - 1], (nxt_ln_index - 1), lines)
if nxt_ln_trim_comment.hasComment:
if nxt_ln_trim_comment.isMultiline:
to_add_line = nxt_ln_trim_comment.comment + \
nxt_ln_trim_comment.line[:last_close_parenth_index] + \
" { " + nxt_ln_trim_comment.line[last_close_parenth_index:].rstrip() \
+ " } " + "\n"
else:
to_add_line = nxt_ln_trim_comment.line[:last_close_parenth_index] + \
" { " + nxt_ln_trim_comment.line[last_close_parenth_index:].rstrip() + \
" } " + nxt_ln_trim_comment.comment + "\n"
else:
to_add_line = lines[nxt_ln_index - 1][:last_close_parenth_index] + " { " + \
lines[nxt_ln_index - 1][last_close_parenth_index:].rstrip() + " }\n"
has_hash = utils.check_for_hash(nxt_ln_index - 1, lines)
if has_hash != -1:
# has hash
if lines[nxt_ln_index - 1].find("{") != -1:
# has {
# raise CurlyBracePresent
return None
else:
print(
"WARN: Ignored %s loop/ condition at line %d in %s as \'#\' found in next line"
% error_print_data)
# raise HashError
return None
del lines[nxt_ln_index - 1]
lines.insert(nxt_ln_index - 1, to_add_line)
return lines
else:
# raise SemiColonError
return None
else:
# raise SemiColonError
return None
closing_brace_line_index = utils.get_closing_brace_line_index(check_for_semi_colon_index, lines)
if closing_brace_line_index is None:
print("FATAL ERROR: ignored %s loop/ condition at %d in file %s. Could be caused due to nested if-elseif, "
"fix in next update" %
error_print_data)
# raise FatalError
return None
else:
nxt_ln_else = False
index_of_else = closing_brace_line_index
# add closing braces at closingBraceLine (inserting a new ln) with indentation
if is_multiline:
spaces = " " * (keyword_index + len(comment_of_current_line))
else:
spaces = " " * keyword_index
# HACK: WHAT IS THIS, EVEN I DON'T KNOW!!!
if len(lines[closing_brace_line_index].strip()) == 0:
# line be empty
add_closing_brace_line = spaces + "}\n"
# check for else
tmp_lines = lines
c = 0
while c < len(tmp_lines[closing_brace_line_index:]):
tmp_line = tmp_lines[c]
comment_check = utils.trim_comment(tmp_line, c, tmp_lines)
first_char = utils.get_first_character_index(tmp_line)
if comment_check.hasComment:
if comment_check.isMultiline:
c = comment_check.multiLineJumpIndex
continue
tmp_line = comment_check.line
if tmp_line.isspace():
c += 1
continue
if tmp_line.find("else") == first_char:
add_closing_brace_line = spaces + "} "
add_closing_brace_line = add_closing_brace_line + tmp_line[
(len(add_closing_brace_line) - 2):].lstrip()
nxt_ln_else = True
index_of_else = c
break
elif len(line.strip()) != 0:
break
c += 1
else:
# line is not empty
if lines[closing_brace_line_index - 1].rstrip().endswith("\\"):
# we found \
add_closing_brace_line = spaces + "} \\\n"
first_char = utils.get_first_character_index(lines[closing_brace_line_index])
if lines[closing_brace_line_index].find("else") == first_char:
add_closing_brace_line = spaces + "} "
add_closing_brace_line = add_closing_brace_line + lines[closing_brace_line_index][
(len(add_closing_brace_line) - 2):].lstrip()
nxt_ln_else = True
index_of_else = closing_brace_line_index
elif lines[closing_brace_line_index - 2].rstrip().endswith("\\"):
# there was a \ in prev line, but not this line
to_add_back_slash = lines[closing_brace_line_index - 1].rstrip() + " \\\n"
del lines[closing_brace_line_index - 1]
lines.insert(closing_brace_line_index - 1, to_add_back_slash)
add_closing_brace_line = spaces + "}\n"
else:
# no \
add_closing_brace_line = spaces + "}\n"
# NOTE: no need to check for comments here as closing brace line index automatically never
# returns that
first_char = utils.get_first_character_index(lines[closing_brace_line_index])
if lines[closing_brace_line_index].find("else") == first_char:
add_closing_brace_line = spaces + "} "
add_closing_brace_line = add_closing_brace_line + lines[closing_brace_line_index][
(len(add_closing_brace_line)-2):].lstrip()
nxt_ln_else = True
index_of_else = closing_brace_line_index
if not nxt_ln_else:
lines.insert(closing_brace_line_index, "")
lines.insert(closing_brace_line_index, add_closing_brace_line)
else:
del lines[index_of_else]
lines.insert(index_of_else, add_closing_brace_line)
return lines