More robust syntax highlighting

I fixed some traps in the syntax coloring code that could cause it to hang in some extreme cases. Hopefully this fixes the problem with it hanging earlier.