Fixed a use-after-free and array-overrun bugs in lexer.c
These didn't seem to trigger problems on Linux, but resulted
in memory exhaustion on OSX.