Parser split into subroutines to simplify logic

Main parser function in parser.c has been split into a number of helpful
subroutines in order to be more readable and potentially allow us to test
individual parser functions without requiring us to test an entire parser
run.

In addition to this, lots of tests are now passing.