fix incorrect parser logic for macro substitution

add tests covering fixed bug