for (Token t : parameters) {
defParamValues.add(t.getValue());
}
for (int i = 0; i < body.size(); ++i) {
Token curr = body.get(i);
int index = defParamValues.indexOf(curr.getValue());
if (index == -1) {
newTokens.add(curr);
}
else if (index == arguments.size()) {
// EXTENSION: GCC's special meaning of token paste operator
// If variable argument is left out then the comma before the paste operator will be deleted
int j = i;
while(j > 0 && body.get(j - 1).getType() == WS)
j--;
if (j == 0 || !body.get(--j).getValue().equals("##"))
continue;
int k = j;
while(j > 0 && body.get(j - 1).getType() == WS)
j--;
if (j > 0 && body.get(j - 1).getValue().equals(",")) {
newTokens.remove(newTokens.size() - 1 + j - i); //remove the comma
newTokens.remove(newTokens.size() - 1 + k - i); //remove the paste operator
}
}
else if (index < arguments.size()) {
Token replacement = arguments.get(index);
// The arguments have to be fully expanded before expanding the body of the macro
String newValue = serialize(expandMacro("", replacement.getValue()));
if (i > 0 && body.get(i - 1).getValue().equals("#")) {
newTokens.remove(newTokens.size() - 1);
newValue = encloseWithQuotes(quote(newValue));
}
newTokens.add(Token.builder()
.setLine(replacement.getLine())
.setColumn(replacement.getColumn())
.setURI(replacement.getURI())
.setValueAndOriginalValue(newValue)
.setType(replacement.getType())
.setGeneratedCode(true)
.build());
}
}
}