-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplace_multi.py
More file actions
258 lines (205 loc) · 7.93 KB
/
Copy pathreplace_multi.py
File metadata and controls
258 lines (205 loc) · 7.93 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
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
import settings as st
from tokens import create_tokens
import re
def replace_multi_line_macro(actual_par, i, prnt, key, lines, n_index):
tab_len = space_len = 0
#find the index of key
ix = prnt[i].index(key)
#calculate how much indentation to give
for c in prnt[i][:ix]:
if( c == '\t'):
tab_len += 1
#else:
#space_len += 1
#form the indentation string
idtn_str = '\t'*tab_len + ' '*space_len
idtn_str_1 = prnt[i][:ix]
k=1
#remove the current macro line from output
prnt.pop(i)
lines.pop(i)
#get the line range of macro
def_r = st.macro_def_table.get(key)[n_index]
if(len(def_r) == 1):
j = def_r[0]
#store the current line
b_end = prnt[j].index(")")
temp = prnt[j][b_end+2:]
#check for keys in that line to be replaced
for key in actual_par:
if(key in lines[j].split()):
pattern = '\\b'+key+'\\b'
try:
s_c = temp.index('"')
e_c = temp.index('"', s_c+1)
except:
temp = re.sub(pattern,actual_par[key],temp)
else:
first = temp[:s_c]
second = temp[s_c:e_c+1]
third = temp[e_c+1:]
temp_1 = re.sub(pattern,actual_par[key],first)
temp_2 = re.sub(pattern,actual_par[key],third)
temp = temp_1 + second + temp_2
#temp = temp.replace(key, actual_par[key])
prnt.insert(i, idtn_str_1+temp)
lines.insert(i,temp)
lines[i] = create_tokens(lines[i])
return 0
#iterate over macro-defination line
for j in range(def_r[0]+2, def_r[1]):
#store the current line
temp = prnt[j].lstrip()
#check for keys in that line to be replaced
for key in actual_par:
if(key in lines[j].split()):
pattern = '\\b'+key+'\\b'
try:
s_c = temp.index('"')
e_c = temp.index('"', s_c+1)
except:
temp = re.sub(pattern,actual_par[key],temp)
else:
first = temp[:s_c]
second = temp[s_c:e_c+1]
third = temp[e_c+1:]
temp_1 = re.sub(pattern,actual_par[key],first)
temp_2 = re.sub(pattern,actual_par[key],third)
temp = temp_1 + second + temp_2
#temp = temp.replace(key, actual_par[key])
if(k==1):
prnt.insert(i, idtn_str_1+temp)
else:
prnt.insert(i, idtn_str+temp)
lines.insert(i,temp)
lines[i] = create_tokens(lines[i])
i=i+1
k=k+1
#return the number of new lines added
return def_r[1]-def_r[0]-3
def create_parameter_table(key, s):
#split to get each arguments passed
p = s.split(",")
key_arg = pos_arg = 0
if(s==""):
len_p = 0
else:
len_p = len(p)
if(len_p != 0):
for qs in p:
if("=" in qs):
key_arg+=1
else:
pos_arg+=1
flag_a = False
name_tab = st.macro_name_table.get(key)
def_n = name_tab[0][0]
if(def_n > 1):
for iq in range(1,def_n+1):
if(name_tab[iq][0] == pos_arg):
name_index = iq
flag_a = True
if(not flag_a):
for iq in range(1,def_n+1):
if( name_tab[iq][0] + name_tab[iq][1] == pos_arg + key_arg):
name_index = iq
flag_a = True
'''if(name_tab[iq][0] + name_tab[iq][1] >= pos_arg+key_arg):
name_index = iq
flag_a = True'''
else:
name_index = 1
flag_a = True
if(not flag_a):
print("Error in parameter passing "+s)
return {}
#check for number of positional arguments
n_of_pos_par = st.macro_name_table.get(key)[name_index][0]
#if( n_of_pos_par > len_p ):
# print("Invalid no of arguments for macro "+key)
opt_arg = len(p)-1
if(s==""):
opt_arg = -1
apt = {}
#store the keyword arguments passed in macro call
for t in p:
if("=" in t):
temp = t.split('=')
apt[temp[0].strip()] = temp[1].strip()
opt_arg -= 1
#get the parameter table for macro
par_list = st.parameter_name_table.get(key)[name_index-1]
#for every key in returned parameter table
for i in range(len(par_list)):
#get the key and value
var_key = list(par_list[i].keys())[0]
var_value = list(par_list[i].values())[0]
#if the key is already in called statement, then continue
if(var_key in apt):
continue
#if key's value in defination is None, then store the called argument
if( var_value == None ):
#print(par_list)
apt[var_key] = p[i].strip()
#else check for their default or passed value
else:
if( i > opt_arg ):
apt[var_key] = var_value
else:
apt[var_key] = p[i].strip()
#return the formed parameter table
return [apt,name_index-1]
def nested_macro(m,n,lines,prnt):
total_n = 0
i = m
k=0
while(k<n):
p = lines[i].split()
try:
c_s = p.index('"')
c_e = p.index('"', c_s+1)
p = p[:c_s] + p[c_e+1:]
except:
pass
#for every key check line
for key in st.macro_name_table:
#if any key is found in that line
if(key in p):
#find the index of key
k_i = p.index(key)
#check if it's multi-line macro name used
def_rn = st.macro_def_table.get(key)[0]
if(len(def_rn)==2 or len(p)-1>k_i and p[k_i+1] == "("):
try:
s_i = prnt[i].index("(")
e_i = prnt[i].index(")", s_i)
except:
actual_par, n_index = create_parameter_table(key, "")
else:
#get the actual parameter table for macro-call statement
actual_par, n_index = create_parameter_table(key, prnt[i][s_i+1:e_i])
#replace macro with its defination
ab = replace_multi_line_macro(actual_par, i, prnt, key, lines, n_index)
total_n += ab
cd = nested_macro(i,ab+1,lines,prnt)
total_n += cd
i = i + cd
i = i + ab + 1
#else macro is single line
else:
pattern = '\\b'+key+'\\b'
try:
s_c = prnt[i].index('"')
e_c = prnt[i].index('"', s_c+1)
except:
prnt[i] = re.sub(pattern,st.parameter_name_table.get(key),prnt[i])
else:
first = prnt[i][:s_c]
second = prnt[i][s_c:e_c+1]
third = prnt[i][e_c+1:]
temp_1 = re.sub(pattern,st.parameter_name_table.get(key),first)
temp_2 = re.sub(pattern,st.parameter_name_table.get(key),third)
prnt[i] = temp_1 + second + temp_2
#prnt[i] = prnt[i].replace(key, st.parameter_name_table.get(key))
k += 1
return total_n