Integrate rewriting and matching