Allow input transformers to raise SyntaxError

This is a rebase and continuation of @vbraun's PR #4089, which allows input transformers to raise a SyntaxError if they consider a line of input code invalid. This is something they want for Sage - it is a degree of extra complexity for us, but it makes some sense.

I've made a few fixes from #4089, refactored `run_cell` a bit, and turned the test for the terminal interact loop into an in-process test using some generator magic.