multiword_entry: make advance a state machine

This modifies the logic of `advance` so it works like a proper state
machine, without changing the existing functionality.

The state machine is needed to better support parsing words containing
an apostrophe.