refactor pattern matcher for better api (not ready for review)

Create the pull request to test.