implemented stateful low-level parser

This is a more invasive commit but the behavior of the parser should be clearer now. We parse char by char and cannot get stuck anymore when reading garbage.