@@ -74,23 +74,32 @@ def expandLine(self, line):
7474 # no substitution of variables needed
7575 expandedLine = line .replace (self .name , self .cont )
7676 else :
77- # substitute variables vars
78- vars = "" .join (line .split ("(" )[1 :]).split (")" )[0 ].split ("," )
79- if len (vars ) != len (self .vars ):
80- print ("ATTENTION" )
81- print ("Substitution error!" )
82- print ("" )
83- self .print ()
84- print ("" )
85- print (" " , line )
86-
87- else :
88- words = split (self .cont )
89- for ind1 in range (len (self .vars )):
90- for ind2 in range (len (words )):
91- if self .vars [ind1 ].strip () in words [ind2 ]:
92- words [ind2 ] = re .sub (self .vars [ind1 ].strip (), vars [ind1 ].strip (), words [ind2 ])
93- expandedLine = block (words )
77+ inds = [i for i in range (len (line )) if line .startswith (self .name , i )]
78+ expandedLine = line [:inds [0 ]]
79+ for i , ind in enumerate (inds ):
80+
81+ # make sure that the name of the define == self.name and not only starts with self.name
82+ words = line [ind :].split ('(' )
83+ if words [0 ].strip () != self .name :
84+ if i < len (inds )- 1 :
85+ expandedLine += line [ind :inds [i + 1 ]]
86+ else :
87+ expandedLine += line [ind :]
88+ continue
89+
90+ # substitute variables vars
91+ vars = "" .join (line [ind :].split ("(" )[1 :]).split (")" )[0 ].split ("," )
92+ if len (vars ) != len (self .vars ):
93+ print ("ATTENTION" )
94+ print ("Substitution error!" )
95+ print ('>> ' , line )
96+ else :
97+ words = split (self .cont )
98+ for ind1 in range (len (self .vars )):
99+ for ind2 in range (len (words )):
100+ if self .vars [ind1 ].strip () in words [ind2 ]:
101+ words [ind2 ] = re .sub (self .vars [ind1 ].strip (), vars [ind1 ].strip (), words [ind2 ])
102+ expandedLine += block (words )
94103
95104 # remove ##, which connects two strings
96105 expandedLine = block (split (expandedLine .replace (" # # " , "" )))
@@ -324,8 +333,7 @@ def pickContent(lines_in_file):
324333 linesWithoutComments [ind ] = res [1 ]
325334
326335 # select all lines starting with #define
327- idfs = [l for l , s in enumerate (
328- linesWithoutComments ) if s .lstrip ().startswith ("#define" )][::- 1 ]
336+ idfs = [l for l , s in enumerate (linesWithoutComments ) if s .lstrip ().startswith ("#define" )]
329337 for idf in idfs :
330338 ws = split (linesWithoutComments [idf ])
331339 defstring = linesWithoutComments [idf ].split (ws [2 ], 1 )[1 ]
@@ -334,8 +342,7 @@ def pickContent(lines_in_file):
334342 # find the corresponding #undef
335343 # if no #undef then apply to the end of the file
336344 iend = len (linesWithoutComments )
337- iudfs = [l for l , s in enumerate (
338- linesWithoutComments ) if s .lstrip ().startswith ("#undef" )][::- 1 ]
345+ iudfs = [l for l , s in enumerate (linesWithoutComments ) if s .lstrip ().startswith ("#undef" )]
339346 for iudf in iudfs :
340347 ws = split (linesWithoutComments [iudf ])
341348 if ws [2 ] == df .name :
0 commit comments