Lexer: rearranged code and added nested func next().

* Shaved off 111 lines of code.