Factor out build-pattern-matching-handler