-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinter.py
More file actions
218 lines (190 loc) · 7.2 KB
/
Copy pathLinter.py
File metadata and controls
218 lines (190 loc) · 7.2 KB
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
import re
def check_tabs(file_path, err, block_lines, tabs):
with open(file_path, 'r') as file:
lines = file.readlines()
tab_count = 0
ind = 0
errors = []
for line in lines:
if line[-1] == "\n":
line = line[:-1]
if line.strip() == "begin":
line0 = " " * tab_count + "begin"
tab_count += tabs
elif line.strip() == "end" or line.strip() == "end." or line.strip() == "end;":
tab_count -= tabs
line0 = " " * tab_count + line.strip()
else:
st = 0
for el in line:
if el != " ":
break
else:
st += 1
line0 = " " * tab_count + line[st:]
if line != line0 and len(line.strip()) != 0 and ind + 1 not in block_lines:
errors.append(f"{file_path}: Tab error in {ind+1} line\n")
ind += 1
return errors
def check_empty_lines(file_path, err, block_lines, between_func, posible):
with open(file_path, 'r') as file:
lines = file.readlines()
cur_emt = 0
ind = 0
empty = True
errors = []
for line in lines:
if line[-1] == "\n":
line = line[:-1]
if len(line.strip()) == 0:
cur_emt += 1
else:
if empty and cur_emt != 0:
for i in range(cur_emt):
if i + 1 not in block_lines:
errors.append(f"{file_path}: Empty string error in {i+1} line\n")
elif cur_emt > posible:
for i in range(cur_emt - posible):
if ind-cur_emt+i+1 not in block_lines:
errors.append(f"{file_path}: Empty string error in {ind-cur_emt+i+1} line\n")
cur_emt = 0
empty = False
ind += 1
if lines[-1][-1] == '\n':
cur_emt += 1
for i in range(cur_emt):
if len(lines)-cur_emt+i+2 not in block_lines:
errors.append(f"{file_path}: Empty string error in {len(lines)-cur_emt+i+2} line\n")
return errors
def check_space_line(line, space_el, l_s = [], r_s = [',']):
res = []
for i in range(len(line)):
if line[i] in space_el:
if i != 0:
if line[i - 1] != " ":
res.append(i)
if i != len(line) - 1:
if line[i + 1] != " ":
res.append(i)
if line[i] in l_s:
if i != 0:
if line[i - 1] != " ":
res.append(i)
if line[i] in r_s:
if i != len(line) - 1:
if line[i + 1] != " ":
res.append(i)
return res
def check_max_spaces(line, err, max_spaces):
for i in range(len(line)):
if line[i] != " ":
break
if " " * (max_spaces + 1) in line[i:]:
return True
return False
def check_space(file_path, err, block_lines, max_space, space_elements):
with open(file_path, 'r') as file:
lines = file.readlines()
errors = []
ind = 0
for line in lines:
ind += 1
line_err = check_space_line(line, space_elements)
if check_max_spaces(line, err, max_space):
errors.append(f"{file_path}: Space in too much in {ind} line\n")
for error in line_err:
if ind not in block_lines:
errors.append(f"{file_path}: Space error in {ind} line {error} pos by element {line[error]}\n")
return errors
def check_lines_len(file_path, err, block_lines, max_len):
with open(file_path, 'r') as file:
lines = file.readlines()
errors = []
ind = 0
for line in lines:
ind += 1
if line.strip() == 0:
continue
if check_line_len(line, max_len):
if ind not in block_lines:
errors.append(f"{file_path}: {ind} line in too large: {len(line)} > {max_len}\n")
return errors
def check_line_len(line, max_len):
if len(line) > max_len:
return True
return False
def check_identificators(file_path, err, block_lines):
camel_case_pattern = r'^[A-Za-z][a-zA-Z0-9]*$'
identifiers_pattern = r'\b[a-zA-Z]+\w*\b'
keywords = [
'and', 'array', 'begin', 'case', 'const', 'div', 'do', 'downto', 'else',
'end', 'file', 'for', 'function', 'goto', 'if', 'in', 'label', 'mod', 'nil',
'not', 'of', 'or', 'packed', 'procedure', 'program', 'record', 'repeat',
'set', 'then', 'to', 'type', 'unit', 'until', 'uses', 'var', 'while', 'with'
]
error_string = ''
errors = []
with open(file_path, 'r') as file:
lines = file.readlines()
for line_index, line in enumerate(lines):
identifiers = re.findall(identifiers_pattern, line)
for ident in identifiers:
if not re.match(camel_case_pattern, ident) and ident not in keywords:
error_string += f"{file_path}: incorrect identifier name {ident} (not in CamelCase)\n"
if line_index not in block_lines:
errors.append(error_string)
error_string = ''
return errors
def check_unused_ref(file_path, err, block_lines):
with open(file_path, 'r') as file:
lines = file.readlines()
errors = []
ind = 0
for line in lines:
continue
return errors
def write_errors(err, errors):
for error in errors:
ind = 0
if '/' in error:
if error.index('/') < error.index('.'):
for i in range(len(error)):
if error[i] == '/':
ind = i + 1
elif error[i] == '.':
break
err.write(error[ind:])
def linter(block_lines, bloc_lines_type, file_names,
max_len_string, use_max_len,
empty_lines, use_empty_count,
tabs_count, use_tabs_count,
max_space, use_max_space,
unused_ref):
err = open("errors.txt", "w")
err.write(f"Block lines is: {' '.join([str(i) for i in block_lines])}\n\n")
for file_name in file_names:
linter_main(err, block_lines, bloc_lines_type, file_name,
max_len_string, use_max_len,
empty_lines, use_empty_count,
tabs_count, use_tabs_count,
max_space, use_max_space,
unused_ref)
def linter_main(err, block_lines, bloc_lines_type, file_name,
max_len_string, use_max_len,
empty_lines, use_empty_count,
tabs_count, use_tabs_count,
max_space, use_max_space,
unused_ref):
space_elements = ['+', '-', '*', '/']
if use_empty_count:
write_errors(err, check_empty_lines(file_name, err, block_lines, 2, empty_lines))
if use_tabs_count:
write_errors(err, check_tabs(file_name, err, block_lines, tabs_count))
if use_max_space:
write_errors(err, check_space(file_name, err, block_lines, max_space, space_elements))
if use_max_len:
write_errors(err, check_lines_len(file_name, err, block_lines, max_len_string))
if bloc_lines_type != "None":
write_errors(err, check_identificators(file_name, err, block_lines))
if unused_ref:
write_errors(err, check_unused_ref(file_name, err, block_lines))