I was wondering about performance issues when parsing a source file that is being edited by the user (for example, when you need to give a syntax highlight).
I think that the simplest approach is to parse every time the code changes, get the results, and replace the current code with the highlighted one. With large files this may be a problem though. Is there a better way to do that?

I suppose a solution may be to parse just the "area" where there was the last edit. Is this a good idea?

  • 1
    Have you identified this as an issue? Bear in mind while editing text actual typing takes up a very small percentage of the time. – ChrisF Apr 13 '11 at 11:06

If you're using a Packrat parser you can invalidate only the cached terminals that overlaps the edited region, and then reparse the whole buffer - only the missing terminals will be reparsed.

Otherwise you'll need a more customised approach, where you'll use regular expressions, for example, for detecting the beginnings of toplevel declarations and start parsing from that points only, and only in an invalidated region. That's what most of the modern IDEs are actually doing.

Not the answer you're looking for? Browse other questions tagged or ask your own question.