The issue is not really with counting the linebreaks, but getting their position cleanly inside ```next()```: they could be in one of the ignored patterns, or in one of the tokens, and keeping track of them is a bit clunky.

I agree that scanning the string twice is not very optimal, but this is the cleanest way I could come up with.