Ah, right. Now that I actually read your code, that's the real bug :p
The issue with this "state machine" approach is you don't have "lookahead".
In the two pass approach (approach from one side, then the other), you essentially have two machines, one implicitly serving as the "lookahead" for the other.