@@ -74,20 +74,27 @@ def ExpandConstants(lines, constants):
74
74
75
75
76
76
def ExpandMacros (lines , macros ):
77
+ def expander (s ):
78
+ return ExpandMacros (s , macros )
77
79
for name , macro in macros .items ():
78
- start = lines .find (name + '(' , 0 )
79
- while start != - 1 :
80
+ name_pattern = re .compile ("\\ b%s\\ (" % name )
81
+ pattern_match = name_pattern .search (lines , 0 )
82
+ while pattern_match is not None :
80
83
# Scan over the arguments
81
- assert lines [start + len (name )] == '('
82
84
height = 1
83
- end = start + len (name ) + 1
85
+ start = pattern_match .start ()
86
+ end = pattern_match .end ()
87
+ assert lines [end - 1 ] == '('
84
88
last_match = end
85
- arg_index = 0
86
- mapping = { }
89
+ arg_index = [ 0 ] # Wrap state into array, to work around Python "scoping"
90
+ mapping = {}
87
91
def add_arg (str ):
88
92
# Remember to expand recursively in the arguments
89
- replacement = ExpandMacros (str .strip (), macros )
90
- mapping [macro .args [arg_index ]] = replacement
93
+ if arg_index [0 ] >= len (macro .args ):
94
+ return
95
+ replacement = expander (str .strip ())
96
+ mapping [macro .args [arg_index [0 ]]] = replacement
97
+ arg_index [0 ] += 1
91
98
while end < len (lines ) and height > 0 :
92
99
# We don't count commas at higher nesting levels.
93
100
if lines [end ] == ',' and height == 1 :
@@ -100,10 +107,13 @@ def add_arg(str):
100
107
end = end + 1
101
108
# Remember to add the last match.
102
109
add_arg (lines [last_match :end - 1 ])
110
+ if arg_index [0 ] < len (macro .args ) - 1 :
111
+ lineno = lines .count (os .linesep , 0 , start ) + 1
112
+ raise Exception ('line %s: Too few arguments for macro "%s"' % (lineno , name ))
103
113
result = macro .expand (mapping )
104
114
# Replace the occurrence of the macro with the expansion
105
115
lines = lines [:start ] + result + lines [end :]
106
- start = lines . find ( name + '(' , start )
116
+ pattern_match = name_pattern . search ( lines , start + len ( result ) )
107
117
return lines
108
118
109
119
0 commit comments