Huge refactoring in the parser engine.

Now the code is much more compact and readable. Hopefully
nothing has broken.